File Coverage

dbdimp.c
Criterion Covered Total %
statement 1173 1351 86.8
branch 996 2102 47.3
condition n/a
subroutine n/a
pod n/a
total 2169 3453 62.8


line stmt bran cond sub pod time code
1             #define PERL_NO_GET_CONTEXT
2              
3             #define NEED_newSVpvn_flags
4             #define NEED_sv_2pvbyte
5              
6             #include "SQLiteXS.h"
7              
8             START_MY_CXT;
9              
10 232 50         DBISTATE_DECLARE;
    50          
11              
12             #define SvPV_nolen_undef_ok(x) (SvOK(x) ? SvPV_nolen(x) : "undef")
13              
14             /*-----------------------------------------------------*
15             * Debug Macros
16             *-----------------------------------------------------*/
17              
18             #undef DBD_SQLITE_CROAK_DEBUG
19              
20             #ifdef DBD_SQLITE_CROAK_DEBUG
21             #define croak_if_db_is_null() if (!imp_dbh->db) croak("imp_dbh->db is NULL at line %d in %s", __LINE__, __FILE__)
22             #define croak_if_stmt_is_null() if (!imp_sth->stmt) croak("imp_sth->stmt is NULL at line %d in %s", __LINE__, __FILE__)
23             #else
24             #define croak_if_db_is_null()
25             #define croak_if_stmt_is_null()
26             #endif
27              
28              
29             /*-----------------------------------------------------*
30             * Helper Methods
31             *-----------------------------------------------------*/
32              
33             #define sqlite_error(h,rc,what) _sqlite_error(aTHX_ __FILE__, __LINE__, h, rc, what)
34             #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)
35             #define sqlite_exec(h,sql) _sqlite_exec(aTHX_ h, imp_dbh->db, sql)
36             #define sqlite_open(dbname,db) _sqlite_open(aTHX_ dbh, dbname, db, 0, 0)
37             #define sqlite_open2(dbname,db,flags,extended) _sqlite_open(aTHX_ dbh, dbname, db, flags, extended)
38             #define _isspace(c) (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v' || c == '\f')
39              
40             #define _skip_whitespaces(sql) \
41             while ( _isspace(sql[0]) || (sql[0] == '-' && sql[1] == '-')) { \
42             if ( _isspace(sql[0]) ) { \
43             while ( _isspace(sql[0]) ) sql++; \
44             continue; \
45             } \
46             else if (sql[0] == '-') { \
47             while ( sql[0] != 0 && sql[0] != '\n' ) sql++; \
48             continue; \
49             } \
50             }
51              
52             bool
53 2284           _starts_with_begin(const char *sql) {
54 2284           return (
55 2284 50         ((sql[0] == 'B' || sql[0] == 'b') &&
    50          
56 25 0         (sql[1] == 'E' || sql[1] == 'e') &&
    50          
57 25 0         (sql[2] == 'G' || sql[2] == 'g') &&
    50          
58 25 0         (sql[3] == 'I' || sql[3] == 'i') &&
    50          
59 0 0         (sql[4] == 'N' || sql[4] == 'n')
60             ) || (
61 2259 100         (sql[0] == 'S' || sql[0] == 's') &&
    100          
62 958 50         (sql[1] == 'A' || sql[1] == 'a') &&
    50          
63 13 0         (sql[2] == 'V' || sql[2] == 'v') &&
    50          
64 13 0         (sql[3] == 'E' || sql[3] == 'e') &&
    50          
65 13 0         (sql[4] == 'P' || sql[4] == 'p') &&
    50          
66 13 0         (sql[5] == 'O' || sql[5] == 'o') &&
    50          
67 13 0         (sql[6] == 'I' || sql[6] == 'i') &&
    50          
68 13 0         (sql[7] == 'N' || sql[7] == 'n') &&
    50          
69 0 0         (sql[8] == 'T' || sql[8] == 't')
70             )
71 6827 100         ) ? TRUE : FALSE;
    100          
72             }
73              
74             /* adopted from sqlite3.c */
75              
76             #define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
77             #define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
78              
79 168           static int compare2pow63(const char *zNum) {
80 168           int c = 0;
81             int i;
82             /* 012345678901234567 */
83 168           const char *pow63 = "922337203685477580";
84 1152 100         for(i = 0; c == 0 && i < 18; i++){
    100          
85 984           c = (zNum[i] - pow63[i]) * 10;
86             }
87 168 100         if(c == 0){
88 48           c = zNum[18] - '8';
89             }
90 168           return c;
91             }
92              
93 1002           int _sqlite_atoi64(const char *zNum, sqlite3_int64 *pNum) {
94 1002           sqlite3_uint64 u = 0;
95 1002           int neg = 0;
96             int i;
97 1002           int c = 0;
98             const char *zStart;
99 1002           const char *zEnd = zNum + strlen(zNum);
100 1003 100         while(zNum < zEnd && _isspace(*zNum)) zNum++;
    100          
    50          
    50          
    50          
    50          
    50          
101 1002 100         if (zNum < zEnd) {
102 784 100         if (*zNum == '-') {
103 188           neg = 1;
104 188           zNum++;
105 596 50         } else if (*zNum == '+') {
106 0           zNum++;
107             }
108             }
109 1002           zStart = zNum;
110 1064 100         while(zNum < zEnd && zNum[0] == '0') zNum++;
    100          
111 6322 100         for(i = 0; &zNum[i] < zEnd && (c = zNum[i]) >= '0' && c <= '9'; i++) {
    100          
    100          
112 5320           u = u * 10 + c - '0';
113             }
114 1002 100         if (u > LARGEST_INT64) {
115 12 50         *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
116 990 100         } else if (neg) {
117 176           *pNum = -(sqlite3_int64)u;
118             } else {
119 814           *pNum = (sqlite3_int64)u;
120             }
121 1002 100         if ((c != 0 && &zNum[i] < zEnd) || (i == 0 && zStart == zNum) || i > 19) {
    100          
    100          
    100          
    50          
122 262           return 1;
123 740 100         } else if (i < 19) {
124 572           return 0;
125             } else {
126 168           c = compare2pow63(zNum);
127 168 100         if (c < 0) {
128 156           return 0;
129 12 50         } else if (c > 0) {
130 0           return 1;
131             } else {
132 12 50         return neg ? 0 : 2;
133             }
134             }
135             }
136              
137             static void
138 0           _sqlite_trace(pTHX_ char *file, int line, SV *h, imp_xxh_t *imp_xxh, const char *what)
139             {
140 0           PerlIO_printf(
141 0           DBIc_LOGPIO(imp_xxh),
142             "sqlite trace: %s at %s line %d\n", what, file, line
143             );
144 0           }
145              
146             static void
147 208           _sqlite_error(pTHX_ char *file, int line, SV *h, int rc, const char *what)
148             {
149 208           D_imp_xxh(h);
150              
151 208           DBIh_SET_ERR_CHAR(h, imp_xxh, Nullch, rc, what, Nullch, Nullch);
152              
153             /* #7753: DBD::SQLite error shouldn't include extraneous info */
154             /* sv_catpvf(errstr, "(%d) at %s line %d", rc, file, line); */
155 208 50         if ( DBIc_TRACE_LEVEL(imp_xxh) >= 3 ) {
156 0           PerlIO_printf(
157 0           DBIc_LOGPIO(imp_xxh),
158             "sqlite error %d recorded: %s at %s line %d\n",
159             rc, what, file, line
160             );
161             }
162 208           }
163              
164             int
165 3033           _sqlite_exec(pTHX_ SV *h, sqlite3 *db, const char *sql)
166             {
167             int rc;
168             char *errmsg;
169              
170 3033           rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
171 3033 100         if ( rc != SQLITE_OK ) {
172 44           sqlite_error(h, rc, errmsg);
173 44 50         if (errmsg) sqlite3_free(errmsg);
174             }
175 3033           return rc;
176             }
177              
178             int
179 317           _sqlite_open(pTHX_ SV *dbh, const char *dbname, sqlite3 **db, int flags, int extended)
180             {
181             int rc;
182 317 100         if (flags) {
183 20           rc = sqlite3_open_v2(dbname, db, flags, NULL);
184             } else {
185 297           rc = sqlite3_open(dbname, db);
186             }
187 317 100         if ( rc != SQLITE_OK ) {
188             #if SQLITE_VERSION_NUMBER >= 3006005
189 6 50         if (extended)
190 0           rc = sqlite3_extended_errcode(*db);
191             #endif
192 6           sqlite_error(dbh, rc, sqlite3_errmsg(*db));
193 6 50         if (*db) sqlite3_close(*db);
194             }
195 317           return rc;
196             }
197              
198             static int
199 2           sqlite_type_to_odbc_type(int type)
200             {
201 2           switch(type) {
202 0           case SQLITE_INTEGER: return SQL_INTEGER;
203 0           case SQLITE_FLOAT: return SQL_DOUBLE;
204 0           case SQLITE_TEXT: return SQL_VARCHAR;
205 0           case SQLITE_BLOB: return SQL_BLOB;
206 2           case SQLITE_NULL: return SQL_UNKNOWN_TYPE;
207 0           default: return SQL_UNKNOWN_TYPE;
208             }
209             }
210              
211             static int
212 1183           sqlite_type_from_odbc_type(int type)
213             {
214 1183           switch(type) {
215             case SQL_UNKNOWN_TYPE:
216 927           return SQLITE_NULL;
217             case SQL_BOOLEAN:
218             case SQL_INTEGER:
219             case SQL_SMALLINT:
220             case SQL_TINYINT:
221             case SQL_BIGINT:
222 207           return SQLITE_INTEGER;
223             case SQL_FLOAT:
224             case SQL_REAL:
225             case SQL_DOUBLE:
226 0           return SQLITE_FLOAT;
227             case SQL_BIT:
228             case SQL_BLOB:
229             case SQL_BINARY:
230             case SQL_VARBINARY:
231             case SQL_LONGVARBINARY:
232 37           return SQLITE_BLOB;
233             default:
234 12           return SQLITE_TEXT;
235             }
236             }
237              
238             void
239 116           init_cxt() {
240             dTHX;
241             MY_CXT_INIT;
242 116           MY_CXT.last_dbh_string_mode = DBD_SQLITE_STRING_MODE_PV;
243 116           }
244              
245             SV *
246 1157           stacked_sv_from_sqlite3_value(pTHX_ sqlite3_value *value, dbd_sqlite_string_mode_t string_mode)
247             {
248             STRLEN len;
249             sqlite_int64 iv;
250 1157           int type = sqlite3_value_type(value);
251             SV *sv;
252              
253 1157           switch(type) {
254             case SQLITE_INTEGER:
255 180           iv = sqlite3_value_int64(value);
256             if ( iv >= IV_MIN && iv <= IV_MAX ) {
257             /* ^^^ compile-time constant (= true) when IV == int64 */
258 180           return sv_2mortal(newSViv((IV)iv));
259             }
260             else if ( iv >= 0 && iv <= UV_MAX ) {
261             /* warn("integer overflow, cast to UV"); */
262             return sv_2mortal(newSVuv((UV)iv));
263             }
264             else {
265             /* warn("integer overflow, cast to NV"); */
266             return sv_2mortal(newSVnv((NV)iv));
267             }
268             case SQLITE_FLOAT:
269 6           return sv_2mortal(newSVnv(sqlite3_value_double(value)));
270             break;
271             case SQLITE_TEXT:
272 799           len = sqlite3_value_bytes(value);
273 799           sv = newSVpvn((const char *)sqlite3_value_text(value), len);
274 799 50         DBD_SQLITE_UTF8_DECODE_IF_NEEDED(sv, string_mode);
    50          
    0          
    100          
    50          
275 799           return sv_2mortal(sv);
276             case SQLITE_BLOB:
277 0           len = sqlite3_value_bytes(value);
278 0           return sv_2mortal(newSVpvn(sqlite3_value_blob(value), len));
279             default:
280 172           return &PL_sv_undef;
281             }
282             }
283              
284              
285              
286              
287              
288              
289             static void
290 622           sqlite_set_result(pTHX_ sqlite3_context *context, SV *result, int is_error)
291             {
292             STRLEN len;
293             char *s;
294             sqlite3_int64 iv;
295             AV *av;
296             SV *result2, *type;
297             SV **presult2, **ptype;
298              
299 622 100         if ( is_error ) {
300 6 50         s = SvPV(result, len);
301 6           sqlite3_result_error( context, s, len );
302 18           return;
303             }
304              
305             /* warn("result: %s\n", SvPV_nolen(result)); */
306 616 100         if ( !SvOK(result) ) {
    50          
    50          
307 146           sqlite3_result_null( context );
308 470 100         } else if( SvROK(result) && SvTYPE(SvRV(result)) == SVt_PVAV ) {
    50          
309 12           av = (AV*)SvRV(result);
310 12 50         if ( av_len(av) == 1 ) {
311 12           presult2 = av_fetch(av, 0, 0);
312 12           ptype = av_fetch(av, 1, 0);
313 12 50         result2 = presult2 ? *presult2 : &PL_sv_undef;
314 12 50         type = ptype ? *ptype : &PL_sv_undef;
315 12 50         if ( SvIOK(type) ) {
316 12 50         switch(sqlite_type_from_odbc_type(SvIV(type))) {
317             case SQLITE_INTEGER:
318 0 0         sqlite3_result_int64( context, SvIV(result2) );
319 0           return;
320             case SQLITE_FLOAT:
321 0 0         sqlite3_result_double( context, SvNV(result2) );
322 0           return;
323             case SQLITE_BLOB:
324 12 50         s = SvPV(result2, len);
325 12           sqlite3_result_blob( context, s, len, SQLITE_TRANSIENT );
326 12           return;
327             case SQLITE_TEXT:
328 0 0         s = SvPV(result2, len);
329 0           sqlite3_result_text( context, s, len, SQLITE_TRANSIENT );
330 0           return;
331             }
332             }
333             }
334 0           sqlite3_result_error( context, "unexpected arrayref", 19 );
335 458 50         } else if( SvIOK_UV(result) ) {
336             if ((UV)(sqlite3_int64)UV_MAX == UV_MAX)
337 0 0         sqlite3_result_int64( context, (sqlite3_int64)SvUV(result));
338             else {
339             s = SvPV(result, len);
340             sqlite3_result_text( context, s, len, SQLITE_TRANSIENT );
341             }
342 458 100         } else if ( !_sqlite_atoi64(SvPV(result, len), &iv) ) {
    100          
343 196           sqlite3_result_int64( context, iv );
344 262 100         } else if ( SvNOK(result) && ( sizeof(NV) == sizeof(double) || SvNVX(result) == (double) SvNVX(result) ) ) {
345 218 50         sqlite3_result_double( context, SvNV(result));
346             } else {
347 44 50         s = SvPV(result, len);
348 604           sqlite3_result_text( context, s, len, SQLITE_TRANSIENT );
349             }
350             }
351              
352             /*
353             * see also sqlite3IsNumber, sqlite3_int64 type definition,
354             * applyNumericAffinity, sqlite3Atoi64, etc from sqlite3.c
355             */
356             static int
357 224           sqlite_is_number(pTHX_ const char *v, int sql_type)
358             {
359             sqlite3_int64 iv;
360 224           const char *z = v;
361 224           const char *d = v;
362             int neg;
363 224           int digit = 0;
364 224           int precision = 0;
365 224           bool has_plus = FALSE;
366 224           bool maybe_int = TRUE;
367             char format[10];
368              
369 224 100         if (sql_type != SQLITE_NULL) {
370 208 100         while (*z == ' ') { z++; v++; d++; }
371             }
372              
373 224 100         if (*z == '-') { neg = 1; z++; d++; }
374 130 50         else if (*z == '+') { neg = 0; z++; d++; has_plus = TRUE; }
375 130           else { neg = 0; }
376 224 100         if (!isdigit(*z)) return 0;
377 2672 100         while (isdigit(*z)) { digit++; z++; }
378 222 50         if (digit > 19) maybe_int = FALSE; /* too large for i64 */
379 222 100         if (digit == 19) {
380             int c;
381             char tmp[22];
382 84           strncpy(tmp, d, z - d + 1);
383 84           c = memcmp(tmp, "922337203685477580", 18);
384 84 100         if (c == 0) {
385 24           c = tmp[18] - '7' - neg;
386             }
387 84 50         if (c > 0) maybe_int = FALSE;
388             }
389 222 100         if (*z == '.') {
390 6           maybe_int = FALSE;
391 6           z++;
392 6 50         if (!isdigit(*z)) return 0;
393 16 100         while (isdigit(*z)) { precision++; z++; }
394             }
395 222 100         if (*z == 'e' || *z == 'E') {
    50          
396 4           maybe_int = FALSE;
397 4           z++;
398 4 50         if (*z == '+' || *z == '-') { z++; }
    50          
399 4 50         if (!isdigit(*z)) return 0;
400 20 100         while (isdigit(*z)) { z++; }
401             }
402 222 50         if (*z && !isdigit(*z)) return 0;
    0          
403              
404 222 100         if (maybe_int && digit) {
    50          
405 214 50         if (!_sqlite_atoi64(v, &iv)) return 1;
406             }
407 8 100         if (sql_type != SQLITE_INTEGER) {
408             #ifdef USE_QUADMATH
409             sprintf(format, (has_plus ? "+%%.%dQf" : "%%.%dQf"), precision);
410             #else
411 6 50         sprintf(format, (has_plus ? "+%%.%df" : "%%.%df" ), precision);
412             #endif
413 6 100         if (strEQ(form(format, atof(v)), v)) return 2;
414             }
415 224           return 0;
416             }
417              
418             /*-----------------------------------------------------*
419             * DBD Methods
420             *-----------------------------------------------------*/
421              
422             void
423 116           sqlite_init(dbistate_t *dbistate)
424             {
425             dTHX;
426 116 50         DBISTATE_INIT; /* Initialize the DBI macros */
427 116           }
428              
429             int
430 110           sqlite_discon_all(SV *drh, imp_drh_t *imp_drh)
431             {
432             dTHX;
433 110           return FALSE; /* no way to do this */
434             }
435              
436             #define _croak_invalid_value(name, value) \
437             croak("Invalid value (%s) given for %s", value, name);
438              
439             /* Like SvUV but croaks on anything other than an unsigned int. */
440             static inline int
441 68           my_SvUV_strict(pTHX_ SV *input, const char* name)
442             {
443 68 50         if (SvUOK(input)) {
444 0 0         return SvUV(input);
445             }
446              
447 68 100         const char* pv = SvPVbyte_nolen(input);
448              
449             UV uv;
450 68           int numtype = grok_number(pv, strlen(pv), &uv);
451              
452             /* Anything else is invalid: */
453 68 50         if (numtype != IS_NUMBER_IN_UV) _croak_invalid_value(name, pv);
454              
455 68           return uv;
456             }
457              
458             static inline dbd_sqlite_string_mode_t
459 68           _extract_sqlite_string_mode_from_sv( pTHX_ SV* input )
460             {
461 68 50         if (SvOK(input)) {
    0          
    0          
462 68           UV val = my_SvUV_strict(aTHX_ input, "sqlite_string_mode");
463              
464 68 50         if (val >= _DBD_SQLITE_STRING_MODE_COUNT) {
465 0 0         _croak_invalid_value("sqlite_string_mode", SvPVbyte_nolen(input));
466             }
467              
468 68           return val;
469             }
470              
471 0           return DBD_SQLITE_STRING_MODE_PV;
472             }
473              
474             int
475 313           sqlite_db_login6(SV *dbh, imp_dbh_t *imp_dbh, char *dbname, char *user, char *pass, SV *attr)
476             {
477             dTHX;
478             int rc;
479 313           HV *hv = NULL;
480             SV **val;
481 313           int extended = 0;
482 313           int flag = 0;
483 313           dbd_sqlite_string_mode_t string_mode = DBD_SQLITE_STRING_MODE_PV;
484              
485 313 50         sqlite_trace(dbh, imp_dbh, 3, form("login '%s' (version %s)", dbname, sqlite3_version));
486              
487 313 50         if (SvROK(attr)) {
488 313           hv = (HV*)SvRV(attr);
489 313 50         if (hv_exists(hv, "sqlite_extended_result_codes", 28)) {
490 0           val = hv_fetch(hv, "sqlite_extended_result_codes", 28, 0);
491 0 0         extended = (val && SvOK(*val)) ? !(!SvTRUE(*val)) : 0;
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
492             }
493 313 100         if (hv_exists(hv, "ReadOnly", 8)) {
494 1           val = hv_fetch(hv, "ReadOnly", 8, 0);
495 1 50         if ((val && SvOK(*val)) ? SvIV(*val) : 0) {
    50          
    0          
    0          
    50          
    50          
496 1           flag |= SQLITE_OPEN_READONLY;
497             }
498             }
499 313 100         if (hv_exists(hv, "sqlite_open_flags", 17)) {
500 19           val = hv_fetch(hv, "sqlite_open_flags", 17, 0);
501 19 50         flag |= (val && SvOK(*val)) ? SvIV(*val) : 0;
    50          
    0          
    0          
    50          
502 19 100         if (flag & SQLITE_OPEN_READONLY) {
503 3           hv_stores(hv, "ReadOnly", newSViv(1));
504             }
505             }
506              
507             /* sqlite_string_mode should be detected earlier, to register default functions correctly */
508              
509 313           SV** string_mode_svp = hv_fetchs(hv, "sqlite_string_mode", 0);
510 313 100         if (string_mode_svp != NULL && SvOK(*string_mode_svp)) {
    50          
    0          
    0          
511 34           string_mode = _extract_sqlite_string_mode_from_sv(aTHX_ *string_mode_svp);
512              
513             /* Legacy alternatives to sqlite_string_mode: */
514 279 50         } else if (hv_exists(hv, "sqlite_unicode", 14)) {
515 0           val = hv_fetch(hv, "sqlite_unicode", 14, 0);
516 0 0         if ( (val && SvOK(*val)) ? SvIV(*val) : 0 ) {
    0          
    0          
    0          
    0          
    0          
517 0           string_mode = DBD_SQLITE_STRING_MODE_UNICODE_NAIVE;
518             }
519 279 50         } else if (hv_exists(hv, "unicode", 7)) {
520 0           val = hv_fetch(hv, "unicode", 7, 0);
521 0 0         if ( (val && SvOK(*val)) ? SvIV(*val) : 0 ) {
    0          
    0          
    0          
    0          
    0          
522 0           string_mode = DBD_SQLITE_STRING_MODE_UNICODE_NAIVE;
523             }
524             }
525             }
526 313           rc = sqlite_open2(dbname, &(imp_dbh->db), flag, extended);
527 313 100         if ( rc != SQLITE_OK ) {
528 6           return FALSE; /* -> undef in lib/DBD/SQLite.pm */
529             }
530 307           DBIc_IMPSET_on(imp_dbh);
531              
532 307           imp_dbh->string_mode = string_mode;
533 307           imp_dbh->functions = newAV();
534 307           imp_dbh->aggregates = newAV();
535 307           imp_dbh->collation_needed_callback = newSVsv( &PL_sv_undef );
536 307           imp_dbh->timeout = SQL_TIMEOUT;
537 307           imp_dbh->handle_binary_nulls = FALSE;
538 307           imp_dbh->allow_multiple_statements = FALSE;
539 307           imp_dbh->use_immediate_transaction = TRUE;
540 307           imp_dbh->see_if_its_a_number = FALSE;
541 307           imp_dbh->extended_result_codes = extended;
542 307           imp_dbh->stmt_list = NULL;
543 307           imp_dbh->began_transaction = FALSE;
544 307           imp_dbh->prefer_numeric_type = FALSE;
545              
546 307           sqlite3_busy_timeout(imp_dbh->db, SQL_TIMEOUT);
547              
548 307 50         if (SvROK(attr)) {
549 307           hv = (HV*)SvRV(attr);
550 307 100         if (hv_exists(hv, "sqlite_defensive", 16)) {
551 1           val = hv_fetch(hv, "sqlite_defensive", 16, 0);
552 1 50         if (val && SvIOK(*val)) {
    50          
553 1 50         sqlite3_db_config(imp_dbh->db, SQLITE_DBCONFIG_DEFENSIVE, (int)SvIV(*val), 0);
554             }
555             }
556             }
557              
558             #if 0
559             /*
560             ** As of 1.26_06 foreign keys support was enabled by default,
561             ** but with further discussion, we agreed to follow what
562             ** sqlite team does, i.e. wait until the team think it
563             ** reasonable to enable the support by default, as they have
564             ** larger users and will allocate enough time for people to
565             ** get used to the foreign keys. However, we should say it loud
566             ** that sometime in the (near?) future, this feature may break
567             ** your applications (and it actually broke applications).
568             ** Let everyone be prepared.
569             */
570             sqlite_exec(dbh, "PRAGMA foreign_keys = ON");
571             #endif
572              
573             #if 0
574             /*
575             ** Enable this to see if you (wrongly) expect an implicit order
576             ** of return values from a SELECT statement without ORDER BY.
577             */
578             sqlite_exec(dbh, "PRAGMA reverse_unordered_selects = ON");
579             #endif
580              
581 307 50         DBIc_ACTIVE_on(imp_dbh);
    50          
    50          
    50          
582              
583 307           return TRUE;
584             }
585              
586             int
587 2847           sqlite_db_do_sv(SV *dbh, imp_dbh_t *imp_dbh, SV *sv_statement)
588             {
589             dTHX;
590 2847           int rc = 0;
591             int i;
592             char *statement;
593              
594 2847 50         if (!DBIc_ACTIVE(imp_dbh)) {
595 0           sqlite_error(dbh, -2, "attempt to do on inactive database handle");
596 0           return -2; /* -> undef in SQLite.xsi */
597             }
598              
599             /* sqlite3_prepare wants an utf8-encoded SQL statement */
600 2847 100         DBD_SQLITE_PREP_SV_FOR_SQLITE(sv_statement, imp_dbh->string_mode);
    100          
601              
602 2847 50         statement = SvPV_nolen(sv_statement);
603              
604 2847 50         sqlite_trace(dbh, imp_dbh, 3, form("do statement: %s", statement));
605              
606             croak_if_db_is_null();
607              
608 2847 100         if (sqlite3_get_autocommit(imp_dbh->db)) {
609 607           const char *sql = statement;
610 780 100         _skip_whitespaces(sql);
    100          
    100          
    50          
    50          
    50          
    100          
    100          
    100          
    50          
    50          
    50          
    50          
    100          
    100          
    100          
    100          
    100          
    50          
    50          
    50          
    100          
    50          
611 607 100         if (_starts_with_begin(sql)) {
612 32 100         if (DBIc_is(imp_dbh, DBIcf_AutoCommit)) {
613 17 50         if (!DBIc_is(imp_dbh, DBIcf_BegunWork)) {
614 17           imp_dbh->began_transaction = TRUE;
615 17           DBIc_on(imp_dbh, DBIcf_BegunWork);
616 32           DBIc_off(imp_dbh, DBIcf_AutoCommit);
617             }
618             }
619             }
620 575 100         else if (!DBIc_is(imp_dbh, DBIcf_AutoCommit)) {
621 74 50         sqlite_trace(dbh, imp_dbh, 3, "BEGIN TRAN");
622 74 50         if (imp_dbh->use_immediate_transaction) {
623 74           rc = sqlite_exec(dbh, "BEGIN IMMEDIATE TRANSACTION");
624             } else {
625 0           rc = sqlite_exec(dbh, "BEGIN TRANSACTION");
626             }
627 74 100         if (rc != SQLITE_OK) {
628 5           return -2; /* -> undef in SQLite.xsi */
629             }
630             }
631             }
632              
633 2842           rc = sqlite_exec(dbh, statement);
634 2842 100         if (rc != SQLITE_OK) {
635 33           sqlite_error(dbh, rc, sqlite3_errmsg(imp_dbh->db));
636 33           return -2;
637             }
638              
639 2809 100         if (DBIc_is(imp_dbh, DBIcf_BegunWork) && sqlite3_get_autocommit(imp_dbh->db)) {
    100          
640 17 100         if (imp_dbh->began_transaction) {
641 16           DBIc_off(imp_dbh, DBIcf_BegunWork);
642 16           DBIc_on(imp_dbh, DBIcf_AutoCommit);
643             }
644             }
645              
646 2809           return sqlite3_changes(imp_dbh->db);
647             }
648              
649             int
650 82           sqlite_db_commit(SV *dbh, imp_dbh_t *imp_dbh)
651             {
652             dTHX;
653             int rc;
654              
655 82 100         if (!DBIc_ACTIVE(imp_dbh)) {
656 2           sqlite_error(dbh, -2, "attempt to commit on inactive database handle");
657 2           return FALSE;
658             }
659              
660 80 100         if (DBIc_is(imp_dbh, DBIcf_AutoCommit)) {
661             /* We don't need to warn, because the DBI layer will do it for us */
662 1           return TRUE;
663             }
664              
665 79 100         if (DBIc_is(imp_dbh, DBIcf_BegunWork)) {
666             /* XXX: for rt_52573
667             imp_dbh->began_transaction = FALSE;
668             */
669 32           DBIc_off(imp_dbh, DBIcf_BegunWork);
670 32           DBIc_on(imp_dbh, DBIcf_AutoCommit);
671             }
672              
673             croak_if_db_is_null();
674              
675 79 100         if (!sqlite3_get_autocommit(imp_dbh->db)) {
676 77 50         sqlite_trace(dbh, imp_dbh, 3, "COMMIT TRAN");
677              
678 77           rc = sqlite_exec(dbh, "COMMIT TRANSACTION");
679 77 100         if (rc != SQLITE_OK) {
680 4           return FALSE; /* -> &sv_no in SQLite.xsi */
681             }
682             }
683              
684 75           return TRUE;
685             }
686              
687             int
688 52           sqlite_db_rollback(SV *dbh, imp_dbh_t *imp_dbh)
689             {
690             dTHX;
691             int rc;
692              
693 52 100         if (!DBIc_ACTIVE(imp_dbh)) {
694 2           sqlite_error(dbh, -2, "attempt to rollback on inactive database handle");
695 2           return FALSE;
696             }
697              
698 50 100         if (DBIc_is(imp_dbh, DBIcf_BegunWork)) {
699             /* XXX: for rt_52573
700             imp_dbh->began_transaction = FALSE;
701             */
702 7           DBIc_off(imp_dbh, DBIcf_BegunWork);
703 7           DBIc_on(imp_dbh, DBIcf_AutoCommit);
704             }
705              
706             croak_if_db_is_null();
707              
708 50 100         if (!sqlite3_get_autocommit(imp_dbh->db)) {
709              
710 18 50         sqlite_trace(dbh, imp_dbh, 3, "ROLLBACK TRAN");
711              
712 18           rc = sqlite_exec(dbh, "ROLLBACK TRANSACTION");
713 18 50         if (rc != SQLITE_OK) {
714 0           return FALSE; /* -> &sv_no in SQLite.xsi */
715             }
716             }
717              
718 50           return TRUE;
719             }
720              
721             int
722 305           sqlite_db_disconnect(SV *dbh, imp_dbh_t *imp_dbh)
723             {
724             dTHX;
725             int rc;
726             stmt_list_s * s;
727              
728 305 100         if (DBIc_is(imp_dbh, DBIcf_AutoCommit) == FALSE) {
729 24           sqlite_db_rollback(dbh, imp_dbh);
730             }
731 305 50         DBIc_ACTIVE_off(imp_dbh);
    50          
    100          
    50          
    50          
732              
733             croak_if_db_is_null();
734              
735 305 50         sqlite_trace( dbh, imp_dbh, 1, "Closing DB" );
736 305           rc = sqlite3_close( imp_dbh->db );
737 305 50         sqlite_trace( dbh, imp_dbh, 1, form("rc = %d", rc) );
738 305 100         if ( SQLITE_BUSY == rc ) { /* We have unfinalized statements */
739             /* Only close the statements that were prepared by this module */
740 22 100         while ( (s = imp_dbh->stmt_list) ) {
741 11 50         sqlite_trace( dbh, imp_dbh, 1, form("Finalizing statement (%p)", s->stmt) );
742 11           sqlite3_finalize( s->stmt );
743 11           imp_dbh->stmt_list = s->prev;
744 11           sqlite3_free( s );
745             }
746 11           imp_dbh->stmt_list = NULL;
747 11 50         sqlite_trace( dbh, imp_dbh, 1, "Trying to close DB again" );
748 11           rc = sqlite3_close( imp_dbh->db );
749             }
750 305 50         if ( SQLITE_OK != rc ) {
751 0           sqlite_error(dbh, rc, sqlite3_errmsg(imp_dbh->db));
752             }
753             /* The list should be empty at this point, but if for some unforseen reason
754             it isn't, free remaining nodes here */
755 309 100         while( (s = imp_dbh->stmt_list) ) {
756 4           imp_dbh->stmt_list = s->prev;
757 4           sqlite3_free( s );
758             }
759 305           imp_dbh->db = NULL;
760              
761 305           av_undef(imp_dbh->functions);
762 305           SvREFCNT_dec(imp_dbh->functions);
763 305           imp_dbh->functions = (AV *)NULL;
764              
765 305           av_undef(imp_dbh->aggregates);
766 305           SvREFCNT_dec(imp_dbh->aggregates);
767 305           imp_dbh->aggregates = (AV *)NULL;
768              
769 305           sv_setsv(imp_dbh->collation_needed_callback, &PL_sv_undef);
770 305           SvREFCNT_dec(imp_dbh->collation_needed_callback);
771 305           imp_dbh->collation_needed_callback = (SV *)NULL;
772              
773 305           return TRUE;
774             }
775              
776             void
777 307           sqlite_db_destroy(SV *dbh, imp_dbh_t *imp_dbh)
778             {
779             dTHX;
780 307 50         if (DBIc_ACTIVE(imp_dbh)) {
781 0           sqlite_db_disconnect(dbh, imp_dbh);
782             }
783              
784 307           DBIc_IMPSET_off(imp_dbh);
785 307           }
786              
787             #define _warn_deprecated_if_possible(old, new) \
788             if (DBIc_has(imp_dbh, DBIcf_WARN)) \
789             warn("\"%s\" attribute will be deprecated. Use \"%s\" instead.", old, new);
790              
791             int
792 1633           sqlite_db_STORE_attrib(SV *dbh, imp_dbh_t *imp_dbh, SV *keysv, SV *valuesv)
793             {
794             dTHX;
795 1633 50         char *key = SvPV_nolen(keysv);
796             int rc;
797              
798             croak_if_db_is_null();
799              
800 1633 100         if (strEQ(key, "AutoCommit")) {
801 388 50         if (SvTRUE(valuesv)) {
    50          
    100          
    50          
    50          
    100          
    50          
    50          
    100          
    50          
    50          
    50          
    100          
    50          
    0          
    100          
802             /* commit tran? */
803 304 100         if ( DBIc_ACTIVE(imp_dbh) && (!DBIc_is(imp_dbh, DBIcf_AutoCommit)) && (!sqlite3_get_autocommit(imp_dbh->db)) ) {
    100          
    100          
804 2 50         sqlite_trace(dbh, imp_dbh, 3, "COMMIT TRAN");
805 2           rc = sqlite_exec(dbh, "COMMIT TRANSACTION");
806 2 50         if (rc != SQLITE_OK) {
807 0           return TRUE; /* XXX: is this correct? */
808             }
809             }
810             }
811 388 50         DBIc_set(imp_dbh, DBIcf_AutoCommit, SvTRUE(valuesv));
    50          
    0          
    100          
    50          
    50          
    100          
    50          
    50          
    100          
    50          
    100          
    50          
    50          
    100          
    50          
    0          
    100          
    0          
812 388           return TRUE;
813             }
814             #if SQLITE_VERSION_NUMBER >= 3007011
815 1245 100         if (strEQ(key, "ReadOnly")) {
816 4 50         if (SvTRUE(valuesv) && !sqlite3_db_readonly(imp_dbh->db, "main")) {
    50          
    50          
    0          
    0          
    50          
    0          
    0          
    0          
    0          
    50          
    50          
    50          
    0          
    0          
    50          
    100          
817 1           sqlite_error(dbh, 0, "ReadOnly is set but it's only advisory");
818             }
819 4           return FALSE;
820             }
821             #endif
822 1241 100         if (strEQ(key, "sqlite_allow_multiple_statements")) {
823 4 50         imp_dbh->allow_multiple_statements = !(! SvTRUE(valuesv));
    50          
    0          
    50          
    0          
    0          
    50          
    0          
    0          
    0          
    0          
    0          
    50          
    50          
    50          
    0          
    0          
    50          
    0          
824 4           return TRUE;
825             }
826 1237 100         if (strEQ(key, "sqlite_use_immediate_transaction")) {
827 1 50         imp_dbh->use_immediate_transaction = !(! SvTRUE(valuesv));
    50          
    0          
    50          
    0          
    0          
    50          
    0          
    0          
    0          
    0          
    0          
    50          
    50          
    50          
    50          
    0          
    50          
    0          
828 1           return TRUE;
829             }
830 1236 100         if (strEQ(key, "sqlite_see_if_its_a_number")) {
831 7 50         imp_dbh->see_if_its_a_number = !(! SvTRUE(valuesv));
    50          
    0          
    50          
    0          
    0          
    50          
    0          
    0          
    0          
    0          
    0          
    50          
    50          
    100          
    50          
    0          
    100          
    0          
832 7           return TRUE;
833             }
834 1229 50         if (strEQ(key, "sqlite_extended_result_codes")) {
835 0 0         imp_dbh->extended_result_codes = !(! SvTRUE(valuesv));
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
836 0           sqlite3_extended_result_codes(imp_dbh->db, imp_dbh->extended_result_codes);
837 0           return TRUE;
838             }
839 1229 100         if (strEQ(key, "sqlite_prefer_numeric_type")) {
840 1 50         imp_dbh->prefer_numeric_type = !(! SvTRUE(valuesv));
    50          
    0          
    50          
    0          
    0          
    50          
    0          
    0          
    0          
    0          
    0          
    50          
    50          
    50          
    0          
    0          
    50          
    0          
841 1           return TRUE;
842             }
843              
844 1228 100         if (strEQ(key, "sqlite_string_mode")) {
845 34           dbd_sqlite_string_mode_t string_mode = _extract_sqlite_string_mode_from_sv(aTHX_ valuesv);
846              
847             #if PERL_UNICODE_DOES_NOT_WORK_WELL
848             if (string_mode & DBD_SQLITE_STRING_MODE_UNICODE_ANY) {
849             sqlite_trace(dbh, imp_dbh, 3, form("Unicode support is disabled for this version of perl."));
850             string_mode = DBD_SQLITE_STRING_MODE_PV;
851             }
852             #endif
853              
854 34           imp_dbh->string_mode = string_mode;
855              
856 34           return TRUE;
857             }
858              
859 1194 50         if (strEQ(key, "sqlite_unicode")) {
860             /* it's too early to warn the deprecation of sqlite_unicode as it's widely used */
861             #if PERL_UNICODE_DOES_NOT_WORK_WELL
862             sqlite_trace(dbh, imp_dbh, 3, form("Unicode support is disabled for this version of perl."));
863             imp_dbh->string_mode = DBD_SQLITE_STRING_MODE_PV;
864             #else
865 0 0         imp_dbh->string_mode = SvTRUE(valuesv) ? DBD_SQLITE_STRING_MODE_UNICODE_NAIVE : DBD_SQLITE_STRING_MODE_PV;
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
866             #endif
867 0           return TRUE;
868             }
869              
870 1194 50         if (strEQ(key, "unicode")) {
871 0 0         _warn_deprecated_if_possible(key, "sqlite_string_mode");
872             #if PERL_UNICODE_DOES_NOT_WORK_WELL
873             sqlite_trace(dbh, imp_dbh, 3, form("Unicode support is disabled for this version of perl."));
874             imp_dbh->string_mode = DBD_SQLITE_STRING_MODE_PV;
875             #else
876 0 0         imp_dbh->string_mode = SvTRUE(valuesv) ? DBD_SQLITE_STRING_MODE_UNICODE_NAIVE : DBD_SQLITE_STRING_MODE_PV;
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
877             #endif
878 0           return TRUE;
879             }
880              
881 1194           return FALSE;
882             }
883              
884             SV *
885 3302           sqlite_db_FETCH_attrib(SV *dbh, imp_dbh_t *imp_dbh, SV *keysv)
886             {
887             dTHX;
888 3302 50         char *key = SvPV_nolen(keysv);
889              
890 3302 100         if (strEQ(key, "sqlite_version")) {
891 4           return sv_2mortal(newSVpv(sqlite3_version, 0));
892             }
893 3298 100         if (strEQ(key, "sqlite_allow_multiple_statements")) {
894 2982           return sv_2mortal(newSViv(imp_dbh->allow_multiple_statements ? 1 : 0));
895             }
896 316 100         if (strEQ(key, "sqlite_use_immediate_transaction")) {
897 2           return sv_2mortal(newSViv(imp_dbh->use_immediate_transaction ? 1 : 0));
898             }
899 314 100         if (strEQ(key, "sqlite_see_if_its_a_number")) {
900 4           return sv_2mortal(newSViv(imp_dbh->see_if_its_a_number ? 1 : 0));
901             }
902 310 50         if (strEQ(key, "sqlite_extended_result_codes")) {
903 0           return sv_2mortal(newSViv(imp_dbh->extended_result_codes ? 1 : 0));
904             }
905 310 50         if (strEQ(key, "sqlite_prefer_numeric_type")) {
906 0           return sv_2mortal(newSViv(imp_dbh->prefer_numeric_type ? 1 : 0));
907             }
908              
909 310 100         if (strEQ(key, "sqlite_string_mode")) {
910 6           return sv_2mortal(newSVuv(imp_dbh->string_mode));
911             }
912              
913 304 50         if (strEQ(key, "sqlite_unicode") || strEQ(key, "unicode")) {
    50          
914 0 0         _warn_deprecated_if_possible(key, "sqlite_string_mode");
915             #if PERL_UNICODE_DOES_NOT_WORK_WELL
916             sqlite_trace(dbh, imp_dbh, 3, "Unicode support is disabled for this version of perl.");
917             return sv_2mortal(newSViv(0));
918             #else
919 0           return sv_2mortal(newSViv(imp_dbh->string_mode == DBD_SQLITE_STRING_MODE_UNICODE_NAIVE ? 1 : 0));
920             #endif
921             }
922              
923 304           return NULL;
924             }
925              
926             SV *
927 212           sqlite_db_last_insert_id(SV *dbh, imp_dbh_t *imp_dbh, SV *catalog, SV *schema, SV *table, SV *field, SV *attr)
928             {
929             dTHX;
930              
931 212 100         if (!DBIc_ACTIVE(imp_dbh)) {
932 2           sqlite_error(dbh, -2, "attempt to get last inserted id on inactive database handle");
933 2           return FALSE;
934             }
935              
936             croak_if_db_is_null();
937              
938 210           return sv_2mortal(newSViv((IV)sqlite3_last_insert_rowid(imp_dbh->db)));
939             }
940              
941             int
942 1380           sqlite_st_prepare_sv(SV *sth, imp_sth_t *imp_sth, SV *sv_statement, SV *attribs)
943             {
944             dTHX;
945             dMY_CXT;
946 1380           int rc = 0;
947             const char *extra;
948             char *statement;
949             stmt_list_s * new_stmt;
950 1380           D_imp_dbh_from_sth;
951              
952 1380           MY_CXT.last_dbh_string_mode = imp_dbh->string_mode;
953              
954 1380 100         if (!DBIc_ACTIVE(imp_dbh)) {
955 4           sqlite_error(sth, -2, "attempt to prepare on inactive database handle");
956 4           return FALSE; /* -> undef in lib/DBD/SQLite.pm */
957             }
958              
959             /* sqlite3_prepare wants an utf8-encoded SQL statement */
960 1376 100         DBD_SQLITE_PREP_SV_FOR_SQLITE(sv_statement, imp_dbh->string_mode);
    100          
961              
962 1376 50         statement = SvPV_nolen(sv_statement);
963              
964             #if 0
965             if (*statement == '\0') {
966             sqlite_error(sth, -2, "attempt to prepare empty statement");
967             return FALSE; /* -> undef in lib/DBD/SQLite.pm */
968             }
969             #endif
970              
971 1376 50         sqlite_trace(sth, imp_sth, 3, form("prepare statement: %s", statement));
972 1376           imp_sth->nrow = -1;
973 1376           imp_sth->retval = SQLITE_OK;
974 1376           imp_sth->params = newAV();
975 1376           imp_sth->col_types = newAV();
976              
977             croak_if_db_is_null();
978              
979             /* COMPAT: sqlite3_prepare_v2 is only available for 3003009 or newer */
980 1376           rc = sqlite3_prepare_v2(imp_dbh->db, statement, -1, &(imp_sth->stmt), &extra);
981 1376 100         if (rc != SQLITE_OK) {
982 17           sqlite_error(sth, rc, sqlite3_errmsg(imp_dbh->db));
983 17 50         if (imp_sth->stmt) {
984 0           rc = sqlite3_finalize(imp_sth->stmt);
985 0           imp_sth->stmt = NULL;
986 0 0         if (rc != SQLITE_OK) {
987 0           sqlite_error(sth, rc, sqlite3_errmsg(imp_dbh->db));
988             }
989             }
990 17           return FALSE; /* -> undef in lib/DBD/SQLite.pm */
991             }
992 1359 100         if (imp_dbh->allow_multiple_statements) {
993 20           imp_sth->unprepared_statements = savepv(extra);
994             }
995             else {
996 1339 50         if (imp_dbh->allow_multiple_statements)
997 0           Safefree(imp_sth->unprepared_statements);
998 1339           imp_sth->unprepared_statements = NULL;
999             }
1000             /* Add the statement to the front of the list to keep track of
1001             statements that might need to be finalized later on disconnect */
1002 1359           new_stmt = (stmt_list_s *) sqlite3_malloc( sizeof(stmt_list_s) );
1003 1359           new_stmt->stmt = imp_sth->stmt;
1004 1359           new_stmt->prev = imp_dbh->stmt_list;
1005 1359           imp_dbh->stmt_list = new_stmt;
1006              
1007 1359           DBIc_NUM_PARAMS(imp_sth) = sqlite3_bind_parameter_count(imp_sth->stmt);
1008 1359           DBIc_NUM_FIELDS(imp_sth) = sqlite3_column_count(imp_sth->stmt);
1009 1359           DBIc_IMPSET_on(imp_sth);
1010              
1011 1380           return TRUE;
1012             }
1013              
1014             int
1015 121           sqlite_st_rows(SV *sth, imp_sth_t *imp_sth)
1016             {
1017 121           return imp_sth->nrow;
1018             }
1019              
1020             int
1021 1740           sqlite_st_execute(SV *sth, imp_sth_t *imp_sth)
1022             {
1023             dTHX;
1024 1740           D_imp_dbh_from_sth;
1025 1740           int rc = 0;
1026 1740           int num_params = DBIc_NUM_PARAMS(imp_sth);
1027             int i;
1028             sqlite3_int64 iv;
1029              
1030 1740 100         if (!DBIc_ACTIVE(imp_dbh)) {
1031 1           sqlite_error(sth, -2, "attempt to execute on inactive database handle");
1032 1           return -2; /* -> undef in SQLite.xsi */
1033             }
1034              
1035 1739 100         if (!imp_sth->stmt) return 0;
1036              
1037             croak_if_db_is_null();
1038             croak_if_stmt_is_null();
1039              
1040             /* COMPAT: sqlite3_sql is only available for 3006000 or newer */
1041 1735 50         sqlite_trace(sth, imp_sth, 3, form("executing %s", sqlite3_sql(imp_sth->stmt)));
1042              
1043 1735 100         if (DBIc_ACTIVE(imp_sth)) {
1044 4 50         sqlite_trace(sth, imp_sth, 3, "execute still active, reset");
1045 4           imp_sth->retval = sqlite3_reset(imp_sth->stmt);
1046 4 50         if (imp_sth->retval != SQLITE_OK) {
1047 0           sqlite_error(sth, imp_sth->retval, sqlite3_errmsg(imp_dbh->db));
1048 0           return -2; /* -> undef in SQLite.xsi */
1049             }
1050             }
1051              
1052 2894 100         for (i = 0; i < num_params; i++) {
1053 1159           SV **pvalue = av_fetch(imp_sth->params, 2*i, 0);
1054 1159           SV **sql_type_sv = av_fetch(imp_sth->params, 2*i+1, 0);
1055 1159 50         SV *value = pvalue ? *pvalue : &PL_sv_undef;
1056 1159 100         int sql_type = sqlite_type_from_odbc_type(sql_type_sv ? SvIV(*sql_type_sv) : SQL_UNKNOWN_TYPE);
    50          
1057              
1058 1159 50         sqlite_trace(sth, imp_sth, 4, form("bind %d type %d as %s", i, sql_type, SvPV_nolen_undef_ok(value)));
    0          
    0          
    0          
    0          
1059              
1060 1159 100         if (!SvOK(value)) {
    50          
    50          
1061 14 50         sqlite_trace(sth, imp_sth, 5, "binding null");
1062 14           rc = sqlite3_bind_null(imp_sth->stmt, i+1);
1063             }
1064 1145 100         else if (sql_type == SQLITE_BLOB) {
1065             STRLEN len;
1066 12 100         char * data = SvPVbyte(value, len);
1067 12           rc = sqlite3_bind_blob(imp_sth->stmt, i+1, data, len, SQLITE_TRANSIENT);
1068             }
1069             else {
1070             STRLEN len;
1071             const char *data;
1072 1133           int numtype = 0;
1073              
1074 1133 100         if (imp_dbh->string_mode & DBD_SQLITE_STRING_MODE_UNICODE_ANY) {
1075 92 100         data = SvPVutf8(value, len);
1076             }
1077 1041 100         else if (imp_dbh->string_mode == DBD_SQLITE_STRING_MODE_BYTES) {
1078 62 100         data = SvPVbyte(value, len);
1079             }
1080             else {
1081 979 100         data = SvPV(value, len);
1082             }
1083              
1084             /*
1085             * XXX: For backward compatibility, it'd be better to
1086             * accept a value like " 4" as an integer for an integer
1087             * type column (see t/19_bindparam.t), at least when
1088             * we explicitly specify its type. However, we should
1089             * keep spaces when we just guess.
1090             *
1091             * see_if_its_a_number should be ignored if an explicit
1092             * SQL type is set via bind_param().
1093             */
1094 1133 100         if (sql_type == SQLITE_NULL && imp_dbh->see_if_its_a_number) {
    100          
1095 17           numtype = sqlite_is_number(aTHX_ data, SQLITE_NULL);
1096             }
1097 1116 100         else if (sql_type == SQLITE_INTEGER || sql_type == SQLITE_FLOAT) {
    50          
1098 207           numtype = sqlite_is_number(aTHX_ data, sql_type);
1099             }
1100              
1101 1133 100         if (numtype == 1 && !_sqlite_atoi64(data, &iv)) {
    50          
1102 214           rc = sqlite3_bind_int64(imp_sth->stmt, i+1, iv);
1103             }
1104 919 100         else if (numtype == 2 && sql_type != SQLITE_INTEGER) {
    50          
1105 2           rc = sqlite3_bind_double(imp_sth->stmt, i+1, atof(data));
1106             }
1107             else {
1108 917 100         if (sql_type == SQLITE_INTEGER || sql_type == SQLITE_FLOAT) {
    50          
1109             /*
1110             * die on datatype mismatch did more harm than good
1111             * especially when DBIC heavily depends on this
1112             * explicit type specification
1113             */
1114 4 50         if (DBIc_has(imp_dbh, DBIcf_PrintWarn))
1115 0 0         warn(
    0          
1116             "datatype mismatch: bind param (%d) %s as %s",
1117 0 0         i, SvPV_nolen_undef_ok(value),
    0          
    0          
1118             (sql_type == SQLITE_INTEGER ? "integer" : "float")
1119             );
1120             }
1121 1133           rc = sqlite3_bind_text(imp_sth->stmt, i+1, data, len, SQLITE_TRANSIENT);
1122             }
1123             }
1124              
1125 1159 50         if (rc != SQLITE_OK) {
1126 0           sqlite_error(sth, rc, sqlite3_errmsg(imp_dbh->db));
1127 0           return -4; /* -> undef in SQLite.xsi */
1128             }
1129             }
1130              
1131 1735 100         if (sqlite3_get_autocommit(imp_dbh->db)) {
1132             /* COMPAT: sqlite3_sql is only available for 3006000 or newer */
1133 1677           const char *sql = sqlite3_sql(imp_sth->stmt);
1134 2033 100         _skip_whitespaces(sql);
    100          
    50          
    0          
    0          
    0          
    100          
    100          
    100          
    50          
    50          
    50          
    0          
    0          
    0          
    100          
    100          
    100          
    50          
    50          
    50          
    50          
    0          
1135 1677 100         if (_starts_with_begin(sql)) {
1136 6 50         if (DBIc_is(imp_dbh, DBIcf_AutoCommit)) {
1137 6 50         if (!DBIc_is(imp_dbh, DBIcf_BegunWork)) {
1138 6           imp_dbh->began_transaction = TRUE;
1139             }
1140 6           DBIc_on(imp_dbh, DBIcf_BegunWork);
1141 6           DBIc_off(imp_dbh, DBIcf_AutoCommit);
1142             }
1143             }
1144 1671 100         else if (!DBIc_is(imp_dbh, DBIcf_AutoCommit)) {
1145 20 50         sqlite_trace(sth, imp_sth, 3, "BEGIN TRAN");
1146 20 50         if (imp_dbh->use_immediate_transaction) {
1147 20           rc = sqlite_exec(sth, "BEGIN IMMEDIATE TRANSACTION");
1148             } else {
1149 0           rc = sqlite_exec(sth, "BEGIN TRANSACTION");
1150             }
1151 20 100         if (rc != SQLITE_OK) {
1152 2           return -2; /* -> undef in SQLite.xsi */
1153             }
1154             }
1155             }
1156              
1157 1733           imp_sth->nrow = 0;
1158              
1159 1733 50         sqlite_trace(sth, imp_sth, 3, form("Execute returned %d cols", DBIc_NUM_FIELDS(imp_sth)));
1160 1733 100         if (DBIc_NUM_FIELDS(imp_sth) == 0) {
1161 582 100         while ((imp_sth->retval = sqlite3_step(imp_sth->stmt)) != SQLITE_DONE) {
1162 8 50         if (imp_sth->retval == SQLITE_ROW) {
1163 0           continue;
1164             }
1165 8           sqlite_error(sth, imp_sth->retval, sqlite3_errmsg(imp_dbh->db));
1166 8 50         if (sqlite3_reset(imp_sth->stmt) != SQLITE_OK) {
1167 8           sqlite_error(sth, imp_sth->retval, sqlite3_errmsg(imp_dbh->db));
1168             }
1169 8           return -5; /* -> undef in SQLite.xsi */
1170             }
1171              
1172             /* transaction ended with commit/rollback/release */
1173 574 100         if (DBIc_is(imp_dbh, DBIcf_BegunWork) && sqlite3_get_autocommit(imp_dbh->db)) {
    100          
1174 5 50         if (imp_dbh->began_transaction) {
1175 5           DBIc_off(imp_dbh, DBIcf_BegunWork);
1176 5           DBIc_on(imp_dbh, DBIcf_AutoCommit);
1177             }
1178             }
1179              
1180             /* warn("Finalize\n"); */
1181 574           sqlite3_reset(imp_sth->stmt);
1182 574           imp_sth->nrow = sqlite3_changes(imp_dbh->db);
1183             /* warn("Total changes: %d\n", sqlite3_total_changes(imp_dbh->db)); */
1184             /* warn("Nrow: %d\n", imp_sth->nrow); */
1185 574           return imp_sth->nrow;
1186             }
1187              
1188 1151           imp_sth->retval = sqlite3_step(imp_sth->stmt);
1189 1151 100         switch (imp_sth->retval) {
1190             case SQLITE_ROW:
1191             case SQLITE_DONE:
1192 1145 100         DBIc_ACTIVE_on(imp_sth);
    50          
    50          
    50          
1193 1145 50         sqlite_trace(sth, imp_sth, 5, form("exec ok - %d rows, %d cols", imp_sth->nrow, DBIc_NUM_FIELDS(imp_sth)));
1194 1145 100         if (DBIc_is(imp_dbh, DBIcf_AutoCommit) && !sqlite3_get_autocommit(imp_dbh->db)) {
    50          
1195             /* XXX: for rt_52573
1196             if (DBIc_is(imp_dbh, DBIcf_BegunWork)) {
1197             imp_dbh->began_transaction = TRUE;
1198             }
1199             */
1200 0           DBIc_on(imp_dbh, DBIcf_BegunWork);
1201 0           DBIc_off(imp_dbh, DBIcf_AutoCommit);
1202             }
1203 1145           return 0; /* -> '0E0' in SQLite.xsi */
1204             default:
1205 6           sqlite_error(sth, imp_sth->retval, sqlite3_errmsg(imp_dbh->db));
1206 6 50         if (sqlite3_reset(imp_sth->stmt) != SQLITE_OK) {
1207 6           sqlite_error(sth, imp_sth->retval, sqlite3_errmsg(imp_dbh->db));
1208             }
1209 1740           return -6; /* -> undef in SQLite.xsi */
1210             }
1211             }
1212              
1213             AV *
1214 2626           sqlite_st_fetch(SV *sth, imp_sth_t *imp_sth)
1215             {
1216             dTHX;
1217              
1218             AV *av;
1219 2626           D_imp_dbh_from_sth;
1220 2626           int numFields = DBIc_NUM_FIELDS(imp_sth);
1221 2626           int chopBlanks = DBIc_is(imp_sth, DBIcf_ChopBlanks);
1222             int i;
1223             sqlite3_int64 iv;
1224              
1225 2626 50         if (!DBIc_ACTIVE(imp_dbh)) {
1226 0           sqlite_error(sth, -2, "attempt to fetch on inactive database handle");
1227 0           return FALSE;
1228             }
1229              
1230             croak_if_db_is_null();
1231             croak_if_stmt_is_null();
1232              
1233 2626 50         sqlite_trace(sth, imp_sth, 6, form("numFields == %d, nrow == %d", numFields, imp_sth->nrow));
1234              
1235 2626 100         if (!DBIc_ACTIVE(imp_sth)) {
1236 2           return Nullav;
1237             }
1238              
1239 2624 100         if (imp_sth->retval == SQLITE_DONE) {
1240 752           sqlite_st_finish(sth, imp_sth);
1241 752           return Nullav;
1242             }
1243              
1244 1872 50         if (imp_sth->retval != SQLITE_ROW) {
1245             /* error */
1246 0           sqlite_error(sth, imp_sth->retval, sqlite3_errmsg(imp_dbh->db));
1247 0           sqlite_st_finish(sth, imp_sth);
1248 0           return Nullav; /* -> undef in SQLite.xsi */
1249             }
1250              
1251 1872           imp_sth->nrow++;
1252              
1253 1872           av = DBIc_DBISTATE((imp_xxh_t *)imp_sth)->get_fbav(imp_sth);
1254 5704 100         for (i = 0; i < numFields; i++) {
1255             int len;
1256             char * val;
1257 3832           int col_type = sqlite3_column_type(imp_sth->stmt, i);
1258 3832           SV **sql_type = av_fetch(imp_sth->col_types, i, 0);
1259 3832 100         if (sql_type && SvOK(*sql_type)) {
    50          
    0          
    0          
1260 1374 50         if (SvIV(*sql_type)) {
    100          
1261 12 50         col_type = sqlite_type_from_odbc_type(SvIV(*sql_type));
1262             }
1263             }
1264 3832           switch(col_type) {
1265             case SQLITE_INTEGER:
1266 1429 50         sqlite_trace(sth, imp_sth, 5, form("fetch column %d as integer", i));
1267 1429           iv = sqlite3_column_int64(imp_sth->stmt, i);
1268             if ( iv >= IV_MIN && iv <= IV_MAX ) {
1269 1429           sv_setiv(AvARRAY(av)[i], (IV)iv);
1270             }
1271             else {
1272             val = (char*)sqlite3_column_text(imp_sth->stmt, i);
1273             sv_setpv(AvARRAY(av)[i], val);
1274             SvUTF8_off(AvARRAY(av)[i]);
1275             }
1276 1429           break;
1277             case SQLITE_FLOAT:
1278             /* fetching as float may lose precision info in the perl world */
1279 8 50         sqlite_trace(sth, imp_sth, 5, form("fetch column %d as float", i));
1280 8           sv_setnv(AvARRAY(av)[i], sqlite3_column_double(imp_sth->stmt, i));
1281 8           break;
1282             case SQLITE_TEXT:
1283 2067 50         sqlite_trace(sth, imp_sth, 5, form("fetch column %d as text", i));
1284 2067           val = (char*)sqlite3_column_text(imp_sth->stmt, i);
1285              
1286 2067           len = sqlite3_column_bytes(imp_sth->stmt, i);
1287 2067 100         if (chopBlanks) {
1288 5 100         while((len > 0) && (val[len-1] == ' ')) {
    100          
1289 2           len--;
1290             }
1291             }
1292 2067           sv_setpvn(AvARRAY(av)[i], val, len);
1293              
1294 2067 100         DBD_SQLITE_UTF8_DECODE_IF_NEEDED(AvARRAY(av)[i], imp_dbh->string_mode);
    50          
    0          
    100          
    50          
1295              
1296 2067           break;
1297             case SQLITE_BLOB:
1298 34 50         sqlite_trace(sth, imp_sth, 5, form("fetch column %d as blob", i));
1299 34           len = sqlite3_column_bytes(imp_sth->stmt, i);
1300 34           val = (char*)sqlite3_column_blob(imp_sth->stmt, i);
1301 34 100         sv_setpvn(AvARRAY(av)[i], len ? val : "", len);
1302 34           SvUTF8_off(AvARRAY(av)[i]);
1303 34           break;
1304             default:
1305 294 50         sqlite_trace(sth, imp_sth, 5, form("fetch column %d as default", i));
1306 294           sv_setsv(AvARRAY(av)[i], &PL_sv_undef);
1307 294           SvUTF8_off(AvARRAY(av)[i]);
1308 294           break;
1309             }
1310 3832 50         SvSETMAGIC(AvARRAY(av)[i]);
1311             }
1312              
1313 1872           imp_sth->retval = sqlite3_step(imp_sth->stmt);
1314              
1315 1872           return av;
1316             }
1317              
1318             int
1319 1144           sqlite_st_finish3(SV *sth, imp_sth_t *imp_sth, int is_destroy)
1320             {
1321             dTHX;
1322              
1323 1144           D_imp_dbh_from_sth;
1324              
1325             croak_if_db_is_null();
1326             croak_if_stmt_is_null();
1327              
1328             /* warn("finish statement\n"); */
1329 1144 100         if (!DBIc_ACTIVE(imp_sth))
1330 4           return TRUE;
1331              
1332 1140 50         DBIc_ACTIVE_off(imp_sth);
    50          
    50          
    50          
    50          
1333              
1334 1140           av_clear(imp_sth->col_types);
1335              
1336 1140 50         if (!DBIc_ACTIVE(imp_dbh)) /* no longer connected */
1337 0           return TRUE;
1338              
1339 1140 100         if (is_destroy) {
1340 114           return TRUE;
1341             }
1342              
1343 1026 50         if ((imp_sth->retval = sqlite3_reset(imp_sth->stmt)) != SQLITE_OK) {
1344 0           sqlite_error(sth, imp_sth->retval, sqlite3_errmsg(imp_dbh->db));
1345 0           return FALSE; /* -> &sv_no (or void) in SQLite.xsi */
1346             }
1347              
1348 1026           return TRUE;
1349             }
1350              
1351             int
1352 752           sqlite_st_finish(SV *sth, imp_sth_t *imp_sth)
1353             {
1354 752           return sqlite_st_finish3(sth, imp_sth, 0);
1355             }
1356              
1357             void
1358 1359           sqlite_st_destroy(SV *sth, imp_sth_t *imp_sth)
1359             {
1360             dTHX;
1361             int rc;
1362             stmt_list_s * i;
1363             stmt_list_s * temp;
1364              
1365 1359           D_imp_dbh_from_sth;
1366              
1367 1359 50         DBIc_ACTIVE_off(imp_sth);
    0          
    0          
    0          
    0          
1368 1359 100         if (DBIc_ACTIVE(imp_dbh)) {
1369 1348 100         if (imp_sth->stmt) {
1370             /* COMPAT: sqlite3_sql is only available for 3006000 or newer */
1371 1344 50         sqlite_trace(sth, imp_sth, 4, form("destroy statement: %s", sqlite3_sql(imp_sth->stmt)));
1372              
1373             croak_if_db_is_null();
1374             croak_if_stmt_is_null();
1375              
1376             /* finalize sth when active connection */
1377 1344 50         sqlite_trace( sth, imp_sth, 1, form("Finalizing statement: %p", imp_sth->stmt) );
1378 1344           rc = sqlite3_finalize(imp_sth->stmt);
1379 1344 50         if (rc != SQLITE_OK) {
1380 0           sqlite_error(sth, rc, sqlite3_errmsg(imp_dbh->db));
1381             }
1382              
1383             /* find the statement in the statement list and delete it */
1384 1344           i = imp_dbh->stmt_list;
1385 1344           temp = i;
1386 1370 50         while( i ) {
1387 1370 100         if ( i->stmt == imp_sth->stmt ) {
1388 1344 100         if ( temp != i ) temp->prev = i->prev;
1389 1344 100         if ( i == imp_dbh->stmt_list ) imp_dbh->stmt_list = i->prev;
1390 1344 50         sqlite_trace( sth, imp_sth, 1, form("Removing statement from list: %p", imp_sth->stmt) );
1391 1344           sqlite3_free( i );
1392 1344           break;
1393             }
1394             else {
1395 26           temp = i;
1396 26           i = i->prev;
1397             }
1398             }
1399 1344           imp_sth->stmt = NULL;
1400             }
1401             }
1402 1359 100         if (imp_dbh->allow_multiple_statements)
1403 20           Safefree(imp_sth->unprepared_statements);
1404 1359           SvREFCNT_dec((SV*)imp_sth->params);
1405 1359           SvREFCNT_dec((SV*)imp_sth->col_types);
1406 1359           DBIc_IMPSET_off(imp_sth);
1407 1359           }
1408              
1409             int
1410 0           sqlite_st_blob_read(SV *sth, imp_sth_t *imp_sth,
1411             int field, long offset, long len, SV *destrv, long destoffset)
1412             {
1413 0           return 0;
1414             }
1415              
1416             int
1417 5           sqlite_st_STORE_attrib(SV *sth, imp_sth_t *imp_sth, SV *keysv, SV *valuesv)
1418             {
1419             dTHX;
1420             /* char *key = SvPV_nolen(keysv); */
1421 5           return FALSE;
1422             }
1423              
1424             SV *
1425 962           sqlite_st_FETCH_attrib(SV *sth, imp_sth_t *imp_sth, SV *keysv)
1426             {
1427             dTHX;
1428 962           D_imp_dbh_from_sth;
1429 962 50         char *key = SvPV_nolen(keysv);
1430 962           SV *retsv = NULL;
1431             int i,n;
1432              
1433             croak_if_db_is_null();
1434             croak_if_stmt_is_null();
1435              
1436 962 100         if (!DBIc_ACTIVE(imp_dbh)) {
1437 1           sqlite_error(sth, -2, "attempt to fetch on inactive database handle");
1438 1           return FALSE;
1439             }
1440              
1441 961 100         if (strEQ(key, "sqlite_unprepared_statements")) {
1442 16           return sv_2mortal(newSVpv(imp_sth->unprepared_statements, 0));
1443             }
1444             /*
1445             if (!DBIc_ACTIVE(imp_sth)) {
1446             return NULL;
1447             }
1448             */
1449             /* warn("fetch: %s\n", key); */
1450              
1451 945           i = DBIc_NUM_FIELDS(imp_sth);
1452              
1453 945 100         if (strEQ(key, "NAME")) {
1454 528           AV *av = newAV();
1455             /* warn("Fetch NAME fields: %d\n", i); */
1456 528           av_extend(av, i);
1457 528           retsv = sv_2mortal(newRV_noinc((SV*)av));
1458 2743 100         for (n = 0; n < i; n++) {
1459             /* warn("Fetch col name %d\n", n); */
1460 2215           const char *fieldname = sqlite3_column_name(imp_sth->stmt, n);
1461 2215 50         if (fieldname) {
1462             /* warn("Name [%d]: %s\n", n, fieldname); */
1463             /* char *dot = instr(fieldname, "."); */
1464             /* if (dot) drop table name from field name */
1465             /* fieldname = ++dot; */
1466 2215           SV *sv_fieldname = newSVpv(fieldname, 0);
1467              
1468 2215 50         DBD_SQLITE_UTF8_DECODE_IF_NEEDED(sv_fieldname, imp_dbh->string_mode);
    50          
    0          
    100          
    50          
1469              
1470 2215           av_store(av, n, sv_fieldname);
1471             }
1472             }
1473             }
1474 417 50         else if (strEQ(key, "PRECISION")) {
1475 0           AV *av = newAV();
1476 0           retsv = sv_2mortal(newRV_noinc((SV*)av));
1477             }
1478 417 100         else if (strEQ(key, "TYPE")) {
1479 1           AV *av = newAV();
1480 1           av_extend(av, i);
1481 1           retsv = sv_2mortal(newRV_noinc((SV*)av));
1482 3 100         for (n = 0; n < i; n++) {
1483 2 50         if (imp_dbh->prefer_numeric_type) {
1484 2           int type = sqlite3_column_type(imp_sth->stmt, n);
1485             /* warn("got type: %d = %s\n", type, fieldtype); */
1486 2           type = sqlite_type_to_odbc_type(type);
1487 2           av_store(av, n, newSViv(type));
1488             } else {
1489 0           const char *fieldtype = sqlite3_column_decltype(imp_sth->stmt, n);
1490 0 0         if (fieldtype)
1491 0           av_store(av, n, newSVpv(fieldtype, 0));
1492             else
1493 0           av_store(av, n, newSVpv("VARCHAR", 0));
1494             }
1495             }
1496             }
1497 416 100         else if (strEQ(key, "NULLABLE")) {
1498 1           AV *av = newAV();
1499 1           av_extend(av, i);
1500 1           retsv = sv_2mortal(newRV_noinc((SV*)av));
1501             #if defined(SQLITE_ENABLE_COLUMN_METADATA)
1502 5 100         for (n = 0; n < i; n++) {
1503 4           const char *database = sqlite3_column_database_name(imp_sth->stmt, n);
1504 4           const char *tablename = sqlite3_column_table_name(imp_sth->stmt, n);
1505 4           const char *fieldname = sqlite3_column_name(imp_sth->stmt, n);
1506             const char *datatype, *collseq;
1507             int notnull, primary, autoinc;
1508 4           int rc = sqlite3_table_column_metadata(imp_dbh->db, database, tablename, fieldname, &datatype, &collseq, &notnull, &primary, &autoinc);
1509 4 50         if (rc != SQLITE_OK) {
1510 0           sqlite_error(sth, rc, sqlite3_errmsg(imp_dbh->db));
1511 0           av_store(av, n, newSViv(2)); /* SQL_NULLABLE_UNKNOWN */
1512             }
1513             else {
1514 4           av_store(av, n, newSViv(!notnull));
1515             }
1516             }
1517             #endif
1518             }
1519 415 50         else if (strEQ(key, "SCALE")) {
1520 0           AV *av = newAV();
1521 0           retsv = sv_2mortal(newRV_noinc((SV*)av));
1522             }
1523 415 100         else if (strEQ(key, "NUM_OF_FIELDS")) {
1524 84           retsv = sv_2mortal(newSViv(i));
1525             }
1526 331 100         else if (strEQ(key, "NUM_OF_PARAMS")) {
1527 128           retsv = sv_2mortal(newSViv(sqlite3_bind_parameter_count(imp_sth->stmt)));
1528             }
1529 203 100         else if (strEQ(key, "ParamValues")) {
1530 4           HV *hv = newHV();
1531 4           int num_params = DBIc_NUM_PARAMS(imp_sth);
1532 4 50         if (num_params) {
1533 8 100         for (n = 0; n < num_params; n++) {
1534 4           SV **pvalue = av_fetch(imp_sth->params, 2 * n, 0);
1535 4 100         SV *value = pvalue ? *pvalue : &PL_sv_undef;
1536 4           const char *pname = sqlite3_bind_parameter_name(imp_sth->stmt, n + 1);
1537 4 100         SV *sv_name = pname ? newSVpv(pname, 0) : newSViv(n + 1);
1538 4           hv_store_ent(hv, sv_name, newSVsv(value), 0);
1539             }
1540             }
1541 4           retsv = sv_2mortal(newRV_noinc((SV*)hv));
1542             }
1543              
1544 945           return retsv;
1545             }
1546              
1547             /* bind parameter
1548             * NB: We store the params instead of bind immediately because
1549             * we might need to re-create the imp_sth->stmt (see top of execute() function)
1550             * and so we can't lose these params
1551             */
1552             int
1553 1167           sqlite_bind_ph(SV *sth, imp_sth_t *imp_sth,
1554             SV *param, SV *value, IV sql_type, SV *attribs,
1555             int is_inout, IV maxlen)
1556             {
1557             dTHX;
1558             int pos;
1559              
1560             croak_if_stmt_is_null();
1561              
1562 1167 50         if (is_inout) {
1563 0           sqlite_error(sth, -2, "InOut bind params not implemented");
1564 0           return FALSE; /* -> &sv_no in SQLite.xsi */
1565             }
1566              
1567 1167 100         if (!looks_like_number(param)) {
1568             STRLEN len;
1569             char *paramstring;
1570 7 50         paramstring = SvPV(param, len);
1571 7 50         if(paramstring[len] == 0 && strlen(paramstring) == len) {
    50          
1572 7           pos = sqlite3_bind_parameter_index(imp_sth->stmt, paramstring);
1573 7 100         if (pos == 0) {
1574 1           sqlite_error(sth, -2, form("Unknown named parameter: %s", paramstring));
1575 1           return FALSE; /* -> &sv_no in SQLite.xsi */
1576             }
1577 6           pos = 2 * (pos - 1);
1578             }
1579             else {
1580 0           sqlite_error(sth, -2, "<param> could not be coerced to a C string");
1581 0           return FALSE; /* -> &sv_no in SQLite.xsi */
1582             }
1583             }
1584             else {
1585 1160 50         pos = 2 * (SvIV(param) - 1);
1586             }
1587 1166 50         sqlite_trace(sth, imp_sth, 3, form("bind into 0x%p: %"IVdf" => %s (%"IVdf") pos %d", imp_sth->params, SvIV(param), SvPV_nolen_undef_ok(value), sql_type, pos));
    0          
    0          
    0          
    0          
    0          
1588 1166           av_store(imp_sth->params, pos, newSVsv(value));
1589 1166 100         if (sql_type) {
1590 165           av_store(imp_sth->params, pos+1, newSViv(sql_type));
1591             }
1592              
1593 1166           return TRUE;
1594             }
1595              
1596             int
1597 450           sqlite_bind_col(SV *sth, imp_sth_t *imp_sth, SV *col, SV *ref, IV sql_type, SV *attribs)
1598             {
1599             dTHX;
1600              
1601             /* store the type */
1602 450 50         av_store(imp_sth->col_types, SvIV(col)-1, newSViv(sql_type));
1603              
1604             /* Allow default implementation to continue */
1605 450           return 1;
1606             }
1607              
1608             /*-----------------------------------------------------*
1609             * Driver Private Methods
1610             *-----------------------------------------------------*/
1611              
1612             AV *
1613 16           sqlite_compile_options()
1614             {
1615             dTHX;
1616 16           int i = 0;
1617             const char *option;
1618 16           AV *av = newAV();
1619              
1620             #if SQLITE_VERSION_NUMBER >= 3006023
1621             #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1622 704 100         while((option = sqlite3_compileoption_get(i++))) {
1623 688           av_push(av, newSVpv(option, 0));
1624             }
1625             #endif
1626             #endif
1627              
1628 16           return (AV*)sv_2mortal((SV*)av);
1629             }
1630              
1631             #define _stores_status(op, key) \
1632             if (sqlite3_status(op, &cur, &hi, reset) == SQLITE_OK) { \
1633             anon = newHV(); \
1634             hv_stores(anon, "current", newSViv(cur)); \
1635             hv_stores(anon, "highwater", newSViv(hi)); \
1636             hv_stores(hv, key, newRV_noinc((SV*)anon)); \
1637             }
1638              
1639             #define _stores_dbstatus(op, key) \
1640             if (sqlite3_db_status(imp_dbh->db, op, &cur, &hi, reset) == SQLITE_OK) { \
1641             anon = newHV(); \
1642             hv_stores(anon, "current", newSViv(cur)); \
1643             hv_stores(anon, "highwater", newSViv(hi)); \
1644             hv_stores(hv, key, newRV_noinc((SV*)anon)); \
1645             }
1646              
1647             #define _stores_ststatus(op, key) \
1648             hv_stores(hv, key, newSViv(sqlite3_stmt_status(imp_sth->stmt, op, reset)))
1649              
1650             HV *
1651 1           _sqlite_status(int reset)
1652             {
1653             dTHX;
1654             int cur, hi;
1655 1           HV *hv = newHV();
1656             HV *anon;
1657              
1658 1 50         _stores_status(SQLITE_STATUS_MEMORY_USED, "memory_used");
1659 1 50         _stores_status(SQLITE_STATUS_PAGECACHE_USED, "pagecache_used");
1660 1 50         _stores_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, "pagecache_overflow");
1661 1 50         _stores_status(SQLITE_STATUS_SCRATCH_USED, "scratch_used");
1662              
1663 1 50         _stores_status(SQLITE_STATUS_SCRATCH_OVERFLOW, "scratch_overflow");
1664              
1665 1 50         _stores_status(SQLITE_STATUS_MALLOC_SIZE, "malloc_size");
1666 1 50         _stores_status(SQLITE_STATUS_PARSER_STACK, "parser_stack");
1667 1 50         _stores_status(SQLITE_STATUS_PAGECACHE_SIZE, "pagecache_size");
1668 1 50         _stores_status(SQLITE_STATUS_SCRATCH_SIZE, "scratch_size");
1669             #if SQLITE_VERSION_NUMBER >= 3007001
1670 1 50         _stores_status(SQLITE_STATUS_MALLOC_COUNT, "malloc_count");
1671             #endif
1672 1 50         _stores_status(SQLITE_STATUS_SCRATCH_OVERFLOW, "scratch_overflow");
1673              
1674 1           return hv;
1675             }
1676              
1677             HV *
1678 2           _sqlite_db_status(pTHX_ SV* dbh, int reset)
1679             {
1680 2           D_imp_dbh(dbh);
1681             int cur, hi;
1682 2           HV *hv = newHV();
1683             HV *anon;
1684              
1685 2 50         _stores_dbstatus(SQLITE_DBSTATUS_LOOKASIDE_USED, "lookaside_used");
1686             #if SQLITE_VERSION_NUMBER >= 3007000
1687 2 50         _stores_dbstatus(SQLITE_DBSTATUS_CACHE_USED, "cache_used");
1688             #endif
1689             #if SQLITE_VERSION_NUMBER >= 3007001
1690 2 50         _stores_dbstatus(SQLITE_DBSTATUS_SCHEMA_USED, "schema_used");
1691 2 50         _stores_dbstatus(SQLITE_DBSTATUS_STMT_USED, "stmt_used");
1692             #endif
1693             #if SQLITE_VERSION_NUMBER >= 3007005
1694 2 50         _stores_dbstatus(SQLITE_DBSTATUS_LOOKASIDE_HIT, "lookaside_hit");
1695 2 50         _stores_dbstatus(SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, "lookaside_miss_size");
1696 2 50         _stores_dbstatus(SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, "lookaside_miss_full");
1697             #endif
1698             #if SQLITE_VERSION_NUMBER >= 3007009
1699 2 50         _stores_dbstatus(SQLITE_DBSTATUS_CACHE_HIT, "cache_hit");
1700 2 50         _stores_dbstatus(SQLITE_DBSTATUS_CACHE_MISS, "cache_miss");
1701             #endif
1702             #if SQLITE_VERSION_NUMBER >= 3007012
1703 2 50         _stores_dbstatus(SQLITE_DBSTATUS_CACHE_WRITE, "cache_write");
1704             #endif
1705              
1706 2           return hv;
1707             }
1708              
1709             HV *
1710 2           _sqlite_st_status(pTHX_ SV* sth, int reset)
1711             {
1712 2           D_imp_sth(sth);
1713 2           HV *hv = newHV();
1714              
1715             #if SQLITE_VERSION_NUMBER >= 3006004
1716 2           _stores_ststatus(SQLITE_STMTSTATUS_FULLSCAN_STEP, "fullscan_step");
1717 2           _stores_ststatus(SQLITE_STMTSTATUS_SORT, "sort");
1718             #endif
1719             #if SQLITE_VERSION_NUMBER >= 3007000
1720 2           _stores_ststatus(SQLITE_STMTSTATUS_AUTOINDEX, "autoindex");
1721             #endif
1722              
1723 2           return hv;
1724             }
1725              
1726             SV *
1727 11           sqlite_db_filename(pTHX_ SV *dbh)
1728             {
1729 11           D_imp_dbh(dbh);
1730 11           const char *filename = NULL;
1731              
1732 11 100         if (!imp_dbh->db) {
1733 2           return &PL_sv_undef;
1734             }
1735              
1736             croak_if_db_is_null();
1737              
1738             #if SQLITE_VERSION_NUMBER >= 3007010
1739 9           filename = sqlite3_db_filename(imp_dbh->db, "main");
1740             #endif
1741 9 50         return filename ? newSVpv(filename, 0) : &PL_sv_undef;
1742             }
1743              
1744             int
1745 26           sqlite_db_busy_timeout(pTHX_ SV *dbh, SV *timeout )
1746             {
1747 26           D_imp_dbh(dbh);
1748              
1749             croak_if_db_is_null();
1750              
1751 26 100         if (timeout && SvIOK(timeout)) {
    50          
1752 20 50         imp_dbh->timeout = SvIV(timeout);
1753 20 100         if (!DBIc_ACTIVE(imp_dbh)) {
1754 4           sqlite_error(dbh, -2, "attempt to set busy timeout on inactive database handle");
1755 4           return -2;
1756             }
1757 16           sqlite3_busy_timeout(imp_dbh->db, imp_dbh->timeout);
1758             }
1759 22           return imp_dbh->timeout;
1760             }
1761              
1762             static void
1763 556           sqlite_db_func_dispatcher(dbd_sqlite_string_mode_t string_mode, sqlite3_context *context, int argc, sqlite3_value **value)
1764             {
1765             dTHX;
1766 556           dSP;
1767             int count;
1768             int i;
1769             SV *func;
1770              
1771 556           func = sqlite3_user_data(context);
1772              
1773 556           ENTER;
1774 556           SAVETMPS;
1775              
1776 556 50         PUSHMARK(SP);
1777 1534 100         for ( i=0; i < argc; i++ ) {
1778 978 50         XPUSHs(stacked_sv_from_sqlite3_value(aTHX_ value[i], string_mode));
1779             }
1780 556           PUTBACK;
1781              
1782 556           count = call_sv(func, G_SCALAR|G_EVAL);
1783              
1784 556           SPAGAIN;
1785              
1786             /* Check for an error */
1787 556 50         if (SvTRUE(ERRSV) ) {
    50          
    50          
    50          
    0          
    0          
    50          
    50          
    0          
    0          
    0          
    0          
    50          
    50          
    50          
    50          
    50          
    100          
    50          
    50          
    0          
    0          
    100          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
1788 6 50         sqlite_set_result(aTHX_ context, ERRSV, 1);
1789 6           POPs;
1790 550 50         } else if ( count != 1 ) {
1791 0           SV *err = sv_2mortal(newSVpvf( "function should return 1 argument, got %d",
1792             count ));
1793              
1794 0           sqlite_set_result(aTHX_ context, err, 1);
1795             /* Clear the stack */
1796 0 0         for ( i=0; i < count; i++ ) {
1797 0           POPs;
1798             }
1799             } else {
1800 550           sqlite_set_result(aTHX_ context, POPs, 0 );
1801             }
1802              
1803 556           PUTBACK;
1804 556 50         FREETMPS;
1805 556           LEAVE;
1806 556           }
1807              
1808             static void
1809 0           sqlite_db_func_dispatcher_unicode_naive(sqlite3_context *context, int argc, sqlite3_value **value)
1810             {
1811 0           sqlite_db_func_dispatcher(DBD_SQLITE_STRING_MODE_UNICODE_NAIVE, context, argc, value);
1812 0           }
1813              
1814             static void
1815 0           sqlite_db_func_dispatcher_unicode_fallback(sqlite3_context *context, int argc, sqlite3_value **value)
1816             {
1817 0           sqlite_db_func_dispatcher(DBD_SQLITE_STRING_MODE_UNICODE_FALLBACK, context, argc, value);
1818 0           }
1819              
1820             static void
1821 204           sqlite_db_func_dispatcher_unicode_strict(sqlite3_context *context, int argc, sqlite3_value **value)
1822             {
1823 204           sqlite_db_func_dispatcher(DBD_SQLITE_STRING_MODE_UNICODE_STRICT, context, argc, value);
1824 204           }
1825              
1826             static void
1827 192           sqlite_db_func_dispatcher_bytes(sqlite3_context *context, int argc, sqlite3_value **value)
1828             {
1829 192           sqlite_db_func_dispatcher(DBD_SQLITE_STRING_MODE_BYTES, context, argc, value);
1830 192           }
1831              
1832             static void
1833 160           sqlite_db_func_dispatcher_pv(sqlite3_context *context, int argc, sqlite3_value **value)
1834             {
1835 160           sqlite_db_func_dispatcher(DBD_SQLITE_STRING_MODE_PV, context, argc, value);
1836 160           }
1837              
1838             typedef void (*dispatch_func_t)(sqlite3_context*, int, sqlite3_value**);
1839              
1840             static dispatch_func_t _FUNC_DISPATCHER[_DBD_SQLITE_STRING_MODE_COUNT] = {
1841             sqlite_db_func_dispatcher_pv,
1842             sqlite_db_func_dispatcher_bytes,
1843             NULL, NULL,
1844             sqlite_db_func_dispatcher_unicode_naive,
1845             sqlite_db_func_dispatcher_unicode_fallback,
1846             sqlite_db_func_dispatcher_unicode_strict,
1847             };
1848              
1849             int
1850 385           sqlite_db_create_function(pTHX_ SV *dbh, const char *name, int argc, SV *func, int flags)
1851             {
1852 385           D_imp_dbh(dbh);
1853             int rc;
1854             SV *func_sv;
1855              
1856 385 100         if (!DBIc_ACTIVE(imp_dbh)) {
1857 4           sqlite_error(dbh, -2, "attempt to create function on inactive database handle");
1858 4           return FALSE;
1859             }
1860              
1861             /* Copy the function reference */
1862 381 100         if (SvOK(func)) {
    50          
    50          
1863 375           func_sv = newSVsv(func);
1864 375           av_push( imp_dbh->functions, func_sv );
1865             }
1866              
1867             croak_if_db_is_null();
1868              
1869             /* warn("create_function %s with %d args\n", name, argc); */
1870 381 100         if (SvOK(func)) {
    50          
    50          
1871 375           rc = sqlite3_create_function( imp_dbh->db, name, argc, SQLITE_UTF8|flags,
1872 375           func_sv, _FUNC_DISPATCHER[imp_dbh->string_mode], NULL, NULL );
1873             } else {
1874 6           rc = sqlite3_create_function( imp_dbh->db, name, argc, SQLITE_UTF8|flags, NULL, NULL, NULL, NULL );
1875             }
1876 381 50         if ( rc != SQLITE_OK ) {
1877 0           sqlite_error(dbh, rc, form("sqlite_create_function failed with error %s", sqlite3_errmsg(imp_dbh->db)));
1878 0           return FALSE;
1879             }
1880 381           return TRUE;
1881             }
1882              
1883             #ifndef SQLITE_OMIT_LOAD_EXTENSION
1884              
1885             int
1886 4           sqlite_db_enable_load_extension(pTHX_ SV *dbh, int onoff)
1887             {
1888 4           D_imp_dbh(dbh);
1889             int rc;
1890              
1891 4 50         if (!DBIc_ACTIVE(imp_dbh)) {
1892 4           sqlite_error(dbh, -2, "attempt to enable load extension on inactive database handle");
1893 4           return FALSE;
1894             }
1895              
1896             croak_if_db_is_null();
1897              
1898             /* COMPAT: sqlite3_enable_load_extension is only available for 3003006 or newer */
1899 0           rc = sqlite3_enable_load_extension( imp_dbh->db, onoff );
1900 0 0         if ( rc != SQLITE_OK ) {
1901 0           sqlite_error(dbh, rc, form("sqlite_enable_load_extension failed with error %s", sqlite3_errmsg(imp_dbh->db)));
1902 0           return FALSE;
1903             }
1904 0           return TRUE;
1905             }
1906              
1907             int
1908 0           sqlite_db_load_extension(pTHX_ SV *dbh, const char *file, const char *proc)
1909             {
1910 0           D_imp_dbh(dbh);
1911             int rc;
1912              
1913 0 0         if (!DBIc_ACTIVE(imp_dbh)) {
1914 0           sqlite_error(dbh, -2, "attempt to load extension on inactive database handle");
1915 0           return FALSE;
1916             }
1917              
1918             croak_if_db_is_null();
1919              
1920             /* COMPAT: sqlite3_load_extension is only available for 3003006 or newer */
1921 0           rc = sqlite3_load_extension( imp_dbh->db, file, proc, NULL );
1922 0 0         if ( rc != SQLITE_OK ) {
1923 0           sqlite_error(dbh, rc, form("sqlite_load_extension failed with error %s", sqlite3_errmsg(imp_dbh->db)));
1924 0           return FALSE;
1925             }
1926 0           return TRUE;
1927             }
1928              
1929             #endif
1930              
1931 2           SV* _lc(pTHX_ SV* sv) {
1932             int i, l;
1933             char* pv;
1934 2 50         if (SvPOK(sv)) {
1935 2 50         pv = SvPV_nolen(sv);
1936 2           l = strlen(pv);
1937 16 100         for(i = 0; i < l; i++) {
1938 14 50         if (pv[i] >= 'A' && pv[i] <= 'Z') {
    50          
1939 14           pv[i] = pv[i] - 'A' + 'a';
1940             }
1941             }
1942             }
1943 2           return sv;
1944             }
1945              
1946             HV*
1947 14           sqlite_db_table_column_metadata(pTHX_ SV *dbh, SV *dbname, SV *tablename, SV *columnname)
1948             {
1949 14           D_imp_dbh(dbh);
1950             const char *datatype, *collseq;
1951             int notnull, primary, autoinc;
1952             int rc;
1953 14           HV *metadata = newHV();
1954              
1955 14 100         if (!DBIc_ACTIVE(imp_dbh)) {
1956 2           sqlite_error(dbh, -2, "attempt to fetch table column metadata on inactive database handle");
1957 2           return metadata;
1958             }
1959              
1960             croak_if_db_is_null();
1961              
1962             /* dbname may be NULL but (table|column)name may not be NULL */
1963 12 50         if (!tablename || !SvPOK(tablename)) {
    100          
1964 2           sqlite_error(dbh, -2, "table_column_metadata requires a table name");
1965 2           return metadata;
1966             }
1967 10 50         if (!columnname || !SvPOK(columnname)) {
    100          
1968 2           sqlite_error(dbh, -2, "table_column_metadata requires a column name");
1969 2           return metadata;
1970             }
1971              
1972             #ifdef SQLITE_ENABLE_COLUMN_METADATA
1973 16 50         rc = sqlite3_table_column_metadata(
    50          
    50          
    0          
1974             imp_dbh->db,
1975 8 50         (dbname && SvPOK(dbname)) ? SvPV_nolen(dbname) : NULL,
1976 8           SvPV_nolen(tablename),
1977 8           SvPV_nolen(columnname),
1978             &datatype, &collseq, &notnull, &primary, &autoinc);
1979             #endif
1980              
1981 8 100         if (rc == SQLITE_OK) {
1982 4 100         hv_stores(metadata, "data_type", datatype ? _lc(aTHX_ newSVpv(datatype, 0)) : newSV(0));
1983 4 50         hv_stores(metadata, "collation_name", collseq ? newSVpv(collseq, 0) : newSV(0));
1984 4           hv_stores(metadata, "not_null", newSViv(notnull));
1985 4           hv_stores(metadata, "primary", newSViv(primary));
1986 4           hv_stores(metadata, "auto_increment", newSViv(autoinc));
1987             }
1988              
1989 14           return metadata;
1990             }
1991              
1992             static void
1993 102           sqlite_db_aggr_new_dispatcher(pTHX_ sqlite3_context *context, aggrInfo *aggr_info)
1994             {
1995 102           dSP;
1996 102           SV *pkg = NULL;
1997 102           int count = 0;
1998              
1999 102           aggr_info->err = NULL;
2000 102           aggr_info->aggr_inst = NULL;
2001              
2002 102           pkg = sqlite3_user_data(context);
2003 102 50         if ( !pkg )
2004 0           return;
2005              
2006 102           ENTER;
2007 102           SAVETMPS;
2008              
2009 102 50         PUSHMARK(SP);
2010 102 50         XPUSHs( sv_2mortal( newSVsv(pkg) ) );
2011 102           PUTBACK;
2012              
2013 102           count = call_method ("new", G_EVAL|G_SCALAR);
2014 102           SPAGAIN;
2015              
2016 102           aggr_info->inited = 1;
2017              
2018 102 50         if ( SvTRUE( ERRSV ) ) {
    50          
    50          
    50          
    0          
    0          
    50          
    50          
    0          
    0          
    0          
    0          
    50          
    50          
    50          
    50          
    50          
    100          
    50          
    50          
    0          
    0          
    100          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
2019 24 50         aggr_info->err = newSVpvf("error during aggregator's new(): %s",
2020 24 50         SvPV_nolen (ERRSV));
    50          
    0          
2021 12           POPs;
2022 90 50         } else if ( count != 1 ) {
2023             int i;
2024              
2025 0           aggr_info->err = newSVpvf("new() should return one value, got %d",
2026             count );
2027             /* Clear the stack */
2028 0 0         for ( i=0; i < count; i++ ) {
2029 0           POPs;
2030             }
2031             } else {
2032 90           SV *aggr = POPs;
2033 90 100         if ( SvROK(aggr) ) {
2034 84           aggr_info->aggr_inst = newSVsv(aggr);
2035             } else{
2036 6           aggr_info->err = newSVpvf( "new() should return a blessed reference" );
2037             }
2038             }
2039              
2040 102           PUTBACK;
2041              
2042 102 50         FREETMPS;
2043 102           LEAVE;
2044              
2045 102           return;
2046             }
2047              
2048             static void
2049 156           sqlite_db_aggr_step_dispatcher(sqlite3_context *context,
2050             int argc, sqlite3_value **value)
2051             {
2052             dTHX;
2053 156           dSP;
2054 156           int i, string_mode = DBD_SQLITE_STRING_MODE_PV; /* TODO : find out from db handle */
2055             aggrInfo *aggr;
2056              
2057 156           aggr = sqlite3_aggregate_context(context, sizeof (aggrInfo));
2058 156 50         if ( !aggr )
2059 0           return;
2060              
2061 156           ENTER;
2062 156           SAVETMPS;
2063              
2064             /* initialize on first step */
2065 156 100         if ( !aggr->inited ) {
2066 66           sqlite_db_aggr_new_dispatcher(aTHX_ context, aggr);
2067             }
2068              
2069 156 100         if ( aggr->err || !aggr->aggr_inst )
    50          
2070             goto cleanup;
2071              
2072              
2073 108 50         PUSHMARK(SP);
2074 108 50         XPUSHs( sv_2mortal( newSVsv( aggr->aggr_inst ) ));
2075 156 100         for ( i=0; i < argc; i++ ) {
2076 48 50         XPUSHs(stacked_sv_from_sqlite3_value(aTHX_ value[i], string_mode));
2077             }
2078 108           PUTBACK;
2079              
2080 108           call_method ("step", G_SCALAR|G_EVAL|G_DISCARD);
2081              
2082             /* Check for an error */
2083 108 50         if (SvTRUE(ERRSV) ) {
    50          
    50          
    50          
    0          
    50          
    50          
    0          
    0          
    0          
    0          
    50          
    50          
    50          
    50          
    50          
    100          
    50          
    50          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    100          
2084 12 50         aggr->err = newSVpvf("error during aggregator's step(): %s",
2085 12 50         SvPV_nolen(ERRSV));
    50          
    0          
2086 6           POPs;
2087             }
2088              
2089             cleanup:
2090 156 100         FREETMPS;
2091 156           LEAVE;
2092             }
2093              
2094             static void
2095 102           sqlite_db_aggr_finalize_dispatcher( sqlite3_context *context )
2096             {
2097             dTHX;
2098 102           dSP;
2099             aggrInfo *aggr, myAggr;
2100 102           int count = 0;
2101              
2102 102           aggr = sqlite3_aggregate_context(context, 0);
2103              
2104 102           ENTER;
2105 102           SAVETMPS;
2106              
2107 102 100         if ( !aggr ) {
2108             /* SQLite seems to refuse to create a context structure
2109             from finalize() */
2110 36           aggr = &myAggr;
2111 36           aggr->aggr_inst = NULL;
2112 36           aggr->err = NULL;
2113 36           sqlite_db_aggr_new_dispatcher(aTHX_ context, aggr);
2114             }
2115              
2116 102 100         if ( ! aggr->err && aggr->aggr_inst ) {
    50          
2117 78 50         PUSHMARK(SP);
2118 78 50         XPUSHs( sv_2mortal( newSVsv( aggr->aggr_inst )) );
2119 78           PUTBACK;
2120              
2121 78           count = call_method( "finalize", G_SCALAR|G_EVAL );
2122 78           SPAGAIN;
2123              
2124 78 50         if ( SvTRUE(ERRSV) ) {
    50          
    50          
    50          
    0          
    0          
    50          
    50          
    0          
    0          
    0          
    0          
    50          
    50          
    50          
    50          
    50          
    100          
    50          
    50          
    0          
    0          
    100          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
2125 24 50         aggr->err = newSVpvf("error during aggregator's finalize(): %s",
2126 24 50         SvPV_nolen(ERRSV) ) ;
    50          
    0          
2127 12           POPs;
2128 66 50         } else if ( count != 1 ) {
2129             int i;
2130 0           aggr->err = newSVpvf("finalize() should return 1 value, got %d",
2131             count );
2132             /* Clear the stack */
2133 0 0         for ( i=0; i<count; i++ ) {
2134 0           POPs;
2135             }
2136             } else {
2137 66           sqlite_set_result(aTHX_ context, POPs, 0);
2138             }
2139 78           PUTBACK;
2140             }
2141              
2142 102 100         if ( aggr->err ) {
2143 36 50         warn( "DBD::SQLite: error in aggregator cannot be reported to SQLite: %s",
2144 72           SvPV_nolen( aggr->err ) );
2145              
2146             /* sqlite_set_result(aTHX_ context, aggr->err, 1); */
2147 36           SvREFCNT_dec( aggr->err );
2148 36           aggr->err = NULL;
2149             }
2150              
2151 102 100         if ( aggr->aggr_inst ) {
2152 84           SvREFCNT_dec( aggr->aggr_inst );
2153 84           aggr->aggr_inst = NULL;
2154             }
2155              
2156 102 50         FREETMPS;
2157 102           LEAVE;
2158 102           }
2159              
2160             int
2161 46           sqlite_db_create_aggregate(pTHX_ SV *dbh, const char *name, int argc, SV *aggr_pkg, int flags)
2162             {
2163 46           D_imp_dbh(dbh);
2164             int rc;
2165             SV *aggr_pkg_copy;
2166              
2167 46 100         if (!DBIc_ACTIVE(imp_dbh)) {
2168 4           sqlite_error(dbh, -2, "attempt to create aggregate on inactive database handle");
2169 4           return FALSE;
2170             }
2171              
2172             /* Copy the aggregate reference */
2173 42           aggr_pkg_copy = newSVsv(aggr_pkg);
2174 42           av_push( imp_dbh->aggregates, aggr_pkg_copy );
2175              
2176             croak_if_db_is_null();
2177              
2178 42           rc = sqlite3_create_function( imp_dbh->db, name, argc, SQLITE_UTF8|flags,
2179             aggr_pkg_copy,
2180             NULL,
2181             sqlite_db_aggr_step_dispatcher,
2182             sqlite_db_aggr_finalize_dispatcher
2183             );
2184              
2185 42 50         if ( rc != SQLITE_OK ) {
2186 0           sqlite_error(dbh, rc, form("sqlite_create_aggregate failed with error %s", sqlite3_errmsg(imp_dbh->db)));
2187 0           return FALSE;
2188             }
2189 42           return TRUE;
2190             }
2191              
2192             #define SQLITE_DB_COLLATION_BASE(func, sv1, sv2) STMT_START { \
2193             int cmp = 0; \
2194             int n_retval, i; \
2195             \
2196             ENTER; \
2197             SAVETMPS; \
2198             PUSHMARK(SP); \
2199             XPUSHs( sv_2mortal( sv1 ) ); \
2200             XPUSHs( sv_2mortal( sv2 ) ); \
2201             PUTBACK; \
2202             n_retval = call_sv(func, G_SCALAR); \
2203             SPAGAIN; \
2204             if (n_retval != 1) { \
2205             warn("collation function returned %d arguments", n_retval); \
2206             } \
2207             for(i = 0; i < n_retval; i++) { \
2208             cmp = POPi; \
2209             } \
2210             PUTBACK; \
2211             FREETMPS; \
2212             LEAVE; \
2213             \
2214             return cmp; \
2215             } STMT_END
2216              
2217             int
2218 382           sqlite_db_collation_dispatcher(void *func, int len1, const void *string1,
2219             int len2, const void *string2)
2220             {
2221             dTHX;
2222 382           dSP;
2223              
2224 764 50         SQLITE_DB_COLLATION_BASE(func, newSVpvn( string1, len1), newSVpvn( string2, len2));
    50          
    50          
    50          
    50          
    100          
    50          
2225             }
2226              
2227             int
2228 0           sqlite_db_collation_dispatcher_utf8_naive(void *func, int len1, const void *string1,
2229             int len2, const void *string2)
2230             {
2231             dTHX;
2232 0           dSP;
2233              
2234 0 0         SQLITE_DB_COLLATION_BASE(func, newSVpvn_flags( string1, len1, SVf_UTF8), newSVpvn_flags( string2, len2, SVf_UTF8));
    0          
    0          
    0          
    0          
    0          
    0          
2235             }
2236              
2237             int
2238 334           sqlite_db_collation_dispatcher_utf8_fallback(void *func, int len1, const void *string1,
2239             int len2, const void *string2)
2240             {
2241             dTHX;
2242 334           dSP;
2243              
2244 334           SV* sv1 = newSVpvn( string1, len1);
2245 334           SV* sv2 = newSVpvn( string2, len2);
2246              
2247 334 50         DBD_SQLITE_UTF8_DECODE_WITH_FALLBACK(sv1);
2248 334 50         DBD_SQLITE_UTF8_DECODE_WITH_FALLBACK(sv2);
2249              
2250 668 50         SQLITE_DB_COLLATION_BASE(func, sv1, sv2);
    50          
    50          
    50          
    50          
    100          
    50          
2251             }
2252              
2253             typedef int (*collation_dispatch_func_t)(void *, int, const void *, int, const void *);
2254              
2255             static collation_dispatch_func_t _COLLATION_DISPATCHER[_DBD_SQLITE_STRING_MODE_COUNT] = {
2256             sqlite_db_collation_dispatcher,
2257             sqlite_db_collation_dispatcher,
2258             NULL, NULL,
2259             sqlite_db_collation_dispatcher_utf8_naive,
2260             sqlite_db_collation_dispatcher_utf8_fallback,
2261             sqlite_db_collation_dispatcher_utf8_fallback,
2262             };
2263              
2264             int
2265 24           sqlite_db_create_collation(pTHX_ SV *dbh, const char *name, SV *func)
2266             {
2267 24           D_imp_dbh(dbh);
2268             int rv, rv2;
2269 24           void *aa = "aa";
2270 24           void *zz = "zz";
2271              
2272 24           SV *func_sv = newSVsv(func);
2273              
2274 24 100         if (!DBIc_ACTIVE(imp_dbh)) {
2275 8           sqlite_error(dbh, -2, "attempt to create collation on inactive database handle");
2276 8           return FALSE;
2277             }
2278              
2279             croak_if_db_is_null();
2280              
2281             /* Check that this is a proper collation function */
2282 16           rv = sqlite_db_collation_dispatcher(func_sv, 2, aa, 2, aa);
2283 16 50         if (rv != 0) {
2284 0 0         sqlite_trace(dbh, imp_dbh, 3, form("improper collation function: %s(aa, aa) returns %d!", name, rv));
2285             }
2286 16           rv = sqlite_db_collation_dispatcher(func_sv, 2, aa, 2, zz);
2287 16           rv2 = sqlite_db_collation_dispatcher(func_sv, 2, zz, 2, aa);
2288 16 50         if (rv2 != (rv * -1)) {
2289 0 0         sqlite_trace(dbh, imp_dbh, 3, form("improper collation function: '%s' is not symmetric", name));
2290             }
2291              
2292             /* Copy the func reference so that it can be deallocated at disconnect */
2293 16           av_push( imp_dbh->functions, func_sv );
2294              
2295             /* Register the func within sqlite3 */
2296 16           rv = sqlite3_create_collation(
2297             imp_dbh->db, name, SQLITE_UTF8,
2298             func_sv,
2299 16           _COLLATION_DISPATCHER[imp_dbh->string_mode]
2300             );
2301              
2302 16 50         if ( rv != SQLITE_OK ) {
2303 0           sqlite_error(dbh, rv, form("sqlite_create_collation failed with error %s", sqlite3_errmsg(imp_dbh->db)));
2304 0           return FALSE;
2305             }
2306 16           return TRUE;
2307             }
2308              
2309             void
2310 12           sqlite_db_collation_needed_dispatcher(
2311             void *dbh,
2312             sqlite3* db, /* unused */
2313             int eTextRep, /* unused */
2314             const char* collation_name
2315             )
2316             {
2317             dTHX;
2318 12           dSP;
2319              
2320 12           D_imp_dbh(dbh);
2321              
2322 12           ENTER;
2323 12           SAVETMPS;
2324 12 50         PUSHMARK(SP);
2325 12 50         XPUSHs( dbh );
2326 12 50         XPUSHs( sv_2mortal( newSVpv( collation_name, 0) ) );
2327 12           PUTBACK;
2328              
2329 12           call_sv( imp_dbh->collation_needed_callback, G_VOID );
2330 12           SPAGAIN;
2331              
2332 12           PUTBACK;
2333 12 50         FREETMPS;
2334 12           LEAVE;
2335 12           }
2336              
2337             void
2338 311           sqlite_db_collation_needed(pTHX_ SV *dbh, SV *callback)
2339             {
2340 311           D_imp_dbh(dbh);
2341              
2342 311 100         if (!DBIc_ACTIVE(imp_dbh)) {
2343 4           sqlite_error(dbh, -2, "attempt to see if collation is needed on inactive database handle");
2344 4           return;
2345             }
2346              
2347             croak_if_db_is_null();
2348              
2349             /* remember the callback within the dbh */
2350 307           sv_setsv(imp_dbh->collation_needed_callback, callback);
2351              
2352             /* Register the func within sqlite3 */
2353 307 50         (void) sqlite3_collation_needed( imp_dbh->db,
2354 0 0         (void*) (SvOK(callback) ? dbh : NULL),
    0          
2355             sqlite_db_collation_needed_dispatcher );
2356             }
2357              
2358             int
2359 332           sqlite_db_generic_callback_dispatcher( void *callback )
2360             {
2361             dTHX;
2362 332           dSP;
2363             int n_retval, i;
2364 332           int retval = 0;
2365              
2366 332           ENTER;
2367 332           SAVETMPS;
2368 332 50         PUSHMARK(SP);
2369 332           n_retval = call_sv( callback, G_SCALAR );
2370 332           SPAGAIN;
2371 332 50         if ( n_retval != 1 ) {
2372 0           warn( "callback returned %d arguments", n_retval );
2373             }
2374 664 100         for(i = 0; i < n_retval; i++) {
2375 332 50         retval = POPi;
2376             }
2377 332           PUTBACK;
2378 332 50         FREETMPS;
2379 332           LEAVE;
2380              
2381 332           return retval;
2382             }
2383              
2384             int
2385 8           sqlite_db_progress_handler(pTHX_ SV *dbh, int n_opcodes, SV *handler)
2386             {
2387 8           D_imp_dbh(dbh);
2388              
2389 8 100         if (!DBIc_ACTIVE(imp_dbh)) {
2390 4           sqlite_error(dbh, -2, "attempt to set progress handler on inactive database handle");
2391 4           return FALSE;
2392             }
2393              
2394             croak_if_db_is_null();
2395              
2396 4 100         if (!SvOK(handler)) {
    50          
    50          
2397             /* remove previous handler */
2398 2           sqlite3_progress_handler( imp_dbh->db, 0, NULL, NULL);
2399             }
2400             else {
2401 2           SV *handler_sv = newSVsv(handler);
2402              
2403             /* Copy the handler ref so that it can be deallocated at disconnect */
2404 2           av_push( imp_dbh->functions, handler_sv );
2405              
2406             /* Register the func within sqlite3 */
2407 2           sqlite3_progress_handler( imp_dbh->db, n_opcodes,
2408             sqlite_db_generic_callback_dispatcher,
2409             handler_sv );
2410             }
2411 4           return TRUE;
2412             }
2413              
2414             SV*
2415 12           sqlite_db_commit_hook(pTHX_ SV *dbh, SV *hook)
2416             {
2417 12           D_imp_dbh(dbh);
2418             void *retval;
2419              
2420 12 100         if (!DBIc_ACTIVE(imp_dbh)) {
2421 4           sqlite_error(dbh, -2, "attempt to set commit hook on inactive database handle");
2422 4           return &PL_sv_undef;
2423             }
2424              
2425             croak_if_db_is_null();
2426              
2427 8 100         if (!SvOK(hook)) {
    50          
    50          
2428             /* remove previous hook */
2429 4           retval = sqlite3_commit_hook( imp_dbh->db, NULL, NULL );
2430             }
2431             else {
2432 4           SV *hook_sv = newSVsv( hook );
2433              
2434             /* Copy the handler ref so that it can be deallocated at disconnect */
2435 4           av_push( imp_dbh->functions, hook_sv );
2436              
2437             /* Register the hook within sqlite3 */
2438 4           retval = sqlite3_commit_hook( imp_dbh->db,
2439             sqlite_db_generic_callback_dispatcher,
2440             hook_sv );
2441             }
2442              
2443 8 100         return retval ? newSVsv(retval) : &PL_sv_undef;
2444             }
2445              
2446             SV*
2447 8           sqlite_db_rollback_hook(pTHX_ SV *dbh, SV *hook)
2448             {
2449 8           D_imp_dbh(dbh);
2450             void *retval;
2451              
2452 8 100         if (!DBIc_ACTIVE(imp_dbh)) {
2453 4           sqlite_error(dbh, -2, "attempt to set rollback hook on inactive database handle");
2454 4           return &PL_sv_undef;
2455             }
2456              
2457             croak_if_db_is_null();
2458              
2459 4 100         if (!SvOK(hook)) {
    50          
    50          
2460             /* remove previous hook */
2461 2           retval = sqlite3_rollback_hook( imp_dbh->db, NULL, NULL );
2462             }
2463             else {
2464 2           SV *hook_sv = newSVsv( hook );
2465              
2466             /* Copy the handler ref so that it can be deallocated at disconnect */
2467 2           av_push( imp_dbh->functions, hook_sv );
2468              
2469             /* Register the hook within sqlite3 */
2470 2           retval = sqlite3_rollback_hook( imp_dbh->db,
2471             (void(*)(void *))
2472             sqlite_db_generic_callback_dispatcher,
2473             hook_sv );
2474             }
2475              
2476 4 100         return retval ? newSVsv(retval) : &PL_sv_undef;
2477             }
2478              
2479             void
2480 60           sqlite_db_update_dispatcher( void *callback, int op,
2481             char const *database, char const *table,
2482             sqlite3_int64 rowid )
2483             {
2484             dTHX;
2485 60           dSP;
2486              
2487 60           ENTER;
2488 60           SAVETMPS;
2489 60 50         PUSHMARK(SP);
2490              
2491 60 50         XPUSHs( sv_2mortal( newSViv( op ) ) );
2492 60 50         XPUSHs( sv_2mortal( newSVpv( database, 0 ) ) );
2493 60 50         XPUSHs( sv_2mortal( newSVpv( table, 0 ) ) );
2494 60 50         XPUSHs( sv_2mortal( newSViv( (IV)rowid ) ) );
2495 60           PUTBACK;
2496              
2497 60           call_sv( callback, G_VOID );
2498 60           SPAGAIN;
2499              
2500 60           PUTBACK;
2501 60 50         FREETMPS;
2502 60           LEAVE;
2503 60           }
2504              
2505             SV*
2506 8           sqlite_db_update_hook(pTHX_ SV *dbh, SV *hook)
2507             {
2508 8           D_imp_dbh(dbh);
2509             void *retval;
2510              
2511 8 100         if (!DBIc_ACTIVE(imp_dbh)) {
2512 4           sqlite_error(dbh, -2, "attempt to set update hook on inactive database handle");
2513 4           return &PL_sv_undef;
2514             }
2515              
2516             croak_if_db_is_null();
2517              
2518 4 100         if (!SvOK(hook)) {
    50          
    50          
2519             /* remove previous hook */
2520 2           retval = sqlite3_update_hook( imp_dbh->db, NULL, NULL );
2521             }
2522             else {
2523 2           SV *hook_sv = newSVsv( hook );
2524              
2525             /* Copy the handler ref so that it can be deallocated at disconnect */
2526 2           av_push( imp_dbh->functions, hook_sv );
2527              
2528             /* Register the hook within sqlite3 */
2529 2           retval = sqlite3_update_hook( imp_dbh->db,
2530             sqlite_db_update_dispatcher,
2531             hook_sv );
2532             }
2533              
2534 4 100         return retval ? newSVsv(retval) : &PL_sv_undef;
2535             }
2536              
2537             int
2538 4           sqlite_db_authorizer_dispatcher (
2539             void *authorizer,
2540             int action_code,
2541             const char *details_1,
2542             const char *details_2,
2543             const char *details_3,
2544             const char *details_4
2545             )
2546             {
2547             dTHX;
2548 4           dSP;
2549 4           int retval = 0;
2550             int n_retval, i;
2551              
2552 4           ENTER;
2553 4           SAVETMPS;
2554 4 50         PUSHMARK(SP);
2555              
2556 4 50         XPUSHs( sv_2mortal ( newSViv ( action_code ) ) );
2557              
2558             /* these ifs are ugly but without them, perl 5.8 segfaults */
2559 4 50         XPUSHs( sv_2mortal( details_1 ? newSVpv( details_1, 0 ) : &PL_sv_undef ) );
    50          
2560 4 50         XPUSHs( sv_2mortal( details_2 ? newSVpv( details_2, 0 ) : &PL_sv_undef ) );
    50          
2561 4 50         XPUSHs( sv_2mortal( details_3 ? newSVpv( details_3, 0 ) : &PL_sv_undef ) );
    50          
2562 4 50         XPUSHs( sv_2mortal( details_4 ? newSVpv( details_4, 0 ) : &PL_sv_undef ) );
    50          
2563 4           PUTBACK;
2564              
2565 4           n_retval = call_sv(authorizer, G_SCALAR);
2566 4           SPAGAIN;
2567 4 50         if ( n_retval != 1 ) {
2568 0           warn( "callback returned %d arguments", n_retval );
2569             }
2570 8 100         for(i = 0; i < n_retval; i++) {
2571 4 50         retval = POPi;
2572             }
2573              
2574 4           PUTBACK;
2575 4 50         FREETMPS;
2576 4           LEAVE;
2577              
2578 4           return retval;
2579             }
2580              
2581             int
2582 8           sqlite_db_set_authorizer(pTHX_ SV *dbh, SV *authorizer)
2583             {
2584 8           D_imp_dbh(dbh);
2585             int retval;
2586              
2587 8 100         if (!DBIc_ACTIVE(imp_dbh)) {
2588 4           sqlite_error(dbh, -2, "attempt to set authorizer on inactive database handle");
2589 4           return FALSE;
2590             }
2591              
2592             croak_if_db_is_null();
2593              
2594 4 100         if (!SvOK(authorizer)) {
    50          
    50          
2595             /* remove previous hook */
2596 2           retval = sqlite3_set_authorizer( imp_dbh->db, NULL, NULL );
2597             }
2598             else {
2599 2           SV *authorizer_sv = newSVsv( authorizer );
2600              
2601             /* Copy the coderef so that it can be deallocated at disconnect */
2602 2           av_push( imp_dbh->functions, authorizer_sv );
2603              
2604             /* Register the hook within sqlite3 */
2605 2           retval = sqlite3_set_authorizer( imp_dbh->db,
2606             sqlite_db_authorizer_dispatcher,
2607             authorizer_sv );
2608             }
2609              
2610 4           return retval;
2611             }
2612              
2613             #ifndef SQLITE_OMIT_TRACE
2614             void
2615 6           sqlite_db_trace_dispatcher(void *callback, const char *sql)
2616             {
2617             dTHX;
2618 6           dSP;
2619             int n_retval, i;
2620 6           int retval = 0;
2621              
2622 6           ENTER;
2623 6           SAVETMPS;
2624 6 50         PUSHMARK(SP);
2625 6 50         XPUSHs( sv_2mortal( newSVpv( sql, 0 ) ) );
2626 6           PUTBACK;
2627              
2628 6           n_retval = call_sv( callback, G_SCALAR );
2629 6           SPAGAIN;
2630 6 50         if ( n_retval != 1 ) {
2631 0           warn( "callback returned %d arguments", n_retval );
2632             }
2633 12 100         for(i = 0; i < n_retval; i++) {
2634 6 50         retval = POPi;
2635             }
2636 6           PUTBACK;
2637 6 50         FREETMPS;
2638 6           LEAVE;
2639 6           }
2640              
2641             int
2642 6           sqlite_db_trace(pTHX_ SV *dbh, SV *func)
2643             {
2644 6           D_imp_dbh(dbh);
2645              
2646 6 50         if (!DBIc_ACTIVE(imp_dbh)) {
2647 0           sqlite_error(dbh, -2, "attempt to set trace on inactive database handle");
2648 0           return FALSE;
2649             }
2650              
2651             croak_if_db_is_null();
2652              
2653 6 100         if (!SvOK(func)) {
    50          
    50          
2654             /* remove previous callback */
2655 2           sqlite3_trace( imp_dbh->db, NULL, NULL );
2656             }
2657             else {
2658 4           SV *func_sv = newSVsv(func);
2659              
2660             /* Copy the func ref so that it can be deallocated at disconnect */
2661 4           av_push( imp_dbh->functions, func_sv );
2662              
2663             /* Register the func within sqlite3 */
2664 4           sqlite3_trace( imp_dbh->db,
2665             sqlite_db_trace_dispatcher,
2666             func_sv );
2667             }
2668 6           return TRUE;
2669             }
2670             #endif
2671              
2672             void
2673 6           sqlite_db_profile_dispatcher(void *callback, const char *sql, sqlite3_uint64 elapsed)
2674             {
2675             dTHX;
2676 6           dSP;
2677             int n_retval, i;
2678 6           int retval = 0;
2679              
2680 6           ENTER;
2681 6           SAVETMPS;
2682 6 50         PUSHMARK(SP);
2683 6 50         XPUSHs( sv_2mortal( newSVpv( sql, 0 ) ) );
2684             /*
2685             * The profile callback time is in units of nanoseconds,
2686             * however the current implementation is only capable of
2687             * millisecond resolution so the six least significant digits
2688             * in the time are meaningless.
2689             * (http://sqlite.org/c3ref/profile.html)
2690             */
2691 6 50         XPUSHs( sv_2mortal( newSViv((IV)( elapsed / 1000000 )) ) );
2692 6           PUTBACK;
2693              
2694 6           n_retval = call_sv( callback, G_SCALAR );
2695 6           SPAGAIN;
2696 6 50         if ( n_retval != 1 ) {
2697 0           warn( "callback returned %d arguments", n_retval );
2698             }
2699 12 100         for(i = 0; i < n_retval; i++) {
2700 6 50         retval = POPi;
2701             }
2702 6           PUTBACK;
2703 6 50         FREETMPS;
2704 6           LEAVE;
2705 6           }
2706              
2707             int
2708 6           sqlite_db_profile(pTHX_ SV *dbh, SV *func)
2709             {
2710 6           D_imp_dbh(dbh);
2711              
2712 6 50         if (!DBIc_ACTIVE(imp_dbh)) {
2713 0           sqlite_error(dbh, -2, "attempt to profile on inactive database handle");
2714 0           return FALSE;
2715             }
2716              
2717             croak_if_db_is_null();
2718              
2719 6 100         if (!SvOK(func)) {
    50          
    50          
2720             /* remove previous callback */
2721 2           sqlite3_profile( imp_dbh->db, NULL, NULL );
2722             }
2723             else {
2724 4           SV *func_sv = newSVsv(func);
2725              
2726             /* Copy the func ref so that it can be deallocated at disconnect */
2727 4           av_push( imp_dbh->functions, func_sv );
2728              
2729             /* Register the func within sqlite3 */
2730 4           sqlite3_profile( imp_dbh->db,
2731             sqlite_db_profile_dispatcher,
2732             func_sv );
2733             }
2734 6           return TRUE;
2735             }
2736              
2737             /* Accesses the SQLite Online Backup API, and fills the currently loaded
2738             * database from the passed filename.
2739             * Usual usage of this would be when you're operating on the :memory:
2740             * special database connection and want to copy it in from a real db.
2741             */
2742             int
2743 6           sqlite_db_backup_from_file(pTHX_ SV *dbh, char *filename)
2744             {
2745 6           D_imp_dbh(dbh);
2746              
2747             #if SQLITE_VERSION_NUMBER >= 3006011
2748             int rc;
2749             sqlite3 *pFrom;
2750             sqlite3_backup *pBackup;
2751              
2752 6 100         if (!DBIc_ACTIVE(imp_dbh)) {
2753 4           sqlite_error(dbh, -2, "attempt to backup from file on inactive database handle");
2754 4           return FALSE;
2755             }
2756              
2757             croak_if_db_is_null();
2758              
2759 2           rc = sqlite_open(filename, &pFrom);
2760 2 50         if ( rc != SQLITE_OK ) {
2761 0           return FALSE;
2762             }
2763              
2764             /* COMPAT: sqlite3_backup_* are only available for 3006011 or newer */
2765 2           pBackup = sqlite3_backup_init(imp_dbh->db, "main", pFrom, "main");
2766 2 50         if (pBackup) {
2767 2           (void)sqlite3_backup_step(pBackup, -1);
2768 2           (void)sqlite3_backup_finish(pBackup);
2769             }
2770 2           rc = sqlite3_errcode(imp_dbh->db);
2771 2           (void)sqlite3_close(pFrom);
2772              
2773 2 50         if ( rc != SQLITE_OK ) {
2774 0           sqlite_error(dbh, rc, form("sqlite_backup_from_file failed with error %s", sqlite3_errmsg(imp_dbh->db)));
2775 0           return FALSE;
2776             }
2777              
2778 6           return TRUE;
2779             #else
2780             sqlite_error(dbh, SQLITE_ERROR, form("backup feature requires SQLite 3.6.11 and newer"));
2781             return FALSE;
2782             #endif
2783             }
2784              
2785             int
2786 2           sqlite_db_backup_from_dbh(pTHX_ SV *dbh, SV *from)
2787             {
2788 2           D_imp_dbh(dbh);
2789              
2790             #if SQLITE_VERSION_NUMBER >= 3006011
2791             int rc;
2792             sqlite3_backup *pBackup;
2793              
2794 2           imp_dbh_t *imp_dbh_from = (imp_dbh_t *)DBIh_COM(from);
2795              
2796 2 50         if (!DBIc_ACTIVE(imp_dbh)) {
2797 0           sqlite_error(dbh, -2, "attempt to backup from file on inactive database handle");
2798 0           return FALSE;
2799             }
2800              
2801 2 50         if (!DBIc_ACTIVE(imp_dbh_from)) {
2802 0           sqlite_error(dbh, -2, "attempt to backup from inactive database handle");
2803 0           return FALSE;
2804             }
2805              
2806             croak_if_db_is_null();
2807              
2808             /* COMPAT: sqlite3_backup_* are only available for 3006011 or newer */
2809 2           pBackup = sqlite3_backup_init(imp_dbh->db, "main", imp_dbh_from->db, "main");
2810 2 50         if (pBackup) {
2811 2           (void)sqlite3_backup_step(pBackup, -1);
2812 2           (void)sqlite3_backup_finish(pBackup);
2813             }
2814 2           rc = sqlite3_errcode(imp_dbh->db);
2815              
2816 2 50         if ( rc != SQLITE_OK ) {
2817 0           sqlite_error(dbh, rc, form("sqlite_backup_from_file failed with error %s", sqlite3_errmsg(imp_dbh->db)));
2818 0           return FALSE;
2819             }
2820              
2821 2           return TRUE;
2822             #else
2823             sqlite_error(dbh, SQLITE_ERROR, form("backup feature requires SQLite 3.6.11 and newer"));
2824             return FALSE;
2825             #endif
2826             }
2827              
2828             /* Accesses the SQLite Online Backup API, and copies the currently loaded
2829             * database into the passed filename.
2830             * Usual usage of this would be when you're operating on the :memory:
2831             * special database connection, and want to back it up to an on-disk file.
2832             */
2833             int
2834 6           sqlite_db_backup_to_file(pTHX_ SV *dbh, char *filename)
2835             {
2836 6           D_imp_dbh(dbh);
2837              
2838             #if SQLITE_VERSION_NUMBER >= 3006011
2839             int rc;
2840             sqlite3 *pTo;
2841             sqlite3_backup *pBackup;
2842              
2843 6 100         if (!DBIc_ACTIVE(imp_dbh)) {
2844 4           sqlite_error(dbh, -2, "attempt to backup to file on inactive database handle");
2845 4           return FALSE;
2846             }
2847              
2848             croak_if_db_is_null();
2849              
2850 2           rc = sqlite_open(filename, &pTo);
2851 2 50         if ( rc != SQLITE_OK ) {
2852 0           return FALSE;
2853             }
2854              
2855             /* COMPAT: sqlite3_backup_* are only available for 3006011 or newer */
2856 2           pBackup = sqlite3_backup_init(pTo, "main", imp_dbh->db, "main");
2857 2 50         if (pBackup) {
2858 2           (void)sqlite3_backup_step(pBackup, -1);
2859 2           (void)sqlite3_backup_finish(pBackup);
2860             }
2861 2           rc = sqlite3_errcode(pTo);
2862 2           (void)sqlite3_close(pTo);
2863              
2864 2 50         if ( rc != SQLITE_OK ) {
2865 0           sqlite_error(dbh, rc, form("sqlite_backup_to_file failed with error %s", sqlite3_errmsg(imp_dbh->db)));
2866 0           return FALSE;
2867             }
2868              
2869 6           return TRUE;
2870             #else
2871             sqlite_error(dbh, SQLITE_ERROR, form("backup feature requires SQLite 3.6.11 and newer"));
2872             return FALSE;
2873             #endif
2874             }
2875              
2876             int
2877 2           sqlite_db_backup_to_dbh(pTHX_ SV *dbh, SV *to)
2878             {
2879 2           D_imp_dbh(dbh);
2880              
2881             #if SQLITE_VERSION_NUMBER >= 3006011
2882             int rc;
2883             sqlite3_backup *pBackup;
2884              
2885 2           imp_dbh_t *imp_dbh_to = (imp_dbh_t *)DBIh_COM(to);
2886              
2887 2 50         if (!DBIc_ACTIVE(imp_dbh)) {
2888 0           sqlite_error(dbh, -2, "attempt to backup to file on inactive database handle");
2889 0           return FALSE;
2890             }
2891              
2892 2 50         if (!DBIc_ACTIVE(imp_dbh_to)) {
2893 0           sqlite_error(dbh, -2, "attempt to backup to inactive database handle");
2894 0           return FALSE;
2895             }
2896              
2897             croak_if_db_is_null();
2898              
2899             /* COMPAT: sqlite3_backup_* are only available for 3006011 or newer */
2900 2           pBackup = sqlite3_backup_init(imp_dbh_to->db, "main", imp_dbh->db, "main");
2901 2 50         if (pBackup) {
2902 2           (void)sqlite3_backup_step(pBackup, -1);
2903 2           (void)sqlite3_backup_finish(pBackup);
2904             }
2905 2           rc = sqlite3_errcode(imp_dbh_to->db);
2906              
2907 2 50         if ( rc != SQLITE_OK ) {
2908 0           sqlite_error(dbh, rc, form("sqlite_backup_to_file failed with error %s", sqlite3_errmsg(imp_dbh->db)));
2909 0           return FALSE;
2910             }
2911              
2912 2           return TRUE;
2913             #else
2914             sqlite_error(dbh, SQLITE_ERROR, form("backup feature requires SQLite 3.6.11 and newer"));
2915             return FALSE;
2916             #endif
2917             }
2918              
2919             int
2920 6           sqlite_db_limit(pTHX_ SV *dbh, int id, int new_value)
2921             {
2922 6           D_imp_dbh(dbh);
2923 6           return sqlite3_limit(imp_dbh->db, id, new_value);
2924             }
2925              
2926             int
2927 90           sqlite_db_config(pTHX_ SV *dbh, int id, int new_value)
2928             {
2929 90           D_imp_dbh(dbh);
2930             int ret;
2931 90           int rc = -1;
2932 90           switch (id) {
2933             case SQLITE_DBCONFIG_LOOKASIDE:
2934 2           sqlite_error(dbh, rc, "SQLITE_DBCONFIG_LOOKASIDE is not supported");
2935 2           return FALSE;
2936             case SQLITE_DBCONFIG_MAINDBNAME:
2937 2           sqlite_error(dbh, rc, "SQLITE_DBCONFIG_MAINDBNAME is not supported");
2938 2           return FALSE;
2939             case SQLITE_DBCONFIG_ENABLE_FKEY:
2940             case SQLITE_DBCONFIG_ENABLE_TRIGGER:
2941             case SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER:
2942             case SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION:
2943             case SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE:
2944             case SQLITE_DBCONFIG_ENABLE_QPSG:
2945             case SQLITE_DBCONFIG_TRIGGER_EQP:
2946             case SQLITE_DBCONFIG_RESET_DATABASE:
2947             case SQLITE_DBCONFIG_DEFENSIVE:
2948             case SQLITE_DBCONFIG_WRITABLE_SCHEMA:
2949             case SQLITE_DBCONFIG_LEGACY_ALTER_TABLE:
2950             case SQLITE_DBCONFIG_DQS_DML:
2951             case SQLITE_DBCONFIG_DQS_DDL:
2952 86           rc = sqlite3_db_config(imp_dbh->db, id, new_value, &ret);
2953 86           break;
2954             default:
2955 0           sqlite_error(dbh, rc, form("Unknown config id: %d", id));
2956 0           return FALSE;
2957             }
2958 86 50         if ( rc != SQLITE_OK ) {
2959 0           sqlite_error(dbh, rc, form("sqlite_db_config failed with error %s", sqlite3_errmsg(imp_dbh->db)));
2960 0           return FALSE;
2961             }
2962 90           return ret;
2963             }
2964              
2965             int
2966 14           sqlite_db_get_autocommit(pTHX_ SV *dbh)
2967             {
2968 14           D_imp_dbh(dbh);
2969 14           return sqlite3_get_autocommit(imp_dbh->db);
2970             }
2971              
2972             int
2973 0           sqlite_db_txn_state(pTHX_ SV *dbh, SV *schema)
2974             {
2975             #if SQLITE_VERSION_NUMBER >= 3034000
2976 0           D_imp_dbh(dbh);
2977 0 0         if (SvOK(schema) && SvPOK(schema)) {
    0          
    0          
    0          
2978 0 0         return sqlite3_txn_state(imp_dbh->db, SvPV_nolen(schema));
2979             } else {
2980 0           return sqlite3_txn_state(imp_dbh->db, NULL);
2981             }
2982             #else
2983             return -1;
2984             #endif
2985             }
2986              
2987             #include "dbdimp_tokenizer.inc"
2988             #include "dbdimp_virtual_table.inc"
2989              
2990             /* end */