File indexing completed on 2025-01-05 04:46:56

0001 /*
0002     SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QSettings>
0010 #include <QSqlDatabase>
0011 
0012 namespace Akonadi
0013 {
0014 namespace Server
0015 {
0016 /**
0017  * A base class that provides an unique access layer to configuration
0018  * and initialization of different database backends.
0019  */
0020 class DbConfig
0021 {
0022 public:
0023     virtual ~DbConfig();
0024 
0025     /**
0026      * Returns whether database have been configured.
0027      */
0028     static bool isConfigured();
0029 
0030     /**
0031      * Returns the DbConfig instance for the database the user has
0032      * configured.
0033      */
0034     static DbConfig *configuredDatabase();
0035 
0036     /**
0037      * Destroys the current global DbConfig instance.
0038      *
0039      * The subsequent call to configuredDatabase() will create a new DbConfig
0040      * from current configuration.
0041      */
0042     static void destroy();
0043 
0044     /**
0045      * Returns the name of the used driver.
0046      */
0047     virtual QString driverName() const = 0;
0048 
0049     /**
0050      * Returns the database name.
0051      */
0052     virtual QString databaseName() const = 0;
0053 
0054     /**
0055      * Returns path to the database file or directory.
0056      */
0057     virtual QString databasePath() const = 0;
0058 
0059     /**
0060      * Set the path to the database file or directory.
0061      */
0062     virtual void setDatabasePath(const QString &path, QSettings &settings) = 0;
0063 
0064     /**
0065      * This method is called whenever the Akonadi server is started
0066      * and before the initial database connection is set up.
0067      *
0068      * At this point the default settings should be determined, merged
0069      * with the given @p settings and written back if @p storeSettings is true.
0070      *
0071      * When overrideDbPath is specified, the database will be stored inside this
0072      * directory instead of the default location.
0073      */
0074     virtual bool init(QSettings &settings, bool storeSettings = true, const QString &overrideDbPath = {}) = 0;
0075 
0076     /**
0077      * This method checks if the requirements for this database connection are met
0078      * in the system (i.e. QMYSQL driver is available, mysqld binary is found, etc.).
0079      */
0080     virtual bool isAvailable(QSettings &settings) = 0;
0081 
0082     /**
0083      * This method applies the configured settings to the QtSql @p database
0084      * instance.
0085      */
0086     virtual void apply(QSqlDatabase &database) = 0;
0087 
0088     /**
0089      * Do session setup/initialization work on @p database.
0090      * An example would be to run some SQL commands on every new session,
0091      * typically stuff like setting encodings, transaction isolation levels, etc.
0092      */
0093     virtual void initSession(const QSqlDatabase &database);
0094 
0095     /**
0096      * Returns whether an internal server needs to be used.
0097      */
0098     virtual bool useInternalServer() const = 0;
0099 
0100     /**
0101      * This method is called to start an external server.
0102      */
0103     virtual bool startInternalServer();
0104 
0105     /**
0106      * This method is called to stop the external server.
0107      */
0108     virtual void stopInternalServer();
0109 
0110     /**
0111      * Payload data bigger than this value will be stored in separate files, instead of the database. Valid
0112      *
0113      * @return the size threshold in bytes, defaults to 4096.
0114      */
0115     virtual qint64 sizeThreshold() const;
0116 
0117     /**
0118      * This method is called to setup initial database settings after a connection is established.
0119      */
0120     virtual void setup();
0121 
0122     /**
0123      * Disables foreign key constraint checks.
0124      */
0125     virtual bool disableConstraintChecks(const QSqlDatabase &db) = 0;
0126 
0127     /**
0128      * Re-enables foreign key constraint checks.
0129      */
0130     virtual bool enableConstraintChecks(const QSqlDatabase &db) = 0;
0131 
0132 protected:
0133     explicit DbConfig();
0134     explicit DbConfig(const QString &configFile);
0135 
0136     /**
0137      * Returns the suggested default database name, if none is specified in the configuration already.
0138      * This includes instance namespaces, so usually this is not necessary to use in combination
0139      * with internal databases (in process or using our own server instance).
0140      */
0141     static QString defaultDatabaseName();
0142 
0143     /*
0144      * Returns the Database backend we should use by default. Usually it should be the same value
0145      * configured as AKONADI_DATABASE_BACKEND at build time, but this method checks if that
0146      * backend is really available and if it's not, it falls back to returning "QSQLITE".
0147      */
0148     static QString defaultAvailableDatabaseBackend(QSettings &settings);
0149 
0150     /**
0151      * Calls QProcess::execute() and also prints the command and arguments via qCDebug()
0152      */
0153     int execute(const QString &cmd, const QStringList &args) const;
0154 
0155 private:
0156     Q_DISABLE_COPY(DbConfig)
0157 
0158     qint64 mSizeThreshold;
0159 };
0160 
0161 } // namespace Server
0162 } // namespace Akonadi