7#include "dbconfigsqlite.h"
8#include "akonadiserver_debug.h"
11#include "private/standarddirs_p.h"
19using namespace Akonadi::Server;
23 QString akonadiHomeDir = dbPathOverride.
isEmpty() ? StandardDirs::saveDir(
"data") : dbPathOverride;
24 if (!
QDir(akonadiHomeDir).exists()) {
26 qCCritical(AKONADISERVER_LOG) <<
"Unable to create" << akonadiHomeDir <<
"during database initialization";
31 akonadiHomeDir += QStringLiteral(
"/");
33 return akonadiHomeDir;
44 QFile file(akonadiPath);
46 qCCritical(AKONADISERVER_LOG) <<
"Unable to create file" << akonadiPath <<
"during database initialization.";
55DbConfigSqlite::DbConfigSqlite(
const QString &configFile)
60QString DbConfigSqlite::driverName()
const
62 return QStringLiteral(
"QSQLITE");
65QString DbConfigSqlite::databaseName()
const
70QString DbConfigSqlite::databasePath()
const
75void DbConfigSqlite::setDatabasePath(
const QString &path,
QSettings &settings)
79 settings.
setValue(QStringLiteral(
"Name"), mDatabaseName);
83bool DbConfigSqlite::init(
QSettings &settings,
bool storeSettings,
const QString &dbPathOverride)
86 const QString defaultDbName = sqliteDataFile(dbPathOverride);
93 mDatabaseName = settings.
value(QStringLiteral(
"Name"), defaultDbName).
toString();
94 mHostName = settings.
value(QStringLiteral(
"Host")).
toString();
95 mUserName = settings.
value(QStringLiteral(
"User")).
toString();
96 mPassword = settings.
value(QStringLiteral(
"Password")).
toString();
97 mConnectionOptions = settings.
value(QStringLiteral(
"Options")).
toString();
103 settings.
setValue(QStringLiteral(
"Name"), mDatabaseName);
111bool DbConfigSqlite::isAvailable(
QSettings &settings)
117 if (!init(settings,
false)) {
126 if (!mDatabaseName.
isEmpty()) {
145bool DbConfigSqlite::useInternalServer()
const
152 if (!
query.exec(QStringLiteral(
"PRAGMA %1").arg(pragma))) {
153 qCCritical(AKONADISERVER_LOG) <<
"Could not set sqlite PRAGMA " << pragma;
154 qCCritical(AKONADISERVER_LOG) <<
"Database: " << mDatabaseName;
155 qCCritical(AKONADISERVER_LOG) <<
"Query error: " <<
query.lastError().text();
156 qCCritical(AKONADISERVER_LOG) <<
"Database error: " << db.
lastError().
text();
162void DbConfigSqlite::setup()
170 qCCritical(AKONADISERVER_LOG) <<
"Invalid database for" << mDatabaseName <<
"with driver" << driverName();
175 if (!finfo.dir().exists()) {
177 dir.mkpath(finfo.path());
181 QFile dbFile(mDatabaseName);
185 if (dbFile.size() == 0) {
186 if (Utils::getDirectoryFileSystem(mDatabaseName) ==
QLatin1StringView(
"btrfs")) {
187 Utils::disableCoW(mDatabaseName);
194 qCCritical(AKONADISERVER_LOG) <<
"Could not open sqlite database" << mDatabaseName <<
"with driver" << driverName() <<
"for initialization";
202 if (!
query.exec(QStringLiteral(
"SELECT sqlite_version()"))) {
203 qCCritical(AKONADISERVER_LOG) <<
"Could not query sqlite version";
204 qCCritical(AKONADISERVER_LOG) <<
"Database: " << mDatabaseName;
205 qCCritical(AKONADISERVER_LOG) <<
"Query error: " <<
query.lastError().text();
206 qCCritical(AKONADISERVER_LOG) <<
"Database error: " << db.
lastError().
text();
212 qCCritical(AKONADISERVER_LOG) <<
"Could not query sqlite version";
213 qCCritical(AKONADISERVER_LOG) <<
"Database: " << mDatabaseName;
214 qCCritical(AKONADISERVER_LOG) <<
"Query error: " <<
query.lastError().text();
215 qCCritical(AKONADISERVER_LOG) <<
"Database error: " << db.
lastError().
text();
221 qCDebug(AKONADISERVER_LOG) <<
"sqlite version is " << sqliteVersion;
224 const int sqliteVersionMajor =
list[0].toInt();
225 const int sqliteVersionMinor =
list[1].toInt();
228 if (!setPragma(db, query, QStringLiteral(
"synchronous=1"))) {
233 if (sqliteVersionMajor < 3 && sqliteVersionMinor < 7) {
240 if (!setPragma(db, query, QStringLiteral(
"journal_mode=wal"))) {
246 qCCritical(AKONADISERVER_LOG) <<
"Could not query sqlite journal mode";
247 qCCritical(AKONADISERVER_LOG) <<
"Database: " << mDatabaseName;
248 qCCritical(AKONADISERVER_LOG) <<
"Query error: " <<
query.lastError().text();
249 qCCritical(AKONADISERVER_LOG) <<
"Database error: " << db.
lastError().
text();
255 qCDebug(AKONADISERVER_LOG) <<
"sqlite journal mode is " << journalMode;
258 if (!setPragma(db, query, QStringLiteral(
"page_size=4096"))) {
264 if (!setPragma(db, query, QStringLiteral(
"cache_size=100000"))) {
270 if (!setPragma(db, query, QStringLiteral(
"temp_store=MEMORY"))) {
276 if (!setPragma(db, query, QStringLiteral(
"foreign_keys=ON"))) {
287bool DbConfigSqlite::disableConstraintChecks(
const QSqlDatabase &db)
290 return query.exec(QStringLiteral(
"PRAGMA ignore_check_constraints=ON"));
293bool DbConfigSqlite::enableConstraintChecks(
const QSqlDatabase &db)
296 return query.exec(QStringLiteral(
"PRAGMA ignore_check_constraints=OFF"));
A base class that provides an unique access layer to configuration and initialization of different da...
Helper integration between Akonadi and Qt.
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
KIOCORE_EXPORT MkpathJob * mkpath(const QUrl &url, const QUrl &baseUrl=QUrl(), JobFlags flags=DefaultFlags)
QString path(const QString &relativePath)
KIOCORE_EXPORT QString dir(const QString &fileClass)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
bool exists() const const
T value(qsizetype i) const const
void beginGroup(QAnyStringView prefix)
void setValue(QAnyStringView key, const QVariant &value)
QVariant value(QAnyStringView key) const const
QSqlDatabase addDatabase(QSqlDriver *driver, const QString &connectionName)
QSqlDriver * driver() const const
bool isValid() const const
QSqlError lastError() const const
void removeDatabase(const QString &connectionName)
void setConnectOptions(const QString &options)
void setDatabaseName(const QString &name)
void setHostName(const QString &host)
void setPassword(const QString &password)
void setUserName(const QString &name)
virtual bool hasFeature(DriverFeature feature) const const=0
QString text() const const
bool isEmpty() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString toString() const const