-
Notifications
You must be signed in to change notification settings - Fork 1
/
SQLite.cpp
236 lines (177 loc) · 6.81 KB
/
SQLite.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "SQLite.h"
#include <cstdio>
#include <sstream>
#include <boost/shared_ptr.hpp>
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++){
// printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
//printf("\n");
return 0;
}
SQLite::SQLite() {
}
bool SQLite::write(Context & ctx) {
char *zErrMsg = 0;
int rc;
stringstream sql_query;
sqlite3_stmt *stmt = NULL;
rc = sqlite3_exec(db, "create table variables ("
"varpos integer primary key asc,"
"varname varchar(255),"
"vartype integer,"
"varformat varchar(20),"
"valuelabel varchar(255),"
"variablelabel varchar(255));" , NULL, 0, &zErrMsg);
if( rc != SQLITE_OK )
{
cerr << "SQL error: " << zErrMsg << endl;
sqlite3_free(zErrMsg);
return false;
}
sql_query.str("");
rc = sqlite3_prepare_v2(db, "INSERT INTO VARIABLES (VARPOS, VARNAME, VARTYPE, VARFORMAT, VALUELABEL, VARIABLELABEL ) VALUES ( NULL, ?, ?, ?, ?, ? )", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
cerr << "SQLError prepare failed: " << sqlite3_errmsg(db) << endl;
return false;
}
sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, &zErrMsg);
for (int j = 0; j < ctx.vList.size(); j++)
{
rc = sqlite3_bind_text(stmt, 1, ((StataVariables)*ctx.vList[j]).varname.c_str(), ((StataVariables)*ctx.vList[j]).varname.length(), SQLITE_STATIC);
if (rc != SQLITE_OK) {
cerr << "SQLError: varname bind failed: " << sqlite3_errmsg(db) << endl;
return false;
}
rc = sqlite3_bind_int(stmt, 2, ((StataVariables)*ctx.vList[j]).type);
if (rc != SQLITE_OK) {
cerr << "SQLError: type bind failed: " << sqlite3_errmsg(db) << endl;
return false;
}
rc = sqlite3_bind_text(stmt, 3, ((StataVariables)*ctx.vList[j]).format.c_str(), ((StataVariables)*ctx.vList[j]).format.length(), SQLITE_STATIC);
if (rc != SQLITE_OK) {
cerr << "SQLError: format bind failed: " << sqlite3_errmsg(db) << endl;
return false;
}
rc = sqlite3_bind_text(stmt, 4, ((StataVariables)*ctx.vList[j]).vallbl.c_str(), ((StataVariables)*ctx.vList[j]).vallbl.length(), SQLITE_STATIC);
if (rc != SQLITE_OK) {
cerr << "SQLError: vallbl bind failed: " << sqlite3_errmsg(db) << endl;
return false;
}
rc = sqlite3_bind_text(stmt, 5, ((StataVariables)*ctx.vList[j]).varlbl.c_str(), ((StataVariables)*ctx.vList[j]).varlbl.length(), SQLITE_STATIC);
if (rc != SQLITE_OK) {
cerr << "SQLError: varlbl bind failed: " << sqlite3_errmsg(db) << endl;
return false;
}
rc = sqlite3_step(stmt);
if( rc != SQLITE_DONE )
{
cerr << "SQL error: " << sqlite3_errmsg(db) << endl;
return false;
}
sqlite3_clear_bindings(stmt); /* Clear bindings */
sqlite3_reset(stmt);
}
sqlite3_exec(db, "END TRANSACTION", NULL, NULL, &zErrMsg);
rc = sqlite3_exec(db, "create table vardata ("
"obs integer,"
"varpos integer,"
"value blob,"
"sizer int);" , NULL, 0, &zErrMsg);
if( rc != SQLITE_OK )
{
cerr << "SQL error: " << zErrMsg << endl;
sqlite3_free(zErrMsg);
return false;
}
sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, &zErrMsg);
rc = sqlite3_prepare_v2(db, "INSERT INTO VARDATA ( OBS, VARPOS, VALUE, SIZER ) VALUES ( ?, ?, ?, ? )", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
cerr << "SQLError prepare failed: " << sqlite3_errmsg(db) << endl;
return false;
}
else
{
for (int k = 0; k < ctx.vData.size(); k++)
{
for (int j = 0; j < ctx.vList.size(); j++)
{
int sz = 0;
rc = sqlite3_bind_int(stmt, 1, k+1);
if (rc != SQLITE_OK) {
cerr << "SQLError: obs bind failed: " << sqlite3_errmsg(db) << endl;
return false;
}
rc = sqlite3_bind_int(stmt, 2, j+1);
if (rc != SQLITE_OK) {
cerr << "SQLError: varpos bind failed: " << sqlite3_errmsg(db) << endl;
return false;
}
switch(ctx.vList[j]->type)
{
case ST_STRL:
cout << "Strl " << endl;
break;
case ST_DOUBLE:
sz=8;
break;
case ST_FLOAT:
sz=4;
break;
case ST_LONG:
sz=4;
break;
case ST_INT:
sz=2;
break;
case ST_BYTE:
sz=1;
break;
default:
sz=strlen((*ctx.vData[k])[j]);
break;
}
rc = sqlite3_bind_blob(stmt,3, (*ctx.vData[k])[j], sz, SQLITE_STATIC);
if (rc != SQLITE_OK) {
cerr << "SQLError blob bind failed: " << sqlite3_errmsg(db) << endl;
return false;
}
rc = sqlite3_bind_int(stmt, 4, sz);
if (rc != SQLITE_OK) {
cerr << "SQLError: sizer bind failed: " << sqlite3_errmsg(db) << endl;
return false;
}
rc = sqlite3_step(stmt);
if( rc != SQLITE_DONE )
{
cerr << "SQL error: " << sqlite3_errmsg(db) << endl;
return false;
}
sqlite3_clear_bindings(stmt); /* Clear bindings */
sqlite3_reset(stmt);
}
}
}
sqlite3_exec(db, "END TRANSACTION", NULL, NULL, &zErrMsg);
sqlite3_finalize(stmt);
return true;
}
bool SQLite::connect(void * ptr) {
char *zErrMsg = 0;
int rc;
char * filename = (char *)ptr;
rc = sqlite3_open(filename, &db);
if (rc)
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return false;
}
return true;
}
bool SQLite::close() {
return true;
}
SQLite::~SQLite() {
}