SqlDBHelper.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "SqlDBHelper.h"
  2. #include <QApplication>
  3. #include <QMessageBox>
  4. #include <QDebug>
  5. bool SqlDBHelper::configFinished = false;
  6. bool SqlDBHelper::configFinished2 = false;
  7. QSqlDatabase SqlDBHelper::getDatabase(SqlDBHelper::SqlDBType dbType, const QString &connectionName)
  8. {
  9. #if QFD2_APP
  10. QSettings config("config.ini", QSettings::IniFormat);
  11. #else
  12. QSettings config("clientConfig.ini", QSettings::IniFormat);
  13. #endif
  14. config.setIniCodec("UTF-8");
  15. dbType = static_cast<SqlDBHelper::SqlDBType>(config.value("USERCONFIG/DbType", "").toInt());
  16. switch (dbType) {
  17. case SqlDBHelper::SqlDBType::SQLITE3: {
  18. if (configFinished) {
  19. return SqlFactory::getInstance()->getDatabase();
  20. } else {
  21. QString databaseName = "./data/qfd.db";
  22. SqlFactory::DBSetting setting("QSQLITE", databaseName);
  23. configFinished = true;
  24. return SqlFactory::getInstance()->config(setting, connectionName)->getDatabase();
  25. }
  26. } break;
  27. case SqlDBHelper::SqlDBType::MYSQL: {
  28. if (configFinished) {
  29. return SqlFactory::getInstance()->getDatabase();
  30. } else {
  31. QSettings datasource("datasource.ini", QSettings::IniFormat);
  32. datasource.setIniCodec("UTF-8");
  33. QString url = datasource.value("DATASOURCE/url", "").toString();
  34. int port = datasource.value("DATASOURCE/port", "").toInt();
  35. QString username = datasource.value("DATASOURCE/username", "").toString();
  36. QString password = datasource.value("DATASOURCE/password", "").toString();
  37. QString database = datasource.value("DATASOURCE/database", "").toString();
  38. qDebug() << url << port << username << password << database;
  39. SqlFactory::DBSetting setting("QMYSQL", url, port, username, password, database);
  40. configFinished = true;
  41. return SqlFactory::getInstance()->config(setting, connectionName)->getDatabase();
  42. }
  43. } break;
  44. }
  45. QMessageBox::critical(nullptr, QObject::tr("数据库连接异常"), QObject::tr("请检查数据库配置文件是否正常"));
  46. QApplication::exec();
  47. return SqlFactory::getInstance()->getDatabase(nullptr);
  48. }
  49. QSqlDatabase SqlDBHelper::getDatabase2(SqlDBHelper::SqlDBType dbType, const QString &connectionName)
  50. {
  51. switch (dbType) {
  52. case SqlDBHelper::SqlDBType::SQLITE3: {
  53. if (configFinished2) {
  54. return SqlFactory::getInstance2()->getDatabase();
  55. } else {
  56. QString databaseName = "./nodeData/node.db";
  57. SqlFactory::DBSetting setting("QSQLITE", databaseName);
  58. configFinished2 = true;
  59. return SqlFactory::getInstance2()->config(setting, connectionName)->getDatabase();
  60. }
  61. } break;
  62. case SqlDBHelper::SqlDBType::MYSQL: {
  63. if (configFinished2) {
  64. return SqlFactory::getInstance2()->getDatabase();
  65. } else {
  66. SqlFactory::DBSetting setting("QMYSQL", "localhost", 3306, "root", "123456", "qfd");
  67. configFinished2 = true;
  68. return SqlFactory::getInstance2()->config(setting, connectionName)->getDatabase();
  69. }
  70. } break;
  71. }
  72. QMessageBox::critical(nullptr, QObject::tr("数据库连接异常"), QObject::tr("请检查数据库配置文件是否正常"));
  73. QApplication::exec();
  74. return SqlFactory::getInstance2()->getDatabase(nullptr);
  75. }