SQLiteConnection.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // Copyright (c) 2008-2016 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_(0)
  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_ = 0;
  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_ = 0;
  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 = 0;
  58. sqlite3_stmt* pStmt = 0;
  59. assert(connectionImpl_);
  60. // 2016-10-09: Prevent string corruption when trimmed is returned.
  61. String trimmedSqlStr = sql.Trimmed();
  62. const char* cSqlStr = trimmedSqlStr.CString();
  63. int rc = sqlite3_prepare_v2(connectionImpl_, cSqlStr, -1, &pStmt, &zLeftover);
  64. if (rc != SQLITE_OK)
  65. {
  66. URHO3D_LOGERRORF("Could not execute: %s", sqlite3_errmsg(connectionImpl_));
  67. assert(!pStmt);
  68. return result;
  69. }
  70. if (*zLeftover)
  71. {
  72. URHO3D_LOGERROR("Could not execute: only one SQL statement is allowed");
  73. sqlite3_finalize(pStmt);
  74. return result;
  75. }
  76. unsigned numCols = (unsigned)sqlite3_column_count(pStmt);
  77. result.columns_.Resize(numCols);
  78. for (unsigned i = 0; i < numCols; ++i)
  79. result.columns_[i] = sqlite3_column_name(pStmt, i);
  80. bool filtered = false;
  81. bool aborted = false;
  82. while (1)
  83. {
  84. rc = sqlite3_step(pStmt);
  85. if (rc == SQLITE_ROW)
  86. {
  87. VariantVector colValues(numCols);
  88. for (unsigned i = 0; i < numCols; ++i)
  89. {
  90. int type = sqlite3_column_type(pStmt, i);
  91. if (type != SQLITE_NULL)
  92. {
  93. // We can only bind primitive data type that our Variant class supports
  94. switch (type)
  95. {
  96. case SQLITE_INTEGER:
  97. colValues[i] = sqlite3_column_int(pStmt, i);
  98. if (String(sqlite3_column_decltype(pStmt, i)).Compare("BOOLEAN", false) == 0)
  99. colValues[i] = colValues[i] != 0;
  100. break;
  101. case SQLITE_FLOAT:
  102. colValues[i] = sqlite3_column_double(pStmt, i);
  103. break;
  104. default:
  105. // All other types are stored using their string representation in the Variant
  106. colValues[i] = (const char*)sqlite3_column_text(pStmt, i);
  107. break;
  108. }
  109. }
  110. }
  111. if (useCursorEvent)
  112. {
  113. using namespace DbCursor;
  114. VariantMap& eventData = GetEventDataMap();
  115. eventData[P_DBCONNECTION] = this;
  116. eventData[P_RESULTIMPL] = pStmt;
  117. eventData[P_SQL] = sql;
  118. eventData[P_NUMCOLS] = numCols;
  119. eventData[P_COLVALUES] = colValues;
  120. eventData[P_COLHEADERS] = result.columns_;
  121. eventData[P_FILTER] = false;
  122. eventData[P_ABORT] = false;
  123. SendEvent(E_DBCURSOR, eventData);
  124. filtered = eventData[P_FILTER].GetBool();
  125. aborted = eventData[P_ABORT].GetBool();
  126. }
  127. if (!filtered)
  128. result.rows_.Push(colValues);
  129. if (aborted)
  130. {
  131. sqlite3_finalize(pStmt);
  132. break;
  133. }
  134. }
  135. else if (rc != SQLITE_DONE)
  136. URHO3D_LOGERRORF("Could not execute: %s", sqlite3_errmsg(connectionImpl_));
  137. if (rc != SQLITE_ROW)
  138. {
  139. sqlite3_finalize(pStmt);
  140. break;
  141. }
  142. }
  143. result.numAffectedRows_ = numCols ? -1 : sqlite3_changes(connectionImpl_);
  144. return result;
  145. }
  146. }