Silicium
sqlite3.hpp
Go to the documentation of this file.
1 #ifndef SILICIUM_SQLITE3_HPP
2 #define SILICIUM_SQLITE3_HPP
3 
4 #include <silicium/c_string.hpp>
5 #include <silicium/error_or.hpp>
6 #include <sqlite3.h>
7 #include <memory>
8 
9 namespace Si
10 {
11  namespace SQLite3
12  {
14  {
15  void operator()(sqlite3 *database) const
16  {
17  assert(database);
18  sqlite3_close(database);
19  }
20  };
21 
22  typedef std::unique_ptr<sqlite3, database_deleter> database_handle;
23 
25  {
26  void operator()(sqlite3_stmt *statement) const
27  {
28  assert(statement);
29  sqlite3_finalize(statement);
30  }
31  };
32 
33  typedef std::unique_ptr<sqlite3_stmt, statement_deleter> statement_handle;
34 
35  struct error_category : boost::system::error_category
36  {
37  virtual const char *name() const BOOST_SYSTEM_NOEXCEPT SILICIUM_OVERRIDE
38  {
39  return "sqlite3";
40  }
41 
42  virtual std::string message(int ev) const SILICIUM_OVERRIDE
43  {
44  return sqlite3_errstr(ev);
45  }
46  };
47 
48  inline boost::system::error_category &sqlite_category()
49  {
50  static error_category instance;
51  return instance;
52  }
53 
54  inline boost::system::error_code make_error_code(int code)
55  {
56  return boost::system::error_code(code, sqlite_category());
57  }
58 
60  {
61  sqlite3 *database;
62  int rc = sqlite3_open_v2(path.c_str(), &database, SQLITE_OPEN_READWRITE, nullptr);
63  if (rc != SQLITE_OK)
64  {
65  return make_error_code(rc);
66  }
67  return database_handle(database);
68  }
69 
70  inline error_or<statement_handle> prepare(sqlite3 &database, c_string query)
71  {
72  sqlite3_stmt *statement;
73  int rc = sqlite3_prepare_v2(&database, query.c_str(), -1, &statement, nullptr);
74  if (rc != SQLITE_OK)
75  {
76  return make_error_code(rc);
77  }
78  return statement_handle(statement);
79  }
80 
81  inline boost::system::error_code bind(sqlite3_stmt &statement, int zero_based_index, sqlite3_int64 value)
82  {
83  return make_error_code(sqlite3_bind_int64(&statement, zero_based_index, value));
84  }
85 
86  inline boost::system::error_code bind(sqlite3_stmt &statement, int zero_based_index, char const *begin, int length)
87  {
88  return make_error_code(sqlite3_bind_text(&statement, zero_based_index, begin, length, nullptr));
89  }
90  }
91 }
92 
93 #endif
Definition: c_string.hpp:10
Definition: sqlite3.hpp:35
virtual const char * name() const BOOST_SYSTEM_NOEXCEPT SILICIUM_OVERRIDE
Definition: sqlite3.hpp:37
boost::system::error_code make_error_code(int code)
Definition: sqlite3.hpp:54
boost::system::error_code bind(sqlite3_stmt &statement, int zero_based_index, sqlite3_int64 value)
Definition: sqlite3.hpp:81
std::unique_ptr< sqlite3, database_deleter > database_handle
Definition: sqlite3.hpp:22
error_or< statement_handle > prepare(sqlite3 &database, c_string query)
Definition: sqlite3.hpp:70
Definition: absolute_path.hpp:19
void operator()(sqlite3_stmt *statement) const
Definition: sqlite3.hpp:26
#define SILICIUM_OVERRIDE
Definition: config.hpp:140
Definition: sqlite3.hpp:24
void operator()(sqlite3 *database) const
Definition: sqlite3.hpp:15
SILICIUM_USE_RESULT char_type const * c_str() const BOOST_NOEXCEPT
Definition: c_string.hpp:45
BOOST_CONSTEXPR Iterator const & begin(iterator_range< Iterator > const &range)
Definition: iterator_range.hpp:123
error_or< database_handle > open(c_string path)
Definition: sqlite3.hpp:59
Definition: path.hpp:11
Definition: error_or.hpp:48
std::unique_ptr< sqlite3_stmt, statement_deleter > statement_handle
Definition: sqlite3.hpp:33
virtual std::string message(int ev) const SILICIUM_OVERRIDE
Definition: sqlite3.hpp:42
boost::system::error_category & sqlite_category()
Definition: sqlite3.hpp:48
Definition: sqlite3.hpp:13