Refactoring of database factory -> preparation for MySQL implementation.

This commit is contained in:
Martin Rotter 2014-02-07 14:55:42 +01:00
parent faf934e7c6
commit e134f1609f
2 changed files with 57 additions and 32 deletions

View File

@ -18,7 +18,6 @@ DatabaseFactory::DatabaseFactory(QObject *parent)
m_sqliteInMemoryDatabaseInitialized(false) { m_sqliteInMemoryDatabaseInitialized(false) {
setObjectName("DatabaseFactory"); setObjectName("DatabaseFactory");
determineDriver(); determineDriver();
sqliteAssemblyDatabaseFilePath();
} }
DatabaseFactory::~DatabaseFactory() { DatabaseFactory::~DatabaseFactory() {
@ -36,13 +35,13 @@ DatabaseFactory *DatabaseFactory::instance() {
void DatabaseFactory::sqliteAssemblyDatabaseFilePath() { void DatabaseFactory::sqliteAssemblyDatabaseFilePath() {
if (Settings::instance()->type() == Settings::Portable) { if (Settings::instance()->type() == Settings::Portable) {
m_sqliteDatabaseFilePath = qApp->applicationDirPath() + m_sqliteDatabaseFilePath = qApp->applicationDirPath() +
QDir::separator() + QDir::separator() +
QString(APP_DB_PATH); QString(APP_DB_PATH);
} }
else { else {
m_sqliteDatabaseFilePath = QDir::homePath() + QDir::separator() + m_sqliteDatabaseFilePath = QDir::homePath() + QDir::separator() +
QString(APP_LOW_H_NAME) + QDir::separator() + QString(APP_LOW_H_NAME) + QDir::separator() +
QString(APP_DB_PATH); QString(APP_DB_PATH);
} }
} }
@ -226,7 +225,7 @@ QSqlDatabase DatabaseFactory::sqliteInitializeFileBasedDatabase(const QString &c
QSqlDatabase DatabaseFactory::connection(const QString &connection_name, QSqlDatabase DatabaseFactory::connection(const QString &connection_name,
DesiredType desired_type) { DesiredType desired_type) {
if (desired_type == DatabaseFactory::StrictlyInMemory || if (desired_type == DatabaseFactory::StrictlyInMemory ||
(desired_type == DatabaseFactory::FromSettings && m_sqliteInMemoryDatabaseEnabled)) { (desired_type == DatabaseFactory::FromSettings && m_activeDatabaseDriver == SQLITE_MEMORY)) {
// We request in-memory database (either user don't care // We request in-memory database (either user don't care
// about the type or user overrided it in the settings). // about the type or user overrided it in the settings).
if (!m_sqliteInMemoryDatabaseInitialized) { if (!m_sqliteInMemoryDatabaseInitialized) {
@ -299,11 +298,7 @@ void DatabaseFactory::removeConnection(const QString &connection_name) {
QSqlDatabase::removeDatabase(connection_name); QSqlDatabase::removeDatabase(connection_name);
} }
void DatabaseFactory::saveMemoryDatabase() { void DatabaseFactory::sqliteSaveMemoryDatabase() {
if (!m_sqliteInMemoryDatabaseEnabled) {
return;
}
qDebug("Saving in-memory working database back to persistent file-based storage."); qDebug("Saving in-memory working database back to persistent file-based storage.");
QSqlDatabase database = connection(objectName(), StrictlyInMemory); QSqlDatabase database = connection(objectName(), StrictlyInMemory);
@ -328,19 +323,58 @@ void DatabaseFactory::saveMemoryDatabase() {
} }
void DatabaseFactory::determineDriver() { void DatabaseFactory::determineDriver() {
m_sqliteInMemoryDatabaseEnabled = Settings::instance()->value(APP_CFG_GEN, "use_in_memory_db", false).toBool(); QString db_driver = Settings::instance()->value(APP_CFG_GEN, "database_driver", APP_DB_DRIVER_SQLITE).toString();
qDebug("Working database source was determined as %s.", if (db_driver == APP_DB_DRIVER_MYSQL && QSqlDatabase::isDriverAvailable(APP_DB_DRIVER_MYSQL)) {
m_sqliteInMemoryDatabaseEnabled ? "in-memory database" : "file-based database"); // User wants to use MySQL and MySQL is actually available. Use it.
// TODO: Perform username & password check if db is really fine.
m_activeDatabaseDriver = MYSQL;
qDebug("Working database source was as MySQL database.");
}
else {
// User wants to use SQLite, which is always available. Check if file-based
// or in-memory database will be used.
if (Settings::instance()->value(APP_CFG_GEN, "use_in_memory_db", false).toBool()) {
// Use in-memory SQLite database.
m_activeDatabaseDriver = SQLITE_MEMORY;
qDebug("Working database source was determined as SQLite in-memory database.");
}
else {
// Use strictly file-base SQLite database.
m_activeDatabaseDriver = SQLITE;
qDebug("Working database source was determined as SQLite file-based database.");
}
sqliteAssemblyDatabaseFilePath();
}
} }
void DatabaseFactory::saveDatabase() { void DatabaseFactory::saveDatabase() {
saveMemoryDatabase(); switch (m_activeDatabaseDriver) {
case SQLITE_MEMORY:
sqliteSaveMemoryDatabase();
break;
default:
break;
}
} }
bool DatabaseFactory::vacuumDatabase() { bool DatabaseFactory::vacuumDatabase() {
QSqlDatabase database = connection(objectName(), FromSettings); switch (m_activeDatabaseDriver) {
QSqlQuery query_vacuum(database); case SQLITE_MEMORY:
case SQLITE: {
QSqlDatabase database = connection(objectName(), FromSettings);
QSqlQuery query_vacuum(database);
return query_vacuum.exec("VACUUM"); return query_vacuum.exec("VACUUM");
break;
}
default:
return false;
}
} }

View File

@ -55,23 +55,18 @@ class DatabaseFactory : public QObject {
// to gracefully exit the application. // to gracefully exit the application.
void saveDatabase(); void saveDatabase();
// Performs cleanup of the database.
bool vacuumDatabase();
// Singleton getter. // Singleton getter.
static DatabaseFactory *instance(); static DatabaseFactory *instance();
//
// SQLITE stuff.
//
// Performs "VACUUM" on the database and
// returns true of operation succeeded.
bool vacuumDatabase();
private: private:
// //
// GENERAL stuff. // GENERAL stuff.
// //
// Conctructor. // Constructor.
explicit DatabaseFactory(QObject *parent = 0); explicit DatabaseFactory(QObject *parent = 0);
// Decides which database backend will be used in this // Decides which database backend will be used in this
@ -90,7 +85,7 @@ class DatabaseFactory : public QObject {
// Performs saving of items from in-memory database // Performs saving of items from in-memory database
// to file-based database. // to file-based database.
void saveMemoryDatabase(); void sqliteSaveMemoryDatabase();
// Assemblies database file path. // Assemblies database file path.
void sqliteAssemblyDatabaseFilePath(); void sqliteAssemblyDatabaseFilePath();
@ -106,10 +101,6 @@ class DatabaseFactory : public QObject {
// Is database file initialized? // Is database file initialized?
bool m_sqliteFileBasedDatabaseinitialized; bool m_sqliteFileBasedDatabaseinitialized;
bool m_sqliteInMemoryDatabaseInitialized; bool m_sqliteInMemoryDatabaseInitialized;
// Is true when user selected in-memory database.
// NOTE: This can be changed only on application startup.
bool m_sqliteInMemoryDatabaseEnabled;
}; };
#endif // DATABASEFACTORY_H #endif // DATABASEFACTORY_H