forked from DBD-SQLite/DBD-SQLite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbdimp.c
2996 lines (2552 loc) · 86.1 KB
/
dbdimp.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
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
#define PERL_NO_GET_CONTEXT
#define NEED_newSVpvn_flags
#define NEED_sv_2pvbyte
#include "SQLiteXS.h"
START_MY_CXT;
DBISTATE_DECLARE;
#define SvPV_nolen_undef_ok(x) (SvOK(x) ? SvPV_nolen(x) : "undef")
/*-----------------------------------------------------*
* Debug Macros
*-----------------------------------------------------*/
#undef DBD_SQLITE_CROAK_DEBUG
#ifdef DBD_SQLITE_CROAK_DEBUG
#define croak_if_db_is_null() if (!imp_dbh->db) croak("imp_dbh->db is NULL at line %d in %s", __LINE__, __FILE__)
#define croak_if_stmt_is_null() if (!imp_sth->stmt) croak("imp_sth->stmt is NULL at line %d in %s", __LINE__, __FILE__)
#else
#define croak_if_db_is_null()
#define croak_if_stmt_is_null()
#endif
/*-----------------------------------------------------*
* Helper Methods
*-----------------------------------------------------*/
#define sqlite_error(h,rc,what) _sqlite_error(aTHX_ __FILE__, __LINE__, h, rc, what)
#define sqlite_trace(h,xxh,level,what) if ( DBIc_TRACE_LEVEL((imp_xxh_t*)xxh) >= level ) _sqlite_trace(aTHX_ __FILE__, __LINE__, h, (imp_xxh_t*)xxh, what)
#define sqlite_exec(h,sql) _sqlite_exec(aTHX_ h, imp_dbh->db, sql)
#define sqlite_open(dbname,db) _sqlite_open(aTHX_ dbh, dbname, db, 0, 0)
#define sqlite_open2(dbname,db,flags,extended) _sqlite_open(aTHX_ dbh, dbname, db, flags, extended)
#define _isspace(c) (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v' || c == '\f')
#define _skip_whitespaces(sql) \
while ( _isspace(sql[0]) || (sql[0] == '-' && sql[1] == '-')) { \
if ( _isspace(sql[0]) ) { \
while ( _isspace(sql[0]) ) sql++; \
continue; \
} \
else if (sql[0] == '-') { \
while ( sql[0] != 0 && sql[0] != '\n' ) sql++; \
continue; \
} \
}
bool
_starts_with_begin(const char *sql) {
return (
((sql[0] == 'B' || sql[0] == 'b') &&
(sql[1] == 'E' || sql[1] == 'e') &&
(sql[2] == 'G' || sql[2] == 'g') &&
(sql[3] == 'I' || sql[3] == 'i') &&
(sql[4] == 'N' || sql[4] == 'n')
) || (
(sql[0] == 'S' || sql[0] == 's') &&
(sql[1] == 'A' || sql[1] == 'a') &&
(sql[2] == 'V' || sql[2] == 'v') &&
(sql[3] == 'E' || sql[3] == 'e') &&
(sql[4] == 'P' || sql[4] == 'p') &&
(sql[5] == 'O' || sql[5] == 'o') &&
(sql[6] == 'I' || sql[6] == 'i') &&
(sql[7] == 'N' || sql[7] == 'n') &&
(sql[8] == 'T' || sql[8] == 't')
)
) ? TRUE : FALSE;
}
/* adopted from sqlite3.c */
#define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
#define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
static int compare2pow63(const char *zNum) {
int c = 0;
int i;
/* 012345678901234567 */
const char *pow63 = "922337203685477580";
for(i = 0; c == 0 && i < 18; i++){
c = (zNum[i] - pow63[i]) * 10;
}
if(c == 0){
c = zNum[18] - '8';
}
return c;
}
int _sqlite_atoi64(const char *zNum, sqlite3_int64 *pNum) {
sqlite3_uint64 u = 0;
int neg = 0;
int i;
int c = 0;
const char *zStart;
const char *zEnd = zNum + strlen(zNum);
while(zNum < zEnd && _isspace(*zNum)) zNum++;
if (zNum < zEnd) {
if (*zNum == '-') {
neg = 1;
zNum++;
} else if (*zNum == '+') {
zNum++;
}
}
zStart = zNum;
while(zNum < zEnd && zNum[0] == '0') zNum++;
for(i = 0; &zNum[i] < zEnd && (c = zNum[i]) >= '0' && c <= '9'; i++) {
u = u * 10 + c - '0';
}
if (u > LARGEST_INT64) {
*pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
} else if (neg) {
*pNum = -(sqlite3_int64)u;
} else {
*pNum = (sqlite3_int64)u;
}
if ((c != 0 && &zNum[i] < zEnd) || (i == 0 && zStart == zNum) || i > 19) {
return 1;
} else if (i < 19) {
return 0;
} else {
c = compare2pow63(zNum);
if (c < 0) {
return 0;
} else if (c > 0) {
return 1;
} else {
return neg ? 0 : 2;
}
}
}
static void
_sqlite_trace(pTHX_ char *file, int line, SV *h, imp_xxh_t *imp_xxh, const char *what)
{
PerlIO_printf(
DBIc_LOGPIO(imp_xxh),
"sqlite trace: %s at %s line %d\n", what, file, line
);
}
static void
_sqlite_error(pTHX_ char *file, int line, SV *h, int rc, const char *what)
{
D_imp_xxh(h);
DBIh_SET_ERR_CHAR(h, imp_xxh, Nullch, rc, what, Nullch, Nullch);
/* #7753: DBD::SQLite error shouldn't include extraneous info */
/* sv_catpvf(errstr, "(%d) at %s line %d", rc, file, line); */
if ( DBIc_TRACE_LEVEL(imp_xxh) >= 3 ) {
PerlIO_printf(
DBIc_LOGPIO(imp_xxh),
"sqlite error %d recorded: %s at %s line %d\n",
rc, what, file, line
);
}
}
int
_sqlite_exec(pTHX_ SV *h, sqlite3 *db, const char *sql)
{
int rc;
char *errmsg;
rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
if ( rc != SQLITE_OK ) {
sqlite_error(h, rc, errmsg);
if (errmsg) sqlite3_free(errmsg);
}
return rc;
}
int
_sqlite_open(pTHX_ SV *dbh, const char *dbname, sqlite3 **db, int flags, int extended)
{
int rc;
if (flags) {
rc = sqlite3_open_v2(dbname, db, flags, NULL);
} else {
rc = sqlite3_open(dbname, db);
}
if ( rc != SQLITE_OK ) {
#if SQLITE_VERSION_NUMBER >= 3006005
if (extended)
rc = sqlite3_extended_errcode(*db);
#endif
sqlite_error(dbh, rc, sqlite3_errmsg(*db));
if (*db) sqlite3_close(*db);
}
return rc;
}
static int
sqlite_type_to_odbc_type(int type)
{
switch(type) {
case SQLITE_INTEGER: return SQL_INTEGER;
case SQLITE_FLOAT: return SQL_DOUBLE;
case SQLITE_TEXT: return SQL_VARCHAR;
case SQLITE_BLOB: return SQL_BLOB;
case SQLITE_NULL: return SQL_UNKNOWN_TYPE;
default: return SQL_UNKNOWN_TYPE;
}
}
static int
sqlite_type_from_odbc_type(int type)
{
switch(type) {
case SQL_UNKNOWN_TYPE:
return SQLITE_NULL;
case SQL_BOOLEAN:
case SQL_INTEGER:
case SQL_SMALLINT:
case SQL_TINYINT:
case SQL_BIGINT:
return SQLITE_INTEGER;
case SQL_FLOAT:
case SQL_REAL:
case SQL_DOUBLE:
return SQLITE_FLOAT;
case SQL_BIT:
case SQL_BLOB:
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
return SQLITE_BLOB;
default:
return SQLITE_TEXT;
}
}
void
init_cxt() {
dTHX;
MY_CXT_INIT;
MY_CXT.last_dbh_string_mode = DBD_SQLITE_STRING_MODE_PV;
}
SV *
stacked_sv_from_sqlite3_value(pTHX_ sqlite3_value *value, dbd_sqlite_string_mode_t string_mode)
{
STRLEN len;
sqlite_int64 iv;
int type = sqlite3_value_type(value);
SV *sv;
switch(type) {
case SQLITE_INTEGER:
iv = sqlite3_value_int64(value);
if ( iv >= IV_MIN && iv <= IV_MAX ) {
/* ^^^ compile-time constant (= true) when IV == int64 */
return sv_2mortal(newSViv((IV)iv));
}
else if ( iv >= 0 && iv <= UV_MAX ) {
/* warn("integer overflow, cast to UV"); */
return sv_2mortal(newSVuv((UV)iv));
}
else {
/* warn("integer overflow, cast to NV"); */
return sv_2mortal(newSVnv((NV)iv));
}
case SQLITE_FLOAT:
return sv_2mortal(newSVnv(sqlite3_value_double(value)));
break;
case SQLITE_TEXT:
len = sqlite3_value_bytes(value);
sv = newSVpvn((const char *)sqlite3_value_text(value), len);
DBD_SQLITE_UTF8_DECODE_IF_NEEDED(sv, string_mode);
return sv_2mortal(sv);
case SQLITE_BLOB:
len = sqlite3_value_bytes(value);
return sv_2mortal(newSVpvn(sqlite3_value_blob(value), len));
default:
return &PL_sv_undef;
}
}
static void
sqlite_set_result(pTHX_ sqlite3_context *context, SV *result, int is_error)
{
STRLEN len;
char *s;
sqlite3_int64 iv;
AV *av;
SV *result2, *type;
SV **presult2, **ptype;
if ( is_error ) {
s = SvPV(result, len);
sqlite3_result_error( context, s, len );
return;
}
/* warn("result: %s\n", SvPV_nolen(result)); */
if ( !SvOK(result) ) {
sqlite3_result_null( context );
} else if( SvROK(result) && SvTYPE(SvRV(result)) == SVt_PVAV ) {
av = (AV*)SvRV(result);
if ( av_len(av) == 1 ) {
presult2 = av_fetch(av, 0, 0);
ptype = av_fetch(av, 1, 0);
result2 = presult2 ? *presult2 : &PL_sv_undef;
type = ptype ? *ptype : &PL_sv_undef;
if ( SvIOK(type) ) {
switch(sqlite_type_from_odbc_type(SvIV(type))) {
case SQLITE_INTEGER:
sqlite3_result_int64( context, SvIV(result2) );
return;
case SQLITE_FLOAT:
sqlite3_result_double( context, SvNV(result2) );
return;
case SQLITE_BLOB:
s = SvPV(result2, len);
sqlite3_result_blob( context, s, len, SQLITE_TRANSIENT );
return;
case SQLITE_TEXT:
s = SvPV(result2, len);
sqlite3_result_text( context, s, len, SQLITE_TRANSIENT );
return;
}
}
}
sqlite3_result_error( context, "unexpected arrayref", 19 );
} else if( SvIOK_UV(result) ) {
if ((UV)(sqlite3_int64)UV_MAX == UV_MAX)
sqlite3_result_int64( context, (sqlite3_int64)SvUV(result));
else {
s = SvPV(result, len);
sqlite3_result_text( context, s, len, SQLITE_TRANSIENT );
}
} else if ( !_sqlite_atoi64(SvPV(result, len), &iv) ) {
sqlite3_result_int64( context, iv );
} else if ( SvNOK(result) && ( sizeof(NV) == sizeof(double) || SvNVX(result) == (double) SvNVX(result) ) ) {
sqlite3_result_double( context, SvNV(result));
} else {
s = SvPV(result, len);
sqlite3_result_text( context, s, len, SQLITE_TRANSIENT );
}
}
/*
* see also sqlite3IsNumber, sqlite3_int64 type definition,
* applyNumericAffinity, sqlite3Atoi64, etc from sqlite3.c
*/
static int
sqlite_is_number(pTHX_ const char *v, int sql_type)
{
sqlite3_int64 iv;
const char *z = v;
const char *d = v;
int neg;
int digit = 0;
int precision = 0;
bool has_plus = FALSE;
bool maybe_int = TRUE;
char format[10];
if (sql_type != SQLITE_NULL) {
while (*z == ' ') { z++; v++; d++; }
}
if (*z == '-') { neg = 1; z++; d++; }
else if (*z == '+') { neg = 0; z++; d++; has_plus = TRUE; }
else { neg = 0; }
if (!isdigit(*z)) return 0;
while (isdigit(*z)) { digit++; z++; }
if (digit > 19) maybe_int = FALSE; /* too large for i64 */
if (digit == 19) {
int c;
char tmp[22];
strncpy(tmp, d, z - d + 1);
c = memcmp(tmp, "922337203685477580", 18);
if (c == 0) {
c = tmp[18] - '7' - neg;
}
if (c > 0) maybe_int = FALSE;
}
if (*z == '.') {
maybe_int = FALSE;
z++;
if (!isdigit(*z)) return 0;
while (isdigit(*z)) { precision++; z++; }
}
if (*z == 'e' || *z == 'E') {
maybe_int = FALSE;
z++;
if (*z == '+' || *z == '-') { z++; }
if (!isdigit(*z)) return 0;
while (isdigit(*z)) { z++; }
}
if (*z && !isdigit(*z)) return 0;
if (maybe_int && digit) {
if (!_sqlite_atoi64(v, &iv)) return 1;
}
if (sql_type != SQLITE_INTEGER) {
#ifdef USE_QUADMATH
sprintf(format, (has_plus ? "+%%.%dQf" : "%%.%dQf"), precision);
# if defined(WIN32)
/* On Windows quadmath, we need to use strtoflt128(), not atov() */
if (strEQ(form(format, strtoflt128(v, NULL)), v)) return 2;
# else
if (strEQ(form(format, atof(v)), v)) return 2;
# endif
#else
sprintf(format, (has_plus ? "+%%.%df" : "%%.%df" ), precision);
if (strEQ(form(format, atof(v)), v)) return 2;
#endif
}
return 0;
}
/*-----------------------------------------------------*
* DBD Methods
*-----------------------------------------------------*/
void
sqlite_init(dbistate_t *dbistate)
{
dTHX;
DBISTATE_INIT; /* Initialize the DBI macros */
}
int
sqlite_discon_all(SV *drh, imp_drh_t *imp_drh)
{
dTHX;
return FALSE; /* no way to do this */
}
#define _croak_invalid_value(name, value) \
croak("Invalid value (%s) given for %s", value, name);
/* Like SvUV but croaks on anything other than an unsigned int. */
static inline int
my_SvUV_strict(pTHX_ SV *input, const char* name)
{
if (SvUOK(input)) {
return SvUV(input);
}
const char* pv = SvPVbyte_nolen(input);
UV uv;
int numtype = grok_number(pv, strlen(pv), &uv);
/* Anything else is invalid: */
if (numtype != IS_NUMBER_IN_UV) _croak_invalid_value(name, pv);
return uv;
}
static inline dbd_sqlite_string_mode_t
_extract_sqlite_string_mode_from_sv( pTHX_ SV* input )
{
if (SvOK(input)) {
UV val = my_SvUV_strict(aTHX_ input, "sqlite_string_mode");
if (val >= _DBD_SQLITE_STRING_MODE_COUNT) {
_croak_invalid_value("sqlite_string_mode", SvPVbyte_nolen(input));
}
return val;
}
return DBD_SQLITE_STRING_MODE_PV;
}
int
sqlite_db_login6(SV *dbh, imp_dbh_t *imp_dbh, char *dbname, char *user, char *pass, SV *attr)
{
dTHX;
int rc;
HV *hv = NULL;
SV **val;
int extended = 0;
int flag = 0;
dbd_sqlite_string_mode_t string_mode = DBD_SQLITE_STRING_MODE_PV;
sqlite_trace(dbh, imp_dbh, 3, form("login '%s' (version %s)", dbname, sqlite3_version));
if (SvROK(attr)) {
hv = (HV*)SvRV(attr);
if (hv_exists(hv, "sqlite_extended_result_codes", 28)) {
val = hv_fetch(hv, "sqlite_extended_result_codes", 28, 0);
extended = (val && SvOK(*val)) ? !(!SvTRUE(*val)) : 0;
}
if (hv_exists(hv, "ReadOnly", 8)) {
val = hv_fetch(hv, "ReadOnly", 8, 0);
if ((val && SvOK(*val)) ? SvIV(*val) : 0) {
flag |= SQLITE_OPEN_READONLY;
}
}
if (hv_exists(hv, "sqlite_open_flags", 17)) {
val = hv_fetch(hv, "sqlite_open_flags", 17, 0);
flag |= (val && SvOK(*val)) ? SvIV(*val) : 0;
if (flag & SQLITE_OPEN_READONLY) {
hv_stores(hv, "ReadOnly", newSViv(1));
}
}
/* sqlite_string_mode should be detected earlier, to register default functions correctly */
SV** string_mode_svp = hv_fetchs(hv, "sqlite_string_mode", 0);
if (string_mode_svp != NULL && SvOK(*string_mode_svp)) {
string_mode = _extract_sqlite_string_mode_from_sv(aTHX_ *string_mode_svp);
/* Legacy alternatives to sqlite_string_mode: */
} else if (hv_exists(hv, "sqlite_unicode", 14)) {
val = hv_fetch(hv, "sqlite_unicode", 14, 0);
if ( (val && SvOK(*val)) ? SvIV(*val) : 0 ) {
string_mode = DBD_SQLITE_STRING_MODE_UNICODE_NAIVE;
}
} else if (hv_exists(hv, "unicode", 7)) {
val = hv_fetch(hv, "unicode", 7, 0);
if ( (val && SvOK(*val)) ? SvIV(*val) : 0 ) {
string_mode = DBD_SQLITE_STRING_MODE_UNICODE_NAIVE;
}
}
}
rc = sqlite_open2(dbname, &(imp_dbh->db), flag, extended);
if ( rc != SQLITE_OK ) {
return FALSE; /* -> undef in lib/DBD/SQLite.pm */
}
DBIc_IMPSET_on(imp_dbh);
imp_dbh->string_mode = string_mode;
imp_dbh->functions = newAV();
imp_dbh->aggregates = newAV();
imp_dbh->collation_needed_callback = newSVsv( &PL_sv_undef );
imp_dbh->timeout = SQL_TIMEOUT;
imp_dbh->handle_binary_nulls = FALSE;
imp_dbh->allow_multiple_statements = FALSE;
imp_dbh->use_immediate_transaction = TRUE;
imp_dbh->see_if_its_a_number = FALSE;
imp_dbh->extended_result_codes = extended;
imp_dbh->stmt_list = NULL;
imp_dbh->began_transaction = FALSE;
imp_dbh->prefer_numeric_type = FALSE;
sqlite3_busy_timeout(imp_dbh->db, SQL_TIMEOUT);
if (SvROK(attr)) {
hv = (HV*)SvRV(attr);
if (hv_exists(hv, "sqlite_defensive", 16)) {
val = hv_fetch(hv, "sqlite_defensive", 16, 0);
if (val && SvIOK(*val)) {
sqlite3_db_config(imp_dbh->db, SQLITE_DBCONFIG_DEFENSIVE, (int)SvIV(*val), 0);
}
}
}
#if 0
/*
** As of 1.26_06 foreign keys support was enabled by default,
** but with further discussion, we agreed to follow what
** sqlite team does, i.e. wait until the team think it
** reasonable to enable the support by default, as they have
** larger users and will allocate enough time for people to
** get used to the foreign keys. However, we should say it loud
** that sometime in the (near?) future, this feature may break
** your applications (and it actually broke applications).
** Let everyone be prepared.
*/
sqlite_exec(dbh, "PRAGMA foreign_keys = ON");
#endif
#if 0
/*
** Enable this to see if you (wrongly) expect an implicit order
** of return values from a SELECT statement without ORDER BY.
*/
sqlite_exec(dbh, "PRAGMA reverse_unordered_selects = ON");
#endif
DBIc_ACTIVE_on(imp_dbh);
return TRUE;
}
int
sqlite_db_do_sv(SV *dbh, imp_dbh_t *imp_dbh, SV *sv_statement)
{
dTHX;
int rc = 0;
int i;
char *statement;
if (!DBIc_ACTIVE(imp_dbh)) {
sqlite_error(dbh, -2, "attempt to do on inactive database handle");
return -2; /* -> undef in SQLite.xsi */
}
/* sqlite3_prepare wants an utf8-encoded SQL statement */
DBD_SQLITE_PREP_SV_FOR_SQLITE(sv_statement, imp_dbh->string_mode);
statement = SvPV_nolen(sv_statement);
sqlite_trace(dbh, imp_dbh, 3, form("do statement: %s", statement));
croak_if_db_is_null();
if (sqlite3_get_autocommit(imp_dbh->db)) {
const char *sql = statement;
_skip_whitespaces(sql);
if (_starts_with_begin(sql)) {
if (DBIc_is(imp_dbh, DBIcf_AutoCommit)) {
if (!DBIc_is(imp_dbh, DBIcf_BegunWork)) {
imp_dbh->began_transaction = TRUE;
DBIc_on(imp_dbh, DBIcf_BegunWork);
DBIc_off(imp_dbh, DBIcf_AutoCommit);
}
}
}
else if (!DBIc_is(imp_dbh, DBIcf_AutoCommit)) {
sqlite_trace(dbh, imp_dbh, 3, "BEGIN TRAN");
if (imp_dbh->use_immediate_transaction) {
rc = sqlite_exec(dbh, "BEGIN IMMEDIATE TRANSACTION");
} else {
rc = sqlite_exec(dbh, "BEGIN TRANSACTION");
}
if (rc != SQLITE_OK) {
return -2; /* -> undef in SQLite.xsi */
}
}
}
rc = sqlite_exec(dbh, statement);
if (rc != SQLITE_OK) {
sqlite_error(dbh, rc, sqlite3_errmsg(imp_dbh->db));
return -2;
}
if (DBIc_is(imp_dbh, DBIcf_BegunWork) && sqlite3_get_autocommit(imp_dbh->db)) {
if (imp_dbh->began_transaction) {
DBIc_off(imp_dbh, DBIcf_BegunWork);
DBIc_on(imp_dbh, DBIcf_AutoCommit);
}
}
return sqlite3_changes(imp_dbh->db);
}
int
sqlite_db_commit(SV *dbh, imp_dbh_t *imp_dbh)
{
dTHX;
int rc;
if (!DBIc_ACTIVE(imp_dbh)) {
sqlite_error(dbh, -2, "attempt to commit on inactive database handle");
return FALSE;
}
if (DBIc_is(imp_dbh, DBIcf_AutoCommit)) {
/* We don't need to warn, because the DBI layer will do it for us */
return TRUE;
}
if (DBIc_is(imp_dbh, DBIcf_BegunWork)) {
/* XXX: for rt_52573
imp_dbh->began_transaction = FALSE;
*/
DBIc_off(imp_dbh, DBIcf_BegunWork);
DBIc_on(imp_dbh, DBIcf_AutoCommit);
}
croak_if_db_is_null();
if (!sqlite3_get_autocommit(imp_dbh->db)) {
sqlite_trace(dbh, imp_dbh, 3, "COMMIT TRAN");
rc = sqlite_exec(dbh, "COMMIT TRANSACTION");
if (rc != SQLITE_OK) {
return FALSE; /* -> &sv_no in SQLite.xsi */
}
}
return TRUE;
}
int
sqlite_db_rollback(SV *dbh, imp_dbh_t *imp_dbh)
{
dTHX;
int rc;
if (!DBIc_ACTIVE(imp_dbh)) {
sqlite_error(dbh, -2, "attempt to rollback on inactive database handle");
return FALSE;
}
if (DBIc_is(imp_dbh, DBIcf_BegunWork)) {
/* XXX: for rt_52573
imp_dbh->began_transaction = FALSE;
*/
DBIc_off(imp_dbh, DBIcf_BegunWork);
DBIc_on(imp_dbh, DBIcf_AutoCommit);
}
croak_if_db_is_null();
if (!sqlite3_get_autocommit(imp_dbh->db)) {
sqlite_trace(dbh, imp_dbh, 3, "ROLLBACK TRAN");
rc = sqlite_exec(dbh, "ROLLBACK TRANSACTION");
if (rc != SQLITE_OK) {
return FALSE; /* -> &sv_no in SQLite.xsi */
}
}
return TRUE;
}
int
sqlite_db_disconnect(SV *dbh, imp_dbh_t *imp_dbh)
{
dTHX;
int rc;
stmt_list_s * s;
if (DBIc_is(imp_dbh, DBIcf_AutoCommit) == FALSE) {
sqlite_db_rollback(dbh, imp_dbh);
}
DBIc_ACTIVE_off(imp_dbh);
croak_if_db_is_null();
sqlite_trace( dbh, imp_dbh, 1, "Closing DB" );
rc = sqlite3_close( imp_dbh->db );
sqlite_trace( dbh, imp_dbh, 1, form("rc = %d", rc) );
if ( SQLITE_BUSY == rc ) { /* We have unfinalized statements */
/* Only close the statements that were prepared by this module */
while ( (s = imp_dbh->stmt_list) ) {
sqlite_trace( dbh, imp_dbh, 1, form("Finalizing statement (%p)", s->stmt) );
sqlite3_finalize( s->stmt );
imp_dbh->stmt_list = s->prev;
sqlite3_free( s );
}
imp_dbh->stmt_list = NULL;
sqlite_trace( dbh, imp_dbh, 1, "Trying to close DB again" );
rc = sqlite3_close( imp_dbh->db );
}
if ( SQLITE_OK != rc ) {
sqlite_error(dbh, rc, sqlite3_errmsg(imp_dbh->db));
}
/* The list should be empty at this point, but if for some unforseen reason
it isn't, free remaining nodes here */
while( (s = imp_dbh->stmt_list) ) {
imp_dbh->stmt_list = s->prev;
sqlite3_free( s );
}
imp_dbh->db = NULL;
av_undef(imp_dbh->functions);
SvREFCNT_dec(imp_dbh->functions);
imp_dbh->functions = (AV *)NULL;
av_undef(imp_dbh->aggregates);
SvREFCNT_dec(imp_dbh->aggregates);
imp_dbh->aggregates = (AV *)NULL;
sv_setsv(imp_dbh->collation_needed_callback, &PL_sv_undef);
SvREFCNT_dec(imp_dbh->collation_needed_callback);
imp_dbh->collation_needed_callback = (SV *)NULL;
return TRUE;
}
void
sqlite_db_destroy(SV *dbh, imp_dbh_t *imp_dbh)
{
dTHX;
if (DBIc_ACTIVE(imp_dbh)) {
sqlite_db_disconnect(dbh, imp_dbh);
}
DBIc_IMPSET_off(imp_dbh);
}
#define _warn_deprecated_if_possible(old, new) \
if (DBIc_has(imp_dbh, DBIcf_WARN)) \
warn("\"%s\" attribute will be deprecated. Use \"%s\" instead.", old, new);
int
sqlite_db_STORE_attrib(SV *dbh, imp_dbh_t *imp_dbh, SV *keysv, SV *valuesv)
{
dTHX;
char *key = SvPV_nolen(keysv);
int rc;
croak_if_db_is_null();
if (strEQ(key, "AutoCommit")) {
if (SvTRUE(valuesv)) {
/* commit tran? */
if ( DBIc_ACTIVE(imp_dbh) && (!DBIc_is(imp_dbh, DBIcf_AutoCommit)) && (!sqlite3_get_autocommit(imp_dbh->db)) ) {
sqlite_trace(dbh, imp_dbh, 3, "COMMIT TRAN");
rc = sqlite_exec(dbh, "COMMIT TRANSACTION");
if (rc != SQLITE_OK) {
return TRUE; /* XXX: is this correct? */
}
}
}
DBIc_set(imp_dbh, DBIcf_AutoCommit, SvTRUE(valuesv));
return TRUE;
}
#if SQLITE_VERSION_NUMBER >= 3007011
if (strEQ(key, "ReadOnly")) {
if (SvTRUE(valuesv) && !sqlite3_db_readonly(imp_dbh->db, "main")) {
sqlite_error(dbh, 0, "ReadOnly is set but it's only advisory");
}
return FALSE;
}
#endif
if (strEQ(key, "sqlite_allow_multiple_statements")) {
imp_dbh->allow_multiple_statements = !(! SvTRUE(valuesv));
return TRUE;
}
if (strEQ(key, "sqlite_use_immediate_transaction")) {
imp_dbh->use_immediate_transaction = !(! SvTRUE(valuesv));
return TRUE;
}
if (strEQ(key, "sqlite_see_if_its_a_number")) {
imp_dbh->see_if_its_a_number = !(! SvTRUE(valuesv));
return TRUE;
}
if (strEQ(key, "sqlite_extended_result_codes")) {
imp_dbh->extended_result_codes = !(! SvTRUE(valuesv));
sqlite3_extended_result_codes(imp_dbh->db, imp_dbh->extended_result_codes);
return TRUE;
}
if (strEQ(key, "sqlite_prefer_numeric_type")) {
imp_dbh->prefer_numeric_type = !(! SvTRUE(valuesv));
return TRUE;
}
if (strEQ(key, "sqlite_string_mode")) {
dbd_sqlite_string_mode_t string_mode = _extract_sqlite_string_mode_from_sv(aTHX_ valuesv);
#if PERL_UNICODE_DOES_NOT_WORK_WELL
if (string_mode & DBD_SQLITE_STRING_MODE_UNICODE_ANY) {
sqlite_trace(dbh, imp_dbh, 3, form("Unicode support is disabled for this version of perl."));
string_mode = DBD_SQLITE_STRING_MODE_PV;
}
#endif
imp_dbh->string_mode = string_mode;
return TRUE;
}
if (strEQ(key, "sqlite_unicode")) {
/* it's too early to warn the deprecation of sqlite_unicode as it's widely used */
#if PERL_UNICODE_DOES_NOT_WORK_WELL
sqlite_trace(dbh, imp_dbh, 3, form("Unicode support is disabled for this version of perl."));
imp_dbh->string_mode = DBD_SQLITE_STRING_MODE_PV;
#else
imp_dbh->string_mode = SvTRUE(valuesv) ? DBD_SQLITE_STRING_MODE_UNICODE_NAIVE : DBD_SQLITE_STRING_MODE_PV;
#endif
return TRUE;
}
if (strEQ(key, "unicode")) {
_warn_deprecated_if_possible(key, "sqlite_string_mode");
#if PERL_UNICODE_DOES_NOT_WORK_WELL
sqlite_trace(dbh, imp_dbh, 3, form("Unicode support is disabled for this version of perl."));
imp_dbh->string_mode = DBD_SQLITE_STRING_MODE_PV;
#else
imp_dbh->string_mode = SvTRUE(valuesv) ? DBD_SQLITE_STRING_MODE_UNICODE_NAIVE : DBD_SQLITE_STRING_MODE_PV;
#endif
return TRUE;
}
return FALSE;
}
SV *
sqlite_db_FETCH_attrib(SV *dbh, imp_dbh_t *imp_dbh, SV *keysv)
{
dTHX;
char *key = SvPV_nolen(keysv);
if (strEQ(key, "sqlite_version")) {
return sv_2mortal(newSVpv(sqlite3_version, 0));
}
if (strEQ(key, "sqlite_allow_multiple_statements")) {
return sv_2mortal(newSViv(imp_dbh->allow_multiple_statements ? 1 : 0));
}
if (strEQ(key, "sqlite_use_immediate_transaction")) {
return sv_2mortal(newSViv(imp_dbh->use_immediate_transaction ? 1 : 0));
}
if (strEQ(key, "sqlite_see_if_its_a_number")) {
return sv_2mortal(newSViv(imp_dbh->see_if_its_a_number ? 1 : 0));
}
if (strEQ(key, "sqlite_extended_result_codes")) {
return sv_2mortal(newSViv(imp_dbh->extended_result_codes ? 1 : 0));
}
if (strEQ(key, "sqlite_prefer_numeric_type")) {
return sv_2mortal(newSViv(imp_dbh->prefer_numeric_type ? 1 : 0));
}
if (strEQ(key, "sqlite_string_mode")) {
return sv_2mortal(newSVuv(imp_dbh->string_mode));
}
if (strEQ(key, "sqlite_unicode") || strEQ(key, "unicode")) {
_warn_deprecated_if_possible(key, "sqlite_string_mode");
#if PERL_UNICODE_DOES_NOT_WORK_WELL
sqlite_trace(dbh, imp_dbh, 3, "Unicode support is disabled for this version of perl.");
return sv_2mortal(newSViv(0));
#else
return sv_2mortal(newSViv(imp_dbh->string_mode == DBD_SQLITE_STRING_MODE_UNICODE_NAIVE ? 1 : 0));
#endif
}
return NULL;
}
SV *
sqlite_db_last_insert_id(SV *dbh, imp_dbh_t *imp_dbh, SV *catalog, SV *schema, SV *table, SV *field, SV *attr)
{
dTHX;
if (!DBIc_ACTIVE(imp_dbh)) {
sqlite_error(dbh, -2, "attempt to get last inserted id on inactive database handle");
return FALSE;
}
croak_if_db_is_null();
return sv_2mortal(newSViv((IV)sqlite3_last_insert_rowid(imp_dbh->db)));
}
int
sqlite_st_prepare_sv(SV *sth, imp_sth_t *imp_sth, SV *sv_statement, SV *attribs)
{
dTHX;
dMY_CXT;
int rc = 0;
const char *extra;
char *statement;
stmt_list_s * new_stmt;
D_imp_dbh_from_sth;
MY_CXT.last_dbh_string_mode = imp_dbh->string_mode;
if (!DBIc_ACTIVE(imp_dbh)) {
sqlite_error(sth, -2, "attempt to prepare on inactive database handle");
return FALSE; /* -> undef in lib/DBD/SQLite.pm */
}
/* sqlite3_prepare wants an utf8-encoded SQL statement */
DBD_SQLITE_PREP_SV_FOR_SQLITE(sv_statement, imp_dbh->string_mode);
statement = SvPV_nolen(sv_statement);
#if 0
if (*statement == '\0') {
sqlite_error(sth, -2, "attempt to prepare empty statement");
return FALSE; /* -> undef in lib/DBD/SQLite.pm */
}
#endif
sqlite_trace(sth, imp_sth, 3, form("prepare statement: %s", statement));
imp_sth->nrow = -1;
imp_sth->retval = SQLITE_OK;
imp_sth->params = newAV();
imp_sth->col_types = newAV();
croak_if_db_is_null();
/* COMPAT: sqlite3_prepare_v2 is only available for 3003009 or newer */
rc = sqlite3_prepare_v2(imp_dbh->db, statement, -1, &(imp_sth->stmt), &extra);
if (rc != SQLITE_OK) {
sqlite_error(sth, rc, sqlite3_errmsg(imp_dbh->db));
if (imp_sth->stmt) {
rc = sqlite3_finalize(imp_sth->stmt);
imp_sth->stmt = NULL;
if (rc != SQLITE_OK) {
sqlite_error(sth, rc, sqlite3_errmsg(imp_dbh->db));
}
}
return FALSE; /* -> undef in lib/DBD/SQLite.pm */
}
if (imp_dbh->allow_multiple_statements) {
imp_sth->unprepared_statements = savepv(extra);
}