|
Krita Source Code Documentation
|
#include <KisSqlQueryLoader.h>
Classes | |
| struct | FileException |
| struct | single_statement_mode_t |
| struct | SQLException |
Public Member Functions | |
| void | exec () |
| KisSqlQueryLoader (const QString &fileName) | |
| KisSqlQueryLoader (const QString &fileName, single_statement_mode_t) | |
| KisSqlQueryLoader (const QString &scriptName, const QString &script) | |
| KisSqlQueryLoader (const QString &scriptName, const QString &script, single_statement_mode_t) | |
| QSqlQuery & | query () |
| ~KisSqlQueryLoader () | |
Static Public Attributes | |
| static constexpr single_statement_mode_t | single_statement_mode {} |
Private Member Functions | |
| void | init (const QString &fileName, QString entireScript, bool singleStatementMode) |
Private Attributes | |
| QString | m_fileName |
| QSqlQuery | m_query |
| bool | m_singleStatementMode {false} |
| QStringList | m_statements |
A utility class to load an SQL query from a file and execute it.
The class reports all the errors in a form of exceptions, making it much easier to write code with multiple steps. The class can also be used in tandem with KisDatabaseTransactionLock to handle the transactions properly.
Usage:
Query provided inline:
\code{.cpp}
try {
/// Start the transaction
KisDatabaseTransactionLock transactionLock(database);
// load the query, instead of the filename pass a fake filename that
// starts with `inline://` prefix
KisSqlQueryLoader loader("inline://enable_foreign_keys",
QString("PRAGMA foreign_keys = %1")
.arg(isEnabled ? "on" : "off"));
// execute the query
loader.exec();
// commit the transaction
transactionLock.commit();
} catch (const KisSqlQueryLoader::SQLException &e) {
// in case of an error an exception will be raised, which can be
// handled gracefully; please note that the transaction will be
// automatically rolled back by KisDatabaseTransactionLock
qWarning().noquote() << "ERROR: failed to execute query:" << e.message;
qWarning().noquote() << " file:" << e.filePath;
qWarning().noquote() << " statement:" << e.statementIndex;
qWarning().noquote() << " error:" << e.sqlError.text();
return false;
}
\endcode
Query loaded from the file:
\code{.cpp}
try {
/// Start the transaction
KisDatabaseTransactionLock transactionLock(database);
// load the query, just pass the filename in a form acceptable
// by QFile
KisSqlQueryLoader loader(":/create_index_metadata_key.sql");
// execute the query
loader.exec();
// commit the transaction
transactionLock.commit();
} catch (const KisSqlQueryLoader::SQLException &e) {
// in case of an error an exception will be raised, which can be
// handled gracefully; please note that the transaction will be
// automatically rolled back by KisDatabaseTransactionLock
qWarning().noquote() << "ERROR: failed to execute query:" << e.message;
qWarning().noquote() << " file:" << e.filePath;
qWarning().noquote() << " statement:" << e.statementIndex;
qWarning().noquote() << " error:" << e.sqlError.text();
return false;
} catch (const KisSqlQueryLoader::FileException &e) {
// for file-related errors there will be another type of an exception,
// please don't forget to handle it as well
qWarning().noquote() << "ERROR: failed to execute query:" << message;
qWarning().noquote() << " error" << e.message;
qWarning().noquote() << " file:" << e.filePath;
qWarning().noquote() << " file-error:" << e.fileErrorString;
return false;
}
\endcode
Definition at line 105 of file KisSqlQueryLoader.h.
| KisSqlQueryLoader::KisSqlQueryLoader | ( | const QString & | fileName | ) |
Load the query from a file fileName. This form of the constructor does not try prepare the query. It means that:
1) The query is allowed to have multiple statements. All these statements will be executed in the exec() stage.
2) One cannot bind any values to the query.
Definition at line 61 of file KisSqlQueryLoader.cpp.
References init().
| KisSqlQueryLoader::KisSqlQueryLoader | ( | const QString & | fileName, |
| single_statement_mode_t | ) |
Load the query from a file fileName in a "single statement mode". In this mode the loader immediately prepares the query, which means you can bind any values to the query before calling exec().
Definition at line 71 of file KisSqlQueryLoader.cpp.
References init().
| KisSqlQueryLoader::KisSqlQueryLoader | ( | const QString & | scriptName, |
| const QString & | script ) |
Load the script from an explicit string script. scriptName is the fake "file name" of the script, which is used for logging and error reporting. Please add prefix inline:// to these script names to be consistent.
This form of the constructor loads the script in a multi-statement more, i.e. it doesn't prepare the query and does not allow value binding.
Definition at line 81 of file KisSqlQueryLoader.cpp.
References init().
| KisSqlQueryLoader::KisSqlQueryLoader | ( | const QString & | scriptName, |
| const QString & | script, | ||
| single_statement_mode_t | ) |
Load the script from an explicit string script. scriptName is the fake "file name" of the script, which is used for logging and error reporting. Please add prefix inline:// to these script names to be consistent.
This form of the constructor loads the script in a single-statement mode, which prepares the query immediately and allows value binding.
Definition at line 86 of file KisSqlQueryLoader.cpp.
References init().
| KisSqlQueryLoader::~KisSqlQueryLoader | ( | ) |
Definition at line 91 of file KisSqlQueryLoader.cpp.
| void KisSqlQueryLoader::exec | ( | ) |
Execute all statements of the query
In single-statement mode, the values can be bound to the query before calling exec().
Definition at line 100 of file KisSqlQueryLoader.cpp.
References m_fileName, m_query, m_singleStatementMode, and m_statements.
|
private |
Definition at line 21 of file KisSqlQueryLoader.cpp.
References KIS_SAFE_ASSERT_RECOVER_RETURN, m_fileName, m_query, m_singleStatementMode, m_statements, and kismpl::mem_equal_to().
| QSqlQuery & KisSqlQueryLoader::query | ( | ) |
|
private |
Definition at line 204 of file KisSqlQueryLoader.h.
|
private |
Definition at line 201 of file KisSqlQueryLoader.h.
|
private |
Definition at line 203 of file KisSqlQueryLoader.h.
|
private |
Definition at line 202 of file KisSqlQueryLoader.h.
|
staticconstexpr |
Definition at line 109 of file KisSqlQueryLoader.h.