2
0

SQLiteConnection.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * SQLiteConnection.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "SQLiteConnection.h"
  12. #include <sqlite3.h>
  13. [[noreturn]] static void handleSQLiteError(sqlite3 * connection)
  14. {
  15. const char * message = sqlite3_errmsg(connection);
  16. throw std::runtime_error(std::string("SQLite error: ") + message);
  17. }
  18. static void checkSQLiteError(sqlite3 * connection, int result)
  19. {
  20. if(result != SQLITE_OK)
  21. handleSQLiteError(connection);
  22. }
  23. SQLiteStatement::SQLiteStatement(SQLiteInstance & instance, sqlite3_stmt * statement)
  24. : m_instance(instance)
  25. , m_statement(statement)
  26. {
  27. }
  28. SQLiteStatement::~SQLiteStatement()
  29. {
  30. sqlite3_finalize(m_statement);
  31. }
  32. bool SQLiteStatement::execute()
  33. {
  34. int result = sqlite3_step(m_statement);
  35. switch(result)
  36. {
  37. case SQLITE_DONE:
  38. return false;
  39. case SQLITE_ROW:
  40. return true;
  41. default:
  42. checkSQLiteError(m_instance.m_connection, result);
  43. return false;
  44. }
  45. }
  46. void SQLiteStatement::reset()
  47. {
  48. int result = sqlite3_reset(m_statement);
  49. checkSQLiteError(m_instance.m_connection, result);
  50. }
  51. void SQLiteStatement::clear()
  52. {
  53. int result = sqlite3_clear_bindings(m_statement);
  54. checkSQLiteError(m_instance.m_connection, result);
  55. }
  56. void SQLiteStatement::setBindSingle(size_t index, const double & value)
  57. {
  58. int result = sqlite3_bind_double(m_statement, static_cast<int>(index), value);
  59. checkSQLiteError(m_instance.m_connection, result);
  60. }
  61. void SQLiteStatement::setBindSingle(size_t index, const bool & value)
  62. {
  63. int result = sqlite3_bind_int(m_statement, static_cast<int>(value), value);
  64. checkSQLiteError(m_instance.m_connection, result);
  65. }
  66. void SQLiteStatement::setBindSingle(size_t index, const uint8_t & value)
  67. {
  68. int result = sqlite3_bind_int(m_statement, static_cast<int>(index), value);
  69. checkSQLiteError(m_instance.m_connection, result);
  70. }
  71. void SQLiteStatement::setBindSingle(size_t index, const uint16_t & value)
  72. {
  73. int result = sqlite3_bind_int(m_statement, static_cast<int>(index), value);
  74. checkSQLiteError(m_instance.m_connection, result);
  75. }
  76. void SQLiteStatement::setBindSingle(size_t index, const uint32_t & value)
  77. {
  78. int result = sqlite3_bind_int(m_statement, static_cast<int>(index), value);
  79. checkSQLiteError(m_instance.m_connection, result);
  80. }
  81. void SQLiteStatement::setBindSingle(size_t index, const int32_t & value)
  82. {
  83. int result = sqlite3_bind_int(m_statement, static_cast<int>(index), value);
  84. checkSQLiteError(m_instance.m_connection, result);
  85. }
  86. void SQLiteStatement::setBindSingle(size_t index, const int64_t & value)
  87. {
  88. int result = sqlite3_bind_int64(m_statement, static_cast<int>(index), value);
  89. checkSQLiteError(m_instance.m_connection, result);
  90. }
  91. void SQLiteStatement::setBindSingle(size_t index, const std::string & value)
  92. {
  93. int result = sqlite3_bind_text(m_statement, static_cast<int>(index), value.data(), static_cast<int>(value.size()), SQLITE_STATIC);
  94. checkSQLiteError(m_instance.m_connection, result);
  95. }
  96. void SQLiteStatement::setBindSingle(size_t index, const char * value)
  97. {
  98. int result = sqlite3_bind_text(m_statement, static_cast<int>(index), value, -1, SQLITE_STATIC);
  99. checkSQLiteError(m_instance.m_connection, result);
  100. }
  101. void SQLiteStatement::getColumnSingle(size_t index, double & value)
  102. {
  103. value = sqlite3_column_double(m_statement, static_cast<int>(index));
  104. }
  105. void SQLiteStatement::getColumnSingle(size_t index, bool & value)
  106. {
  107. value = sqlite3_column_int(m_statement, static_cast<int>(index)) != 0;
  108. }
  109. void SQLiteStatement::getColumnSingle(size_t index, uint8_t & value)
  110. {
  111. value = static_cast<uint8_t>(sqlite3_column_int(m_statement, static_cast<int>(index)));
  112. }
  113. void SQLiteStatement::getColumnSingle(size_t index, uint16_t & value)
  114. {
  115. value = static_cast<uint16_t>(sqlite3_column_int(m_statement, static_cast<int>(index)));
  116. }
  117. void SQLiteStatement::getColumnSingle(size_t index, int32_t & value)
  118. {
  119. value = sqlite3_column_int(m_statement, static_cast<int>(index));
  120. }
  121. void SQLiteStatement::getColumnSingle(size_t index, uint32_t & value)
  122. {
  123. value = sqlite3_column_int(m_statement, static_cast<int>(index));
  124. }
  125. void SQLiteStatement::getColumnSingle(size_t index, int64_t & value)
  126. {
  127. value = sqlite3_column_int64(m_statement, static_cast<int>(index));
  128. }
  129. void SQLiteStatement::getColumnSingle(size_t index, std::string & value)
  130. {
  131. const auto * value_raw = sqlite3_column_text(m_statement, static_cast<int>(index));
  132. value = reinterpret_cast<const char *>(value_raw);
  133. }
  134. SQLiteInstancePtr SQLiteInstance::open(const boost::filesystem::path & db_path, bool allow_write)
  135. {
  136. int flags = allow_write ? (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) : SQLITE_OPEN_READONLY;
  137. sqlite3 * connection;
  138. int result = sqlite3_open_v2(db_path.c_str(), &connection, flags, nullptr);
  139. if(result == SQLITE_OK)
  140. return SQLiteInstancePtr(new SQLiteInstance(connection));
  141. sqlite3_close(connection);
  142. handleSQLiteError(connection);
  143. }
  144. SQLiteInstance::SQLiteInstance(sqlite3 * connection)
  145. : m_connection(connection)
  146. {
  147. }
  148. SQLiteInstance::~SQLiteInstance()
  149. {
  150. sqlite3_close(m_connection);
  151. }
  152. SQLiteStatementPtr SQLiteInstance::prepare(const std::string & sql_text)
  153. {
  154. sqlite3_stmt * statement;
  155. int result = sqlite3_prepare_v2(m_connection, sql_text.data(), static_cast<int>(sql_text.size()), &statement, nullptr);
  156. if(result == SQLITE_OK)
  157. return SQLiteStatementPtr(new SQLiteStatement(*this, statement));
  158. sqlite3_finalize(statement);
  159. handleSQLiteError(m_connection);
  160. }