-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqlitebuilder.cpp
196 lines (168 loc) · 5.19 KB
/
sqlitebuilder.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "sqlitebuilder.hpp"
#include <iostream>
#include <stdexcept>
#include <cstdlib>
#include <cstring>
// ---------------------------------------------------------------------------
// helper functions
time_t parsedate(const char *value) {
if (!value) {
std::cerr << "warning: date parse failed for NULL" << std::endl;
return 0;
}
tm t = {0};
// try 2009-07-12T22:51:42.563
const char *rem = strptime(value, "%Y-%m-%dT%H:%M:%S", &t);
if (!rem) {
// try 2009-07-12
rem = strptime(value, "%Y-%m-%d", &t);
if (!rem) {
std::cerr << "warning: date parse failed for '" << value << "'" << std::endl;
return 0;
}
else if (*rem != '\0') {
// error
std::cerr << "warning: date parse has unknown trailing data for '" << value << "'" << std::endl;
return 0;
}
}
else if (*rem != '.') {
// error
std::cerr << "warning: date parse has unknown trailing data for '" << value << "'" << std::endl;
return 0;
}
return timegm(&t);
}
int parsebool(const char *value) {
if (std::strcmp(value, "True") == 0)
return 1;
else if (std::strcmp(value, "False") == 0)
return 0;
else
throw std::runtime_error(std::string("cannot parse value as bool: ") + value);
}
// ---------------------------------------------------------------------------
// sqlitebuilder
sqlitebuilder::sqlitebuilder(sqlite3 *a_db)
: db(a_db), prepared(NULL), cur_index(0), row_count(0), name() {
}
void sqlitebuilder::begin_transaction() {
int rv = sqlite3_exec(db, "BEGIN;", 0, 0, 0);
if (rv != SQLITE_OK)
std::cerr << "can't begin transaction: (" << sqlite3_errmsg(db) << ")" << std::endl;
}
void sqlitebuilder::end_transaction() {
int rv = sqlite3_exec(db, "COMMIT;", 0, 0, 0);
if (rv != SQLITE_OK)
std::cerr << "can't commit transaction: (" << sqlite3_errmsg(db) << ")" << std::endl;
}
void sqlitebuilder::open_table(const table_spec &spec) {
int rv;
name = spec.name;
columns_t columns = spec.column_names();
std::string drop = "DROP TABLE IF EXISTS " + name + ";";
std::string create = "CREATE TABLE " + name + " (";
std::string prepare = "INSERT INTO " + name + " VALUES (";
for (columns_t::iterator it = columns.begin(); it != columns.end(); ++it) {
create += *it + ", ";
prepare += "?, ";
}
create.erase(create.size() - 2);
prepare.erase(prepare.size() - 2);
create += ");";
prepare += ");";
rv = sqlite3_exec(db, drop.c_str(), 0, 0, 0);
if (rv != SQLITE_OK)
std::cerr << "can't drop existing table: (" << sqlite3_errmsg(db) << ")" << std::endl;
rv = sqlite3_exec(db, create.c_str(), 0, 0, 0);
if (rv != SQLITE_OK)
throw std::runtime_error("can't create table " + name + " (" + sqlite3_errmsg(db) + ")");
rv = sqlite3_prepare_v2(db, prepare.c_str(), -1, &prepared, 0);
if (rv != SQLITE_OK)
throw std::runtime_error("can't prepare insert for table " + name + " (" + sqlite3_errmsg(db) + ")");
row_count = 0;
begin_transaction();
}
void sqlitebuilder::table_complete() {
sqlite3_finalize(prepared);
prepared = NULL;
end_transaction();
}
int sqlite3_bind_any(sqlite3_stmt* s, int i, const char *v) {
if (v)
return sqlite3_bind_text(s, i, v, -1, SQLITE_STATIC);
else
return sqlite3_bind_null(s, i);
}
int sqlite3_bind_any(sqlite3_stmt* s, int i, const std::string &v) {
return sqlite3_bind_any(s, i, v.c_str());
}
int sqlite3_bind_any(sqlite3_stmt* s, int i, int v) {
return sqlite3_bind_int(s, i, v);
}
int sqlite3_bind_any(sqlite3_stmt* s, int i, double v) {
return sqlite3_bind_double(s, i, v);
}
// now that's a case where a simple copy & paste would probably have been
// better...
template<typename T>
void sqlitebuilder::add_column_tmpl(T value){
int rv;
rv = sqlite3_bind_any(prepared, ++cur_index, value);
if (rv != SQLITE_OK)
std::cerr << "can't bind parameter in " << name <<
" (" << sqlite3_errmsg(db) << "), value '" << value << "'" <<
std::endl;
}
void sqlitebuilder::add_column(const column_spec &col, const char *value) {
if (!value) {
// NULL
add_column_tmpl(value);
return;
}
switch (col.type) {
case CT_VCHR12:
case CT_VCHR64:
case CT_TEXT:
add_column_tmpl(value);
break;
case CT_INT:
add_column_tmpl(atoi(value));
break;
case CT_DATE:
add_column_tmpl(static_cast<int>(parsedate(value)));
break;
case CT_BOOL:
add_column_tmpl(parsebool(value));
break;
default:
throw std::logic_error("invalid column type");
break;
}
}
void sqlitebuilder::open_row() {
int rv;
if (++row_count % 100000 == 0) {
end_transaction();
begin_transaction();
}
rv = sqlite3_reset(prepared);
if (rv != SQLITE_OK)
std::cerr << "sqlite3_reset: (" << sqlite3_errmsg(db) << ")" << std::endl;
rv = sqlite3_clear_bindings(prepared);
if (rv != SQLITE_OK)
std::cerr << "sqlite3_clear_bindings: (" << sqlite3_errmsg(db) << ")" << std::endl;
cur_index = 0;
}
void sqlitebuilder::row_complete() {
int rv = sqlite3_step(prepared);
if (rv != SQLITE_DONE)
std::cerr << "sqlite3_step: (" << sqlite3_errmsg(db) << ")" << std::endl;
}
void sqlitebuilder::add_index(const std::string &column) {
std::string indexname = name + "_" + column;
std::string sql = "CREATE INDEX " + indexname + " ON " + name + " (" + column + ");";
int rv = sqlite3_exec(db, sql.c_str(), 0, 0, 0);
if (rv != SQLITE_OK)
std::cerr << "sqlite3_exec: (" << sqlite3_errmsg(db) << ")" << std::endl;
}