Database.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  26. {
  27. Database::Database(Context* context_) :
  28. Object(context_),
  29. #ifdef ODBC_3_OR_LATER
  30. poolSize_(0)
  31. #else
  32. poolSize_(M_MAX_UNSIGNED)
  33. #endif
  34. {
  35. }
  36. DBAPI Database::GetAPI()
  37. {
  38. #ifdef ATOMIC_DATABASE_ODBC
  39. return DBAPI_ODBC;
  40. #else
  41. return DBAPI_SQLITE;
  42. #endif
  43. }
  44. DbConnection* Database::Connect(const String& connectionString)
  45. {
  46. ATOMIC_PROFILE(DatabaseConnect);
  47. SharedPtr<DbConnection> connection;
  48. if (IsPooling())
  49. {
  50. Vector<SharedPtr<DbConnection> >& connectionsPool = connectionsPool_[connectionString];
  51. while (!connectionsPool.Empty())
  52. {
  53. connection = connectionsPool.Back();
  54. connectionsPool.Pop();
  55. if (connection->IsConnected())
  56. break;
  57. connection = 0;
  58. }
  59. }
  60. if (!connection)
  61. connection = new DbConnection(context_, connectionString);
  62. if (connection->IsConnected())
  63. {
  64. connections_.Push(connection);
  65. return connection;
  66. }
  67. else
  68. return 0;
  69. }
  70. void Database::Disconnect(DbConnection* connection)
  71. {
  72. if (!connection)
  73. return;
  74. ATOMIC_PROFILE(DatabaseDisconnect);
  75. SharedPtr<DbConnection> dbConnection(connection);
  76. connections_.Remove(dbConnection);
  77. // Must finalize the connection before closing the connection or returning it to the pool
  78. connection->Finalize();
  79. if (IsPooling())
  80. {
  81. Vector<SharedPtr<DbConnection> >& connectionsPool = connectionsPool_[connection->GetConnectionString()];
  82. if (connectionsPool.Size() < poolSize_)
  83. connectionsPool.Push(dbConnection);
  84. }
  85. }
  86. }