forked from sqliteai/sqlite-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.c
More file actions
7908 lines (6453 loc) · 305 KB
/
unit.c
File metadata and controls
7908 lines (6453 loc) · 305 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// main.c
// unittest
//
// Created by Marco Bambini on 31/10/24.
//
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <inttypes.h>
#include <fcntl.h>
#include "sqlite3.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#include "pk.h"
#include "dbutils.h"
#include "database.h"
#include "cloudsync.h"
#include "cloudsync_sqlite.h"
// declared only if macro CLOUDSYNC_UNITTEST is defined
extern char *OUT_OF_MEMORY_BUFFER;
extern bool force_vtab_filter_abort;
extern bool force_uncompressed_blob;
void dbvm_reset (dbvm_t *stmt);
int dbvm_count (dbvm_t *stmt, const char *value, size_t len, int type);
int dbvm_execute (dbvm_t *stmt, void *data);
int dbutils_settings_get_value (cloudsync_context *data, const char *key, char *buffer, size_t *blen, int64_t *intvalue);
int dbutils_settings_table_load_callback (void *xdata, int ncols, char **values, char **names);
int dbutils_settings_check_version (cloudsync_context *data, const char *version);
bool dbutils_settings_migrate (cloudsync_context *data);
const char *vtab_opname_from_value (int value);
int vtab_colname_is_legal (const char *name);
int dbutils_binary_comparison (int x, int y);
sqlite3 *do_create_database (void);
int cloudsync_table_sanity_check (cloudsync_context *data, const char *name, bool skip_int_pk_check);
bool database_system_exists (cloudsync_context *data, const char *name, const char *type);
static int stdout_backup = -1; // Backup file descriptor for stdout
static int dev_null_fd = -1; // File descriptor for /dev/null
static int test_counter = 1;
#define TEST_INSERT (1 << 0) // 0x01
#define TEST_UPDATE (1 << 1) // 0x02
#define TEST_DELETE (1 << 2) // 0x04
#define TEST_ALTER (1 << 3) // 0x08
#define TEST_PRIKEYS (1 << 0) // 0x01
#define TEST_NOCOLS (1 << 1) // 0x02
#define TEST_NOPRIKEYS (1 << 2) // 0x04
#define NINSERT 9
#define NUPDATE 4
#define NDELETE 3
#define MAX_SIMULATED_CLIENTS 128
#define CUSTOMERS_TABLE "customers test table name with quotes ' and \" "
#define CUSTOMERS_NOCOLS_TABLE "customers nocols"
#define CUSTOMERS_TABLE_COLUMN_LASTNAME "last name with ' and \"\""
typedef struct {
int type;
union {
int64_t ivalue;
double dvalue;
struct {
int plen;
char *pvalue;
};
};
} test_value;
#ifndef CLOUDSYNC_OMIT_PRINT_RESULT
static bool first_time = true;
static const char *query_table = "QUERY_TABLE";
static const char *query_siteid = "QUERY_SITEID";
static const char *query_changes = "QUERY_CHANGES";
#endif
// MARK: -
typedef struct {
int type;
int len;
int rc;
union {
sqlite3_int64 intValue;
double doubleValue;
char *stringValue;
} value;
} DATABASE_RESULT;
DATABASE_RESULT unit_exec (cloudsync_context *data, const char *sql, const char **values, int types[], int lens[], int count, DATABASE_RESULT results[], int expected_types[], int result_count) {
DEBUG_DBFUNCTION("unit_exec %s", sql);
sqlite3_stmt *pstmt = NULL;
bool is_write = (result_count == 0);
int type = 0;
// compile sql
int rc = databasevm_prepare(data, sql, (void **)&pstmt, 0);
if (rc != SQLITE_OK) goto unitexec_finalize;
// check bindings
for (int i=0; i<count; ++i) {
switch (types[i]) {
case SQLITE_NULL:
rc = databasevm_bind_null(pstmt, i+1);
break;
case SQLITE_TEXT:
rc = databasevm_bind_text(pstmt, i+1, values[i], lens[i]);
break;
case SQLITE_BLOB:
rc = databasevm_bind_blob(pstmt, i+1, values[i], lens[i]);
break;
case SQLITE_INTEGER: {
sqlite3_int64 value = strtoll(values[i], NULL, 0);
rc = databasevm_bind_int(pstmt, i+1, value);
} break;
case SQLITE_FLOAT: {
double value = strtod(values[i], NULL);
rc = databasevm_bind_double(pstmt, i+1, value);
} break;
}
if (rc != SQLITE_OK) goto unitexec_finalize;
}
// execute statement
rc = databasevm_step(pstmt);
// check return value based on pre-condition
if (rc != SQLITE_ROW) goto unitexec_finalize;
if (is_write) goto unitexec_finalize;
// process result (if any)
for (int i=0; i<result_count; ++i) {
type = database_column_type(pstmt, i);
results[i].type = type;
if (type == SQLITE_NULL) {
rc = SQLITE_OK;
continue;
}
if (type != expected_types[i]) {
rc = SQLITE_ERROR;
goto unitexec_finalize;
}
// type == expected_type
if (type == SQLITE_INTEGER) results[i].value.intValue = database_column_int(pstmt, i);
else if (type == SQLITE_FLOAT) results[i].value.doubleValue = database_column_double(pstmt, i);
else {
// TEXT or BLOB
int len = database_column_bytes(pstmt, i);
results[i].len = len;
char *buffer = NULL;
if (type == SQLITE_BLOB) {
const void *bvalue = database_column_blob(pstmt, i);
if (bvalue) {
buffer = (char *)cloudsync_memory_alloc(len);
if (!buffer) {rc = SQLITE_NOMEM; goto unitexec_finalize;}
memcpy(buffer, bvalue, len);
}
} else {
const char *value = database_column_text(pstmt, i);
if (value) buffer = cloudsync_string_dup((const char *)value);
}
results[i].value.stringValue = buffer;
}
}
rc = SQLITE_OK;
unitexec_finalize:
if (rc == SQLITE_DONE) rc = SQLITE_OK;
if (rc != SQLITE_OK) {
if (count != -1) DEBUG_ALWAYS("Error executing %s in dbutils_exec (%s).", sql, database_errmsg(data));
}
if (pstmt) databasevm_finalize(pstmt);
if (is_write) {
DATABASE_RESULT result = {0};
result.rc = rc;
return result;
}
results[0].rc = rc;
return results[0];
}
sqlite3_int64 unit_select (cloudsync_context *data, const char *sql, const char **values, int types[], int lens[], int count, int expected_type) {
// used only in unit-test
DATABASE_RESULT results[1] = {0};
int expected_types[1] = {expected_type};
unit_exec(data, sql, values, types, lens, count, results, expected_types, 1);
return results[0].value.intValue;
}
int unit_debug (sqlite3 *db, bool print_result) {
sqlite3_stmt *stmt = NULL;
int counter = 0;
while ((stmt = sqlite3_next_stmt(db, stmt))) {
++counter;
if (print_result) printf("Unfinalized stmt statement: %p (%s)\n", stmt, sqlite3_sql(stmt));
}
return counter;
}
// MARK: -
int64_t random_int64_range (int64_t min, int64_t max) {
// generate a high-quality pseudo-random number in the full 64-bit space
uint64_t random_number = 0;
sqlite3_randomness(sizeof(random_number), &random_number);
// scale result to fit the int64_t range
uint64_t range = (uint64_t)(max - min + 1);
// map the random number to the specified range
return (int64_t)(random_number % range) + min;
}
int64_t random_int64 (void) {
int64_t random_number = 0;
sqlite3_randomness(sizeof(random_number), &random_number);
return random_number;
}
double random_double (void) {
int64_t random_number = random_int64();
return (double)random_number;
}
void random_blob (char buffer[4096], int max_size) {
assert(max_size <= 4096);
sqlite3_randomness(sizeof(max_size), buffer);
}
void random_string (char buffer[4096], int max_size) {
random_blob(buffer, max_size);
for (int i=0; i<max_size; ++i) {
// map the random value to the printable ASCII range (32 to 126)
buffer[i] = (buffer[i] % 95) + 32;
}
}
// MARK: -
void suppress_printf_output (void) {
// check if already suppressed
if (stdout_backup != -1) return;
// open /dev/null for writing
#ifdef _WIN32
dev_null_fd = open("nul", O_WRONLY);
#else
dev_null_fd = open("/dev/null", O_WRONLY);
#endif
if (dev_null_fd == -1) {
perror("Failed to open /dev/null");
return;
}
// backup the current stdout file descriptor
stdout_backup = dup(fileno(stdout));
if (stdout_backup == -1) {
perror("Failed to duplicate stdout");
close(dev_null_fd);
dev_null_fd = -1;
return;
}
// redirect stdout to /dev/null
if (dup2(dev_null_fd, fileno(stdout)) == -1) {
perror("Failed to redirect stdout to /dev/null");
close(dev_null_fd);
dev_null_fd = -1;
close(stdout_backup);
stdout_backup = -1;
return;
}
}
void resume_printf_output (void) {
// check if already resumed
if (stdout_backup == -1) return;
// restore the original stdout
if (dup2(stdout_backup, fileno(stdout)) == -1) {
perror("Failed to restore stdout");
}
// close the backup and /dev/null file descriptors
close(stdout_backup);
stdout_backup = -1;
if (dev_null_fd != -1) {
close(dev_null_fd);
dev_null_fd = -1;
}
}
void generate_pk_test_sql (int nkeys, char buffer[4096]) {
size_t bsize = 4096;
int len = snprintf(buffer, bsize, "SELECT cloudsync_pk_encode(");
for (int i=0; i<nkeys; ++i) {
len += snprintf(buffer+len, bsize-len, "?,");
}
buffer[len-1] = ')';
buffer[len] = 0;
}
char *build_long_tablename (void) {
static char name[4096] = {0};
if (name[0] == 0) {
memset(name, 'A', sizeof(name));
name[sizeof(name)-1] = 0;
}
return name;
}
const char *build_huge_table (void) {
const char *sql = "CREATE TABLE dummy_table ("
"c1, c2, c3, c4, c5, "
"c6, c7, c8, c9, c10, "
"c11, c12, c13, c14, c15, "
"c16, c17, c18, c19, c20, "
"c21, c22, c23, c24, c25, "
"c26, c27, c28, c29, c30, "
"c31, c32, c33, c34, c35, "
"c36, c37, c38, c39, c40, "
"c41, c42, c43, c44, c45, "
"c46, c47, c48, c49, c50, "
"c51, c52, c53, c54, c55, "
"c56, c57, c58, c59, c60, "
"c61, c62, c63, c64, c65, "
"c66, c67, c68, c69, c70, "
"c71, c72, c73, c74, c75, "
"c76, c77, c78, c79, c80, "
"c81, c82, c83, c84, c85, "
"c86, c87, c88, c89, c90, "
"c91, c92, c93, c94, c95, "
"c96, c97, c98, c99, c100, "
"c101, c102, c103, c104, c105, "
"c106, c107, c108, c109, c110, "
"c111, c112, c113, c114, c115, "
"c116, c117, c118, c119, c120, "
"c121, c122, c123, c124, c125, "
"c126, c127, c128, c129, c130, "
"PRIMARY KEY ("
"c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, "
"c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, "
"c21, c22, c23, c24, c25, c26, c27, c28, c29, c30, "
"c31, c32, c33, c34, c35, c36, c37, c38, c39, c40, "
"c41, c42, c43, c44, c45, c46, c47, c48, c49, c50, "
"c51, c52, c53, c54, c55, c56, c57, c58, c59, c60, "
"c61, c62, c63, c64, c65, c66, c67, c68, c69, c70, "
"c71, c72, c73, c74, c75, c76, c77, c78, c79, c80, "
"c81, c82, c83, c84, c85, c86, c87, c88, c89, c90, "
"c91, c92, c93, c94, c95, c96, c97, c98, c99, c100, "
"c101, c102, c103, c104, c105, c106, c107, c108, c109, c110, "
"c111, c112, c113, c114, c115, c116, c117, c118, c119, c120, "
"c121, c122, c123, c124, c125, c126, c127, c128, c129, c130"
"));";
return sql;
}
int close_db (sqlite3 *db) {
int counter = 0;
if (db) {
sqlite3_exec(db, "SELECT cloudsync_terminate();", NULL, NULL, NULL);
counter = unit_debug(db, true);
sqlite3_close(db);
}
return counter;
}
bool file_delete_internal (const char *path) {
#ifdef _WIN32
if (DeleteFile(path) == 0) return false;
#else
if (unlink(path) != 0) return false;
#endif
return true;
}
// MARK: -
#ifndef UNITTEST_OMIT_RLS_VALIDATION
typedef struct {
bool in_savepoint;
bool is_approved;
bool last_is_delete;
char *last_tbl;
void *last_pk;
int64_t last_pk_len;
int64_t last_db_version;
} unittest_payload_apply_rls_status;
bool unittest_validate_changed_row(sqlite3 *db, cloudsync_context *data, char *tbl_name, void *pk, int64_t pklen) {
// verify row
bool ret = false;
bool vm_persistent;
sqlite3_stmt *vm = cloudsync_colvalue_stmt(data, tbl_name, &vm_persistent);
if (!vm) goto cleanup;
// bind primary key values (the return code is the pk count)
int rc = pk_decode_prikey((char *)pk, (size_t)pklen, pk_decode_bind_callback, (void *)vm);
if (rc < 0) goto cleanup;
// execute vm
rc = sqlite3_step(vm);
if (rc == SQLITE_DONE) {
rc = SQLITE_OK;
} else if (rc == SQLITE_ROW) {
rc = SQLITE_OK;
ret = true;
}
cleanup:
if (vm_persistent) sqlite3_reset(vm);
else sqlite3_finalize(vm);
return ret;
}
int unittest_payload_apply_reset_transaction(sqlite3 *db, unittest_payload_apply_rls_status *s, bool create_new) {
int rc = SQLITE_OK;
if (s->in_savepoint == true) {
if (s->is_approved) rc = sqlite3_exec(db, "RELEASE unittest_payload_apply_transaction", NULL, NULL, NULL);
else rc = sqlite3_exec(db, "ROLLBACK TO unittest_payload_apply_transaction; RELEASE unittest_payload_apply_transaction", NULL, NULL, NULL);
if (rc == SQLITE_OK) s->in_savepoint = false;
}
if (create_new) {
rc = sqlite3_exec(db, "SAVEPOINT unittest_payload_apply_transaction", NULL, NULL, NULL);
if (rc == SQLITE_OK) s->in_savepoint = true;
}
return rc;
}
bool unittest_payload_apply_rls_callback(void **xdata, cloudsync_pk_decode_bind_context *d, void *_db, void *_data, int step, int rc) {
sqlite3 *db = (sqlite3 *)_db;
cloudsync_context *data = (cloudsync_context *)_data;
bool is_approved = false;
unittest_payload_apply_rls_status *s;
if (*xdata) {
s = (unittest_payload_apply_rls_status *)*xdata;
} else {
s = cloudsync_memory_zeroalloc(sizeof(unittest_payload_apply_rls_status));
s->is_approved = true;
*xdata = s;
}
// extract context info
int64_t colname_len = 0;
char *colname = cloudsync_pk_context_colname(d, &colname_len);
int64_t tbl_len = 0;
char *tbl = cloudsync_pk_context_tbl(d, &tbl_len);
int64_t pk_len = 0;
void *pk = cloudsync_pk_context_pk(d, &pk_len);
int64_t cl = cloudsync_pk_context_cl(d);
int64_t db_version = cloudsync_pk_context_dbversion(d);
switch (step) {
case CLOUDSYNC_PAYLOAD_APPLY_WILL_APPLY: {
// if the tbl name or the prikey has changed, then verify if the row is valid
// must use strncmp because strings in xdata are not zero-terminated
bool tbl_changed = (s->last_tbl && (strlen(s->last_tbl) != (size_t)tbl_len || strncmp(s->last_tbl, tbl, (size_t)tbl_len) != 0));
bool pk_changed = (s->last_pk && pk && cloudsync_blob_compare(s->last_pk, s->last_pk_len, pk, pk_len) != 0);
if (s->is_approved
&& !s->last_is_delete
&& (tbl_changed || pk_changed)) {
s->is_approved = unittest_validate_changed_row(db, data, s->last_tbl, s->last_pk, s->last_pk_len);
}
s->last_is_delete = ((size_t)colname_len == strlen(CLOUDSYNC_TOMBSTONE_VALUE) &&
strncmp(colname, CLOUDSYNC_TOMBSTONE_VALUE, (size_t)colname_len) == 0
) && cl % 2 == 0;
// update the last_tbl value, if needed
if (!s->last_tbl ||
!tbl ||
(strlen(s->last_tbl) != (size_t)tbl_len) ||
strncmp(s->last_tbl, tbl, (size_t)tbl_len) != 0) {
if (s->last_tbl) cloudsync_memory_free(s->last_tbl);
if (tbl && tbl_len > 0) s->last_tbl = cloudsync_string_ndup(tbl, tbl_len);
else s->last_tbl = NULL;
}
// update the last_prikey and len values, if needed
if (!s->last_pk || !pk || cloudsync_blob_compare(s->last_pk, s->last_pk_len, pk, pk_len) != 0) {
if (s->last_pk) cloudsync_memory_free(s->last_pk);
if (pk && pk_len > 0) {
s->last_pk = cloudsync_memory_alloc(pk_len);
memcpy(s->last_pk, pk, pk_len);
s->last_pk_len = pk_len;
} else {
s->last_pk = NULL;
s->last_pk_len = 0;
}
}
// commit the previous transaction, if any
// begin new transacion, if needed
if (s->last_db_version != db_version) {
rc = unittest_payload_apply_reset_transaction(db, s, true);
if (rc != SQLITE_OK) printf("unittest_payload_apply error in reset_transaction: (%d) %s\n", rc, sqlite3_errmsg(db));
// reset local variables
s->last_db_version = db_version;
s->is_approved = true;
}
is_approved = s->is_approved;
break;
}
case CLOUDSYNC_PAYLOAD_APPLY_DID_APPLY:
is_approved = s->is_approved;
break;
case CLOUDSYNC_PAYLOAD_APPLY_CLEANUP:
if (s->is_approved && !s->last_is_delete) s->is_approved = unittest_validate_changed_row(db, data, s->last_tbl, s->last_pk, s->last_pk_len);
rc = unittest_payload_apply_reset_transaction(db, s, false);
if (s->last_tbl) cloudsync_memory_free(s->last_tbl);
if (s->last_pk) {
cloudsync_memory_free(s->last_pk);
s->last_pk_len = 0;
}
is_approved = s->is_approved;
cloudsync_memory_free(s);
*xdata = NULL;
break;
}
return is_approved;
}
#endif
// MARK: -
#ifndef CLOUDSYNC_OMIT_PRINT_RESULT
int do_query_cb (void *type, int argc, char **argv, char **azColName) {
int query_type = 0;
if (type == query_table) query_type = 1;
else if (type == query_changes) query_type = 2;
else if (type == query_siteid) query_type = 1;
if (first_time) {
for (int i = 0; i < argc; i++) {
if (query_type == 1 && i == 0) {
printf("%-40s", azColName[i]);
continue;
}
if (query_type == 2 && (i == 1)) {
printf("%-50s", azColName[i]);
continue;
}
if (query_type == 2 && (i == 6)) {
printf("%-40s", azColName[i]);
continue;
}
printf("%-12s", azColName[i]);
}
printf("\n");
first_time = false;
}
for (int i = 0; i < argc; i++) {
if (query_type == 1 && i == 0) {
printf("%-40s", argv[i]);
continue;
}
if (query_type == 2 && (i == 1)) {
printf("%-50s", argv[i]);
continue;
}
if (query_type == 2 && (i == 6)) {
printf("%-40s", argv[i]);
continue;
}
printf("%-12s", argv[i]);
}
printf("\n");
return SQLITE_OK;
}
void do_query (sqlite3 *db, const char *sql, const char *type) {
first_time = true;
int rc = sqlite3_exec(db, sql, do_query_cb, (void *)type, NULL);
if (rc != SQLITE_OK) {
printf("Error in %s: %s\n", sql, sqlite3_errmsg(db));
}
}
#else
#define do_query(db, sql, type)
#endif
// MARK: -
void do_insert (sqlite3 *db, int table_mask, int ninsert, bool print_result) {
if (table_mask & TEST_PRIKEYS) {
const char *table_name = CUSTOMERS_TABLE;
if (print_result) printf("TESTING INSERT on %s\n", table_name);
for (int i=0; i<ninsert; ++i) {
char *sql = sqlite3_mprintf("INSERT INTO \"%w\" (first_name, \"" CUSTOMERS_TABLE_COLUMN_LASTNAME "\", age, note, stamp) VALUES ('name%d', 'surname%d', %d, 'note%d', 'stamp%d');", table_name, i+1, i+1, i+1, i+1, i+1);
if (!sql) exit (-3);
int rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
sqlite3_free(sql);
if (rc != SQLITE_OK) {
printf("error in do_insert with sql %s (%s)\n", sql, sqlite3_errmsg(db));
exit(-4);
}
}
}
if (table_mask & TEST_NOCOLS) {
const char *table_name = CUSTOMERS_NOCOLS_TABLE;
if (print_result) printf("TESTING INSERT on %s\n", table_name);
for (int i=0; i<ninsert; ++i) {
char *sql = sqlite3_mprintf("INSERT INTO \"%w\" (first_name, \"" CUSTOMERS_TABLE_COLUMN_LASTNAME "\") VALUES ('name%d', 'surname%d');", table_name, (i+1)+100000, (i+1)+100000);
if (!sql) exit (-3);
int rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
sqlite3_free(sql);
if (rc != SQLITE_OK) {
printf("error in do_insert (TEST_NOCOLS) with sql %s (%s)\n", sql, sqlite3_errmsg(db));
exit(-4);
}
}
}
if (table_mask & TEST_NOPRIKEYS) {
const char *table_name = "customers_noprikey";
if (print_result) printf("TESTING INSERT on %s\n", table_name);
for (int i=0; i<ninsert; ++i) {
char *sql = sqlite3_mprintf("INSERT INTO \"%w\" (first_name, \"" CUSTOMERS_TABLE_COLUMN_LASTNAME "\") VALUES ('name%d', 'surname%d');", table_name, (i+1)+200000, (i+1)+200000);
if (!sql) exit (-3);
int rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
sqlite3_free(sql);
if (rc != SQLITE_OK) {
printf("error in do_insert with sql %s (%s)\n", sql, sqlite3_errmsg(db));
exit(-4);
}
}
}
}
void do_insert_val (sqlite3 *db, int table_mask, int val, bool print_result) {
if (table_mask & TEST_PRIKEYS) {
const char *table_name = CUSTOMERS_TABLE;
if (print_result) printf("TESTING INSERT on %s\n", table_name);
char *sql = sqlite3_mprintf("INSERT INTO \"%w\" (first_name, \"" CUSTOMERS_TABLE_COLUMN_LASTNAME "\", age, note, stamp) VALUES ('name%d', 'surname%d', %d, 'note%d', 'stamp%d');", table_name, val, val, val, val, val);
if (!sql) exit (-3);
int rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
sqlite3_free(sql);
if (rc != SQLITE_OK) {
printf("error in do_insert with sql %s (%s)\n", sql, sqlite3_errmsg(db));
exit(-4);
}
}
if (table_mask & TEST_NOCOLS) {
const char *table_name = CUSTOMERS_NOCOLS_TABLE;
if (print_result) printf("TESTING INSERT on %s\n", table_name);
char *sql = sqlite3_mprintf("INSERT INTO \"%w\" (first_name, \"" CUSTOMERS_TABLE_COLUMN_LASTNAME "\") VALUES ('name%d', 'surname%d');", table_name, (val)+100000, (val)+100000);
if (!sql) exit (-3);
int rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
sqlite3_free(sql);
if (rc != SQLITE_OK) {
printf("error in do_insert (TEST_NOCOLS) with sql %s (%s)\n", sql, sqlite3_errmsg(db));
exit(-4);
}
}
if (table_mask & TEST_NOPRIKEYS) {
const char *table_name = "customers_noprikey";
if (print_result) printf("TESTING INSERT on %s\n", table_name);
char *sql = sqlite3_mprintf("INSERT INTO %w (first_name, \"" CUSTOMERS_TABLE_COLUMN_LASTNAME "\") VALUES ('name%d', 'surname%d');", table_name, (val)+200000, (val)+200000);
if (!sql) exit (-3);
int rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
sqlite3_free(sql);
if (rc != SQLITE_OK) {
printf("error in do_insert with sql %s (%s)\n", sql, sqlite3_errmsg(db));
exit(-4);
}
}
}
void do_update (sqlite3 *db, int table_mask, bool print_result) {
int rc = SQLITE_OK;
if (table_mask & TEST_PRIKEYS) {
const char *table_name = CUSTOMERS_TABLE;
if (print_result) printf("TESTING UPDATE on %s\n", table_name);
char sql[512];
sqlite3_snprintf(sizeof(sql), sql, "UPDATE \"%w\" SET age = 40 WHERE first_name='name1';", table_name);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
sqlite3_snprintf(sizeof(sql), sql, "UPDATE \"%w\" SET age = 2000, note='hello2000' WHERE first_name='name2';", table_name);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
sqlite3_snprintf(sizeof(sql), sql, "UPDATE \"%w\" SET age = 6000, note='hello6000' WHERE first_name='name6';", table_name);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
// update primary key here
sqlite3_snprintf(sizeof(sql), sql, "UPDATE \"%w\" SET first_name = 'name1updated' WHERE first_name='name1';", table_name);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
sqlite3_snprintf(sizeof(sql), sql, "UPDATE \"%w\" SET first_name = 'name8updated' WHERE first_name='name8';", table_name);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
// update two primary keys here
sqlite3_snprintf(sizeof(sql), sql, "UPDATE \"%w\" SET first_name = 'name9updated', \"" CUSTOMERS_TABLE_COLUMN_LASTNAME "\" = 'surname9updated' WHERE first_name='name9';", table_name);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
}
if (table_mask & TEST_NOCOLS) {
const char *table_name = CUSTOMERS_NOCOLS_TABLE;
if (print_result) printf("TESTING UPDATE on %s\n", table_name);
// update primary key here
rc = sqlite3_exec(db, "UPDATE \"" CUSTOMERS_NOCOLS_TABLE "\" SET first_name = 'name100001updated' WHERE first_name='name100001';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
rc = sqlite3_exec(db, "UPDATE \"" CUSTOMERS_NOCOLS_TABLE "\" SET first_name = 'name100008updated' WHERE first_name='name100008';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
// update two primary keys here
rc = sqlite3_exec(db, "UPDATE \"" CUSTOMERS_NOCOLS_TABLE "\" SET first_name = 'name100009updated', \"" CUSTOMERS_TABLE_COLUMN_LASTNAME "\" = 'surname100009updated' WHERE first_name='name100009';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
}
if (table_mask & TEST_NOPRIKEYS) {
const char *table_name = "customers_noprikey";
if (print_result) printf("TESTING UPDATE on %s\n", table_name);
// update primary key here
rc = sqlite3_exec(db, "UPDATE customers_noprikey SET first_name = 'name200001updated' WHERE first_name='name200001';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
rc = sqlite3_exec(db, "UPDATE customers_noprikey SET first_name = 'name200008updated' WHERE first_name='name200008';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
// update two columns
rc = sqlite3_exec(db, "UPDATE customers_noprikey SET first_name = 'name200009updated', \"" CUSTOMERS_TABLE_COLUMN_LASTNAME "\" = 'surname200009updated' WHERE first_name='name200009';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
}
finalize:
if (rc != SQLITE_OK) {
printf("error in do_update: %s\n", sqlite3_errmsg(db));
exit(-4);
}
}
void do_update_random (sqlite3 *db, int table_mask, bool print_result) {
int rc = SQLITE_OK;
if (table_mask & TEST_PRIKEYS) {
const char *table_name = CUSTOMERS_TABLE;
if (print_result) printf("TESTING RANDOM UPDATE on %s\n", table_name);
char sql[512];
sqlite3_snprintf(sizeof(sql), sql, "UPDATE \"%w\" SET age = ABS(RANDOM()) WHERE rowid=(ABS(RANDOM() %% 10)+1);", table_name);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
sqlite3_snprintf(sizeof(sql), sql, "UPDATE \"%w\" SET age = ABS(RANDOM()), note='hello' || HEX(RANDOMBLOB(2)), stamp='stamp' || ABS(RANDOM() %% 99) WHERE rowid=(ABS(RANDOM() %% 10)+1);", table_name);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
sqlite3_snprintf(sizeof(sql), sql, "UPDATE \"%w\" SET age = ABS(RANDOM()), note='hello' || HEX(RANDOMBLOB(2)), stamp='stamp' || ABS(RANDOM() %% 99) WHERE rowid=(ABS(RANDOM() %% 10)+1);", table_name);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
// update primary key here
sqlite3_snprintf(sizeof(sql), sql, "UPDATE \"%w\" SET first_name = 'name' || HEX(RANDOMBLOB(4)) WHERE rowid=(ABS(RANDOM() %% 10)+1);", table_name);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
sqlite3_snprintf(sizeof(sql), sql, "UPDATE \"%w\" SET first_name = 'name' || HEX(RANDOMBLOB(4)) WHERE rowid=(ABS(RANDOM() %% 10)+1);", table_name);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
// update two primary keys here
sqlite3_snprintf(sizeof(sql), sql, "UPDATE \"%w\" SET first_name = 'name' || HEX(RANDOMBLOB(4)), \"" CUSTOMERS_TABLE_COLUMN_LASTNAME "\" = 'surname' || HEX(RANDOMBLOB(4)) WHERE rowid=(ABS(RANDOM() %% 10)+1);", table_name);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
}
if (table_mask & TEST_NOCOLS) {
const char *table_name = CUSTOMERS_NOCOLS_TABLE;
if (print_result) printf("TESTING RANDMOM UPDATE on %s\n", table_name);
// update primary key here
rc = sqlite3_exec(db, "UPDATE \"" CUSTOMERS_NOCOLS_TABLE "\" SET first_name = HEX(RANDOMBLOB(8)) WHERE first_name='name100001';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
rc = sqlite3_exec(db, "UPDATE \"" CUSTOMERS_NOCOLS_TABLE "\" SET first_name = HEX(RANDOMBLOB(8)) WHERE first_name='name100008';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
// update two primary keys here
rc = sqlite3_exec(db, "UPDATE \"" CUSTOMERS_NOCOLS_TABLE "\" SET first_name = HEX(RANDOMBLOB(8)), \"" CUSTOMERS_TABLE_COLUMN_LASTNAME "\" = HEX(RANDOMBLOB(8)) WHERE first_name='name100009';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
}
if (table_mask & TEST_NOPRIKEYS) {
const char *table_name = "customers_noprikey";
if (print_result) printf("TESTING RANDOM UPDATE on %s\n", table_name);
// update primary key here
rc = sqlite3_exec(db, "UPDATE customers_noprikey SET first_name = HEX(RANDOMBLOB(8)) WHERE first_name='name200001';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
rc = sqlite3_exec(db, "UPDATE customers_noprikey SET first_name = HEX(RANDOMBLOB(8)) WHERE first_name='name200008';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
// update two columns
rc = sqlite3_exec(db, "UPDATE customers_noprikey SET first_name = HEX(RANDOMBLOB(8)), \"" CUSTOMERS_TABLE_COLUMN_LASTNAME "\" = HEX(RANDOMBLOB(8)) WHERE first_name='name200009';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
}
finalize:
if (rc != SQLITE_OK) {
printf("error in do_update_random: %s\n", sqlite3_errmsg(db));
exit(-4);
}
}
void do_delete (sqlite3 *db, int table_mask, bool print_result) {
int rc = SQLITE_OK;
if (table_mask & TEST_PRIKEYS) {
const char *table_name = CUSTOMERS_TABLE;
if (print_result) printf("TESTING DELETE on %s\n", table_name);
char *sql = sqlite3_mprintf("DELETE FROM \"%w\" WHERE first_name='name5';", table_name);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
sqlite3_free(sql);
if (rc != SQLITE_OK) goto finalize;
sql = sqlite3_mprintf("DELETE FROM \"%w\" WHERE first_name='name7';", table_name);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
sqlite3_free(sql);
if (rc != SQLITE_OK) goto finalize;
}
if (table_mask & TEST_NOCOLS) {
const char *table_name = CUSTOMERS_NOCOLS_TABLE;
if (print_result) printf("TESTING DELETE on %s\n", table_name);
rc = sqlite3_exec(db, "DELETE FROM \"" CUSTOMERS_NOCOLS_TABLE "\" WHERE first_name='name100005';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
rc = sqlite3_exec(db, "DELETE FROM \"" CUSTOMERS_NOCOLS_TABLE "\" WHERE first_name='name100007';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
}
if (table_mask & TEST_NOPRIKEYS) {
const char *table_name = "customers_noprikey";
if (print_result) printf("TESTING DELETE on %s\n", table_name);
rc = sqlite3_exec(db, "DELETE FROM customers_noprikey WHERE first_name='name200005';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
rc = sqlite3_exec(db, "DELETE FROM customers_noprikey WHERE first_name='name200007';", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
}
return;
finalize:
if (rc != SQLITE_OK) {
printf("error in do_delete: %s\n", sqlite3_errmsg(db));
exit(-4);
}
}
bool do_test_vtab2 (void) {
bool result = false;
sqlite3_stmt *stmt = NULL;
int rc = SQLITE_ERROR;
// test in an in-memory database
sqlite3 *db = do_create_database();
if (!db) goto finalize;
// create dummy table
rc = sqlite3_exec(db, "CREATE TABLE foo (name TEXT PRIMARY KEY NOT NULL, age INTEGER);", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
// augment table
rc = sqlite3_exec(db, "SELECT cloudsync_init('foo');", NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
// insert 10 rows
for (int i=0; i<10; ++i) {
char sql[512];
snprintf(sql, sizeof(sql), "INSERT INTO foo (name, age) VALUES ('name%d', %d);", i+1, i+1);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (rc != SQLITE_OK) goto finalize;
}
// at this point cloudsync_changes contains 10 rows
// trigger cloudsync_changesvtab_close with vm not null
const char *sql = "SELECT tbl, quote(pk), col_name, col_value, col_version, db_version, quote(site_id), cl, seq FROM cloudsync_changes;";
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) goto finalize;
// step and finalize BEFORE eof
for (int i=0; i<5; ++i) {
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW) goto finalize;
}
rc = sqlite3_finalize(stmt);
if (rc != SQLITE_OK) goto finalize;
stmt = NULL;
// end trigger
// trigger error inside cloudsync_changesvtab_filter
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) goto finalize;
force_vtab_filter_abort = true;
sqlite3_step(stmt);
force_vtab_filter_abort = false;
sqlite3_finalize(stmt);
rc = SQLITE_OK;
stmt = NULL;
// end trigger
// trigger error inside cloudsync_changesvtab_next
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) goto finalize;
// step and finalize BEFORE eof
for (int i=0; i<10; ++i) {
rc = sqlite3_step(stmt);
if (rc == SQLITE_INTERRUPT) break;
if (rc != SQLITE_ROW) goto finalize;
if (i == 5) sqlite3_interrupt(db);
}
sqlite3_finalize(stmt);
rc = SQLITE_OK;
stmt = NULL;
// end trigger