SQLiteConnection.cpp 4.4 KB

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