Database.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Profiler.h"
  24. #include "../Database/Database.h"
  25. namespace Urho3D
  26. {
  27. Database::Database(Context* context_) :
  28. Object(context_),
  29. #ifdef ODBC_3_OR_LATER
  30. usePooling_(false)
  31. #else
  32. usePooling_(true)
  33. #endif
  34. {
  35. // Register Database library object factories
  36. RegisterDatabaseLibrary(context_);
  37. }
  38. Database::~Database()
  39. {
  40. PROFILE(DatabaseDestruct);
  41. // Disconnect all the active and in-pool database connections
  42. DisconnectAll(connections_);
  43. for (HashMap<String, PODVector<DbConnection*> >::Iterator i = connectionsPool_.Begin(); i != connectionsPool_.End(); ++i)
  44. DisconnectAll(i->second_);
  45. }
  46. DBAPI Database::GetAPI()
  47. {
  48. #ifdef URHO3D_DATABASE_ODBC
  49. return DBAPI_ODBC;
  50. #else
  51. return DBAPI_SQLITE;
  52. #endif
  53. }
  54. DbConnection* Database::Connect(const String& connectionString)
  55. {
  56. PROFILE(DatabaseConnect);
  57. DbConnection* connection = 0;
  58. if (usePooling_)
  59. {
  60. PODVector<DbConnection*>& connectionsPool = connectionsPool_[connectionString];
  61. if (!connectionsPool.Empty())
  62. {
  63. connection = connectionsPool.Back();
  64. connectionsPool.Pop();
  65. assert(connection->IsConnected());
  66. }
  67. }
  68. if (!connection)
  69. connection = new DbConnection(context_, connectionString);
  70. if (connection->IsConnected())
  71. {
  72. connections_.Push(connection);
  73. return connection;
  74. }
  75. else
  76. {
  77. delete connection;
  78. return 0;
  79. }
  80. }
  81. void Database::Disconnect(DbConnection* connection)
  82. {
  83. if (!connection)
  84. return;
  85. PROFILE(DatabaseDisconnect);
  86. connections_.Remove(connection);
  87. // Must finalize the connection before closing the connection or returning it to the pool
  88. connection->Finalize();
  89. if (usePooling_)
  90. {
  91. PODVector<DbConnection*>& connectionsPool = connectionsPool_[connection->GetConnectionString()];
  92. connectionsPool.Push(connection);
  93. }
  94. else
  95. delete connection;
  96. }
  97. void Database::DisconnectAll(PODVector<DbConnection*>& collection)
  98. {
  99. PROFILE (DatabaseDisconnectAll);
  100. for (PODVector<DbConnection*>::Iterator i = collection.Begin(); i != collection.End(); ++i)
  101. {
  102. delete *i; // The destructor ensures the connection is finalized and closed
  103. *i = 0;
  104. }
  105. }
  106. void URHO3D_API RegisterDatabaseLibrary(Context* context)
  107. {
  108. // todo
  109. }
  110. }