/
store.c
923 lines (848 loc) · 23.3 KB
/
store.c
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
typedef struct store_context_t store_context_t;
/*
Factored-out differences between the UTF-8 and UTF-16 modes of operation.
*/
typedef struct store_vt {
int (*prepare)(
context_t *context,
void const *text,
size_t size,
sqlite3_stmt **stmt);
int (*column_text)(
context_t *context,
sqlite3_stmt *stmt,
int colix,
conststr_t *text);
int (*str_app_7)(
str_t *str,
void const *text,
size_t size);
int (*str_app_id)(
str_t *str,
void const *text,
size_t size);
int (*write_text)(
store_context_t *context,
void const *text,
size_t size);
conststr_t pragmas_id;
conststr_t schema_id;
} store_vt;
/*
Extend the common context with store-specific parts.
*/
struct store_context_t {
context_t c;
FILE *outfile;
store_vt const *vt;
unsigned char have_pragmas;
unsigned char have_schema;
};
static int wd(
store_context_t *context,
void const *data,
size_t size)
{
if (size>0 && !fwrite(data,size,1,context->outfile)) {
errf(
&context->c,SQLITE_IOERR_WRITE,
"Write error: %s",strerror(errno));
return -1;
}
return 0;
}
static int wc(
store_context_t *context,
int c)
{
if (putc(c,context->outfile)==EOF) {
errf(
&context->c,SQLITE_IOERR_WRITE,
"Write error: %s",strerror(errno));
return -1;
}
return 0;
}
static int write_text16(
store_context_t *context,
void const *text,
size_t size)
{
if (context->c.db_enc==context->c.native_enc) {
return wd(context,text,size);
} else {
FILE *outfile=context->outfile;
unsigned char const *src=text;
while (size>=2) {
unsigned int c0,c1;
c0=src[0];
c1=src[1];
if (putc(c1,outfile)==EOF || putc(c0,outfile)==EOF) {
errf(
&context->c,SQLITE_IOERR_WRITE,
"Write error: %s",strerror(errno));
return -1;
}
src+=2;
size-=2;
}
}
return 0;
}
static store_vt store_vt8 =
{
prepare8,
column_text8,
str8app_7,
str8app_id,
wd,
CONSTSTR(s3bd_id8_pragmas),
CONSTSTR(s3bd_id8_schema)
};
static store_vt store_vt16 =
{
prepare16,
column_text16,
str16app_7,
str16app_id,
write_text16,
CONSTSTR(s3bd_id16_pragmas),
CONSTSTR(s3bd_id16_schema)
};
static unsigned int encode_uint(
unsigned char *buf,
sqlite3_uint64 u)
{
unsigned int width,ix;
for (width=0; width<8 && u>=s3bd_uint_bias[width+1]; width++)
;
u-=s3bd_uint_bias[width];
for (ix=1; ix<=width; ix++) {
buf[width-ix]=u;
u>>=8;
}
return width;
}
static unsigned int encode_sint(
unsigned char *buf,
sqlite3_int64 i)
{
sqlite3_uint64 u;
unsigned int flip;
unsigned int width,ix;
if (i<0) {
u=-i;
flip=0xFF;
} else {
u=i;
flip=0x00;
}
for (width=0; width<8 && u>=s3bd_sint_bias[width+1]; width++)
;
u-=s3bd_sint_bias[width];
for (ix=1; ix<=width; ix++) {
buf[width-ix]=u^flip;
u>>=8;
}
return width;
}
static int store_intcol(
store_context_t *context,
sqlite3_int64 i)
{
unsigned char buf[9];
unsigned int width;
width=encode_sint(buf+1,i);
buf[0]=INTCOL(width);
return wd(context,buf,1+width);
}
static unsigned int encode_float(
unsigned char *buf,
double f,
int endian)
{
union {
double f;
unsigned char c[sizeof (double)];
} convert;
int ix,step,end;
unsigned int width;
convert.f=f;
if (endian==2) {
ix=0;
step=1;
end=8;
} else {
ix=7;
step=-1;
end=-1;
}
while (end!=ix && !convert.c[end-step])
end-=step;
width=0;
while (ix!=end) {
buf[width]=convert.c[ix];
ix+=step;
width++;
}
return width;
}
static int store_floatcol(
store_context_t *context,
double f)
{
unsigned char buf[9];
unsigned int width;
width=encode_float(buf+1,f,context->c.double_end);
buf[0]=FLOATCOL(width);
return wd(context,buf,1+width);
}
static int store_textcol(
store_context_t *context,
void const *text,
size_t size)
{
unsigned char buf[9];
unsigned int width;
width=encode_uint(buf+1,size);
buf[0]=TEXTCOL(width);
if (wd(context,buf,1+width))
return -1;
if ((*context->vt->write_text)(context,text,size))
return -1;
return 0;
}
static int store_blobcol(
store_context_t *context,
void const *data,
size_t size)
{
unsigned char buf[9];
unsigned int width;
width=encode_uint(buf+1,size);
buf[0]=BLOBCOL(width);
if (wd(context,buf,1+width))
return -1;
if (wd(context,data,size))
return -1;
return 0;
}
static int store_rowset(
store_context_t *context,
conststr_t ident,
sqlite3_stmt *stmt)
{
store_vt const *vt=context->vt;
unsigned char buf[17];
unsigned int namewidth,colswidth;
int status;
int colcnt,colix;
colcnt=sqlite3_column_count(stmt);
if (!colcnt)
return 0;
colswidth=encode_uint(buf+1,colcnt-1);
namewidth=encode_uint(buf+1+colswidth,ident.size);
buf[0]=ROWSET(colswidth,namewidth);
if (wd(context,buf,1+colswidth+namewidth))
return -1;
if ((*vt->write_text)(context,ident.text,ident.size))
return -1;
for (;;) {
status=sqlite3_step(stmt);
if (status!=SQLITE_ROW)
break;
if (colcnt!=sqlite3_data_count(stmt)) {
errf(
&context->c,SQLITE_ERROR,
"While extracting rows: Column count mismatch");
return -1;
}
for (colix=0; colix<colcnt; colix++) {
int type;
conststr_t text;
void const *blob;
size_t size;
type=sqlite3_column_type(stmt,colix);
switch (type) {
case SQLITE_NULL:
if (wc(context,NULLCOL()))
return -1;
break;
case SQLITE_INTEGER:
if (store_intcol(context,sqlite3_column_int64(stmt,colix)))
return -1;
break;
case SQLITE_FLOAT:
if (store_floatcol(context,sqlite3_column_double(stmt,colix)))
return -1;
break;
case SQLITE_TEXT:
if ((*vt->column_text)(&context->c,stmt,colix,&text))
return -1;
if (store_textcol(context,text.text,text.size))
return -1;
break;
case SQLITE_BLOB:
size=sqlite3_column_bytes(stmt,colix);
if (size>0) {
blob=sqlite3_column_blob(stmt,colix);
if (!blob) {
context->c.status=SQLITE_NOMEM;
return -1;
}
} else {
blob=NULL;
}
if (store_blobcol(context,blob,size))
return -1;
break;
default:
errf(
&context->c,SQLITE_ERROR,
"While extracting rows: Unknown column type %d",type);
return -1;
}
}
}
if (status!=SQLITE_DONE) {
errf(
&context->c,status,
"While extracting rows: sqlite3_step: %s",
sqlite3_errmsg(context->c.connection));
return -1;
}
return wc(context,ENDSET());
}
static char const getenc_sql[] =
"pragma encoding";
static int store_header(
store_context_t *context)
{
int status;
sqlite3_stmt *get=NULL;
int encoding=-1;
s3bd_header_t header;
status=sqlite3_prepare_v2(
context->c.connection,
getenc_sql,sizeof getenc_sql,
&get,
NULL);
if (status!=SQLITE_OK) {
errf(
&context->c,status,
"While getting database encoding: sqlite3_prepare: %s",
sqlite3_errmsg(context->c.connection));
goto cleanup;
}
for (;;) {
conststr_t encname;
int encix;
status=sqlite3_step(get);
if (status!=SQLITE_ROW)
break;
if (column_text8(&context->c,get,0,&encname))
goto cleanup;
for (encix=1; encix<=3; encix++) {
if (conststr_eq(encname,encoding_names[encix])) {
encoding=encix;
break;
}
}
}
if (status!=SQLITE_DONE) {
errf(
&context->c,status,
"While getting database encoding: sqlite3_step: %s",
sqlite3_errmsg(context->c.connection));
goto cleanup;
}
sqlite3_finalize(get);
get=NULL;
if (encoding<1) {
errf(
&context->c,SQLITE_ERROR,
"Failed to determine database text encoding");
goto cleanup;
}
memcpy(header.magic,s3bd_header_magic,sizeof header.magic);
header.ver_major=CURVER_MAJOR;
header.ver_minor=CURVER_MINOR;
header.encoding=encoding;
if (wd(context,&header,sizeof header))
goto cleanup;
context->c.db_enc=encoding;
if (encoding>SQLITE_UTF8) {
context->vt=&store_vt16;
context->c.native_enc=endian_short();
if (!context->c.native_enc) {
errf(
&context->c,SQLITE_ERROR,
"Failed to determine short integer endianness");
goto cleanup;
}
} else {
context->vt=&store_vt8;
context->c.native_enc=encoding;
}
context->c.double_end=endian_double();
if (!context->c.double_end) {
errf(
&context->c,SQLITE_ERROR,
"Unsupported floating-point format");
goto cleanup;
}
return 0;
cleanup:
if (get)
sqlite3_finalize(get);
return -1;
}
typedef struct pragma_def_t {
int phase;
conststr_t name;
} pragma_def_t;
static pragma_def_t const pragma_defs[] =
{
{PRAGMA_PHASE_PRE_TRANSACTION, CONSTSTR0("page_size")},
{PRAGMA_PHASE_PRE_TRANSACTION, CONSTSTR0("auto_vacuum")},
{PRAGMA_PHASE_IN_TRANSACTION, CONSTSTR0("application_id")},
{PRAGMA_PHASE_IN_TRANSACTION, CONSTSTR0("user_version")},
{PRAGMA_PHASE_POST_TRANSACTION, CONSTSTR0("journal_mode")},
{0, {NULL, 0}}
};
static char const pragmas_select_sql[] =
"select * from temp.pragmas";
static char const pragma_sql_1[] =
"pragma ";
static int extract_pragmas(
store_context_t *context)
{
int status;
char *errmsg=NULL;
sqlite3_stmt *remember=NULL;
sqlite3_stmt *extract=NULL;
str_t sql;
pragma_def_t const *def;
str_init(&sql,&context->c);
status=sqlite3_exec(
context->c.connection,pragmas_create_sql,0,NULL,&errmsg);
if (status!=SQLITE_OK) {
errf(
&context->c,status,
"Failed to create temporary pragmas table: %s",errmsg);
goto cleanup;
}
context->have_pragmas=1;
status=sqlite3_prepare_v2(
context->c.connection,
pragmas_insert_sql,sizeof pragmas_insert_sql,
&remember,
NULL);
if (status!=SQLITE_OK) {
errf(
&context->c,status,
"While getting pragma values: sqlite3_prepare: %s",
sqlite3_errmsg(context->c.connection));
goto cleanup;
}
for (def=pragma_defs; def->name.text; def++) {
status=sqlite3_bind_int(remember,1,def->phase);
if (status!=SQLITE_OK) {
errf(
&context->c,status,
"While getting pragma %s value: sqlite3_bind: %s",
def->name.text,sqlite3_errmsg(context->c.connection));
goto cleanup;
}
status=sqlite3_bind_text(
remember,
2,
def->name.text,def->name.size,
SQLITE_STATIC);
if (status!=SQLITE_OK) {
errf(
&context->c,status,
"While getting pragma %s value: sqlite3_bind: %s",
def->name.size,sqlite3_errmsg(context->c.connection));
goto cleanup;
}
if (str8app_7(&sql,pragma_sql_1,sizeof pragma_sql_1-1))
goto cleanup;
if (str8app_id(&sql,def->name.text,def->name.size))
goto cleanup;
status=sqlite3_prepare_v2(
context->c.connection,
sql.text,sql.size,
&extract,
NULL);
if (status!=SQLITE_OK) {
errf(
&context->c,status,
"While getting pragma %s value: sqlite3_prepare: %s",
def->name.text,sqlite3_errmsg(context->c.connection));
goto cleanup;
}
str_free(&sql);
for (;;) {
sqlite3_value *value;
status=sqlite3_step(extract);
if (status!=SQLITE_ROW)
break;
value=sqlite3_column_value(extract,0);
if (!value) {
context->c.status=SQLITE_NOMEM;
goto cleanup;
}
status=sqlite3_bind_value(remember,3,value);
if (status!=SQLITE_OK) {
errf(
&context->c,status,
"While getting pragma %s value: sqlite3_bind: %s",
def->name.text,sqlite3_errmsg(context->c.connection));
goto cleanup;
}
status=sqlite3_step(remember);
if (status!=SQLITE_DONE) {
errf(
&context->c,status,
"While getting pragma %s value: sqlite3_step: %s",
def->name.text,sqlite3_errmsg(context->c.connection));
goto cleanup;
}
sqlite3_reset(remember);
sqlite3_bind_null(remember,2);
}
if (status!=SQLITE_DONE) {
errf(
&context->c,status,
"While getting pragma %s value: sqlite3_step: %s",
def->name.text,sqlite3_errmsg(context->c.connection));
goto cleanup;
}
sqlite3_finalize(extract);
extract=NULL;
}
sqlite3_finalize(remember);
remember=NULL;
return 0;
cleanup:
if (errmsg)
sqlite3_free(errmsg);
str_free(&sql);
if (remember)
sqlite3_finalize(remember);
if (extract)
sqlite3_finalize(extract);
return -1;
}
static int store_pragmas(
store_context_t *context)
{
int status;
sqlite3_stmt *get_rows;
status=sqlite3_prepare_v2(
context->c.connection,
pragmas_select_sql,sizeof pragmas_select_sql,
&get_rows,
NULL);
if (status!=SQLITE_OK) {
errf(
&context->c,status,
"While getting pragma values: sqlite3_prepare: %s",
sqlite3_errmsg(context->c.connection));
goto cleanup;
}
if (store_rowset(context,context->vt->pragmas_id,get_rows))
goto cleanup;
sqlite3_finalize(get_rows);
get_rows=NULL;
return 0;
cleanup:
if (get_rows)
sqlite3_finalize(get_rows);
return -1;
}
static void store_done_pragmas(
store_context_t *context)
{
if (context->have_pragmas) {
sqlite3_exec(context->c.connection,pragmas_drop_sql,0,NULL,NULL);
context->have_pragmas=0;
}
}
#define __(x) #x
#define _(x) __(x)
static char const schema_extract_sql[] =
"create table temp.schema as "
"select "
" case type "
" when 'table' then "
" case "
" when rootpage>0 then "
" " _(SCHEMA_PHASE_TABLE) " "
" else "
" " _(SCHEMA_PHASE_VIRTUAL_TABLE) " "
" end "
" when 'index' then "
" " _(SCHEMA_PHASE_INDEX) " "
" when 'view' then "
" " _(SCHEMA_PHASE_VIEW) " "
" when 'trigger' then "
" " _(SCHEMA_PHASE_TRIGGER) " "
" end as phase, "
" name, "
" sql "
"from sqlite_schema "
" where sql is not null";
static char const schema_select_sql[] =
"select * from temp.schema";
static char const table_names_sql[] =
"select name from temp.schema "
" where phase=" _(SCHEMA_PHASE_TABLE) " "
" order by name='sqlite_sequence'";
static char const get_rows_sql_1[] =
"select ";
static char const get_rows_sql_2[] =
" from ";
static int store_schema(
store_context_t *context)
{
store_vt const *vt=context->vt;
char *errmsg=NULL;
sqlite3_stmt *get_rows=NULL;
int status;
status=sqlite3_exec(
context->c.connection,schema_extract_sql,0,NULL,&errmsg);
if (status!=SQLITE_OK) {
errf(
&context->c,status,
"Failed to extract schema: %s",errmsg);
goto cleanup;
}
context->have_schema=1;
status=sqlite3_prepare_v2(
context->c.connection,
schema_select_sql,sizeof schema_select_sql,
&get_rows,
NULL);
if (status!=SQLITE_OK) {
errf(
&context->c,status,
"While extracting schema: sqlite3_prepare: %s",
sqlite3_errmsg(context->c.connection));
goto cleanup;
}
if (store_rowset(context,vt->schema_id,get_rows))
goto cleanup;
sqlite3_finalize(get_rows);
get_rows=NULL;
return 0;
cleanup:
if (errmsg)
sqlite3_free(errmsg);
if (get_rows)
sqlite3_finalize(get_rows);
return -1;
}
static void store_done_schema(
store_context_t *context)
{
if (context->have_schema) {
sqlite3_exec(context->c.connection,schema_drop_sql,0,NULL,NULL);
context->have_schema=0;
}
}
static int store_tables(
store_context_t *context)
{
store_vt const *vt=context->vt;
sqlite3_stmt *list_tables=NULL;
sqlite3_stmt *list_columns=NULL;
sqlite3_stmt *get_rows=NULL;
str_t sql;
int status;
str_init(&sql,&context->c);
status=sqlite3_prepare_v2(
context->c.connection,
table_names_sql, sizeof table_names_sql,
&list_tables,
NULL);
if (status!=SQLITE_OK) {
errf(
&context->c,status,
"While extracting schema: sqlite3_prepare: %s",
sqlite3_errmsg(context->c.connection));
goto cleanup;
}
for (;;) {
conststr_t tablename;
int colcnt;
status=sqlite3_step(list_tables);
if (status!=SQLITE_ROW)
break;
if ((*vt->column_text)(&context->c,list_tables,0,&tablename))
goto cleanup;
if ((*vt->str_app_7)(&sql,table_info_sql_1,sizeof table_info_sql_1-1))
goto cleanup;
if ((*vt->str_app_id)(&sql,tablename.text,tablename.size))
goto cleanup;
status=(*vt->prepare)(&context->c,sql.text,sql.size,&list_columns);
if (status!=SQLITE_OK) {
errf(
&context->c,status,
"While extracting tables: sqlite3_prepare: %s",
sqlite3_errmsg(context->c.connection));
goto cleanup;
}
sql.size=0;
if ((*vt->str_app_7)(&sql,get_rows_sql_1,sizeof get_rows_sql_1-1))
goto cleanup;
colcnt=0;
for (;;) {
conststr_t colname;
status=sqlite3_step(list_columns);
if (status!=SQLITE_ROW)
break;
if ((*vt->column_text)(&context->c,list_columns,1,&colname))
goto cleanup;
if (colcnt>0) {
if ((*vt->str_app_7)(&sql,",",1))
goto cleanup;
}
if ((*vt->str_app_id)(&sql,colname.text,colname.size))
goto cleanup;
colcnt++;
}
if (status!=SQLITE_DONE) {
errf(
&context->c,status,
"While extracting tables: sqlite3_step: %s",
sqlite3_errmsg(context->c.connection));
goto cleanup;
}
if (!colcnt) {
errf(
&context->c,SQLITE_ERROR,
"While extracting tables: pragma table_info returned no rows");
goto cleanup;
}
sqlite3_finalize(list_columns);
list_columns=NULL;
if ((*vt->str_app_7)(&sql,get_rows_sql_2,sizeof get_rows_sql_2-1))
goto cleanup;
if ((*vt->str_app_id)(&sql,tablename.text,tablename.size))
goto cleanup;
status=(*vt->prepare)(&context->c,sql.text,sql.size,&get_rows);
if (status!=SQLITE_OK) {
errf(
&context->c,status,
"While extracting tables: sqlite3_prepare: %s",
sqlite3_errmsg(context->c.connection));
goto cleanup;
}
sql.size=0;
if (store_rowset(context,tablename,get_rows))
goto cleanup;
sqlite3_finalize(get_rows);
get_rows=NULL;
}
if (status!=SQLITE_DONE) {
errf(
&context->c,status,
"While extracting tables: sqlite3_step: %s",
sqlite3_errmsg(context->c.connection));
goto cleanup;
}
str_free(&sql);
sqlite3_finalize(list_tables);
list_tables=NULL;
return 0;
cleanup:
if (list_tables)
sqlite3_finalize(list_tables);
if (list_columns)
sqlite3_finalize(list_columns);
if (get_rows)
sqlite3_finalize(get_rows);
str_free(&sql);
return -1;
}
static char const begin_sql[] =
"begin transaction";
static int store_begin_transaction(
store_context_t *context)
{
char *errmsg=NULL;
int status;
sqlite3_busy_timeout(context->c.connection,999999999);
status=sqlite3_exec(context->c.connection,begin_sql,0,NULL,&errmsg);
if (status!=SQLITE_OK) {
errf(
&context->c,status,
"Failed to start transaction: %s",errmsg);
goto cleanup;
}
context->c.in_transaction=1;
return 0;
cleanup:
if (errmsg)
sqlite3_free(errmsg);
return -1;
}
static int store_end(
store_context_t *context)
{
if (wc(context,ENDDUMP()))
return -1;
if (fflush(context->outfile)) {
errf(
&context->c,SQLITE_IOERR_WRITE,
"Write error: %s",strerror(errno));
return -1;
}
return 0;
}
int s3bd_store(
sqlite3 *connection,
FILE *outfile,
unsigned int flags,
char const * const *overrides,
char **errmsg)
{
store_context_t context;
memset(&context,0,sizeof context);
context.outfile=outfile;
if (context_init(&context.c,connection))
goto cleanup;
if (!(flags & S3BD_STORE_IN_TRANSACTION)
&& store_begin_transaction(&context))
goto cleanup;
if (store_header(&context))
goto cleanup;
if (extract_pragmas(&context))
goto cleanup;
if (overrides) {
if (override_pragmas(&context.c,overrides))
goto cleanup;
}
if (store_pragmas(&context))
goto cleanup;
store_done_pragmas(&context);
if (store_schema(&context))
goto cleanup;
if (!(flags & S3BD_STORE_SCHEMA_ONLY)) {
if (store_tables(&context))
goto cleanup;
}
store_done_schema(&context);
rollback_transaction(&context.c);
if (store_end(&context))
goto cleanup;
context_term(&context.c,errmsg);
return SQLITE_OK;
cleanup:
store_done_pragmas(&context);
store_done_schema(&context);
rollback_transaction(&context.c);
return context_term(&context.c,errmsg);
}