Repository navigation
Expand file tree
/
Copy pathtest1.c
More file actions
383 lines (298 loc) · 11.9 KB
/
Copy pathtest1.c
File metadata and controls
383 lines (298 loc) · 11.9 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright (c) 2022-2024 Buzz Moschetti <buzz.moschetti@gmail.com>
//
// Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement is hereby granted,
// provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
// ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#include <stdio.h>
#include <sqlite3.h>
#include <bson.h>
char err_buf[1024]; // nice big err msg buffer...
static char* basic_changes_test(sqlite3 *db, const char* sql, int exp_changes)
{
sqlite3_stmt* stmt = 0;
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0 );
if(rc != SQLITE_OK) {
sprintf(err_buf, "sqlite3_prepare_v2 [%s]: rc: %d\n", sql, rc);
return err_buf;
}
rc = sqlite3_step(stmt);
if(rc != SQLITE_DONE) {
sprintf(err_buf, "sqlite3_step [%s]: did not return DONE\n", sql);
return err_buf;
}
int act_changes = sqlite3_changes(db);
if(act_changes != exp_changes) {
sprintf(err_buf, "sqlite3_changes [%s]: expect [%d], got [%d]\n", sql, exp_changes, act_changes);
return err_buf;
}
// Step, Clear and Reset the statement after each bind.
rc = sqlite3_clear_bindings( stmt );
rc = sqlite3_reset( stmt );
rc = sqlite3_finalize( stmt ); // Finalize the prepared stat
return 0; // OK
}
static void exec_bct(sqlite3 *db, const char* desc, const char* sql, int exp_changes)
{
char* err;
printf("%s ... ", desc);
if((err = basic_changes_test(db, sql, exp_changes)) != NULL) {
printf("FAIL; %s: [%s]\n", sql, err);
} else {
printf("ok\n");
}
}
static char* basic_scalar_test(sqlite3 *db, const char* sql, bson_type_t exp_type, const void* exp_value)
{
sqlite3_stmt* stmt = 0;
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0 );
if(rc != SQLITE_OK) {
sprintf(err_buf, "sqlite3_prepare_v2 [%s]: rc: %d\n", sql, rc);
return err_buf;
}
int one_row_rc = sqlite3_step(stmt);
if(one_row_rc == SQLITE_ROW && exp_type == BSON_TYPE_EOD) {
sprintf(err_buf, "expected empty response but got at least 1");
return err_buf;
}
{
if(exp_type == BSON_TYPE_UTF8) {
const char* v = (const char*)sqlite3_column_text(stmt, 0);
//const unsigned char* v = sqlite3_column_text(stmt, 0);
//
if(v == NULL) {
sprintf(err_buf, "expect [%s]; got [null]\n", (const char*)exp_value);
return err_buf;
}
if(strcmp(v,exp_value)) {
sprintf(err_buf, "expect [%s]; got [%s]\n", (const char*)exp_value, v);
return err_buf;
}
}
if(exp_type == BSON_TYPE_DOUBLE) {
double v = *((double*)exp_value);
double v2 = sqlite3_column_double(stmt, 0);
//if(fabs(v - v2) > 0.000000001) {
if(v - v2 != 0.0) {
sprintf(err_buf, "expect [%.10lf]; got [%.10lf]\n", v, v2);
return err_buf;
}
}
if(exp_type == BSON_TYPE_INT32) {
int v = *((int*)exp_value);
int v2 = sqlite3_column_int(stmt, 0);
if(v != v2) {
sprintf(err_buf, "expect [%d]; got [%d]\n", v, v2);
return err_buf;
}
}
if(exp_type == BSON_TYPE_INT64) {
long v = *((long*)exp_value);
long v2 = sqlite3_column_int64(stmt, 0);
if(v != v2) {
sprintf(err_buf, "expect [%ld]; got [%ld]\n", v, v2);
return err_buf;
}
}
if(exp_type == BSON_TYPE_NULL) {
int st = sqlite3_column_type(stmt,0);
if(SQLITE_NULL != st) {
sprintf(err_buf, "expect NULL; got sqlite type %d", st);
return err_buf;
}
}
}
// Step, Clear and Reset the statement after each bind.
rc = sqlite3_clear_bindings( stmt );
rc = sqlite3_reset( stmt );
rc = sqlite3_finalize( stmt ); // Finalize the prepared stat
return 0; // OK
}
static void exec_bst(sqlite3 *db, const char* desc, const char* sql, bson_type_t exp_type, const void* exp_value)
{
char* err;
printf("%s ... ", desc);
if((err = basic_scalar_test(db, sql, exp_type, exp_value)) != NULL) {
printf("FAIL; %s: [%s]\n", sql, err);
} else {
printf("ok\n");
}
}
double dval = 3.14159;
int ival = 7;
long lval = 743859238573L;
int boolval = 1; //
static int insert(sqlite3 *db) {
sqlite3_stmt* stmt = 0;
char jbuf[512];
char jbuf2[512];
//
// You can create BSON from scratch but for the purposes of this
// example we will just make it from some Extended JSON.
//
// That weird binary is: Pretend this is a JPEG
//
char *jj = "{\"hdr\":{\"id\":\"A%d\", \"ts\":{\"$date\":\"2023-01-12T13:14:15.678Z\"}, \"bigint\":{\"$numberLong\":\"%ld\"}}, \"amt\":{\"$numberDecimal\":\"10.09\"}, \"flag\":true, \"A\":{\"B\":[ %d ,{\"X\":\"QQ\", \"Y\":[\"ee\",\"ff\"]}, %lf ]}, \"thumbnail\" : { \"$binary\" : { \"base64\" : \"UHJldGVuZCB0aGlzIGlzIGEgSlBFRw==\", \"subType\" : \"00\" }} }";
//sprintf(jbuf, jj, 0, lval, dval, ival, dval); // id:"A0"
//sprintf(jbuf2, jj, 3, lval, dval, ival, dval); // id:"A0"
sprintf(jbuf, jj, 0, lval, ival, dval);
sprintf(jbuf2, jj, 3, lval, ival, dval);
printf("jbuf: [%s]\n", jbuf);
bson_error_t err; // on stack
bson_t* b = bson_new_from_json((const uint8_t *)jbuf, strlen(jbuf), &err);
if(b == NULL) {
printf("ERROR bad JSON 1 upon insert");
return 1;
}
// Now, get the raw bytes from the BSON object to save as a BLOB:
const uint8_t* data = bson_get_data(b);
int32_t len = b->len; // yes; there is no bson_get_len(b)
b = bson_new_from_json((const uint8_t *)jbuf2, strlen(jbuf2), &err);
if(b == NULL) {
printf("ERROR bad JSON 2 upon insert");
return 1;
}
// Now, get the raw bytes from the BSON object to save as a BLOB:
const uint8_t* data2 = bson_get_data(b);
int32_t len2 = b->len; // yes; there is no bson_get_len(b)
int rc = sqlite3_prepare_v2(db, "INSERT INTO bsontest (bdata,bdata2) values (?,?)", -1, &stmt, 0 );
if(rc != SQLITE_OK) {
printf("ERROR prep rc: %d\n", rc);
return 1;
}
rc = sqlite3_bind_blob( stmt, 1, (const void*) data, len, SQLITE_STATIC);
if(rc != SQLITE_OK) {
printf("ERROR: bind BSON 1 rc: %d\n", rc);
return 1;
}
rc = sqlite3_bind_blob( stmt, 2, (const void*) data2, len2, SQLITE_STATIC);
if(rc != SQLITE_OK) {
printf("ERROR: bind BSON 2 rc: %d\n", rc);
return 1;
}
// Doing an insert means we expect a single result DONE back:
rc = sqlite3_step( stmt );
if(rc != SQLITE_DONE) {
printf("? INSERT yields rc %d\n", rc);
}
rc = sqlite3_clear_bindings( stmt );
rc = sqlite3_reset( stmt );
rc = sqlite3_finalize( stmt ); // Finalize the prepared stat
return 0; // OK
}
static int create(sqlite3 *db) {
sqlite3_stmt* stmt = 0;
// Note we can call the column type something other than the other
// official sqlite types; it doesn't matter.
char* sql = "create table if NOT EXISTS bsontest (bdata BSON, bdata2 BSON)";
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0 );
if(rc != SQLITE_OK) {
printf("ERROR prep rc: %d\n", rc);
return 1;
}
// Doing an insert means we expect a single result DONE back:
rc = sqlite3_step( stmt );
if(rc != SQLITE_DONE) {
printf("? CREATE yields rc %d\n", rc);
return 1;
}
rc = sqlite3_clear_bindings( stmt );
rc = sqlite3_reset( stmt );
rc = sqlite3_finalize( stmt ); // Finalize the prepared stat
return 0; // OK
}
int activate_extension(sqlite3 *db)
{
const char* ext_path = "bsonext"; // prob should be a cmdline option...
const char* entry_point = "sqlite3_bson_init"; // ALWAYS the same!
int rc = sqlite3_enable_load_extension(db, 1); // TRUE
if(rc != SQLITE_OK) {
printf("error: cannot enable extension loading");
return 1;
}
char *zErrMsg = 0;
rc = sqlite3_load_extension(db, ext_path, entry_point, &zErrMsg);
if(rc != SQLITE_OK) {
printf("error: load ext [%s] failed: %d: %s\n", ext_path, rc, zErrMsg);
sqlite3_free(zErrMsg);
return 1;
}
return 0; // OK
}
struct scalar_test {
const char* name;
char* (*f)(sqlite3*,const char*,bson_type_t,const void*);
const char* a1;
bson_type_t a2;
const void* a3;
};
struct changes_test {
const char* name;
char* (*f)(sqlite3*,const char*,int exp_changes);
const char* a1; // sql
int a2; // exp changes
};
#define DO_TEST(f) (
/*
usage: test1 [ someFile.sqlite3 ]
*/
int main(int argc, char* argv[]) {
sqlite3 *db;
int rc;
char* dbf = argv[1];
rc = sqlite3_open(dbf, &db);
if( rc ) {
fprintf(stderr, "cannot open [%s]: %s\n", dbf, sqlite3_errmsg(db));
return(0);
} else {
fprintf(stderr, "Opened [%s] successfully\n", dbf);
}
if(0 != activate_extension(db)) { return 1; }
if(0 != create(db)) { return 1; }
if(0 != insert(db)) { return 1; }
//basic_scalar_test(db, "select bson_get(bdata,'hdr.id') from bsontest", BSON_TYPE_UTF8, "A0");
int zval = 0;
int oval = 1;
const char* fake_binary = "Pretend this is a JPEG";
char bval[128];
int n, idx = 0;
for(n = 0; n < strlen(fake_binary); n++) {
// Love that pointer math....
sprintf(bval+idx, "%02x", (uint8_t)fake_binary[n]);
idx += 2;
}
bval[idx] = '\0';
struct scalar_test XXX[] = {
{"string exists", basic_scalar_test, "select bson_get(bdata,'hdr.id') from bsontest", BSON_TYPE_UTF8, "A0"},
{"field !exists", basic_scalar_test, "select bson_get(bdata,'not.here') from bsontest", BSON_TYPE_NULL, 0},
{"no row at all", basic_scalar_test, "select bson_get(bdata,'hdr.id') from bsontest where FALSE", BSON_TYPE_EOD, 0},
{"double exists", basic_scalar_test, "select bson_get(bdata,'A.B.2') from bsontest", BSON_TYPE_DOUBLE, &dval}, // 3.14159
{"double exists", basic_scalar_test, "select bson_get(bdata,'A.B.2') from bsontest", BSON_TYPE_DOUBLE, &dval}, // 3.14159
{"int32 exists", basic_scalar_test, "select bson_get(bdata,'A.B.0') from bsontest", BSON_TYPE_INT32, &ival},
{"int64 exists", basic_scalar_test, "select bson_get(bdata,'hdr.bigint') from bsontest", BSON_TYPE_INT64, &lval},
{"bool exists", basic_scalar_test, "select bson_get(bdata,'flag') from bsontest", BSON_TYPE_INT32, &boolval},
// decimal, dates, and binary have no type equiv in sqlite; they
// emerge as strings:
{"date exists", basic_scalar_test, "select bson_get(bdata,'hdr.ts') from bsontest", BSON_TYPE_UTF8, "2023-01-12T13:14:15.678Z"},
{"decimal exists", basic_scalar_test, "select bson_get(bdata,'amt') from bsontest", BSON_TYPE_UTF8, "10.09"},
{"binary exists", basic_scalar_test, "select bson_get(bdata,'thumbnail') from bsontest", BSON_TYPE_UTF8, &bval},
};
for(int q = 0; q < sizeof(XXX)/sizeof(struct scalar_test); q++) {
exec_bst(db,XXX[q].name, XXX[q].a1, XXX[q].a2, XXX[q].a3);
}
// Recall jbuf and jbuf2 differ by a bit!
exec_bst(db,"verify bdata = bdata2 is false", "select bdata = bdata2 from bsontest", BSON_TYPE_INT32, &zval);
exec_bct(db,"internal BSON copy", "update bsontest set bdata2 = bdata", 1);
exec_bst(db,"internal BSON copy verify", "select bdata = bdata2 from bsontest", BSON_TYPE_INT32, &oval);
// Note this changes the column from type BLOB to type integer which
// should be caught by the "if not BLOB" logic...
exec_bct(db,"break bdata2 on purpose", "update bsontest set bdata2 = 17", 1);
exec_bst(db,"verify broken bdata2", "select bdata = bdata2 from bsontest", BSON_TYPE_INT32, &zval);
ival = 20;
exec_bst(db,"check int ops broken bdata2", "select 3 + bdata2 from bsontest", BSON_TYPE_INT32, &ival);
sqlite3_close(db);
}