sqlitepp
C++ binding for the SQLite3 library
sqlitepp.h
Go to the documentation of this file.
1 // Copyright (C) 2014--2015 Robin Krahl <robin.krahl@ireas.org>
2 // MIT license -- http://opensource.org/licenses/MIT
3 
4 #ifndef SQLITEPP_SQLITEPP_H_
5 #define SQLITEPP_SQLITEPP_H_
6 
7 #include <sqlite3.h>
8 #include <stdexcept>
9 #include <memory>
10 #include <string>
11 
15 
107 
109 namespace sqlitepp {
110 
116 class Uncopyable {
117  protected:
118  Uncopyable() {}
119  ~Uncopyable() {}
120 
121  private:
122  Uncopyable(const Uncopyable&);
123  Uncopyable& operator=(const Uncopyable&);
124 };
125 
138 class Openable {
139  public:
143  bool isOpen() const;
144 
145  protected:
151  Openable(const bool open, const std::string& name);
152 
161  void requireOpen() const;
162 
167  void setOpen(const bool open);
168 
169  private:
170  bool m_open;
171  const std::string& m_name;
172 };
173 
184 class DatabaseError : public std::runtime_error {
185  public:
193  explicit DatabaseError(const int errorCode);
194 
200  DatabaseError(const int errorCode, const std::string& errorMessage);
201 
205  int errorCode() const;
206 
207  private:
208  const int m_errorCode;
209 
210  static std::string getErrorMessage(const int errorCode,
211  const std::string& errorMessage);
212 };
213 
214 class Database;
215 class ResultSet;
216 
224 class Statement : private Uncopyable, public Openable {
225  public:
230  ~Statement();
231 
241  void bind(const int index, const double value);
242 
252  void bind(const std::string& name, const double value);
253 
263  void bind(const int index, const int value);
264 
274  void bind(const std::string& name, const int value);
275 
285  void bind(const int index, const std::string& value);
286 
296  void bind(const std::string& name, const std::string& value);
297 
303  void close();
304 
311  ResultSet execute();
312 
320  bool reset();
321 
322  private:
323  explicit Statement(sqlite3_stmt* handle);
324 
325  int getParameterIndex(const std::string& name) const;
326  void handleBindResult(const int index, const int result) const;
327  void requireCanRead() const;
328  void setInstancePointer(const std::weak_ptr<Statement>& instancePointer);
329  bool step();
330 
331  sqlite3_stmt* m_handle;
332  bool m_canRead;
333  std::weak_ptr<Statement> m_instancePointer;
334 
335  friend class Database;
336  friend class ResultSet;
337 };
338 
350 class Database : private Uncopyable, public Openable {
351  public:
356  Database();
357 
371  explicit Database(const std::string& file);
372 
376  ~Database();
377 
381  void close();
382 
392  void execute(const std::string& sql);
393 
403  int lastInsertRowId() const;
404 
416  void open(const std::string& file);
417 
428  std::shared_ptr<Statement> prepare(const std::string& sql);
429 
430  private:
431  sqlite3* m_handle;
432 };
433 
438 class ResultSet {
439  public:
443  bool canRead() const;
444 
452  int columnCount() const;
453 
461  bool next();
462 
472  double readDouble(const int column) const;
473 
483  int readInt(const int column) const;
484 
494  std::string readString(const int column) const;
495 
496  private:
497  explicit ResultSet(const std::shared_ptr<Statement> statement);
498 
499  const std::shared_ptr<Statement> m_statement;
500 
501  friend class Statement;
502 };
503 
504 } // namespace sqlitepp
505 
506 #endif // SQLITEPP_SQLITEPP_H_
A result set returned from a SQL query.
Definition: sqlitepp.h:438
A handle for a SQLite3 statement.
Definition: sqlitepp.h:224
A class that forbids copying and assignments for all subclasses.
Definition: sqlitepp.h:116
An error that occurred during a database operation.
Definition: sqlitepp.h:184
Contains all classes of the sqlitepp library.
Definition: sqlitepp.cpp:10
A handle for a SQLite3 database.
Definition: sqlitepp.h:350
An element that has the two states open and closed.
Definition: sqlitepp.h:138