Akonadi

dbconfig.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "dbconfig.h"
8
9#include "akonadiserver_debug.h"
10#include "dbconfigmysql.h"
11#include "dbconfigpostgresql.h"
12#include "dbconfigsqlite.h"
13
14#include <config-akonadi.h>
15
16#include "private/instance_p.h"
17#include "private/standarddirs_p.h"
18
19#include <QProcess>
20#include <memory>
21
22using namespace Akonadi;
23using namespace Akonadi::Server;
24
25// TODO: make me Q_GLOBAL_STATIC
26static DbConfig *s_DbConfigInstance = nullptr;
27
28DbConfig::DbConfig()
29 : DbConfig(StandardDirs::serverConfigFile(StandardDirs::ReadWrite))
30{
31}
32
33DbConfig::DbConfig(const QString &configFile)
34{
35 QSettings settings(configFile, QSettings::IniFormat);
36
37 mSizeThreshold = 4096;
38 const QVariant value = settings.value(QStringLiteral("General/SizeThreshold"), mSizeThreshold);
39 if (value.canConvert<qint64>()) {
40 mSizeThreshold = value.value<qint64>();
41 if (mSizeThreshold < 0) {
42 mSizeThreshold = 0;
43 }
44 } else {
45 mSizeThreshold = 0;
46 }
47}
48
49DbConfig::~DbConfig()
50{
51}
52
54{
55 return s_DbConfigInstance;
56}
57
58QString DbConfig::defaultAvailableDatabaseBackend(QSettings &settings)
59{
60 QString driverName = QStringLiteral(AKONADI_DATABASE_BACKEND);
61
62 std::unique_ptr<DbConfig> dbConfigFallbackTest;
63 if (driverName == QLatin1StringView("QMYSQL")) {
64 dbConfigFallbackTest = std::make_unique<DbConfigMysql>();
65 } else if (driverName == QLatin1StringView("QPSQL")) {
66 dbConfigFallbackTest = std::make_unique<DbConfigPostgresql>();
67 }
68
69 if (dbConfigFallbackTest && !dbConfigFallbackTest->isAvailable(settings) && DbConfigSqlite().isAvailable(settings)) {
70 qCWarning(AKONADISERVER_LOG) << driverName << " requirements not available. Falling back to using QSQLITE.";
71 driverName = QStringLiteral("QSQLITE");
72 }
73
74 return driverName;
75}
76
78{
79 if (!s_DbConfigInstance) {
80 const QString serverConfigFile = StandardDirs::serverConfigFile(StandardDirs::ReadWrite);
81 QSettings settings(serverConfigFile, QSettings::IniFormat);
82
83 // determine driver to use
84 QString driverName = settings.value(QStringLiteral("General/Driver")).toString();
85 if (driverName.isEmpty()) {
86 driverName = defaultAvailableDatabaseBackend(settings);
87
88 // when using the default, write it explicitly, in case the default changes later
89 settings.setValue(QStringLiteral("General/Driver"), driverName);
90 settings.sync();
91 }
92
93 if (driverName == QLatin1StringView("QMYSQL")) {
94 s_DbConfigInstance = new DbConfigMysql;
95 } else if (driverName == QLatin1StringView("QSQLITE") || driverName == QLatin1StringView("QSQLITE3")) {
96 // QSQLITE3 is legacy name for the Akonadi fork of the upstream QSQLITE driver.
97 // It is kept here for backwards compatibility with old server config files.
98 s_DbConfigInstance = new DbConfigSqlite();
99 } else if (driverName == QLatin1StringView("QPSQL")) {
100 s_DbConfigInstance = new DbConfigPostgresql;
101 } else {
102 qCCritical(AKONADISERVER_LOG) << "Unknown database driver: " << driverName;
103 qCCritical(AKONADISERVER_LOG) << "Available drivers are: " << QSqlDatabase::drivers();
104 return nullptr;
105 }
106
107 if (!s_DbConfigInstance->init(settings)) {
108 delete s_DbConfigInstance;
109 s_DbConfigInstance = nullptr;
110 }
111 }
112
113 return s_DbConfigInstance;
114}
115
117{
118 delete s_DbConfigInstance;
119 s_DbConfigInstance = nullptr;
120}
121
123{
124 // do nothing
125 return true;
126}
127
129{
130 // do nothing
131}
132
134{
135 // do nothing
136}
137
139{
140 return mSizeThreshold;
141}
142
144{
145 if (!Instance::hasIdentifier()) {
146 return QStringLiteral("akonadi");
147 }
148 // dash is not allowed in PSQL
149 return QLatin1StringView("akonadi_") % Instance::identifier().replace(QLatin1Char('-'), QLatin1Char('_'));
150}
151
153{
154 Q_UNUSED(database)
155}
156
157int DbConfig::execute(const QString &cmd, const QStringList &args) const
158{
159 qCDebug(AKONADISERVER_LOG) << "Executing: " << cmd << args.join(QLatin1Char(' '));
160 return QProcess::execute(cmd, args);
161}
A base class that provides an unique access layer to configuration and initialization of different da...
Definition dbconfig.h:21
virtual void setup()
This method is called to setup initial database settings after a connection is established.
Definition dbconfig.cpp:133
virtual void initSession(const QSqlDatabase &database)
Do session setup/initialization work on database.
Definition dbconfig.cpp:152
static QString defaultDatabaseName()
Returns the suggested default database name, if none is specified in the configuration already.
Definition dbconfig.cpp:143
static bool isConfigured()
Returns whether database have been configured.
Definition dbconfig.cpp:53
virtual QString driverName() const =0
Returns the name of the used driver.
virtual bool isAvailable(QSettings &settings)=0
This method checks if the requirements for this database connection are met in the system (i....
virtual qint64 sizeThreshold() const
Payload data bigger than this value will be stored in separate files, instead of the database.
Definition dbconfig.cpp:138
virtual void stopInternalServer()
This method is called to stop the external server.
Definition dbconfig.cpp:128
virtual bool init(QSettings &settings, bool storeSettings=true, const QString &overrideDbPath={})=0
This method is called whenever the Akonadi server is started and before the initial database connecti...
static DbConfig * configuredDatabase()
Returns the DbConfig instance for the database the user has configured.
Definition dbconfig.cpp:77
virtual bool startInternalServer()
This method is called to start an external server.
Definition dbconfig.cpp:122
int execute(const QString &cmd, const QStringList &args) const
Calls QProcess::execute() and also prints the command and arguments via qCDebug()
Definition dbconfig.cpp:157
static void destroy()
Destroys the current global DbConfig instance.
Definition dbconfig.cpp:116
Helper integration between Akonadi and Qt.
int execute(const QString &program, const QStringList &arguments)
void setValue(QAnyStringView key, const QVariant &value)
void sync()
QVariant value(QAnyStringView key) const const
QStringList drivers()
bool isEmpty() const const
QString join(QChar separator) const const
bool canConvert() const const
QString toString() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.