|
KanoopDatabaseQt 1.1.1
Qt database abstraction library
|
|
#include <datasource.h>
Inheritance diagram for DataSource:
Collaboration diagram for DataSource:Public Member Functions | |
| DataSource () | |
| Construct a DataSource with default (empty) credentials. | |
| DataSource (const DatabaseCredentials &credentials) | |
| Construct a DataSource with the given database credentials. | |
| virtual | ~DataSource () |
| Destructor. | |
| virtual bool | openConnection () |
| Open a database connection using the stored credentials. | |
| virtual bool | closeConnection () |
| Close the current database connection. | |
| bool | isOpen () |
| Return true if the database connection is currently open. | |
| DatabaseCredentials | credentials () const |
| Get the current database credentials. | |
| void | setCredentials (const DatabaseCredentials &value) |
| Set the database credentials. | |
| QString | connectionName () const |
| Get the Qt connection name for this data source. | |
| void | setConnectionName (const QString &value) |
| Set the Qt connection name. | |
| bool | createOnOpenFailure () const |
| Return true if the database should be created when opening fails. | |
| void | setCreateOnOpenFailure (bool value) |
| Set whether to create the database when opening fails. | |
| QString | errorText () const |
| Get a human-readable string describing the last error. | |
Static Public Member Functions | |
| static bool | isSqlite (const QString &filename) |
| Check whether the given file is a valid SQLite database. | |
Protected Member Functions | |
| QSqlQuery | prepareQuery (const QString &sql, bool *success=nullptr) |
| Prepare a QSqlQuery from the given SQL string. | |
| QSqlQuery | executeQuery (const QString &sql, bool *success=nullptr) |
| Execute a SQL string and return the resulting query. | |
| bool | executeQuery (QSqlQuery &query) |
| Execute an already-prepared QSqlQuery. | |
| bool | querySuccessful (const QSqlQuery &query) |
| Check whether a query completed without error. | |
| bool | executeMultiple (const QStringList &queries) |
| Execute multiple SQL statements in sequence. | |
| virtual QString | createSql () const |
| Return the SQL used to create the database schema. | |
| virtual bool | migrate () |
| Perform any necessary database migrations. | |
| virtual bool | integrityCheck () |
| Run an integrity check on the database. | |
| void | logSql (const char *file, int line, Log::LogLevel level, const QString &sql) |
| Log a SQL statement at the given log level. | |
| void | logFailure (const QSqlQuery &query) const |
| Log details of a failed query. | |
| void | setDataSourceError (const QString &value) |
| Set the data source error string. | |
| bool | recreateSqliteDatabase () |
| Delete and recreate the SQLite database file from createSql(). | |
| virtual bool | executePostCreateScripts () |
| Execute any post-creation scripts after a new database is created. | |
| bool | isSqlite () const |
| Return true if the current credentials specify a SQLite engine. | |
Static Protected Member Functions | |
| static QDateTime | utcTime (const QVariant &value) |
| Convert a QVariant database value to a UTC QDateTime. | |
| static QString | currentTimestamp () |
| Get the current UTC timestamp as a formatted string suitable for SQL. | |
| template<typename T > | |
| static QString | commaDelimitedList (const QList< T > &list) |
| Build a comma-delimited string from a list of values. | |
| static QString | commaDelimitedIntList (const QList< int > &list) |
| Build a comma-delimited string from a list of integers. | |
| static QString | commaDelimitedUuidList (const QList< QUuid > &list) |
| Build a comma-delimited string from a list of UUIDs. | |
| static QString | commaDelimitedStringList (const QStringList &list) |
| Build a comma-delimited string from a list of strings. | |
| static QString | escapedString (const QString &unescaped) |
| Escape special characters in a string for safe inclusion in SQL. | |
Protected Attributes | |
| QSqlDatabase | _db |
| The underlying QSqlDatabase connection object. | |
An abstraction model for databases.
Subclass this class to provide a Controller in the MVC programming paradigm.
Stephen Punak, January 11 2025
Abstract database access layer providing connection management, query execution, and utility methods.
Subclass this class to provide a Controller in the MVC programming paradigm.
Definition at line 21 of file datasource.h.
|
inlineexplicit |
Construct a DataSource with default (empty) credentials.
Definition at line 27 of file datasource.h.
|
inlineexplicit |
Construct a DataSource with the given database credentials.
| credentials | The database connection credentials. |
Definition at line 34 of file datasource.h.
|
virtual |
Destructor.
Closes the database connection if open.
|
virtual |
Close the current database connection.
|
staticprotected |
Build a comma-delimited string from a list of integers.
| list | The list of integers. |
|
staticprotected |
Build a comma-delimited string from a list of values.
| list | The list of values to join. |
|
staticprotected |
Build a comma-delimited string from a list of strings.
| list | The string list. |
|
staticprotected |
Build a comma-delimited string from a list of UUIDs.
| list | The list of QUuid values. |
|
inline |
Get the Qt connection name for this data source.
Definition at line 69 of file datasource.h.
|
inline |
Return true if the database should be created when opening fails.
Definition at line 78 of file datasource.h.
|
inlineprotectedvirtual |
Return the SQL used to create the database schema.
Override in subclasses.
Definition at line 131 of file datasource.h.
|
inline |
Get the current database credentials.
Definition at line 60 of file datasource.h.
|
staticprotected |
Get the current UTC timestamp as a formatted string suitable for SQL.
| QString DataSource::errorText | ( | ) | const |
Get a human-readable string describing the last error.
|
staticprotected |
Escape special characters in a string for safe inclusion in SQL.
| unescaped | The raw string. |
|
protected |
Execute multiple SQL statements in sequence.
| queries | The list of SQL statements to execute. |
|
inlineprotectedvirtual |
Execute any post-creation scripts after a new database is created.
Override in subclasses.
Definition at line 169 of file datasource.h.
|
protected |
Execute a SQL string and return the resulting query.
| sql | The SQL statement to execute. |
| success | Optional pointer set to true on success, false on failure. |
|
protected |
Execute an already-prepared QSqlQuery.
| query | The query to execute. |
|
inlineprotectedvirtual |
Run an integrity check on the database.
Override in subclasses.
Definition at line 141 of file datasource.h.
|
inline |
Return true if the database connection is currently open.
Definition at line 55 of file datasource.h.
References _db.
|
inlineprotected |
Return true if the current credentials specify a SQLite engine.
Definition at line 174 of file datasource.h.
References DatabaseCredentials::isSqlite().
|
static |
Check whether the given file is a valid SQLite database.
| filename | The file path to check. |
|
protected |
Log details of a failed query.
| query | The failed QSqlQuery. |
|
protected |
Log a SQL statement at the given log level.
| file | The source file name (use FILE). |
| line | The source line number (use LINE). |
| level | The log level. |
| sql | The SQL statement to log. |
|
inlineprotectedvirtual |
Perform any necessary database migrations.
Override in subclasses.
Definition at line 136 of file datasource.h.
|
virtual |
Open a database connection using the stored credentials.
|
protected |
Prepare a QSqlQuery from the given SQL string.
| sql | The SQL statement to prepare. |
| success | Optional pointer set to true on success, false on failure. |
|
protected |
Check whether a query completed without error.
| query | The query to check. |
|
protected |
Delete and recreate the SQLite database file from createSql().
|
inline |
Set the Qt connection name.
| value | The connection name string. |
Definition at line 73 of file datasource.h.
|
inline |
Set whether to create the database when opening fails.
| value | true to auto-create on failure. |
Definition at line 82 of file datasource.h.
|
inline |
Set the database credentials.
| value | The new credentials. |
Definition at line 64 of file datasource.h.
|
inlineprotected |
Set the data source error string.
| value | The error description. |
Definition at line 159 of file datasource.h.
|
staticprotected |
Convert a QVariant database value to a UTC QDateTime.
| value | The QVariant containing the datetime value. |
|
protected |
The underlying QSqlDatabase connection object.
Definition at line 188 of file datasource.h.
Referenced by isOpen().