Initial commit
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
#include "execute_sql_file.h"
|
||||
|
||||
#include <QRegularExpression>
|
||||
|
||||
ExecuteSqlFile::ExecuteSqlFile(QSqlDatabase &db)
|
||||
: db_(db), delimiter_(";") { // Default delimiter is ";"
|
||||
// Constructor does not open/close the connection
|
||||
}
|
||||
|
||||
bool ExecuteSqlFile::operator()(const QString &filePath) {
|
||||
// Ensure the database connection is open
|
||||
if (!db_.isOpen()) {
|
||||
qWarning() << "Database connection is not open";
|
||||
return false;
|
||||
}
|
||||
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
qWarning() << "Failed to open file:" << filePath;
|
||||
return false;
|
||||
}
|
||||
|
||||
QTextStream in(&file);
|
||||
QString sqlContent = in.readAll();
|
||||
file.close();
|
||||
|
||||
QStringList statements = sqlContent.split("\n", Qt::SkipEmptyParts);
|
||||
|
||||
qInfo() << "Executing SQL file:" << filePath;
|
||||
|
||||
return processStatements(statements);
|
||||
}
|
||||
|
||||
bool ExecuteSqlFile::processStatements(QStringList &statements) {
|
||||
QString currentStatement;
|
||||
|
||||
for (QString &line : statements) {
|
||||
qInfo() << "Processing line:" << line;
|
||||
if (!processLine(line, currentStatement)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Execute any remaining statement
|
||||
if (!currentStatement.isEmpty() && !executeStatement(currentStatement)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ExecuteSqlFile::processLine(QString &line, QString ¤tStatement) {
|
||||
if (!filterLine(line)) {
|
||||
qInfo() << "Line is empty after filtering";
|
||||
return true; // Line was empty after filtering
|
||||
}
|
||||
|
||||
if (processDelimiterChange(line)) {
|
||||
qInfo() << "Delimiter changed to:" << delimiter_;
|
||||
return true;
|
||||
}
|
||||
|
||||
currentStatement += line + " ";
|
||||
|
||||
auto delimiterIndex = currentStatement.lastIndexOf(delimiter_);
|
||||
if (delimiterIndex == -1) {
|
||||
qInfo() << "Delimiter not found, current statement is not finished";
|
||||
return true;
|
||||
}
|
||||
qInfo() << "Statement finished, delimiter found at index:" << delimiterIndex;
|
||||
|
||||
// Execute the current statement
|
||||
QString completedStatement =
|
||||
currentStatement.replace(delimiter_, ";").trimmed();
|
||||
if (!executeStatement(completedStatement)) {
|
||||
qWarning() << "Failed to execute statement:" << completedStatement;
|
||||
return false; // Execution failed
|
||||
}
|
||||
qInfo() << "Successfully executed statement:" << completedStatement;
|
||||
|
||||
currentStatement.clear();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ExecuteSqlFile::processDelimiterChange(QString &line) {
|
||||
QRegularExpression regex(R"(^DELIMITER\s+(\S+))");
|
||||
QRegularExpressionMatch match = regex.match(line);
|
||||
if (!match.hasMatch()) {
|
||||
return false;
|
||||
}
|
||||
delimiter_ = match.captured(1); // Update the delimiter_
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ExecuteSqlFile::filterLine(QString &line) {
|
||||
line = line.section("--", 0, 0).trimmed(); // Remove comments and trim
|
||||
return !line.isEmpty(); // Return true if line is not empty
|
||||
}
|
||||
|
||||
bool ExecuteSqlFile::executeStatement(const QString &statement) const {
|
||||
QSqlQuery query(db_);
|
||||
if (!query.exec(statement)) {
|
||||
qWarning() << "Error:" << query.lastError().text();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user