ECDatabaseFactory.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <memory>
  19. #include <kopano/tie.hpp>
  20. #include "ECDatabase.h"
  21. #include "ECDatabaseFactory.h"
  22. #include "ECServerEntrypoint.h"
  23. using namespace KCHL;
  24. namespace KC {
  25. // The ECDatabaseFactory creates database objects connected to the server database. Which
  26. // database is returned is chosen by the database_engine configuration setting.
  27. ECDatabaseFactory::ECDatabaseFactory(ECConfig *lpConfig)
  28. {
  29. this->m_lpConfig = lpConfig;
  30. }
  31. ECRESULT ECDatabaseFactory::GetDatabaseFactory(ECDatabase **lppDatabase)
  32. {
  33. const char *szEngine = m_lpConfig->GetSetting("database_engine");
  34. if(strcasecmp(szEngine, "mysql") == 0) {
  35. *lppDatabase = new ECDatabase(m_lpConfig);
  36. } else {
  37. ec_log_crit("ECDatabaseFactory::GetDatabaseFactory(): database not mysql");
  38. return KCERR_DATABASE_ERROR;
  39. }
  40. return erSuccess;
  41. }
  42. ECRESULT ECDatabaseFactory::CreateDatabaseObject(ECDatabase **lppDatabase, std::string &ConnectError)
  43. {
  44. ECRESULT er;
  45. std::unique_ptr<ECDatabase> lpDatabase;
  46. er = GetDatabaseFactory(&unique_tie(lpDatabase));
  47. if(er != erSuccess) {
  48. ConnectError = "Invalid database engine";
  49. return er;
  50. }
  51. er = lpDatabase->Connect();
  52. if(er != erSuccess) {
  53. ConnectError = lpDatabase->GetError();
  54. return er;
  55. }
  56. *lppDatabase = lpDatabase.release();
  57. return erSuccess;
  58. }
  59. ECRESULT ECDatabaseFactory::CreateDatabase()
  60. {
  61. ECRESULT er = erSuccess;
  62. std::unique_ptr<ECDatabase> lpDatabase;
  63. std::string strQuery;
  64. er = GetDatabaseFactory(&unique_tie(lpDatabase));
  65. if(er != erSuccess)
  66. return er;
  67. return lpDatabase->CreateDatabase();
  68. }
  69. ECRESULT ECDatabaseFactory::UpdateDatabase(bool bForceUpdate, std::string &strReport)
  70. {
  71. ECRESULT er = erSuccess;
  72. std::unique_ptr<ECDatabase> lpDatabase;
  73. er = CreateDatabaseObject(&unique_tie(lpDatabase), strReport);
  74. if(er != erSuccess)
  75. return er;
  76. return lpDatabase->UpdateDatabase(bForceUpdate, strReport);
  77. }
  78. extern pthread_key_t database_key;
  79. ECRESULT GetThreadLocalDatabase(ECDatabaseFactory *lpFactory, ECDatabase **lppDatabase)
  80. {
  81. ECRESULT er;
  82. ECDatabase *lpDatabase = NULL;
  83. std::string error;
  84. // We check to see whether the calling thread already
  85. // has an open database connection. If so, we return that, or otherwise
  86. // we create a new one.
  87. // database_key is defined in ECServer.cpp, and allocated in the running_server routine
  88. lpDatabase = (ECDatabase *)pthread_getspecific(database_key);
  89. if(lpDatabase == NULL) {
  90. er = lpFactory->CreateDatabaseObject(&lpDatabase, error);
  91. if(er != erSuccess) {
  92. ec_log_err("Unable to get database connection: %s", error.c_str());
  93. lpDatabase = NULL;
  94. return er;
  95. }
  96. // Add database into a list, for close all database connections
  97. AddDatabaseObject(lpDatabase);
  98. pthread_setspecific(database_key, (void *)lpDatabase);
  99. }
  100. *lppDatabase = lpDatabase;
  101. return erSuccess;
  102. }
  103. } /* namespace */