-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.cpp
169 lines (134 loc) · 4.46 KB
/
database.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* Copyright (C)
* 2012 - Paul Weingardt
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "sqlitepp.h"
namespace sqlitepp {
SQLiteException DatabaseNotOpened("Database has not been opened yet.");
SQLiteException DatabaseOpened("A database has been opened already.");
inline void Database::checkDatabaseOpened() const {
if(!this->isopen) {
throw DatabaseNotOpened;
}
}
int Database::getLastRowId(void) {
this->checkDatabaseOpened();
return sqlite3_last_insert_rowid(this->database);
}
Database::Database(const std::string& file, const OpenFlags flags) {
this->init();
this->open(file, flags);
}
Database::Database(void) {
this->init();
}
void Database::init(void) {
this->isopen = this->transaction = false;
this->database = NULL;
this->lastResult = 0;
}
void Database::open(const std::string& file, const OpenFlags flags) {
if(this->isopen) {
throw DatabaseOpened;
}
int flag = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
switch(flags) {
case READONLY:
flag = SQLITE_OPEN_READONLY;
break;
case READWRITE:
flag = SQLITE_OPEN_READWRITE;
break;
case CREATE:
flag = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
break;
}
this->lastResult = sqlite3_open_v2(file.c_str(), &this->database, flag, NULL);
if(this->lastResult != SQLITE_OK) {
throw SQLiteException(this->database);
} else {
this->isopen = true;
}
}
void Database::activateForeignKeys(void) {
this->exec("PRAGMA foreign_keys = ON;");
}
void Database::deactivateForeignKeys(void) {
this->exec("PRAGMA foreign_keys = OFF;");
}
bool Database::isOpen(void) {
return this->isopen;
}
Database::~Database(void) {
this->close();
}
int Database::exec(const std::string& str) {
this->checkDatabaseOpened();
this->lastResult = sqlite3_exec(this->database, str.c_str(), NULL, NULL, NULL);
if(this->lastResult != SQLITE_OK) {
throw SQLiteException(this->database);
}
return sqlite3_changes(this->database);
}
void Database::beginTransaction(const TransactionFlags flags) {
// No need do check, if a database has been opened, exec does that
if(this->transaction) {
return;
}
std::string flag("DEFERRED");
switch(flags) {
case DEFERRED:
flag = "DEFERRED";
break;
case IMMEDIATE:
flag = "IMMEDIATE";
break;
case EXCLUSIVE:
flag = "EXCLUSIVE";
break;
}
std::string execStr = "BEGIN " + flag + " TRANSACTION;";
exec(execStr);
this->transaction = true;
}
void Database::rollback(void) {
// No need do check, if a database has been opened, exec does that
if(!this->transaction) {
return;
}
this->exec("ROLLBACK;");
this->transaction = false;
}
void Database::endTransaction(void) {
// No need do check, if a database has been opened, exec does that
if(!this->transaction) {
return;
}
this->exec("END TRANSACTION;");
this->transaction = false;
}
void Database::close(void) {
if(this->isopen) {
if(this->transaction) {
this->rollback();
}
sqlite3_close(this->database);
this->database = NULL;
this->isopen = false;
this->transaction = false;
}
}
}