Akonadi

dbconfig.h
1/*
2 SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include <QSettings>
10#include <QSqlDatabase>
11
12namespace Akonadi
13{
14namespace Server
15{
16/**
17 * A base class that provides an unique access layer to configuration
18 * and initialization of different database backends.
19 */
21{
22public:
23 virtual ~DbConfig();
24
25 /**
26 * Returns whether database have been configured.
27 */
28 static bool isConfigured();
29
30 /**
31 * Returns the DbConfig instance for the database the user has
32 * configured.
33 */
35
36 /**
37 * Destroys the current global DbConfig instance.
38 *
39 * The subsequent call to configuredDatabase() will create a new DbConfig
40 * from current configuration.
41 */
42 static void destroy();
43
44 /**
45 * Returns the name of the used driver.
46 */
47 virtual QString driverName() const = 0;
48
49 /**
50 * Returns the database name.
51 */
52 virtual QString databaseName() const = 0;
53
54 /**
55 * Returns path to the database file or directory.
56 */
57 virtual QString databasePath() const = 0;
58
59 /**
60 * Set the path to the database file or directory.
61 */
62 virtual void setDatabasePath(const QString &path, QSettings &settings) = 0;
63
64 /**
65 * This method is called whenever the Akonadi server is started
66 * and before the initial database connection is set up.
67 *
68 * At this point the default settings should be determined, merged
69 * with the given @p settings and written back if @p storeSettings is true.
70 *
71 * When overrideDbPath is specified, the database will be stored inside this
72 * directory instead of the default location.
73 */
74 virtual bool init(QSettings &settings, bool storeSettings = true, const QString &overrideDbPath = {}) = 0;
75
76 /**
77 * This method checks if the requirements for this database connection are met
78 * in the system (i.e. QMYSQL driver is available, mysqld binary is found, etc.).
79 */
80 virtual bool isAvailable(QSettings &settings) = 0;
81
82 /**
83 * This method applies the configured settings to the QtSql @p database
84 * instance.
85 */
86 virtual void apply(QSqlDatabase &database) = 0;
87
88 /**
89 * Do session setup/initialization work on @p database.
90 * An example would be to run some SQL commands on every new session,
91 * typically stuff like setting encodings, transaction isolation levels, etc.
92 */
93 virtual void initSession(const QSqlDatabase &database);
94
95 /**
96 * Returns whether an internal server needs to be used.
97 */
98 virtual bool useInternalServer() const = 0;
99
100 /**
101 * This method is called to start an external server.
102 */
103 virtual bool startInternalServer();
104
105 /**
106 * This method is called to stop the external server.
107 */
108 virtual void stopInternalServer();
109
110 /**
111 * Payload data bigger than this value will be stored in separate files, instead of the database. Valid
112 *
113 * @return the size threshold in bytes, defaults to 4096.
114 */
115 virtual qint64 sizeThreshold() const;
116
117 /**
118 * This method is called to setup initial database settings after a connection is established.
119 */
120 virtual void setup();
121
122 /**
123 * Disables foreign key constraint checks.
124 */
125 virtual bool disableConstraintChecks(const QSqlDatabase &db) = 0;
126
127 /**
128 * Re-enables foreign key constraint checks.
129 */
130 virtual bool enableConstraintChecks(const QSqlDatabase &db) = 0;
131
132protected:
133 explicit DbConfig();
134 explicit DbConfig(const QString &configFile);
135
136 /**
137 * Returns the suggested default database name, if none is specified in the configuration already.
138 * This includes instance namespaces, so usually this is not necessary to use in combination
139 * with internal databases (in process or using our own server instance).
140 */
142
143 /*
144 * Returns the Database backend we should use by default. Usually it should be the same value
145 * configured as AKONADI_DATABASE_BACKEND at build time, but this method checks if that
146 * backend is really available and if it's not, it falls back to returning "QSQLITE".
147 */
148 static QString defaultAvailableDatabaseBackend(QSettings &settings);
149
150 /**
151 * Calls QProcess::execute() and also prints the command and arguments via qCDebug()
152 */
153 int execute(const QString &cmd, const QStringList &args) const;
154
155private:
156 Q_DISABLE_COPY(DbConfig)
157
158 qint64 mSizeThreshold;
159};
160
161} // namespace Server
162} // namespace Akonadi
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 apply(QSqlDatabase &database)=0
This method applies the configured settings to the QtSql database instance.
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 QString databaseName() const =0
Returns the database name.
virtual void stopInternalServer()
This method is called to stop the external server.
Definition dbconfig.cpp:128
virtual bool enableConstraintChecks(const QSqlDatabase &db)=0
Re-enables foreign key constraint checks.
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...
virtual bool disableConstraintChecks(const QSqlDatabase &db)=0
Disables foreign key constraint checks.
virtual void setDatabasePath(const QString &path, QSettings &settings)=0
Set the path to the database file or directory.
virtual QString databasePath() const =0
Returns path to the database file or directory.
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
virtual bool useInternalServer() const =0
Returns whether an internal server needs to be used.
static void destroy()
Destroys the current global DbConfig instance.
Definition dbconfig.cpp:116
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:49:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.