13#include <Kanoop/utility/loggingbaseclass.h>
14#include <Kanoop/database/databasecredentials.h>
15#include <QSqlDatabase>
22 public LoggingBaseClass
29 LoggingBaseClass(
"db") {}
36 LoggingBaseClass(
"db"),
149 void logSql(
const char* file,
int line, Log::LogLevel level,
const QString& sql);
180 static QDateTime
utcTime(
const QVariant& value);
194 template <
typename T>
223 bool checkExecutingThread()
const;
224 void recordQueryError(
const QSqlQuery& query);
225 void createSqliteDatabase();
226 bool setSqliteForeignKeyChecking(
bool value);
229 QString _connectionName;
231 bool _createOnOpenFailure =
true;
233 QString _dataSourceError;
234 QString _driverError;
235 QString _databaseError;
236 QString _nativeError;
238 int64_t _threadId = 0;
static QString commaDelimitedStringList(const QStringList &list)
Build a comma-delimited string from a list of strings.
virtual bool closeConnection()
Close the current database connection.
virtual bool migrate()
Perform any necessary database migrations.
bool executeMultiple(const QStringList &queries)
Execute multiple SQL statements in sequence.
DatabaseCredentials credentials() const
Get the current database credentials.
QSqlQuery prepareQuery(const QString &sql, bool *success=nullptr)
Prepare a QSqlQuery from the given SQL string.
bool executeQuery(QSqlQuery &query)
Execute an already-prepared QSqlQuery.
static QString commaDelimitedList(const QList< T > &list)
Build a comma-delimited string from a list of values.
bool createOnOpenFailure() const
Return true if the database should be created when opening fails.
bool isSqlite() const
Return true if the current credentials specify a SQLite engine.
QSqlQuery executeQuery(const QString &sql, bool *success=nullptr)
Execute a SQL string and return the resulting query.
virtual QString createSql() const
Return the SQL used to create the database schema.
static QString commaDelimitedUuidList(const QList< QUuid > &list)
Build a comma-delimited string from a list of UUIDs.
virtual bool executePostCreateScripts()
Execute any post-creation scripts after a new database is created.
bool querySuccessful(const QSqlQuery &query)
Check whether a query completed without error.
void setDataSourceError(const QString &value)
Set the data source error string.
void setCredentials(const DatabaseCredentials &value)
Set the database credentials.
virtual bool openConnection()
Open a database connection using the stored credentials.
QString errorText() const
Get a human-readable string describing the last error.
void setConnectionName(const QString &value)
Set the Qt connection name.
static bool isSqlite(const QString &filename)
Check whether the given file is a valid SQLite database.
static QString commaDelimitedIntList(const QList< int > &list)
Build a comma-delimited string from a list of integers.
static QString escapedString(const QString &unescaped)
Escape special characters in a string for safe inclusion in SQL.
static QDateTime utcTime(const QVariant &value)
Convert a QVariant database value to a UTC QDateTime.
virtual ~DataSource()
Destructor.
void logFailure(const QSqlQuery &query) const
Log details of a failed query.
DataSource(const DatabaseCredentials &credentials)
Construct a DataSource with the given database credentials.
void logSql(const char *file, int line, Log::LogLevel level, const QString &sql)
Log a SQL statement at the given log level.
virtual bool integrityCheck()
Run an integrity check on the database.
bool isOpen()
Return true if the database connection is currently open.
static QString currentTimestamp()
Get the current UTC timestamp as a formatted string suitable for SQL.
bool recreateSqliteDatabase()
Delete and recreate the SQLite database file from createSql().
QSqlDatabase _db
The underlying QSqlDatabase connection object.
QString connectionName() const
Get the Qt connection name for this data source.
DataSource()
Construct a DataSource with default (empty) credentials.
void setCreateOnOpenFailure(bool value)
Set whether to create the database when opening fails.
bool isSqlite() const
Return true if the engine is SQLite.