C++ binding for the SQLite library https://code.ireas.org/sqlitepp/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlitepp.h 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * (C) 2014 Robin Krahl
  3. * MIT license -- http://opensource.org/licenses/MIT
  4. */
  5. #ifndef __SQLITEPP_H
  6. #define __SQLITEPP_H
  7. #include <memory>
  8. #include <string>
  9. #include <boost/noncopyable.hpp>
  10. #include <sqlite3.h>
  11. namespace sqlitepp {
  12. class Openable {
  13. public:
  14. const bool isOpen() const;
  15. protected:
  16. Openable(const bool open, const std::string & name);
  17. void requireOpen() const;
  18. void setOpen(const bool open);
  19. private:
  20. bool m_open;
  21. const std::string & m_name;
  22. };
  23. class DatabaseError : public std::runtime_error {
  24. public:
  25. DatabaseError(const int errorCode);
  26. DatabaseError(const int errorCode, const std::string & errorMessage);
  27. const int errorCode() const;
  28. private:
  29. const int m_errorCode;
  30. static const std::string getErrorMessage(const int errorCode, const std::string & errorMessage);
  31. };
  32. class Statement;
  33. class Database : private boost::noncopyable, public Openable {
  34. friend class Statement;
  35. public:
  36. Database();
  37. Database(const std::string & file);
  38. ~Database();
  39. void close();
  40. void execute(const std::string & sql);
  41. void open(const std::string & file);
  42. std::shared_ptr<Statement> prepare(const std::string & sql);
  43. private:
  44. sqlite3 * m_handle;
  45. };
  46. class Statement : private boost::noncopyable, public Openable {
  47. public:
  48. Statement(Database & database, const std::string & statement);
  49. ~Statement();
  50. void bindDouble(const int index, const double value);
  51. void bindDouble(const std::string & name, const double value);
  52. void bindInt(const int index, const int value);
  53. void bindInt(const std::string & name, const int value);
  54. void bindString(const int index, const std::string & value);
  55. void bindString(const std::string & name, const std::string & value);
  56. const bool canRead() const;
  57. const int columnCount() const;
  58. const double readDouble(const int column) const;
  59. const int readInt(const int column) const;
  60. const std::string readString(const int column) const;
  61. const bool step();
  62. void finalize();
  63. const bool reset();
  64. private:
  65. sqlite3_stmt * m_handle;
  66. bool m_canRead;
  67. int getParameterIndex(const std::string & name) const;
  68. void handleBindResult(const int index, const int result) const;
  69. void requireCanRead() const;
  70. };
  71. }
  72. #endif