1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#ifndef DBMANAGERIMPL_H
#define DBMANAGERIMPL_H
#include "db/db.h"
#include "coreSQLiteStudio_global.h"
#include "common/strhash.h"
#include "common/global.h"
#include "services/dbmanager.h"
#include <QObject>
#include <QList>
#include <QHash>
#include <QReadWriteLock>
#include <QSharedPointer>
class InvalidDb;
class API_EXPORT DbManagerImpl : public DbManager
{
Q_OBJECT
public:
/**
* @brief Creates database manager.
* @param parent Parent object passed to QObject constructor.
*/
explicit DbManagerImpl(QObject *parent = 0);
/**
* @brief Default destructor.
*/
~DbManagerImpl();
bool addDb(const QString &name, const QString &path, const QHash<QString, QVariant> &options, bool permanent = true);
bool addDb(const QString &name, const QString &path, bool permanent = true);
bool updateDb(Db* db, const QString &name, const QString &path, const QHash<QString, QVariant> &options, bool permanent);
void removeDbByName(const QString& name, Qt::CaseSensitivity cs = Qt::CaseSensitive);
void removeDbByPath(const QString& path);
void removeDb(Db* db);
QList<Db*> getDbList();
QList<Db*> getValidDbList();
QList<Db*> getConnectedDbList();
QStringList getDbNames();
QStringList getValidDbNames();
Db* getByName(const QString& name, Qt::CaseSensitivity cs = Qt::CaseInsensitive);
Db* getByPath(const QString& path);
Db* createInMemDb(bool pureInit = false);
bool isTemporary(Db* db);
QString quickAddDb(const QString &path, const QHash<QString, QVariant> &options);
DbPlugin* getPluginForDbFile(const QString& filePath);
QString generateUniqueDbName(const QString& filePath);
QString generateUniqueDbName(DbPlugin* plugin, const QString& filePath);
/**
* @brief Defines database plugin used for creating in-memory databases.
* @param plugin Plugin to use.
*/
void setInMemDbCreatorPlugin(DbPlugin* plugin);
private:
/**
* @brief Internal manager initialization.
*
* Called from any constructor.
*/
void init();
/**
* @brief Loads initial list of databases.
*
* Loaded databases are initially the invalid databases.
* They are turned into valid databases once their plugins are loaded.
*/
void loadInitialDbList();
/**
* @brief Removes database from application.
* @param db Database to be removed.
* @param alsoFromConfig If true, database will also be removed from configuration file, otherwise it's just from the manager.
*
* This method is internally called by public methods, as they all do pretty much the same thing,
* except they accept different input parameter. Then this method does the actual job.
*/
void removeDbInternal(Db* db, bool alsoFromConfig = true);
/**
* @brief Adds database to the application.
* @param db Database to be added.
* @param alsoToConfig If true, the database will also be added to configuration file, otherwise it will be onle to the manager.
*
* When addDb() is called, it calls DbPlugin#getInstance() and if it returns object, then this method
* is called to register the database object in dbList variable.
*/
void addDbInternal(Db* db, bool alsoToConfig = true);
/**
* @brief Filters invalid databases from all managed databases.
* @return Only invalid databases from this manager.
*/
QList<Db*> getInvalidDatabases() const;
Db* tryToLoadDb(InvalidDb* invalidDb, bool emitNotifySignal = true);
/**
* @brief Creates database object.
* @param name Symbolic name of the database.
* @param path Database file path.
* @param options Database options, such as password, etc.
* @param errorMessages If not null, then the error messages from DbPlugins are stored in that string (in case this method returns null).
* @return Database object, or null pointer.
*
* This method is used internally by addDb() methods. It goes through all DbPlugin instances
* and checks if any of them supports given file path and options and returns a database object.
* First plugin that provides database object is accepted and its result is returned from the method.
*/
static Db* createDb(const QString &name, const QString &path, const QHash<QString, QVariant> &options, QString* errorMessages = nullptr);
/**
* @brief Registered databases list. Both permanent and transient databases.
*/
QList<Db*> dbList;
/**
* @brief Database ame to database instance mapping, with keys being case insensitive.
*/
StrHash<Db*> nameToDb;
/**
* @brief Mapping from file path to the database.
*
* Mapping from database file path (as passed to addDb() or updateDb()) to the actual database object.
*/
QHash<QString,Db*> pathToDb;
/**
* @brief Lock for dbList.
* Lock for dbList, so the list can be accessed from multiple threads.
*/
QReadWriteLock listLock;
/**
* @brief Database plugin used to create in-memory databases.
*/
DbPlugin* inMemDbCreatorPlugin = nullptr;
QList<DbPlugin*> dbPlugins;
private slots:
/**
* @brief Slot called when connected to db.
*
* The slot is connected to the database object, therefore the database object has to be extracted from signal sender
* and converted to database type, then passed to the dbConnected(Db* db) signal.
*/
void dbConnectedSlot();
/**
* @brief Slot called when connected to db.
*
* The slot is connected to the database object, therefore the database object has to be extracted from signal sender
* and converted to database type, then passed to the dbConnected(Db* db) signal.
*/
void dbDisconnectedSlot();
/**
* @brief Passes Db::aboutToDisconnect() signal to dbAboutToBeDisconnected() signal.
*/
void dbAboutToDisconnect(bool& deny);
/**
* @brief Removes databases handled by the plugin from the list.
* @param plugin DbPlugin (any other will be ignored).
* @param type DbPlugin type.
* It removes all databases handled by the plugin being unloaded from the list of managed databases.
*/
void aboutToUnload(Plugin* plugin, PluginType* type);
/**
* @brief Adds all configured databases handled by the plugin to managed list.
* @param plugin DbPlugin (any other will be ignored).
* @param type DbPlugin type.
* Checks configuration for any databases managed by the plugin and if there is any, it's loaded into the managed list.
*/
void loaded(Plugin* plugin, PluginType* type);
public slots:
void notifyDatabasesAreLoaded();
void scanForNewDatabasesInConfig();
void rescanInvalidDatabasesForPlugin(DbPlugin* dbPlugin);
};
#endif // DBMANAGERIMPL_H
|