All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
sqlite3-statement.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
23 #ifndef NDN_SQLITE3_STATEMENT_HPP
24 #define NDN_SQLITE3_STATEMENT_HPP
25 
26 // Only compile if ndn-cpp-config.h defines NDN_CPP_HAVE_SQLITE3.
27 #include <ndn-cpp/ndn-cpp-config.h>
28 #ifdef NDN_CPP_HAVE_SQLITE3
29 
30 #include <string>
31 #include <sqlite3.h>
32 #include <ndn-cpp/util/blob.hpp>
33 
34 namespace ndn {
35 
36 /*
37  * Sqlite3Statement is a utility class to wrap an SQLite3 prepared statement,
38  * provide access methods, and finalize the statement in the destructor.
39  */
40 class Sqlite3Statement
41 {
42 public:
49  Sqlite3Statement(sqlite3* database, const std::string& statement);
50 
54  ~Sqlite3Statement();
55 
64  int
65  bind(int index, const char* value, size_t size)
66  {
67  return sqlite3_bind_text(statement_, index, value, size, SQLITE_TRANSIENT);
68  }
69 
77  int
78  bind(int index, const std::string& value)
79  {
80  return bind(index, value.c_str(), value.size());
81  }
82 
93  int
94  bind(int index, const uint8_t* buf, size_t size, bool bufIsStatic = false)
95  {
96  return sqlite3_bind_blob
97  (statement_, index, buf, size,
98  bufIsStatic ? SQLITE_STATIC : SQLITE_TRANSIENT);
99  }
100 
110  int
111  bind(int index, const Blob& blob, bool bufIsStatic = false)
112  {
113  return bind(index, blob.buf(), blob.size(), bufIsStatic);
114  }
115 
122  int
123  bind(int index, int integer)
124  {
125  return sqlite3_bind_int(statement_, index, integer);
126  }
127 
133  std::string
134  getString(int column)
135  {
136  return std::string
137  (reinterpret_cast<const char*>(sqlite3_column_text(statement_, column)),
138  sqlite3_column_bytes(statement_, column));
139  }
140 
146  int
147  getInt(int column) { return sqlite3_column_int(statement_, column); }
148 
154  const uint8_t*
155  getBuf(int column)
156  {
157  return static_cast<const uint8_t*>(sqlite3_column_blob(statement_, column));
158  }
159 
165  int
166  getSize(int column) { return sqlite3_column_bytes(statement_, column); }
167 
173  Blob
174  getBlob(int column) { return Blob(getBuf(column), getSize(column)); }
175 
180  int
181  step() { return sqlite3_step(statement_); }
182 
183 private:
184  // Disable the copy constructor and assignment operator.
185  Sqlite3Statement(const Sqlite3Statement& other);
186  Sqlite3Statement& operator=(const Sqlite3Statement& other);
187 
188  sqlite3_stmt* statement_;
189 };
190 
191 }
192 
193 #endif // NDN_CPP_HAVE_SQLITE3
194 
195 #endif