SQLiteConnection.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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( Args const & ... 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, double const & value );
  37. void setBindSingle( size_t index, uint8_t const & value );
  38. void setBindSingle( size_t index, uint16_t const & value );
  39. void setBindSingle( size_t index, uint32_t const & value );
  40. void setBindSingle( size_t index, int32_t const & value );
  41. void setBindSingle( size_t index, int64_t const & value );
  42. void setBindSingle( size_t index, std::string const & value );
  43. void setBindSingle( size_t index, char const * 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. SQLiteStatement( SQLiteInstance & instance, sqlite3_stmt * statement );
  52. template<typename T, typename ... Args >
  53. void setBindSingle( size_t index, T const & arg, Args const & ... args )
  54. {
  55. setBindSingle( index, arg );
  56. setBindSingle( index + 1, args... );
  57. }
  58. template<typename T, typename ... Args >
  59. void getColumnSingle( size_t index, T & arg, Args & ... args )
  60. {
  61. getColumnSingle( index, arg );
  62. getColumnSingle( index + 1, args... );
  63. }
  64. void getColumnBlob(size_t index, std::byte * value, size_t capacity);
  65. SQLiteInstance & m_instance;
  66. sqlite3_stmt * m_statement;
  67. };
  68. class SQLiteInstance : boost::noncopyable
  69. {
  70. public:
  71. friend class SQLiteStatement;
  72. static SQLiteInstancePtr open(std::string const & db_path, bool allow_write );
  73. ~SQLiteInstance( );
  74. SQLiteStatementPtr prepare( std::string const & statement );
  75. private:
  76. SQLiteInstance( sqlite3 * connection );
  77. sqlite3 * m_connection;
  78. };