-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cpp
More file actions
65 lines (44 loc) · 1.19 KB
/
Database.cpp
File metadata and controls
65 lines (44 loc) · 1.19 KB
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
#include "Database.hpp"
#include "DatabaseException.hpp"
#include "SqliteErrorCodes.hpp"
#include <iostream>
#include <string>
Database::Database(std::string dbName) {
references = new unsigned int;
*references = 1;
char* zErrMsg = 0;
int rc;
if(!dbName.compare("")) {
rc = sqlite3_open_v2("memory", &db, SQLITE_OPEN_MEMORY, NULL);
} else {
rc = sqlite3_open(dbName.c_str(), &db);
}
if(rc) {
sqlite3_close(db);
std::cout << SqliteErrorCodes::getErrorName(sqlite3_extended_errcode(db)) << std::endl;
throw DatabaseException(std::string("The database could not be opened at this times: ") + sqlite3_errmsg(db));
}
}
//The big three
Database::~Database() {
if(*references == 1) {
sqlite3_close(db);
delete references;
} else {
(*references)--;
}
}
Database& Database::operator=(const Database& db) {
references = db.references;
*references++;
return *this;
}
Database::Database(const Database& db) {
*this = db;
}
Statement Database::prepare(const std::string& query) {
sqlite3_stmt* stmtptr = NULL;
const char* zTail = NULL;
int success = sqlite3_prepare_v2(db, query.c_str(), -1, &stmtptr, &zTail);
return Statement(stmtptr);
}