SQLiteConnection.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 setBinds(const Args &... args)
  27. {
  28. setBindSingle(1, args...); // The leftmost SQL parameter has an index of 1
  29. }
  30. template<typename... Args>
  31. void getColumns(Args &... args)
  32. {
  33. getColumnSingle(0, args...); // The leftmost column of the result set has the index 0
  34. }
  35. private:
  36. void setBindSingle(size_t index, const double & value);
  37. void setBindSingle(size_t index, const uint8_t & value);
  38. void setBindSingle(size_t index, const uint16_t & value);
  39. void setBindSingle(size_t index, const uint32_t & value);
  40. void setBindSingle(size_t index, const int32_t & value);
  41. void setBindSingle(size_t index, const int64_t & value);
  42. void setBindSingle(size_t index, const std::string & value);
  43. void setBindSingle(size_t index, const char * value);
  44. void getColumnSingle(size_t index, double & value);
  45. void getColumnSingle(size_t index, uint8_t & value);
  46. void getColumnSingle(size_t index, uint16_t & value);
  47. void getColumnSingle(size_t index, uint32_t & value);
  48. void getColumnSingle(size_t index, int32_t & value);
  49. void getColumnSingle(size_t index, int64_t & value);
  50. void getColumnSingle(size_t index, std::string & value);
  51. template<typename Rep, typename Period>
  52. void getColumnSingle(size_t index, std::chrono::duration<Rep, Period> & value)
  53. {
  54. int64_t durationValue = 0;
  55. getColumnSingle(index, durationValue);
  56. value = std::chrono::duration<Rep, Period>(durationValue);
  57. }
  58. SQLiteStatement(SQLiteInstance & instance, sqlite3_stmt * statement);
  59. template<typename T, typename... Args>
  60. void setBindSingle(size_t index, T const & arg, const Args &... args)
  61. {
  62. setBindSingle(index, arg);
  63. setBindSingle(index + 1, args...);
  64. }
  65. template<typename T, typename... Args>
  66. void getColumnSingle(size_t index, T & arg, Args &... args)
  67. {
  68. getColumnSingle(index, arg);
  69. getColumnSingle(index + 1, args...);
  70. }
  71. SQLiteInstance & m_instance;
  72. sqlite3_stmt * m_statement;
  73. };
  74. class SQLiteInstance : boost::noncopyable
  75. {
  76. public:
  77. friend class SQLiteStatement;
  78. static SQLiteInstancePtr open(const std::string & db_path, bool allow_write);
  79. ~SQLiteInstance();
  80. SQLiteStatementPtr prepare(const std::string & statement);
  81. private:
  82. SQLiteInstance(sqlite3 * connection);
  83. sqlite3 * m_connection;
  84. };