File indexing completed on 2025-01-19 03:53:33

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2007-03-18
0007  * Description : Database Engine storage container for connection parameters.
0008  *
0009  * SPDX-FileCopyrightText: 2007-2008 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  * SPDX-FileCopyrightText: 2010-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * SPDX-FileCopyrightText:      2018 by Mario Frank    <mario dot frank at uni minus potsdam dot de>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #ifndef DIGIKAM_DB_ENGINE_PARAMETERS_H
0018 #define DIGIKAM_DB_ENGINE_PARAMETERS_H
0019 
0020 // Qt includes
0021 
0022 #include <QString>
0023 #include <QtGlobal>
0024 #include <QUrl>
0025 
0026 // Local includes
0027 
0028 #include "digikam_export.h"
0029 #include "dbengineconfig.h"
0030 
0031 namespace Digikam
0032 {
0033 
0034 /**
0035  * This class encapsulates all parameters needed to establish
0036  * a connection to a database (inspired by the API of Qt::Sql).
0037  * The values can be read from and written to a QUrl.
0038  */
0039 class DIGIKAM_EXPORT DbEngineParameters
0040 {
0041 
0042 public:
0043 
0044     DbEngineParameters(const QString& _type,
0045                        const QString& _databaseNameCore,
0046                        const QString& _connectOptions = QString(),
0047                        const QString& _hostName = QString(),
0048                        int            _port = -1,
0049                        bool           _walMode = false,
0050                        bool           _internalServer = false,
0051                        const QString& _userName = QString(),
0052                        const QString& _password = QString(),
0053                        const QString& _databaseNameThumbnails = QString(),
0054                        const QString& _databaseNameFace = QString(),
0055                        const QString& _databaseNameSimilarity = QString(),
0056                        const QString& _internalServerDBPath = QString(),
0057                        const QString& _internalServerMysqlInitCmd = QString(),
0058                        const QString& _internalServerMysqlAdminCmd = QString(),
0059                        const QString& _internalServerMysqlServerCmd = QString(),
0060                        const QString& _internalServerMysqlUpgradeCmd = QString());
0061 
0062     DbEngineParameters();
0063 
0064     /**
0065      * QUrl helpers.
0066      */
0067     explicit DbEngineParameters(const QUrl& url);
0068     void insertInUrl(QUrl& url)                                 const;
0069     static void removeFromUrl(QUrl& url);
0070 
0071     bool operator==(const DbEngineParameters& other)            const;
0072     bool operator!=(const DbEngineParameters& other)            const;
0073 
0074     /**
0075      * Performs basic checks that the parameters are not empty and have the information
0076      * required for the databaseType.
0077      */
0078     bool    isValid()                                           const;
0079 
0080     bool    isSQLite()                                          const;
0081     bool    isMySQL()                                           const;
0082     QString SQLiteDatabaseFile()                                const;
0083 
0084     /**
0085      *  Returns the databaseType designating the said database.
0086      *  If you have a DbEngineParameters object already, you can use isSQLite() as well.
0087      *  These strings are identical to the driver identifiers in the Qt SQL module.
0088      */
0089     static QString SQLiteDatabaseType();
0090     static QString MySQLDatabaseType();
0091 
0092     /**
0093      * Creates a unique hash of the values stored in this object.
0094      */
0095     QByteArray hash()                                           const;
0096 
0097     /**
0098      * Return a set of default parameters for the given type. For Mysql, it return internal server configuration.
0099      */
0100     static DbEngineParameters defaultParameters(const QString& databaseType);
0101 
0102     static DbEngineParameters parametersFromConfig(const QString& configGroup = QString());
0103     /**
0104      * Read and write parameters from config. You can specify the group,
0105      * or use the default value.
0106      */
0107     void readFromConfig(const QString& configGroup = QString());
0108     void writeToConfig(const QString& configGroup = QString())  const;
0109 
0110     /**
0111      * NOTE: In case of SQLite, the database name typically is a file.
0112      * For non-SQLite, this simply handle the database name.
0113      */
0114     QString getCoreDatabaseNameOrDir()                          const;
0115     QString getThumbsDatabaseNameOrDir()                        const;
0116     QString getFaceDatabaseNameOrDir()                          const;
0117     QString getSimilarityDatabaseNameOrDir()                    const;
0118 
0119     /**
0120      * Use these methods if you set a file or a folder.
0121      */
0122     void setCoreDatabasePath(const QString& folderOrFileOrName);
0123     void setThumbsDatabasePath(const QString& folderOrFileOrName);
0124     void setFaceDatabasePath(const QString& folderOrFileOrName);
0125     void setSimilarityDatabasePath(const QString& folderOrFileOrName);
0126 
0127     static QString coreDatabaseFileSQLite(const QString& folderOrFile);
0128     static QString thumbnailDatabaseFileSQLite(const QString& folderOrFile);
0129     static QString faceDatabaseFileSQLite(const QString& folderOrFile);
0130     static QString similarityDatabaseFileSQLite(const QString& folderOrFile);
0131 
0132     static QString coreDatabaseDirectorySQLite(const QString& path);
0133     static QString thumbnailDatabaseDirectorySQLite(const QString& path);
0134     static QString faceDatabaseDirectorySQLite(const QString& path);
0135     static QString similarityDatabaseDirectorySQLite(const QString& path);
0136 
0137     /**
0138      * For Mysql internal server: manage the database path to store database files.
0139      */
0140     void    setInternalServerPath(const QString& path);
0141     QString internalServerPath()                                const;
0142 
0143     /**
0144      * Replaces databaseName with databaseNameThumbnails.
0145      */
0146     DbEngineParameters thumbnailParameters()                    const;
0147 
0148     /**
0149      * Replaces databaseName with databaseNameFace.
0150      */
0151     DbEngineParameters faceParameters()                         const;
0152 
0153     /**
0154      * Replaces databaseName with databaseNameFace.
0155      */
0156     DbEngineParameters similarityParameters()                   const;
0157 
0158     void legacyAndDefaultChecks(const QString& suggestedPath = QString());
0159     void removeLegacyConfig();
0160 
0161     /**
0162      * Convenience methods to create a DbEngineParameters object for an
0163      * SQLITE database specified by the local file path.
0164      */
0165     static DbEngineParameters parametersForSQLite(const QString& databaseFile);
0166     static DbEngineParameters parametersForSQLiteDefaultFile(const QString& directory);
0167 
0168     /**
0169      * Return the hidden path from home directory to store private
0170      * data used by internal Mysql server.
0171      */
0172     static QString serverPrivatePath();
0173 
0174     /**
0175      * Return the default Mysql initialization command name (Internal server only).
0176      */
0177     static QString defaultMysqlInitCmd();
0178 
0179     /**
0180      * Return the default Mysql server administration name (Internal server only).
0181      */
0182     static QString defaultMysqlAdminCmd();
0183 
0184     /**
0185      * Return the default Mysql server command name (Internal server only).
0186      */
0187     static QString defaultMysqlServerCmd();
0188 
0189     /**
0190      * Return the default Mysql upgrade command name (Internal server only).
0191      */
0192     static QString defaultMysqlUpgradeCmd();
0193 
0194 public:
0195 
0196     QString databaseType;
0197     QString databaseNameCore;
0198     QString connectOptions;
0199     QString hostName;
0200     int     port;
0201     bool    walMode;
0202     bool    internalServer;
0203     QString userName;
0204     QString password;
0205 
0206     QString databaseNameThumbnails;
0207     QString databaseNameFace;
0208     QString databaseNameSimilarity;
0209     QString internalServerDBPath;
0210 
0211     /**
0212      * Settings stored in config file and used only with internal server at runtime
0213      * to start server instance or init database tables.
0214      */
0215     QString internalServerMysqlInitCmd;
0216     QString internalServerMysqlAdminCmd;
0217     QString internalServerMysqlServerCmd;
0218     QString internalServerMysqlUpgradeCmd;
0219 };
0220 
0221 DIGIKAM_EXPORT QDebug operator<<(QDebug dbg, const DbEngineParameters& t);
0222 
0223 } // namespace Digikam
0224 
0225 #endif // DIGIKAM_DB_ENGINE_PARAMETERS_H