| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "database/databasefactory.h" |
| 4 | |
| 5 | #include "3rd-party/boolinq/boolinq.h" |
| 6 | #include "database/mariadbdriver.h" |
| 7 | #include "database/sqlitedriver.h" |
| 8 | #include "exceptions/applicationexception.h" |
| 9 | #include "gui/messagebox.h" |
| 10 | #include "miscellaneous/application.h" |
| 11 | #include "miscellaneous/iofactory.h" |
| 12 | #include "miscellaneous/settings.h" |
| 13 | #include "miscellaneous/textfactory.h" |
| 14 | |
| 15 | #include <QDir> |
| 16 | #include <QSqlDriver> |
| 17 | #include <QSqlQuery> |
| 18 | #include <QSqlResult> |
| 19 | #include <QVariant> |
| 20 | |
| 21 | DatabaseFactory::DatabaseFactory(QObject* parent) : QObject(parent), m_dbDriver(nullptr) { |
| 22 | determineDriver(); |
| 23 | } |
| 24 | |
| 25 | void DatabaseFactory::removeConnection(const QString& connection_name) { |
| 26 | qDebugNN << LOGSEC_DB << "Removing database connection '" << connection_name << "'."; |
| 27 | QSqlDatabase::removeDatabase(connection_name); |
| 28 | } |
| 29 | |
| 30 | void DatabaseFactory::determineDriver() { |
| 31 | m_allDbDrivers = { |
| 32 | new SqliteDriver(qApp->settings()->value(GROUP(Database), SETTING(Database::UseInMemory)).toBool(), this)}; |
| 33 | |
| 34 | if (QSqlDatabase::isDriverAvailable(QSL(APP_DB_MYSQL_DRIVER))) { |
| 35 | m_allDbDrivers.append(new MariaDbDriver(this)); |
| 36 | } |
| 37 | |
| 38 | const QString db_driver = qApp->settings()->value(GROUP(Database), SETTING(Database::ActiveDriver)).toString(); |
| 39 | |
| 40 | m_dbDriver = boolinq::from(m_allDbDrivers).firstOrDefault([db_driver](DatabaseDriver* driv) { |
| 41 | return QString::compare(driv->qtDriverCode(), db_driver, Qt::CaseSensitivity::CaseInsensitive) == 0; |
| 42 | }); |
| 43 | |
| 44 | if (m_dbDriver == nullptr) { |
| 45 | qFatal("DB driver for '%s' was not found.", qPrintable(db_driver)); |
| 46 | } |
| 47 | |
| 48 | // Try to setup connection and fallback to SQLite. |
| 49 | try { |
| 50 | m_dbDriver->connection(QSL("DatabaseFactory")); |
| 51 | } |
| 52 | catch (const ApplicationException& ex) { |
| 53 | qCriticalNN << LOGSEC_DB << "Failed to reach connection to DB source:" << QUOTE_W_SPACE_DOT(ex.message()); |
| 54 | |
| 55 | if (m_dbDriver->driverType() != DatabaseDriver::DriverType::SQLite) { |
| 56 | MsgBox::show(nullptr, |
| 57 | QMessageBox::Icon::Critical, |
| 58 | tr("Cannot connect to database"), |
| 59 | tr("Connection to your database was not established with error: '%1'. " |
| 60 | "Falling back to SQLite.") |
| 61 | .arg(ex.message())); |
| 62 | |
| 63 | m_dbDriver = boolinq::from(m_allDbDrivers).first([](DatabaseDriver* driv) { |
| 64 | return driv->driverType() == DatabaseDriver::DriverType::SQLite; |
| 65 | }); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | DatabaseDriver* DatabaseFactory::driver() const { |
| 71 | return m_dbDriver; |
| 72 | } |
| 73 | |
| 74 | DatabaseDriver* DatabaseFactory::driverForType(DatabaseDriver::DriverType d) const { |
| 75 | return boolinq::from(m_allDbDrivers).firstOrDefault([d](DatabaseDriver* driv) { |
| 76 | return driv->driverType() == d; |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | QString DatabaseFactory::lastExecutedQuery(const QSqlQuery& query) { |
| 81 | QString str = query.lastQuery(); |
| 82 | |
| 83 | #if QT_VERSION_MAJOR == 5 |
| 84 | QMapIterator<QString, QVariant> it(query.boundValues()); |
| 85 | |
| 86 | while (it.hasNext()) { |
| 87 | it.next(); |
| 88 | |
| 89 | if (it.value().type() == QVariant::Type::Char || it.value().type() == QVariant::Type::String) { |
| 90 | str.replace(it.key(), QSL("'%1'").arg(it.value().toString())); |
| 91 | } |
| 92 | else { |
| 93 | str.replace(it.key(), it.value().toString()); |
| 94 | } |
| 95 | } |
| 96 | #endif |
| 97 | |
| 98 | return str; |
| 99 | } |
| 100 | |
| 101 | QString DatabaseFactory::escapeQuery(const QString& query) { |
| 102 | return QString(query).replace(QSL("'"), QSL("''")); |
| 103 | |
| 104 | //.replace(QSL("\""), QSL("\\\"")); |
| 105 | } |
| 106 | |
| 107 | DatabaseDriver::DriverType DatabaseFactory::activeDatabaseDriver() const { |
| 108 | return m_dbDriver->driverType(); |
| 109 | } |