dbdimp.c

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