diff options
| author | 2023-04-30 18:30:36 -0400 | |
|---|---|---|
| committer | 2023-04-30 18:30:36 -0400 | |
| commit | 3565aad630864ecdbe53fdaa501ea708555b3c7c (patch) | |
| tree | c743e4ad0bad39ebdb2f514c7cc52d34a257ebbe /SQLiteStudio3/coreSQLiteStudio/db/abstractdb3.h | |
| parent | 1fdc150116cad39aae5c5da407c3312b47a59e3a (diff) | |
New upstream version 3.4.4+dfsg.upstream/3.4.4+dfsg
Diffstat (limited to 'SQLiteStudio3/coreSQLiteStudio/db/abstractdb3.h')
| -rw-r--r-- | SQLiteStudio3/coreSQLiteStudio/db/abstractdb3.h | 70 |
1 files changed, 58 insertions, 12 deletions
diff --git a/SQLiteStudio3/coreSQLiteStudio/db/abstractdb3.h b/SQLiteStudio3/coreSQLiteStudio/db/abstractdb3.h index 72c1614..f8812e9 100644 --- a/SQLiteStudio3/coreSQLiteStudio/db/abstractdb3.h +++ b/SQLiteStudio3/coreSQLiteStudio/db/abstractdb3.h @@ -58,10 +58,11 @@ class AbstractDb3 : public AbstractDb bool initAfterCreated(); void initAfterOpen(); SqlQueryPtr prepare(const QString& query); - QString getTypeLabel(); + bool flushWalInternal(); + QString getTypeLabel() const; bool deregisterFunction(const QString& name, int argCount); - bool registerScalarFunction(const QString& name, int argCount); - bool registerAggregateFunction(const QString& name, int argCount); + bool registerScalarFunction(const QString& name, int argCount, bool deterministic); + bool registerAggregateFunction(const QString& name, int argCount, bool deterministic); bool registerCollationInternal(const QString& name); bool deregisterCollationInternal(const QString& name); @@ -121,6 +122,7 @@ class AbstractDb3 : public AbstractDb }; QString extractLastError(); + QString extractLastError(typename T::handle* handle); void cleanUp(); void resetError(); @@ -407,6 +409,22 @@ int AbstractDb3<T>::getErrorCodeInternal() } template <class T> +bool AbstractDb3<T>::flushWalInternal() +{ + resetError(); + if (!dbHandle) + return false; + + int res = T::wal_checkpoint_v2(dbHandle, nullptr, T::CHECKPOINT_FULL, nullptr, nullptr); + if (res != T::OK) + { + dbErrorMessage = QObject::tr("Could not run WAL checkpoint: %1").arg(extractLastError()); + dbErrorCode = res; + } + return res == T::OK; +} + +template <class T> bool AbstractDb3<T>::openInternal() { resetError(); @@ -414,11 +432,11 @@ bool AbstractDb3<T>::openInternal() int res = T::open_v2(path.toUtf8().constData(), &handle, T::OPEN_READWRITE|T::OPEN_CREATE, nullptr); if (res != T::OK) { + dbErrorMessage = QObject::tr("Could not open database: %1").arg(extractLastError(handle)); + dbErrorCode = res; if (handle) T::close(handle); - dbErrorMessage = QObject::tr("Could not open database: %1").arg(extractLastError()); - dbErrorCode = res; return false; } dbHandle = handle; @@ -469,7 +487,7 @@ SqlQueryPtr AbstractDb3<T>::prepare(const QString& query) } template <class T> -QString AbstractDb3<T>::getTypeLabel() +QString AbstractDb3<T>::getTypeLabel() const { return T::label; } @@ -485,7 +503,7 @@ bool AbstractDb3<T>::deregisterFunction(const QString& name, int argCount) } template <class T> -bool AbstractDb3<T>::registerScalarFunction(const QString& name, int argCount) +bool AbstractDb3<T>::registerScalarFunction(const QString& name, int argCount, bool deterministic) { if (!dbHandle) return false; @@ -495,7 +513,11 @@ bool AbstractDb3<T>::registerScalarFunction(const QString& name, int argCount) userData->name = name; userData->argCount = argCount; - int res = T::create_function_v2(dbHandle, name.toUtf8().constData(), argCount, T::UTF8, userData, + int opts = T::UTF8; + if (deterministic) + opts |= T::DETERMINISTIC; + + int res = T::create_function_v2(dbHandle, name.toUtf8().constData(), argCount, opts, userData, &AbstractDb3<T>::evaluateScalar, nullptr, nullptr, @@ -505,7 +527,7 @@ bool AbstractDb3<T>::registerScalarFunction(const QString& name, int argCount) } template <class T> -bool AbstractDb3<T>::registerAggregateFunction(const QString& name, int argCount) +bool AbstractDb3<T>::registerAggregateFunction(const QString& name, int argCount, bool deterministic) { if (!dbHandle) return false; @@ -515,7 +537,11 @@ bool AbstractDb3<T>::registerAggregateFunction(const QString& name, int argCount userData->name = name; userData->argCount = argCount; - int res = T::create_function_v2(dbHandle, name.toUtf8().constData(), argCount, T::UTF8, userData, + int opts = T::UTF8; + if (deterministic) + opts |= T::DETERMINISTIC; + + int res = T::create_function_v2(dbHandle, name.toUtf8().constData(), argCount, opts, userData, nullptr, &AbstractDb3<T>::evaluateAggregateStep, &AbstractDb3<T>::evaluateAggregateFinal, @@ -552,8 +578,14 @@ bool AbstractDb3<T>::deregisterCollationInternal(const QString& name) template <class T> QString AbstractDb3<T>::extractLastError() { - dbErrorCode = T::extended_errcode(dbHandle); - dbErrorMessage = QString::fromUtf8(T::errmsg(dbHandle)); + return extractLastError(dbHandle); +} + +template<class T> +QString AbstractDb3<T>::extractLastError(typename T::handle* handle) +{ + dbErrorCode = T::extended_errcode(handle); + dbErrorMessage = QString::fromUtf8(T::errmsg(handle)); return dbErrorMessage; } @@ -799,11 +831,25 @@ void AbstractDb3<T>::registerDefaultCollation(void* fnUserData, typename T::hand return; } + SqlQueryPtr results = db->exec("PRAGMA collation_list", Db::Flag::NO_LOCK|Db::Flag::SKIP_DROP_DETECTION); + if (results->isError()) + qWarning() << "Unable to query existing collations while registering needed collation" << collationName << ":" << db->getErrorText(); + + QStringList existingCollations = results->columnAsList<QString>("name"); + if (existingCollations.contains(collationName)) + { + qDebug() << "Requested collation" << collationName << "already exists. Probably different input encoding was expected," + << "but SQLite should deal with it. Skipping default collation registration."; + return; + } + int res = T::create_collation_v2(fnDbHandle, collationName, T::UTF8, nullptr, &AbstractDb3<T>::evaluateDefaultCollation, nullptr); if (res != T::OK) qWarning() << "Could not register default collation in AbstractDb3<T>::registerDefaultCollation()."; + else + qDebug() << "Registered default collation on demand, under name:" << collationName; } template <class T> |
