sqlitepp
A small sqlite3 C++ library
 All Classes Functions Pages
database.cpp
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 #include "sqlitepp.h"
20 
21 namespace sqlitepp {
22 
23  SQLiteException DatabaseNotOpened("Database has not been opened yet.");
24  SQLiteException DatabaseOpened("A database has been opened already.");
25 
26  inline void Database::checkDatabaseOpened() const {
27  if(!this->isopen) {
28  throw DatabaseNotOpened;
29  }
30  }
31 
33  this->checkDatabaseOpened();
34 
35  return sqlite3_last_insert_rowid(this->database);
36  }
37 
38  Database::Database(const std::string& file, const OpenFlags flags) {
39  this->init();
40  this->open(file, flags);
41  }
42 
44  this->init();
45  }
46 
47  void Database::init(void) {
48  this->isopen = this->transaction = false;
49  this->database = NULL;
50  this->lastResult = 0;
51  }
52 
53  void Database::open(const std::string& file, const OpenFlags flags) {
54  if(this->isopen) {
55  throw DatabaseOpened;
56  }
57 
58  int flag = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
59 
60  switch(flags) {
61  case READONLY:
62  flag = SQLITE_OPEN_READONLY;
63  break;
64 
65  case READWRITE:
66  flag = SQLITE_OPEN_READWRITE;
67  break;
68 
69  case CREATE:
70  flag = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
71  break;
72  }
73 
74  this->lastResult = sqlite3_open_v2(file.c_str(), &this->database, flag, NULL);
75  if(this->lastResult != SQLITE_OK) {
76  throw SQLiteException(this->database);
77  } else {
78  this->isopen = true;
79  }
80  }
81 
83  this->exec("PRAGMA foreign_keys = ON;");
84  }
85 
87  this->exec("PRAGMA foreign_keys = OFF;");
88  }
89 
90  bool Database::isOpen(void) {
91  return this->isopen;
92  }
93 
95  this->close();
96  }
97 
98  int Database::exec(const std::string& str) {
99  this->checkDatabaseOpened();
100 
101  this->lastResult = sqlite3_exec(this->database, str.c_str(), NULL, NULL, NULL);
102  if(this->lastResult != SQLITE_OK) {
103  throw SQLiteException(this->database);
104  }
105 
106  return sqlite3_changes(this->database);
107  }
108 
109  void Database::beginTransaction(const TransactionFlags flags) {
110  // No need do check, if a database has been opened, exec does that
111 
112  if(this->transaction) {
113  return;
114  }
115 
116  std::string flag("DEFERRED");
117  switch(flags) {
118  case DEFERRED:
119  flag = "DEFERRED";
120  break;
121 
122  case IMMEDIATE:
123  flag = "IMMEDIATE";
124  break;
125 
126  case EXCLUSIVE:
127  flag = "EXCLUSIVE";
128  break;
129  }
130  std::string execStr = "BEGIN " + flag + " TRANSACTION;";
131 
132  exec(execStr);
133  this->transaction = true;
134  }
135 
136  void Database::rollback(void) {
137  // No need do check, if a database has been opened, exec does that
138  if(!this->transaction) {
139  return;
140  }
141 
142  this->exec("ROLLBACK;");
143  this->transaction = false;
144  }
145 
147  // No need do check, if a database has been opened, exec does that
148  if(!this->transaction) {
149  return;
150  }
151 
152  this->exec("END TRANSACTION;");
153  this->transaction = false;
154  }
155 
156  void Database::close(void) {
157  if(this->isopen) {
158  if(this->transaction) {
159  this->rollback();
160  }
161 
162  sqlite3_close(this->database);
163  this->database = NULL;
164  this->isopen = false;
165  this->transaction = false;
166  }
167  }
168 }
169 
~Database(void)
destructor
Definition: database.cpp:94
bool isOpen(void)
returns true, when a database has been opened, false otherwise
Definition: database.cpp:90
void deactivateForeignKeys(void)
deactivates foreign keys
Definition: database.cpp:86
void open(const std::string &path, const OpenFlags flag=CREATE)
Opens a database with the passed flag.
Definition: database.cpp:53
void endTransaction(void)
commits a transaction
Definition: database.cpp:146
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
Database(void)
empty constructor. Does not open a database
Definition: database.cpp:43
void close(void)
closes the database
Definition: database.cpp:156
void activateForeignKeys(void)
activates foreign keys
Definition: database.cpp:82
int exec(const std::string &sql)
executes a single statement without return values
Definition: database.cpp:98
void rollback(void)
executes a rollback on the current transaction
Definition: database.cpp:136