SQLiteConnection.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * SQLiteConnection.h, 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. #pragma once
  11. typedef struct sqlite3 sqlite3;
  12. typedef struct sqlite3_stmt sqlite3_stmt;
  13. class SQLiteInstance;
  14. class SQLiteStatement;
  15. using SQLiteInstancePtr = std::unique_ptr<SQLiteInstance>;
  16. using SQLiteStatementPtr = std::unique_ptr<SQLiteStatement>;
  17. class SQLiteStatement : boost::noncopyable
  18. {
  19. public:
  20. friend class SQLiteInstance;
  21. bool execute();
  22. void reset();
  23. void clear();
  24. ~SQLiteStatement();
  25. template<typename... Args>
  26. void executeOnce(const Args &... args)
  27. {
  28. setBinds(args...);
  29. execute();
  30. reset();
  31. }
  32. template<typename... Args>
  33. void setBinds(const Args &... args)
  34. {
  35. setBindSingle(1, args...); // The leftmost SQL parameter has an index of 1
  36. }
  37. template<typename... Args>
  38. void getColumns(Args &... args)
  39. {
  40. getColumnSingle(0, args...); // The leftmost column of the result set has the index 0
  41. }
  42. private:
  43. void setBindSingle(size_t index, const double & value);
  44. void setBindSingle(size_t index, const bool & value);
  45. void setBindSingle(size_t index, const uint8_t & value);
  46. void setBindSingle(size_t index, const uint16_t & value);
  47. void setBindSingle(size_t index, const uint32_t & value);
  48. void setBindSingle(size_t index, const int32_t & value);
  49. void setBindSingle(size_t index, const int64_t & value);
  50. void setBindSingle(size_t index, const std::string & value);
  51. void setBindSingle(size_t index, const char * value);
  52. void getColumnSingle(size_t index, double & value);
  53. void getColumnSingle(size_t index, bool & value);
  54. void getColumnSingle(size_t index, uint8_t & value);
  55. void getColumnSingle(size_t index, uint16_t & value);
  56. void getColumnSingle(size_t index, uint32_t & value);
  57. void getColumnSingle(size_t index, int32_t & value);
  58. void getColumnSingle(size_t index, int64_t & value);
  59. void getColumnSingle(size_t index, std::string & value);
  60. template<typename Rep, typename Period>
  61. void getColumnSingle(size_t index, std::chrono::duration<Rep, Period> & value)
  62. {
  63. int64_t durationValue = 0;
  64. getColumnSingle(index, durationValue);
  65. value = std::chrono::duration<Rep, Period>(durationValue);
  66. }
  67. SQLiteStatement(SQLiteInstance & instance, sqlite3_stmt * statement);
  68. template<typename T, typename... Args>
  69. void setBindSingle(size_t index, T const & arg, const Args &... args)
  70. {
  71. setBindSingle(index, arg);
  72. setBindSingle(index + 1, args...);
  73. }
  74. template<typename T, typename... Args>
  75. void getColumnSingle(size_t index, T & arg, Args &... args)
  76. {
  77. getColumnSingle(index, arg);
  78. getColumnSingle(index + 1, args...);
  79. }
  80. SQLiteInstance & m_instance;
  81. sqlite3_stmt * m_statement;
  82. };
  83. class SQLiteInstance : boost::noncopyable
  84. {
  85. public:
  86. friend class SQLiteStatement;
  87. static SQLiteInstancePtr open(const boost::filesystem::path & db_path, bool allow_write);
  88. ~SQLiteInstance();
  89. SQLiteStatementPtr prepare(const std::string & statement);
  90. private:
  91. SQLiteInstance(sqlite3 * connection);
  92. sqlite3 * m_connection;
  93. };