sqlitepp
A small sqlite3 C++ library
 All Classes Functions Pages
sqlitepp.h
1 /* Copyright (C)
2  * 2012 - Paul Weingardt
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License
5  * as published by the Free Software Foundation; either version 2
6  * of the License, or (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16  *
17  */
18 
19 #ifndef SQLITEPP_H
20 #define SQLITEPP_H
21 
22 #include <sqlite3.h>
23 #include <time.h>
24 #include <stdlib.h>
25 #include <map>
26 #include <string>
27 
28 namespace sqlitepp {
29 
33  std::string doubleToString(const double value);
34 
38  std::string floatToString(const float value);
39 
44  std::string intToString(const int value);
45 
46  class Statement;
47 
51  enum TransactionFlags {DEFERRED, IMMEDIATE, EXCLUSIVE};
52 
60  enum StepValue {ROW, DONE, UNKNOWN};
61 
67  enum OpenFlags {READONLY, READWRITE, CREATE};
68 
69 
73  class Database {
74  friend class Statement;
75  private:
79  void init(void);
80 
84  bool isopen;
85 
89  bool transaction;
90 
94  sqlite3* database;
95 
99  int lastResult;
100 
101  inline void checkDatabaseOpened() const;
102  public:
106  Database(void);
107 
114  Database(const std::string& path, const OpenFlags flag = CREATE);
115 
119  ~Database(void);
120 
127  void open(const std::string& path, const OpenFlags flag = CREATE);
128 
132  bool isOpen(void);
133 
137  void close(void);
138 
142  void activateForeignKeys(void);
143 
147  void deactivateForeignKeys(void);
148 
154  int exec(const std::string& sql);
155 
159  void beginTransaction(const TransactionFlags = DEFERRED);
160 
164  void endTransaction(void);
165 
169  void rollback(void);
170 
176  int getLastRowId(void);
177  };
178 
179 
183  class Statement {
184  private:
188  int lastResult;
189 
196  bool finalized;
197 
201  sqlite3_stmt* statement;
202 
206  Database& db;
207 
211  std::map<std::string, int> columns;
212 
216  StepValue step(void);
217 
221  inline void checkPrepared() const;
222 
223  public:
229  Statement(Database& db);
230 
234  ~Statement();
235 
242  void bindString(const int n, const std::string& value);
243 
250  void bindInt(const int n, const int value);
251 
258  void bindDouble(const int n, const double value);
259 
265  void bindNull(const int n);
266 
274  std::string getString(const std::string& column) const;
275 
283  int getInt(const std::string& column) const;
284 
292  double getDouble(const std::string& column) const;
293 
300  void getString(const int n, std::string& out) const;
301 
308  void getInt(const int n, int& out) const;
309 
316  void getDouble(const int n, double& out) const;
317 
323  std::string getString(const int n) const ;
324 
330  int getInt(const int n) const;
331 
337  double getDouble(const int n) const;
338 
344  void prepare(const std::string& sql);
345 
351  bool fetchRow(void);
352 
356  void exec(void);
357 
362  void finalize(void);
363  };
364 
365 
366  class SQLiteException : public std::exception {
367  private:
368  std::string error;
369 
370  public:
374  SQLiteException(const std::string& str) {
375  this->error = str;
376  }
377 
384  SQLiteException(sqlite3* db) {
385  this->error = std::string(sqlite3_errmsg(db));
386  }
387 
388  virtual ~SQLiteException() throw() {
389  }
390 
394  virtual const char* what() const throw() {
395  return this->error.c_str();
396  }
397  };
398 
399  extern SQLiteException DatabaseNotOpened;
400  extern SQLiteException DatabaseOpened;
401  extern SQLiteException StatementNotPrepared;
402 }
403 
404 #endif
SQLiteException(sqlite3 *db)
The error message is the last sqlite error in the passed sqlite database.
Definition: sqlitepp.h:384
void bindNull(const int n)
Binds the nth parameter with the null value.
Definition: statement.cpp:185
A prepared statement.
Definition: sqlitepp.h:183
~Database(void)
destructor
Definition: database.cpp:94
bool fetchRow(void)
fetches the next row
Definition: statement.cpp:93
bool isOpen(void)
returns true, when a database has been opened, false otherwise
Definition: database.cpp:90
std::string getString(const std::string &column) const
gets a column as string
Definition: statement.cpp:131
int getInt(const std::string &column) const
gets a column as integer
Definition: statement.cpp:99
void bindString(const int n, const std::string &value)
Binds the nth parameter with the passed value as string.
Definition: statement.cpp:173
void deactivateForeignKeys(void)
deactivates foreign keys
Definition: database.cpp:86
void exec(void)
executes the prepared statement
Definition: statement.cpp:42
Statement(Database &db)
Default constructor.
Definition: statement.cpp:33
~Statement()
Destructor.
Definition: statement.cpp:200
void open(const std::string &path, const OpenFlags flag=CREATE)
Opens a database with the passed flag.
Definition: database.cpp:53
virtual const char * what() const
gets the error message
Definition: sqlitepp.h:394
void endTransaction(void)
commits a transaction
Definition: database.cpp:146
void bindDouble(const int n, const double value)
Binds the nth parameter with the passed value as integer.
Definition: statement.cpp:179
SQLiteException(const std::string &str)
Constructs a SQLiteException with the passed error message.
Definition: sqlitepp.h:374
void beginTransaction(const TransactionFlags=DEFERRED)
Begins a transaction.
Definition: database.cpp:109
int getLastRowId(void)
gets the last ID, that has been inserted
Definition: database.cpp:32
void prepare(const std::string &sql)
prepares a string as statement
Definition: statement.cpp:49
Database(void)
empty constructor. Does not open a database
Definition: database.cpp:43
double getDouble(const std::string &column) const
gets a column as double
Definition: statement.cpp:146
void finalize(void)
releases any allocated resources. Use it, when you want to reuse a statement object.
Definition: statement.cpp:191
void close(void)
closes the database
Definition: database.cpp:156
void activateForeignKeys(void)
activates foreign keys
Definition: database.cpp:82
void bindInt(const int n, const int value)
Binds the nth parameter with the passed value as integer.
Definition: statement.cpp:167
int exec(const std::string &sql)
executes a single statement without return values
Definition: database.cpp:98
The main database class.
Definition: sqlitepp.h:73
void rollback(void)
executes a rollback on the current transaction
Definition: database.cpp:136