dbdimp_virtual_table.inc

filter: Statementstmt 0.0% 0 / 451 Branchbran 0.0% 0 / 242 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 693 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« dbdimp_tokenizer.inc ↑ Summary sqlite3.c »
1 /***********************************************************************
2 ** The set of routines that implement the perl "module"
3 ** (i.e support for virtual tables written in Perl)
4 ************************************************************************/
5  
6 typedef struct perl_vtab {
7 sqlite3_vtab base;
8 SV *perl_vtab_obj;
9 HV *functions;
10 } perl_vtab;
11  
12 typedef struct perl_vtab_cursor {
13 sqlite3_vtab_cursor base;
14 SV *perl_cursor_obj;
15 } perl_vtab_cursor;
16  
17 typedef struct perl_vtab_init {
18 SV *dbh;
19 const char *perl_class;
20 } perl_vtab_init;
21  
22  
23  
24 /* auxiliary routine for generalized method calls. Arg "i" may be unused */
25 0 static int _call_perl_vtab_method(sqlite3_vtab *pVTab,
26 const char *method, int i) {
27 dTHX;
28 0 dSP;
29 int count;
30  
31 0 ENTER;
32 0 SAVETMPS;
33  
34 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
35 0 ▶ XPUSHs(((perl_vtab *) pVTab)->perl_vtab_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(((perl_vtab *) pVTab)->perl_vtab_obj); 0 0 0
36 0 ▶ XPUSHs(sv_2mortal(newSViv(i)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(i))); 0 0 0
37 0 PUTBACK;
38 0 count = call_method (method, G_VOID);
39 0 SPAGAIN;
40 0 SP -= count;
41  
42 0 PUTBACK;
43 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
44 0 LEAVE;
45  
46 0 return SQLITE_OK;
47 }
48  
49  
50  
51 0 static int perl_vt_New(const char *method,
52 sqlite3 *db, void *pAux,
53 int argc, const char *const *argv,
54 sqlite3_vtab **ppVTab, char **pzErr){
55 dTHX;
56 0 dSP;
57 perl_vtab *vt;
58 0 perl_vtab_init *init_data = (perl_vtab_init *)pAux;
59 int count, i;
60 0 int rc = SQLITE_ERROR;
61 SV *perl_vtab_obj;
62 SV *sql;
63  
64 /* allocate a perl_vtab structure */
65 0 vt = (perl_vtab *) sqlite3_malloc(sizeof(*vt));
66 0 ▶ if( vt==NULL ) return SQLITE_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if( vt==NULL ) return SQLITE_NOMEM; 0 0 0
67 0 memset(vt, 0, sizeof(*vt));
68 0 vt->functions = newHV();
69  
70 0 ENTER;
71 0 SAVETMPS;
72  
73 /* call the ->CREATE/CONNECT() method */
74 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
75 0 ▶ XPUSHs(sv_2mortal(newSVpv(init_data->perl_class, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(init_data->perl_class, 0))); 0 0 0
76 0 ▶ XPUSHs(init_data->dbh);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(init_data->dbh); 0 0 0
77 0 ▶ for(i = 0; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < argc; i++) { 0 0 0
78 0 ▶ XPUSHs(newSVpvn_flags(argv[i], strlen(argv[i]), SVs_TEMP|SVf_UTF8));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(newSVpvn_flags(argv[i], strlen(argv[i]), SVs_TEMP|SVf_UTF8)); 0 0 0
79 }
80 0 PUTBACK;
81 0 count = call_method (method, G_SCALAR);
82 0 SPAGAIN;
83  
84 /* check the return value */
85 0 ▶ if ( count != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( count != 1 ) { 0 0 0
86 0 *pzErr = sqlite3_mprintf("vtab->%s() should return one value, got %d",
87 method, count );
88 0 SP -= count; /* Clear the stack */
89 0 goto cleanup;
90 }
91  
92 /* get the VirtualTable instance */
93 0 perl_vtab_obj = POPs;
94 0 ▶ if ( !sv_isobject(perl_vtab_obj) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !sv_isobject(perl_vtab_obj) ) { 0 0 0
95 0 *pzErr = sqlite3_mprintf("vtab->%s() should return a blessed reference",
96 method);
97 0 goto cleanup;
98 }
99  
100 /* call the ->VTAB_TO_DECLARE() method */
101 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
102 0 ▶ XPUSHs(perl_vtab_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(perl_vtab_obj); 0 0 0
103 0 PUTBACK;
104 0 count = call_method ("VTAB_TO_DECLARE", G_SCALAR);
105 0 SPAGAIN;
106  
107 /* check the return value */
108 0 ▶ if (count != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1 ) { 0 0 0
109 0 *pzErr = sqlite3_mprintf("vtab->VTAB_TO_DECLARE() should return one value, got %d",
110 count );
111 0 SP -= count; /* Clear the stack */
112 0 goto cleanup;
113 }
114  
115 /* call sqlite3_declare_vtab with the sql returned from
116 method VTAB_TO_DECLARE(), converted to utf8 */
117 0 sql = POPs;
118 0 rc = sqlite3_declare_vtab(db, SvPVutf8_nolen(sql));
119  
120 0 cleanup:
121 0 ▶ if (rc == SQLITE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == SQLITE_OK) { 0 0 0
122 /* record the VirtualTable perl instance within the vtab structure */
123 0 vt->perl_vtab_obj = SvREFCNT_inc(perl_vtab_obj);
124 0 *ppVTab = &vt->base;
125 }
126 else {
127 0 sqlite3_free(vt);
128 }
129  
130 0 PUTBACK;
131 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
132 0 LEAVE;
133  
134 0 return rc;
135 }
136  
137  
138 0 static int perl_vt_Create(sqlite3 *db, void *pAux,
139 int argc, const char *const *argv,
140 sqlite3_vtab **ppVTab, char **pzErr){
141 0 return perl_vt_New("CREATE", db, pAux, argc, argv, ppVTab, pzErr);
142 }
143  
144 0 static int perl_vt_Connect(sqlite3 *db, void *pAux,
145 int argc, const char *const *argv,
146 sqlite3_vtab **ppVTab, char **pzErr){
147 0 return perl_vt_New("CONNECT", db, pAux, argc, argv, ppVTab, pzErr);
148 }
149  
150  
151 0 static int _free_perl_vtab(perl_vtab *pVTab){
152 dTHX;
153  
154 0 SvREFCNT_dec(pVTab->perl_vtab_obj);
155  
156 /* deallocate coderefs that were declared through FindFunction() */
157 0 hv_undef(pVTab->functions);
158 0 SvREFCNT_dec(pVTab->functions);
159  
160 0 sqlite3_free(pVTab);
161 0 return SQLITE_OK;
162 }
163  
164 0 static int perl_vt_Disconnect(sqlite3_vtab *pVTab){
165 0 _call_perl_vtab_method(pVTab, "DISCONNECT", 0);
166 0 return _free_perl_vtab((perl_vtab *)pVTab);
167 }
168  
169 0 static int perl_vt_Drop(sqlite3_vtab *pVTab){
170 0 _call_perl_vtab_method(pVTab, "DROP", 0);
171 0 return _free_perl_vtab((perl_vtab *)pVTab);
172 }
173  
174  
175 static char *
176 0 _constraint_op_to_string(unsigned char op) {
177 0 switch (op) {
178 0 case SQLITE_INDEX_CONSTRAINT_EQ:
179 0 return "=";
180 0 case SQLITE_INDEX_CONSTRAINT_GT:
181 0 return ">";
182 0 case SQLITE_INDEX_CONSTRAINT_GE:
183 0 return ">=";
184 0 case SQLITE_INDEX_CONSTRAINT_LT:
185 0 return "<";
186 0 case SQLITE_INDEX_CONSTRAINT_LE:
187 0 return "<=";
188 0 case SQLITE_INDEX_CONSTRAINT_MATCH:
189 0 return "MATCH";
190 #if SQLITE_VERSION_NUMBER >= 3010000
191 0 case SQLITE_INDEX_CONSTRAINT_LIKE:
192 0 return "LIKE";
193 0 case SQLITE_INDEX_CONSTRAINT_GLOB:
194 0 return "GLOB";
195 0 case SQLITE_INDEX_CONSTRAINT_REGEXP:
196 0 return "REGEXP";
197 #endif
198 0 default:
199 0 return "unknown";
200 }
201 }
202  
203  
204 0 static int perl_vt_BestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *pIdxInfo){
205 dTHX;
206 0 dSP;
207 int i, count;
208 int argvIndex;
209 AV *constraints;
210 AV *order_by;
211 SV *hashref;
212 SV **val;
213 HV *hv;
214 struct sqlite3_index_constraint_usage *pConsUsage;
215  
216 0 ENTER;
217 0 SAVETMPS;
218  
219 /* build the "where_constraints" datastructure */
220 0 constraints = newAV();
221 0 ▶ for (i=0; i<pIdxInfo->nConstraint; i++){
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<pIdxInfo->nConstraint; i++){ 0 0 0
222 0 struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[i];
223 0 HV *constraint = newHV();
224 0 char *op_str = _constraint_op_to_string(pCons->op);
225 0 hv_stores(constraint, "col", newSViv(pCons->iColumn));
226 0 hv_stores(constraint, "op", newSVpv(op_str, 0));
227 0 ▶ hv_stores(constraint, "usable", pCons->usable ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(constraint, "usable", pCons->usable ? &PL_sv_yes : &PL_sv_no); 0 0 0
228 0 av_push(constraints, newRV_noinc((SV*) constraint));
229 }
230  
231 /* build the "order_by" datastructure */
232 0 order_by = newAV();
233 0 ▶ for (i=0; i<pIdxInfo->nOrderBy; i++){
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<pIdxInfo->nOrderBy; i++){ 0 0 0
234 0 struct sqlite3_index_orderby const *pOrder = &pIdxInfo->aOrderBy[i];
235 0 HV *order = newHV();
236 0 hv_stores(order, "col", newSViv(pOrder->iColumn));
237 0 ▶ hv_stores(order, "desc", pOrder->desc ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(order, "desc", pOrder->desc ? &PL_sv_yes : &PL_sv_no); 0 0 0
238 0 av_push( order_by, newRV_noinc((SV*) order));
239 }
240  
241 /* call the ->BEST_INDEX() method */
242 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
243 0 ▶ XPUSHs( ((perl_vtab *) pVTab)->perl_vtab_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( ((perl_vtab *) pVTab)->perl_vtab_obj); 0 0 0
244 0 ▶ XPUSHs( sv_2mortal( newRV_noinc((SV*) constraints)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newRV_noinc((SV*) constraints))); 0 0 0
245 0 ▶ XPUSHs( sv_2mortal( newRV_noinc((SV*) order_by)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newRV_noinc((SV*) order_by))); 0 0 0
246 0 PUTBACK;
247 0 count = call_method ("BEST_INDEX", G_SCALAR);
248 0 SPAGAIN;
249  
250 /* get values back from the returned hashref */
251 0 ▶ if (count != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) 0 0 0
252 0 croak("BEST_INDEX() method returned %d vals instead of 1", count);
253 0 hashref = POPs;
254 0 ▶ if (!(hashref && SvROK(hashref) && SvTYPE(SvRV(hashref)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(hashref && SvROK(hashref) && SvTYPE(SvRV(hashref)) == SVt_PVHV)) 0 0 0
if (!(hashref && SvROK(hashref) && SvTYPE(SvRV(hashref)) == SVt_PVHV)) 0 0 0
if (!(hashref && SvROK(hashref) && SvTYPE(SvRV(hashref)) == SVt_PVHV)) 0 0 0
255 0 croak("BEST_INDEX() method did not return a hashref");
256 0 hv = (HV*)SvRV(hashref);
257 0 val = hv_fetch(hv, "idxNum", 6, FALSE);
258 0 ▶ pIdxInfo->idxNum = (val && SvOK(*val)) ? SvIV(*val) : 0;
Branch: 2 branches
BranchTrueFalseTotal
pIdxInfo->idxNum = (val && SvOK(*val)) ? SvIV(*val) : 0; 0 0 0
pIdxInfo->idxNum = (val && SvOK(*val)) ? SvIV(*val) : 0; 0 0 0
259 0 val = hv_fetch(hv, "idxStr", 6, FALSE);
260 0 ▶ if (val && SvOK(*val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvOK(*val)) { 0 0 0
if (val && SvOK(*val)) { 0 0 0
261 STRLEN len;
262 0 char *str = SvPVutf8(*val, len);
263 0 pIdxInfo->idxStr = sqlite3_malloc(len+1);
264 0 memcpy(pIdxInfo->idxStr, str, len);
265 0 pIdxInfo->idxStr[len] = 0;
266 0 pIdxInfo->needToFreeIdxStr = 1;
267 }
268 0 val = hv_fetch(hv, "orderByConsumed", 15, FALSE);
269 0 ▶ pIdxInfo->orderByConsumed = (val && SvTRUE(*val)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
pIdxInfo->orderByConsumed = (val && SvTRUE(*val)) ? 1 : 0; 0 0 0
pIdxInfo->orderByConsumed = (val && SvTRUE(*val)) ? 1 : 0; 0 0 0
270 0 val = hv_fetch(hv, "estimatedCost", 13, FALSE);
271 0 ▶ pIdxInfo->estimatedCost = (val && SvOK(*val)) ? SvNV(*val) : 0;
Branch: 2 branches
BranchTrueFalseTotal
pIdxInfo->estimatedCost = (val && SvOK(*val)) ? SvNV(*val) : 0; 0 0 0
pIdxInfo->estimatedCost = (val && SvOK(*val)) ? SvNV(*val) : 0; 0 0 0
272 #if SQLITE_VERSION_NUMBER >= 3008002
273 0 val = hv_fetch(hv, "estimatedRows", 13, FALSE);
274 0 ▶ pIdxInfo->estimatedRows = (val && SvOK(*val)) ? SvIV(*val) : 0;
Branch: 2 branches
BranchTrueFalseTotal
pIdxInfo->estimatedRows = (val && SvOK(*val)) ? SvIV(*val) : 0; 0 0 0
pIdxInfo->estimatedRows = (val && SvOK(*val)) ? SvIV(*val) : 0; 0 0 0
275 #endif
276  
277 /* loop over constraints to get back the "argvIndex" and "omit" keys
278 that shoud have been added by the best_index() method call */
279 0 ▶ for (i=0; i<pIdxInfo->nConstraint; i++){
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<pIdxInfo->nConstraint; i++){ 0 0 0
280 0 SV **rv = av_fetch(constraints, i, FALSE);
281 0 ▶ if (!(rv && SvROK(*rv) && SvTYPE(SvRV(*rv)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(rv && SvROK(*rv) && SvTYPE(SvRV(*rv)) == SVt_PVHV)) 0 0 0
if (!(rv && SvROK(*rv) && SvTYPE(SvRV(*rv)) == SVt_PVHV)) 0 0 0
if (!(rv && SvROK(*rv) && SvTYPE(SvRV(*rv)) == SVt_PVHV)) 0 0 0
282 0 croak("the call to BEST_INDEX() has corrupted constraint data");
283 0 hv = (HV*)SvRV(*rv);
284 0 val = hv_fetch(hv, "argvIndex", 9, FALSE);
285 0 ▶ argvIndex = (val && SvOK(*val)) ? SvIV(*val) + 1: 0;
Branch: 2 branches
BranchTrueFalseTotal
argvIndex = (val && SvOK(*val)) ? SvIV(*val) + 1: 0; 0 0 0
argvIndex = (val && SvOK(*val)) ? SvIV(*val) + 1: 0; 0 0 0
286  
287 0 pConsUsage = &pIdxInfo->aConstraintUsage[i];
288 0 pConsUsage->argvIndex = argvIndex;
289 0 val = hv_fetch(hv, "omit", 4, FALSE);
290 0 ▶ pConsUsage->omit = (val && SvTRUE(*val)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
pConsUsage->omit = (val && SvTRUE(*val)) ? 1 : 0; 0 0 0
pConsUsage->omit = (val && SvTRUE(*val)) ? 1 : 0; 0 0 0
291 }
292  
293 0 PUTBACK;
294 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
295 0 LEAVE;
296  
297 0 return SQLITE_OK;
298 }
299  
300  
301  
302 0 static int perl_vt_Open(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
303 dTHX;
304 0 dSP;
305 int count;
306 0 int rc = SQLITE_ERROR;
307 SV *perl_cursor;
308 perl_vtab_cursor *cursor;
309  
310 0 ENTER;
311 0 SAVETMPS;
312  
313 /* allocate a perl_vtab_cursor structure */
314 0 cursor = (perl_vtab_cursor *) sqlite3_malloc(sizeof(*cursor));
315 0 ▶ if( cursor==NULL ) return SQLITE_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if( cursor==NULL ) return SQLITE_NOMEM; 0 0 0
316 0 memset(cursor, 0, sizeof(*cursor));
317  
318 /* call the ->OPEN() method */
319 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
320 0 ▶ XPUSHs( ((perl_vtab *) pVTab)->perl_vtab_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( ((perl_vtab *) pVTab)->perl_vtab_obj); 0 0 0
321 0 PUTBACK;
322 0 count = call_method ("OPEN", G_SCALAR);
323 0 SPAGAIN;
324 0 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 0 0
325 0 warn("vtab->OPEN() method returned %d vals instead of 1", count);
326 0 SP -= count;
327 0 goto cleanup;
328  
329 }
330 0 perl_cursor = POPs;
331 0 ▶ if ( !sv_isobject(perl_cursor) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !sv_isobject(perl_cursor) ) { 0 0 0
332 0 warn("vtab->OPEN() method did not return a blessed cursor");
333 0 goto cleanup;
334 }
335  
336 /* everything went OK */
337 0 rc = SQLITE_OK;
338  
339 0 cleanup:
340  
341 0 ▶ if (rc == SQLITE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == SQLITE_OK) { 0 0 0
342 0 cursor->perl_cursor_obj = SvREFCNT_inc(perl_cursor);
343 0 *ppCursor = &cursor->base;
344 }
345 else {
346 0 sqlite3_free(cursor);
347 }
348  
349 0 PUTBACK;
350 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
351 0 LEAVE;
352  
353 0 return rc;
354 }
355  
356 0 static int perl_vt_Close(sqlite3_vtab_cursor *pVtabCursor){
357 dTHX;
358 0 dSP;
359 perl_vtab_cursor *perl_pVTabCursor;
360  
361 0 ENTER;
362 0 SAVETMPS;
363  
364 /* Note : there is no explicit call to a CLOSE() method; if
365 needed, the Perl class can implement a DESTROY() method */
366  
367 0 perl_pVTabCursor = (perl_vtab_cursor *) pVtabCursor;
368 0 SvREFCNT_dec(perl_pVTabCursor->perl_cursor_obj);
369 0 sqlite3_free(perl_pVTabCursor);
370  
371 0 PUTBACK;
372 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
373 0 LEAVE;
374  
375 0 return SQLITE_OK;
376 }
377  
378 0 static int perl_vt_Filter( sqlite3_vtab_cursor *pVtabCursor,
379 int idxNum, const char *idxStr,
380 int argc, sqlite3_value **argv ){
381 dTHX;
382 0 dSP;
383 dMY_CXT;
384 int i, count;
385 0 int is_unicode = MY_CXT.last_dbh_is_unicode;
386  
387 0 ENTER;
388 0 SAVETMPS;
389  
390 /* call the FILTER() method with ($idxNum, $idxStr, @args) */
391 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
392 0 ▶ XPUSHs(((perl_vtab_cursor *) pVtabCursor)->perl_cursor_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(((perl_vtab_cursor *) pVtabCursor)->perl_cursor_obj); 0 0 0
393 0 ▶ XPUSHs(sv_2mortal(newSViv(idxNum)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(idxNum))); 0 0 0
394 0 ▶ XPUSHs(sv_2mortal(newSVpv(idxStr, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(idxStr, 0))); 0 0 0
395 0 ▶ for(i = 0; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < argc; i++) { 0 0 0
396 0 ▶ XPUSHs(stacked_sv_from_sqlite3_value(aTHX_ argv[i], is_unicode));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(stacked_sv_from_sqlite3_value(aTHX_ argv[i], is_unicode)); 0 0 0
397 }
398 0 PUTBACK;
399 0 count = call_method("FILTER", G_VOID);
400 0 SPAGAIN;
401 0 SP -= count;
402  
403 0 PUTBACK;
404 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
405 0 LEAVE;
406  
407 0 return SQLITE_OK;
408 }
409  
410  
411 0 static int perl_vt_Next(sqlite3_vtab_cursor *pVtabCursor){
412 dTHX;
413 0 dSP;
414 int count;
415  
416 0 ENTER;
417 0 SAVETMPS;
418  
419 /* call the next() method */
420 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
421 0 ▶ XPUSHs(((perl_vtab_cursor *) pVtabCursor)->perl_cursor_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(((perl_vtab_cursor *) pVtabCursor)->perl_cursor_obj); 0 0 0
422 0 PUTBACK;
423 0 count = call_method ("NEXT", G_VOID);
424 0 SPAGAIN;
425 0 SP -= count;
426  
427 0 PUTBACK;
428 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
429 0 LEAVE;
430  
431 0 return SQLITE_OK;
432 }
433  
434 0 static int perl_vt_Eof(sqlite3_vtab_cursor *pVtabCursor){
435 dTHX;
436 0 dSP;
437 int count, eof;
438  
439 0 ENTER;
440 0 SAVETMPS;
441  
442 /* call the eof() method */
443 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
444 0 ▶ XPUSHs(((perl_vtab_cursor *) pVtabCursor)->perl_cursor_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(((perl_vtab_cursor *) pVtabCursor)->perl_cursor_obj); 0 0 0
445 0 PUTBACK;
446 0 count = call_method ("EOF", G_SCALAR);
447 0 SPAGAIN;
448 0 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 0 0
449 0 warn("cursor->EOF() method returned %d vals instead of 1", count);
450 0 SP -= count;
451 }
452 else {
453 0 SV *sv = POPs; /* need 2 lines, because this doesn't work : */
454 0 eof = SvTRUE(sv); /* eof = SvTRUE(POPs); # I don't understand why :-( */
455 }
456  
457 0 PUTBACK;
458 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
459 0 LEAVE;
460  
461 0 return eof;
462 }
463  
464  
465 0 static int perl_vt_Column(sqlite3_vtab_cursor *pVtabCursor,
466 sqlite3_context* context,
467 int col){
468 dTHX;
469 0 dSP;
470 int count;
471 0 int rc = SQLITE_ERROR;
472  
473 0 ENTER;
474 0 SAVETMPS;
475  
476 /* call the column() method */
477 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
478 0 ▶ XPUSHs(((perl_vtab_cursor *) pVtabCursor)->perl_cursor_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(((perl_vtab_cursor *) pVtabCursor)->perl_cursor_obj); 0 0 0
479 0 ▶ XPUSHs(sv_2mortal(newSViv(col)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(col))); 0 0 0
480 0 PUTBACK;
481 0 count = call_method ("COLUMN", G_SCALAR);
482 0 SPAGAIN;
483 0 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 0 0
484 0 warn("cursor->COLUMN() method returned %d vals instead of 1", count);
485 0 SP -= count;
486 0 sqlite3_result_error(context, "column error", 12);
487 }
488 else {
489 0 SV *result = POPs;
490 0 sqlite_set_result(aTHX_ context, result, 0 );
491 0 rc = SQLITE_OK;
492 }
493  
494 0 PUTBACK;
495 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
496 0 LEAVE;
497  
498 0 return rc;
499 }
500  
501 0 static int perl_vt_Rowid( sqlite3_vtab_cursor *pVtabCursor,
502 sqlite3_int64 *pRowid ){
503 dTHX;
504 0 dSP;
505 int count;
506 0 int rc = SQLITE_ERROR;
507  
508 0 ENTER;
509 0 SAVETMPS;
510  
511 /* call the rowid() method */
512 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
513 0 ▶ XPUSHs(((perl_vtab_cursor *) pVtabCursor)->perl_cursor_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(((perl_vtab_cursor *) pVtabCursor)->perl_cursor_obj); 0 0 0
514 0 PUTBACK;
515 0 count = call_method ("ROWID", G_SCALAR);
516 0 SPAGAIN;
517 0 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 0 0
518 0 warn("cursor->ROWID() returned %d vals instead of 1", count);
519 0 SP -= count;
520 }
521 else {
522 0 *pRowid =POPi;
523 0 rc = SQLITE_OK;
524 }
525  
526 0 PUTBACK;
527 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
528 0 LEAVE;
529  
530 0 return rc;
531 }
532  
533 0 static int perl_vt_Update( sqlite3_vtab *pVTab,
534 int argc, sqlite3_value **argv,
535 sqlite3_int64 *pRowid ){
536 dTHX;
537 0 dSP;
538 dMY_CXT;
539 int count, i;
540 0 int is_unicode = MY_CXT.last_dbh_is_unicode;
541 0 int rc = SQLITE_ERROR;
542 SV *rowidsv;
543  
544 0 ENTER;
545 0 SAVETMPS;
546  
547 /* call the _SQLITE_UPDATE() method */
548 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
549 0 ▶ XPUSHs(((perl_vtab *) pVTab)->perl_vtab_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(((perl_vtab *) pVTab)->perl_vtab_obj); 0 0 0
550 0 ▶ for(i = 0; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < argc; i++) { 0 0 0
551 0 ▶ XPUSHs(stacked_sv_from_sqlite3_value(aTHX_ argv[i], is_unicode));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(stacked_sv_from_sqlite3_value(aTHX_ argv[i], is_unicode)); 0 0 0
552 }
553 0 PUTBACK;
554 0 count = call_method ("_SQLITE_UPDATE", G_SCALAR);
555 0 SPAGAIN;
556 0 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 0 0
557 0 warn("cursor->_SQLITE_UPDATE() returned %d vals instead of 1", count);
558 0 SP -= count;
559 }
560 else {
561 0 ▶ if (argc > 1 && sqlite3_value_type(argv[0]) == SQLITE_NULL
Branch: 2 branches
BranchTrueFalseTotal
if (argc > 1 && sqlite3_value_type(argv[0]) == SQLITE_NULL 0 0 0
if (argc > 1 && sqlite3_value_type(argv[0]) == SQLITE_NULL 0 0 0
562 0 ▶ && sqlite3_value_type(argv[1]) == SQLITE_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
&& sqlite3_value_type(argv[1]) == SQLITE_NULL) { 0 0 0
563 /* this was an insert without any given rowid, so the result of
564 the method call must be passed in *pRowid*/
565 0 rowidsv = POPs;
566 0 ▶ if (!SvOK(rowidsv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(rowidsv)) 0 0 0
567 0 *pRowid = 0;
568 0 ▶ else if (SvUOK(rowidsv))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvUOK(rowidsv)) 0 0 0
569 0 *pRowid = SvUV(rowidsv);
570 0 ▶ else if (SvIOK(rowidsv))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvIOK(rowidsv)) 0 0 0
571 0 *pRowid = SvIV(rowidsv);
572 else
573 0 *pRowid = (sqlite3_int64)SvNV(rowidsv);
574 }
575 0 rc = SQLITE_OK;
576 }
577  
578  
579 0 PUTBACK;
580 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
581 0 LEAVE;
582  
583 0 return rc;
584 }
585  
586 0 static int perl_vt_Begin(sqlite3_vtab *pVTab){
587 0 return _call_perl_vtab_method(pVTab, "BEGIN_TRANSACTION", 0);
588 }
589  
590 0 static int perl_vt_Sync(sqlite3_vtab *pVTab){
591 0 return _call_perl_vtab_method(pVTab, "SYNC_TRANSACTION", 0);
592 }
593  
594 0 static int perl_vt_Commit(sqlite3_vtab *pVTab){
595 0 return _call_perl_vtab_method(pVTab, "COMMIT_TRANSACTION", 0);
596 }
597  
598 0 static int perl_vt_Rollback(sqlite3_vtab *pVTab){
599 0 return _call_perl_vtab_method(pVTab, "ROLLBACK_TRANSACTION", 0);
600 }
601  
602 0 static int perl_vt_FindFunction(sqlite3_vtab *pVTab,
603 int nArg, const char *zName,
604 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
605 void **ppArg){
606 dTHX;
607 0 dSP;
608 dMY_CXT;
609 int count;
610 0 int is_overloaded = 0;
611 0 char *func_name = sqlite3_mprintf("%s\t%d", zName, nArg);
612 0 STRLEN len = strlen(func_name);
613 0 HV *functions = ((perl_vtab *) pVTab)->functions;
614 0 SV* coderef = NULL;
615 SV** val;
616 SV *result;
617  
618 0 ENTER;
619 0 SAVETMPS;
620  
621 /* check if that function was already in cache */
622 0 ▶ if (hv_exists(functions, func_name, len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(functions, func_name, len)) { 0 0 0
623 0 val = hv_fetch(functions, func_name, len, FALSE);
624 0 ▶ if (val && SvOK(*val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvOK(*val)) { 0 0 0
if (val && SvOK(*val)) { 0 0 0
625 0 coderef = *val;
626 }
627 }
628 else {
629 /* call the FIND_FUNCTION() method */
630 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
631 0 ▶ XPUSHs(((perl_vtab *) pVTab)->perl_vtab_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(((perl_vtab *) pVTab)->perl_vtab_obj); 0 0 0
632 0 ▶ XPUSHs(sv_2mortal(newSViv(nArg)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(nArg))); 0 0 0
633 0 ▶ XPUSHs(sv_2mortal(newSVpv(zName, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(zName, 0))); 0 0 0
634 0 PUTBACK;
635 0 count = call_method ("FIND_FUNCTION", G_SCALAR);
636 0 SPAGAIN;
637 0 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 0 0
638 0 warn("vtab->FIND_FUNCTION() method returned %d vals instead of 1", count);
639 0 SP -= count;
640 0 goto cleanup;
641 }
642 0 result = POPs;
643 0 ▶ if (SvTRUE(result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(result)) { 0 0 0
644 /* the coderef must be valid for the lifetime of pVTab, so
645 make a copy */
646 0 coderef = newSVsv(result);
647 }
648  
649 /* store result in cache */
650 0 ▶ hv_store(functions, func_name, len, coderef ? coderef : &PL_sv_undef, 0);
Branch: 1 branch
BranchTrueFalseTotal
hv_store(functions, func_name, len, coderef ? coderef : &PL_sv_undef, 0); 0 0 0
651 }
652  
653 /* return function information for sqlite3 within *pxFunc and *ppArg */
654 0 ▶ is_overloaded = coderef && SvTRUE(coderef);
Branch: 2 branches
BranchTrueFalseTotal
is_overloaded = coderef && SvTRUE(coderef); 0 0 0
is_overloaded = coderef && SvTRUE(coderef); 0 0 0
655 0 ▶ if (is_overloaded) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_overloaded) { 0 0 0
656 0 *pxFunc = MY_CXT.last_dbh_is_unicode ? sqlite_db_func_dispatcher_unicode
657 0 ▶ : sqlite_db_func_dispatcher_no_unicode;
Branch: 1 branch
BranchTrueFalseTotal
: sqlite_db_func_dispatcher_no_unicode; 0 0 0
658 0 *ppArg = coderef;
659 }
660  
661 0 cleanup:
662 0 PUTBACK;
663 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
664 0 LEAVE;
665 0 sqlite3_free(func_name);
666 0 return is_overloaded;
667 }
668  
669  
670 0 static int perl_vt_Rename(sqlite3_vtab *pVTab, const char *zNew){
671 dTHX;
672 0 dSP;
673 int count;
674 0 int rc = SQLITE_ERROR;
675  
676 0 ENTER;
677 0 SAVETMPS;
678  
679 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
680 0 ▶ XPUSHs(((perl_vtab *) pVTab)->perl_vtab_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(((perl_vtab *) pVTab)->perl_vtab_obj); 0 0 0
681 0 ▶ XPUSHs(sv_2mortal(newSVpv(zNew, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(zNew, 0))); 0 0 0
682 0 PUTBACK;
683 0 count = call_method("RENAME", G_SCALAR);
684 0 SPAGAIN;
685 0 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 0 0
686 0 warn("vtab->RENAME() returned %d args instead of 1", count);
687 0 SP -= count;
688 }
689 else {
690 0 rc = POPi;
691 }
692  
693 0 PUTBACK;
694 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
695 0 LEAVE;
696  
697 0 return rc;
698 }
699  
700 0 static int perl_vt_Savepoint(sqlite3_vtab *pVTab, int point){
701 0 return _call_perl_vtab_method(pVTab, "SAVEPOINT", point);
702 }
703  
704 0 static int perl_vt_Release(sqlite3_vtab *pVTab, int point){
705 0 return _call_perl_vtab_method(pVTab, "RELEASE", point);
706 }
707  
708 0 static int perl_vt_RollbackTo(sqlite3_vtab *pVTab, int point){
709 0 return _call_perl_vtab_method(pVTab, "ROLLBACK_TO", point);
710 }
711  
712 static sqlite3_module perl_vt_Module = {
713 1, /* iVersion */
714 perl_vt_Create, /* xCreate */
715 perl_vt_Connect, /* xConnect */
716 perl_vt_BestIndex, /* xBestIndex */
717 perl_vt_Disconnect, /* xDisconnect */
718 perl_vt_Drop, /* xDestroy */
719 perl_vt_Open, /* xOpen - open a cursor */
720 perl_vt_Close, /* xClose - close a cursor */
721 perl_vt_Filter, /* xFilter - configure scan constraints */
722 perl_vt_Next, /* xNext - advance a cursor */
723 perl_vt_Eof, /* xEof - check for end of scan */
724 perl_vt_Column, /* xColumn - read data */
725 perl_vt_Rowid, /* xRowid - read data */
726 perl_vt_Update, /* xUpdate (optional) */
727 perl_vt_Begin, /* xBegin (optional) */
728 perl_vt_Sync, /* xSync (optional) */
729 perl_vt_Commit, /* xCommit (optional) */
730 perl_vt_Rollback, /* xRollback (optional) */
731 perl_vt_FindFunction, /* xFindFunction (optional) */
732 perl_vt_Rename, /* xRename */
733 #if SQLITE_VERSION_NUMBER >= 3007007
734 perl_vt_Savepoint, /* xSavepoint (optional) */
735 perl_vt_Release, /* xRelease (optional) */
736 perl_vt_RollbackTo /* xRollbackTo (optional) */
737 #endif
738 };
739  
740  
741 void
742 0 sqlite_db_destroy_module_data(void *pAux)
743 {
744 dTHX;
745 0 dSP;
746 int count;
747 0 int rc = SQLITE_ERROR;
748 perl_vtab_init *init_data;
749  
750 0 ENTER;
751 0 SAVETMPS;
752  
753 0 init_data = (perl_vtab_init *)pAux;
754  
755 /* call the DESTROY_MODULE() method */
756 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
757 0 ▶ XPUSHs(sv_2mortal(newSVpv(init_data->perl_class, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(init_data->perl_class, 0))); 0 0 0
758 0 PUTBACK;
759 0 count = call_method("DESTROY_MODULE", G_VOID);
760 0 SPAGAIN;
761 0 SP -= count;
762  
763 /* free module memory */
764 0 SvREFCNT_dec(init_data->dbh);
765 0 sqlite3_free((char *)init_data->perl_class);
766  
767 0 PUTBACK;
768 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
769 0 LEAVE;
770 0 }
771  
772  
773  
774 int
775 0 sqlite_db_create_module(pTHX_ SV *dbh, const char *name, const char *perl_class)
776 {
777 0 dSP;
778 0 D_imp_dbh(dbh);
779 0 int count, rc, retval = TRUE;
780 char *module_ISA;
781 char *loading_code;
782 perl_vtab_init *init_data;
783  
784 0 ENTER;
785 0 SAVETMPS;
786  
787 0 ▶ if (!DBIc_ACTIVE(imp_dbh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!DBIc_ACTIVE(imp_dbh)) { 0 0 0
788 0 sqlite_error(dbh, -2, "attempt to create module on inactive database handle");
789 0 return FALSE;
790 }
791  
792 /* load the module if needed */
793 0 module_ISA = sqlite3_mprintf("%s::ISA", perl_class);
794 0 ▶ if (!get_av(module_ISA, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!get_av(module_ISA, 0)) { 0 0 0
795 0 loading_code = sqlite3_mprintf("use %s", perl_class);
796 0 eval_pv(loading_code, TRUE);
797 0 sqlite3_free(loading_code);
798 }
799 0 sqlite3_free(module_ISA);
800  
801 /* build the init datastructure that will be passed to perl_vt_New() */
802 0 init_data = sqlite3_malloc(sizeof(*init_data));
803 0 init_data->dbh = newRV(dbh);
804 0 sv_rvweaken(init_data->dbh);
805 0 init_data->perl_class = sqlite3_mprintf(perl_class);
806  
807 /* register within sqlite */
808 0 rc = sqlite3_create_module_v2( imp_dbh->db,
809 name,
810 &perl_vt_Module,
811 init_data,
812 sqlite_db_destroy_module_data
813 );
814 0 ▶ if ( rc != SQLITE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( rc != SQLITE_OK ) { 0 0 0
815 0 sqlite_error(dbh, rc, form("sqlite_create_module failed with error %s",
816 sqlite3_errmsg(imp_dbh->db)));
817 0 retval = FALSE;
818 }
819  
820  
821 /* call the CREATE_MODULE() method */
822 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
823 0 ▶ XPUSHs(sv_2mortal(newSVpv(perl_class, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(perl_class, 0))); 0 0 0
824 0 ▶ XPUSHs(sv_2mortal(newSVpv(name, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(name, 0))); 0 0 0
825 0 PUTBACK;
826 0 count = call_method("CREATE_MODULE", G_VOID);
827 0 SPAGAIN;
828 0 SP -= count;
829  
830 0 PUTBACK;
831 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
832 0 LEAVE;
833  
834 0 return retval;
835 }
« dbdimp_tokenizer.inc ↑ Summary sqlite3.c »