dbdimp.c

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