dbdimp.c

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