SQLiteConnection.cpp 5.2 KB

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