#include "SqlDBHelper.h" #include #include #include bool SqlDBHelper::configFinished = false; bool SqlDBHelper::configFinished2 = false; QSqlDatabase SqlDBHelper::getDatabase(SqlDBHelper::SqlDBType dbType, const QString &connectionName) { #if QFD2_APP QSettings config("config.ini", QSettings::IniFormat); #else QSettings config("clientConfig.ini", QSettings::IniFormat); #endif config.setIniCodec("UTF-8"); dbType = static_cast(config.value("USERCONFIG/DbType", "").toInt()); switch (dbType) { case SqlDBHelper::SqlDBType::SQLITE3: { if (configFinished) { return SqlFactory::getInstance()->getDatabase(); } else { QString databaseName = "./data/qfd.db"; SqlFactory::DBSetting setting("QSQLITE", databaseName); configFinished = true; return SqlFactory::getInstance()->config(setting, connectionName)->getDatabase(); } } break; case SqlDBHelper::SqlDBType::MYSQL: { if (configFinished) { return SqlFactory::getInstance()->getDatabase(); } else { QSettings datasource("datasource.ini", QSettings::IniFormat); datasource.setIniCodec("UTF-8"); QString url = datasource.value("DATASOURCE/url", "").toString(); int port = datasource.value("DATASOURCE/port", "").toInt(); QString username = datasource.value("DATASOURCE/username", "").toString(); QString password = datasource.value("DATASOURCE/password", "").toString(); QString database = datasource.value("DATASOURCE/database", "").toString(); qDebug() << url << port << username << password << database; SqlFactory::DBSetting setting("QMYSQL", url, port, username, password, database); configFinished = true; return SqlFactory::getInstance()->config(setting, connectionName)->getDatabase(); } } break; } QMessageBox::critical(nullptr, QObject::tr("数据库连接异常"), QObject::tr("请检查数据库配置文件是否正常")); QApplication::exec(); return SqlFactory::getInstance()->getDatabase(nullptr); } QSqlDatabase SqlDBHelper::getDatabase2(SqlDBHelper::SqlDBType dbType, const QString &connectionName) { switch (dbType) { case SqlDBHelper::SqlDBType::SQLITE3: { if (configFinished2) { return SqlFactory::getInstance2()->getDatabase(); } else { QString databaseName = "./nodeData/node.db"; SqlFactory::DBSetting setting("QSQLITE", databaseName); configFinished2 = true; return SqlFactory::getInstance2()->config(setting, connectionName)->getDatabase(); } } break; case SqlDBHelper::SqlDBType::MYSQL: { if (configFinished2) { return SqlFactory::getInstance2()->getDatabase(); } else { SqlFactory::DBSetting setting("QMYSQL", "localhost", 3306, "root", "123456", "qfd"); configFinished2 = true; return SqlFactory::getInstance2()->config(setting, connectionName)->getDatabase(); } } break; } QMessageBox::critical(nullptr, QObject::tr("数据库连接异常"), QObject::tr("请检查数据库配置文件是否正常")); QApplication::exec(); return SqlFactory::getInstance2()->getDatabase(nullptr); }