-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSqliteModel.cpp
More file actions
119 lines (96 loc) · 2.11 KB
/
SqliteModel.cpp
File metadata and controls
119 lines (96 loc) · 2.11 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
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
/**
* Copyright (C) 2012-2013 Allan SIMON (Sysko) <allan.simon@supinfo.com>
* See accompanying file COPYING.TXT file for licensing details.
*
* @category Cppcms-skeleton
* @author Allan SIMON <allan.simon@supinfo.com>
* @package Models
*
*/
#include <fstream>
#include <booster/log.h>
#include "cppcms_skel/generics/Config.h"
#include "SqliteModel.h"
#include "SqlImporter.h"
namespace cppcmsskel {
namespace models {
/**
*
*/
SqliteModel::SqliteModel() {
create_session(
Config::get_instance()->sqlite3Path
);
}
/**
*
*/
SqliteModel::SqliteModel(cppdb::session sqliteDbParam) : sqliteDb(sqliteDbParam) {
}
/**
*
*/
SqliteModel::SqliteModel(const std::string &databasePath) {
create_session(databasePath);
}
/**
*
*/
void SqliteModel::create_session(
const std::string &databasePath
) {
try {
sqliteDb = cppdb::session(
"sqlite3:db=" + databasePath
);
// We need this to have triggers call even in some tricky case
// (for example "update or replace" that cause a deletion, will not call
// the delete trigger otherwise)
sqliteDb.create_statement("PRAGMA recursive_triggers = 1 ;").exec();
} catch (cppdb::cppdb_error const &e) {
BOOSTER_ERROR("cppcms") << e.what();
}
}
/**
*
*/
bool SqliteModel::import_sql_file(
const std::string &sqlFilePath
) {
SqlImporter importer(sqliteDb);
return importer.from_file(sqlFilePath);
}
/**
*
*/
bool SqliteModel::execute_simple(
cppdb::statement &statement
) {
try {
statement.exec();
} catch (cppdb::cppdb_error const& e) {
BOOSTER_ERROR("cppcms") << e.what();
statement.reset();
return false;
}
statement.reset();
return true;
}
/**
*
*/
bool SqliteModel::check_existence(
cppdb::statement &statement
) {
cppdb::result res = statement.row();
int checkresult = 0;
res.fetch(0,checkresult);
// Don't forget to reset statement
statement.reset();
if (checkresult == 1 ) {
return true;
}
return false;
}
} // end of namespace models
} // end namespace cppcmsskel