SQLiteConnection.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "../../Database/DatabaseEvents.h"
  24. #include "../../IO/Log.h"
  25. namespace Urho3D
  26. {
  27. DbConnection::DbConnection(Context* context, const String& connectionString) :
  28. Object(context),
  29. connectionString_(connectionString),
  30. connectionImpl_(nullptr)
  31. {
  32. if (sqlite3_open(connectionString.CString(), &connectionImpl_) != SQLITE_OK)
  33. {
  34. URHO3D_LOGERRORF("Could not connect: %s", sqlite3_errmsg(connectionImpl_));
  35. sqlite3_close(connectionImpl_);
  36. connectionImpl_ = nullptr;
  37. }
  38. }
  39. DbConnection::~DbConnection()
  40. {
  41. Finalize();
  42. if (sqlite3_close(connectionImpl_) != SQLITE_OK)
  43. {
  44. // This should not happen after finalizing the connection, log error in Release but assert in Debug
  45. URHO3D_LOGERRORF("Could not disconnect: %s", sqlite3_errmsg(connectionImpl_));
  46. assert(false);
  47. }
  48. connectionImpl_ = nullptr;
  49. }
  50. void DbConnection::Finalize()
  51. {
  52. // TODO
  53. }
  54. DbResult DbConnection::Execute(const String& sql, bool useCursorEvent)
  55. {
  56. DbResult result;
  57. const char* zLeftover = nullptr;
  58. sqlite3_stmt* pStmt = nullptr;
  59. assert(connectionImpl_);
  60. // 2016-10-09: Prevent string corruption when trimmed is returned.
  61. String trimmedSqlStr = sql.Trimmed();
  62. int rc = sqlite3_prepare_v2(connectionImpl_, trimmedSqlStr.CString(), -1, &pStmt, &zLeftover);
  63. if (rc != SQLITE_OK)
  64. {
  65. URHO3D_LOGERRORF("Could not execute: %s", sqlite3_errmsg(connectionImpl_));
  66. assert(!pStmt);
  67. return result;
  68. }
  69. if (*zLeftover)
  70. {
  71. URHO3D_LOGERROR("Could not execute: only one SQL statement is allowed");
  72. sqlite3_finalize(pStmt);
  73. return result;
  74. }
  75. auto numCols = (unsigned)sqlite3_column_count(pStmt);
  76. result.columns_.Resize(numCols);
  77. for (unsigned i = 0; i < numCols; ++i)
  78. result.columns_[i] = sqlite3_column_name(pStmt, i);
  79. bool filtered = false;
  80. bool aborted = false;
  81. while (1)
  82. {
  83. rc = sqlite3_step(pStmt);
  84. if (rc == SQLITE_ROW)
  85. {
  86. VariantVector colValues(numCols);
  87. for (unsigned i = 0; i < numCols; ++i)
  88. {
  89. int type = sqlite3_column_type(pStmt, i);
  90. if (type != SQLITE_NULL)
  91. {
  92. // We can only bind primitive data type that our Variant class supports
  93. switch (type)
  94. {
  95. case SQLITE_INTEGER:
  96. colValues[i] = sqlite3_column_int(pStmt, i);
  97. if (String(sqlite3_column_decltype(pStmt, i)).Compare("BOOLEAN", false) == 0)
  98. colValues[i] = colValues[i] != 0;
  99. break;
  100. case SQLITE_FLOAT:
  101. colValues[i] = sqlite3_column_double(pStmt, i);
  102. break;
  103. default:
  104. // All other types are stored using their string representation in the Variant
  105. colValues[i] = (const char*)sqlite3_column_text(pStmt, i);
  106. break;
  107. }
  108. }
  109. }
  110. if (useCursorEvent)
  111. {
  112. using namespace DbCursor;
  113. VariantMap& eventData = GetEventDataMap();
  114. eventData[P_DBCONNECTION] = this;
  115. eventData[P_RESULTIMPL] = pStmt;
  116. eventData[P_SQL] = sql;
  117. eventData[P_NUMCOLS] = numCols;
  118. eventData[P_COLVALUES] = colValues;
  119. eventData[P_COLHEADERS] = result.columns_;
  120. eventData[P_FILTER] = false;
  121. eventData[P_ABORT] = false;
  122. SendEvent(E_DBCURSOR, eventData);
  123. filtered = eventData[P_FILTER].GetBool();
  124. aborted = eventData[P_ABORT].GetBool();
  125. }
  126. if (!filtered)
  127. result.rows_.Push(colValues);
  128. if (aborted)
  129. {
  130. sqlite3_finalize(pStmt);
  131. break;
  132. }
  133. }
  134. else if (rc != SQLITE_DONE)
  135. URHO3D_LOGERRORF("Could not execute: %s", sqlite3_errmsg(connectionImpl_));
  136. if (rc != SQLITE_ROW)
  137. {
  138. sqlite3_finalize(pStmt);
  139. break;
  140. }
  141. }
  142. result.numAffectedRows_ = numCols ? -1 : sqlite3_changes(connectionImpl_);
  143. return result;
  144. }
  145. }