… | |
… | |
5 | /* import some stuff from DBIXS.h and DBI.xs */ |
5 | /* import some stuff from DBIXS.h and DBI.xs */ |
6 | #define DBIXS_VERSION 93 |
6 | #define DBIXS_VERSION 93 |
7 | #define DBI_MAGIC '~' |
7 | #define DBI_MAGIC '~' |
8 | |
8 | |
9 | #define DBISTATE_PERLNAME "DBI::_dbistate" |
9 | #define DBISTATE_PERLNAME "DBI::_dbistate" |
10 | #define DBISTATE_ADDRSV (perl_get_sv(DBISTATE_PERLNAME, 0x05)) |
10 | #define DBISTATE_ADDRSV (perl_get_sv (DBISTATE_PERLNAME, 0x05)) |
11 | #define DBIS_PUBLISHED_LVALUE (*(INT2PTR(dbistate_t**, &SvIVX(DBISTATE_ADDRSV)))) |
11 | #define DBIS_PUBLISHED_LVALUE (*(INT2PTR(dbistate_t**, &SvIVX(DBISTATE_ADDRSV)))) |
|
|
12 | |
|
|
13 | static SV *sql_varchar, *sql_integer, *sql_double; |
|
|
14 | static SV *tmp_iv; |
12 | |
15 | |
13 | struct dbistate_st { |
16 | struct dbistate_st { |
14 | #define DBISTATE_VERSION 94 /* Must change whenever dbistate_t does */ |
17 | #define DBISTATE_VERSION 94 /* Must change whenever dbistate_t does */ |
15 | /* this must be the first member in structure */ |
18 | /* this must be the first member in structure */ |
16 | void (*check_version) _((const char *name, |
19 | void (*check_version) _((const char *name, |
… | |
… | |
57 | |
60 | |
58 | static SV * |
61 | static SV * |
59 | sql_upgrade_utf8 (SV *sv) |
62 | sql_upgrade_utf8 (SV *sv) |
60 | { |
63 | { |
61 | #if CAN_UTF8 |
64 | #if CAN_UTF8 |
62 | if (SvPOK (sv)) |
65 | if (SvPOKp (sv)) |
63 | sv_utf8_upgrade (sv); |
66 | sv_utf8_upgrade (sv); |
64 | #endif |
67 | #endif |
65 | return sv; |
68 | return sv; |
66 | } |
69 | } |
67 | |
70 | |
68 | static SV * |
71 | static SV * |
69 | mortalcopy_and_maybe_force_utf8(int utf8, SV *sv) |
72 | mortalcopy_and_maybe_force_utf8(int utf8, SV *sv) |
70 | { |
73 | { |
71 | sv = sv_mortalcopy (sv); |
74 | sv = sv_mortalcopy (sv); |
72 | #if CAN_UTF8 |
75 | #if CAN_UTF8 |
73 | if (utf8 && SvPOK (sv)) |
76 | if (utf8 && SvPOKp (sv)) |
74 | SvUTF8_on (sv); |
77 | SvUTF8_on (sv); |
75 | #endif |
78 | #endif |
76 | return sv; |
79 | return sv; |
77 | } |
80 | } |
78 | |
81 | |
79 | #define maybe_upgrade_utf8(utf8,sv) ((utf8) ? sql_upgrade_utf8 (sv) : (sv)) |
82 | #define maybe_upgrade_utf8(utf8,sv) ((utf8) ? sql_upgrade_utf8 (sv) : (sv)) |
80 | |
83 | |
81 | #define is_dbh(sv) ((sv) && sv_isobject (sv) && sv_derived_from ((sv), "DBI::db")) |
84 | #define is_dbh(sv) ((sv) && sv_isobject (sv) && sv_derived_from ((sv), "DBI::db")) |
82 | |
85 | |
|
|
86 | typedef struct mc_node |
|
|
87 | { |
|
|
88 | struct mc_node *next; |
|
|
89 | HV *stash; |
|
|
90 | U32 gen; |
|
|
91 | |
|
|
92 | /* DBH */ |
|
|
93 | SV *prepare; |
|
|
94 | |
|
|
95 | /* STH */ |
|
|
96 | SV *execute; |
|
|
97 | SV *bind_param; |
|
|
98 | SV *bind_columns; |
|
|
99 | SV *fetchrow_arrayref; |
|
|
100 | SV *fetchall_arrayref; |
|
|
101 | SV *finish; |
|
|
102 | } mc_node; |
|
|
103 | |
|
|
104 | static mc_node *first; |
|
|
105 | |
|
|
106 | static mc_node * |
|
|
107 | mc_find (HV *stash) |
|
|
108 | { |
|
|
109 | mc_node *mc; |
|
|
110 | U32 gen = PL_sub_generation; |
|
|
111 | |
|
|
112 | #ifdef HvMROMETA |
|
|
113 | gen += HvMROMETA (stash)->cache_gen; |
|
|
114 | #endif |
|
|
115 | |
|
|
116 | for (mc = first; mc; mc = mc->next) |
|
|
117 | if (mc->stash == stash && mc->gen == gen) |
|
|
118 | return mc; |
|
|
119 | |
|
|
120 | if (!mc) |
|
|
121 | { |
|
|
122 | Newz (0, mc, 1, mc_node); |
|
|
123 | mc->stash = stash; |
|
|
124 | |
|
|
125 | mc->next = first; |
|
|
126 | first = mc; |
|
|
127 | } |
|
|
128 | else |
|
|
129 | { |
|
|
130 | mc->execute = |
|
|
131 | mc->bind_param = |
|
|
132 | mc->bind_columns = |
|
|
133 | mc->fetchrow_arrayref = |
|
|
134 | mc->fetchall_arrayref = |
|
|
135 | mc->finish = 0; |
|
|
136 | } |
|
|
137 | |
|
|
138 | mc->gen = gen; |
|
|
139 | |
|
|
140 | return mc; |
|
|
141 | } |
|
|
142 | |
|
|
143 | static void |
|
|
144 | mc_cache (mc_node *mc, SV **method, const char *name) |
|
|
145 | { |
|
|
146 | *method = (SV *)gv_fetchmethod_autoload (mc->stash, name, 0); |
|
|
147 | |
|
|
148 | if (!method) |
|
|
149 | croak ("%s: method not found in stash, please report.", name); |
|
|
150 | } |
|
|
151 | |
|
|
152 | #define mc_cache(mc, method) mc_cache ((mc), &((mc)->method), # method) |
|
|
153 | |
83 | typedef struct lru_node { |
154 | typedef struct lru_node |
|
|
155 | { |
84 | struct lru_node *next; |
156 | struct lru_node *next; |
85 | struct lru_node *prev; |
157 | struct lru_node *prev; |
86 | U32 hash; /* bit 31 is used to mark active nodes */ |
158 | |
|
|
159 | U32 hash; |
87 | SV *dbh; |
160 | SV *dbh; |
88 | SV *sql; |
161 | SV *sql; |
89 | |
162 | |
90 | SV *sth; |
163 | SV *sth; |
91 | imp_sth *sth_imp; |
164 | imp_sth *sth_imp; |
92 | #if 0 /* method cache */ |
165 | |
93 | GV *execute; |
166 | mc_node *mc; |
94 | GV *bind_columns; |
|
|
95 | GV *fetch; |
|
|
96 | GV *finish; |
|
|
97 | #endif |
|
|
98 | } lru_node; |
167 | } lru_node; |
99 | |
168 | |
100 | static lru_node lru_list; |
169 | static lru_node lru_list; |
101 | static int lru_size; |
170 | static int lru_size; |
102 | static int lru_maxsize; |
171 | static int lru_maxsize; |
103 | |
172 | |
104 | #define lru_init() lru_list.next = &lru_list; lru_list.prev = &lru_list /* other fields are zero */ |
173 | #define lru_init() lru_list.next = &lru_list; lru_list.prev = &lru_list /* other fields are zero */ |
105 | |
174 | |
106 | /* this is primitive, yet effective */ |
175 | /* this is primitive, yet effective */ |
107 | /* the returned value must never be zero (or bad things will happen) */ |
176 | /* the returned value must never be zero (or bad things will happen) */ |
108 | #define lru_hash \ |
177 | static U32 |
109 | do { \ |
178 | lru_hash (SV *dbh, SV *sql) |
110 | hash = (((U32)(long)dbh)>>4); \ |
179 | { |
111 | hash += *statement; \ |
180 | STRLEN i, l; |
112 | hash += len; \ |
181 | char *b = SvPV (sql, l); |
113 | } while (0) |
182 | U32 hash = 2166136261U; |
|
|
183 | |
|
|
184 | hash = (hash ^ (U32)dbh) * 16777619U; |
|
|
185 | hash = (hash ^ l) * 16777619U; |
|
|
186 | |
|
|
187 | /* start hashing at char 8, as this skips the "select " prefix */ |
|
|
188 | for (i = 7; i < l; i += i >> 2) |
|
|
189 | hash = (hash ^ b [i]) * 16777619U; |
|
|
190 | |
|
|
191 | return hash; |
|
|
192 | } |
114 | |
193 | |
115 | /* fetch and "use" */ |
194 | /* fetch and "use" */ |
116 | /* could be done using a single call (we could call prepare!) */ |
195 | static lru_node * |
117 | static SV * |
|
|
118 | lru_fetch (SV *dbh, SV *sql) |
196 | lru_fetch (SV *dbh, SV *sql) |
119 | { |
197 | { |
120 | lru_node *n; |
198 | lru_node *n; |
121 | |
|
|
122 | U32 hash; |
199 | U32 hash; |
123 | STRLEN len; |
|
|
124 | char *statement = SvPV (sql, len); |
|
|
125 | |
200 | |
126 | dbh = SvRV (dbh); |
201 | dbh = SvRV (dbh); |
127 | |
202 | hash = lru_hash (dbh, sql); |
128 | lru_hash; |
|
|
129 | |
203 | |
130 | n = &lru_list; |
204 | n = &lru_list; |
131 | do { |
205 | do { |
132 | n = n->next; |
206 | n = n->next; |
133 | |
207 | |
… | |
… | |
145 | n->next = lru_list.next; |
219 | n->next = lru_list.next; |
146 | n->prev = &lru_list; |
220 | n->prev = &lru_list; |
147 | lru_list.next->prev = n; |
221 | lru_list.next->prev = n; |
148 | lru_list.next = n; |
222 | lru_list.next = n; |
149 | |
223 | |
150 | return sv_2mortal (SvREFCNT_inc (n->sth)); |
224 | return n; |
151 | } |
225 | } |
152 | |
226 | |
153 | static void |
227 | static void |
154 | lru_trim (void) |
228 | lru_trim (void) |
155 | { |
229 | { |
… | |
… | |
172 | } |
246 | } |
173 | } |
247 | } |
174 | |
248 | |
175 | /* store a not-yet existing entry(!) */ |
249 | /* store a not-yet existing entry(!) */ |
176 | static void |
250 | static void |
177 | lru_store (SV *dbh, SV *sql, SV *sth) |
251 | lru_store (SV *dbh, SV *sql, SV *sth, mc_node *mc) |
178 | { |
252 | { |
179 | lru_node *n; |
253 | lru_node *n; |
180 | U32 hash; |
254 | U32 hash; |
181 | STRLEN len; |
|
|
182 | char *statement; |
|
|
183 | |
255 | |
184 | if (!lru_maxsize) |
256 | if (!lru_maxsize) |
185 | return; |
257 | return; |
186 | |
258 | |
187 | statement = SvPV (sql, len); |
|
|
188 | dbh = SvRV (dbh); |
259 | dbh = SvRV (dbh); |
189 | |
260 | hash = lru_hash (dbh, sql); |
190 | lru_hash; |
|
|
191 | |
261 | |
192 | lru_size++; |
262 | lru_size++; |
193 | lru_trim (); |
263 | lru_trim (); |
194 | |
264 | |
195 | New (0, n, 1, lru_node); |
265 | New (0, n, 1, lru_node); |
… | |
… | |
197 | n->hash = hash; |
267 | n->hash = hash; |
198 | n->dbh = dbh; SvREFCNT_inc (dbh); /* note: this is the dbi hash itself, not the reference */ |
268 | n->dbh = dbh; SvREFCNT_inc (dbh); /* note: this is the dbi hash itself, not the reference */ |
199 | n->sql = newSVsv (sql); |
269 | n->sql = newSVsv (sql); |
200 | n->sth = sth; SvREFCNT_inc (sth); |
270 | n->sth = sth; SvREFCNT_inc (sth); |
201 | n->sth_imp = sth_get_imp (sth); |
271 | n->sth_imp = sth_get_imp (sth); |
|
|
272 | n->mc = mc; |
202 | |
273 | |
203 | n->next = lru_list.next; |
274 | n->next = lru_list.next; |
204 | n->prev = &lru_list; |
275 | n->prev = &lru_list; |
205 | lru_list.next->prev = n; |
276 | lru_list.next->prev = n; |
206 | lru_list.next = n; |
277 | lru_list.next = n; |
… | |
… | |
216 | } |
287 | } |
217 | } |
288 | } |
218 | |
289 | |
219 | static GV *sql_exec; |
290 | static GV *sql_exec; |
220 | static GV *DBH; |
291 | static GV *DBH; |
221 | static SV *sv_prepare, *sv_execute, *sv_bind_columns, |
|
|
222 | *sv_fetchrow_arrayref, *sv_fetchall_arrayref, |
|
|
223 | *sv_finish; |
|
|
224 | |
292 | |
225 | #define newconstpv(str) newSVpvn ((str), sizeof (str)) |
293 | #define newconstpv(str) newSVpvn ((str), sizeof (str)) |
226 | |
294 | |
227 | MODULE = PApp::SQL PACKAGE = PApp::SQL |
295 | MODULE = PApp::SQL PACKAGE = PApp::SQL |
228 | |
296 | |
… | |
… | |
234 | |
302 | |
235 | /* this is actually wrong, we should call the check member, apparently */ |
303 | /* this is actually wrong, we should call the check member, apparently */ |
236 | assert (dbis->version == DBISTATE_VERSION); |
304 | assert (dbis->version == DBISTATE_VERSION); |
237 | assert (dbis->xs_version == DBIXS_VERSION); |
305 | assert (dbis->xs_version == DBIXS_VERSION); |
238 | |
306 | |
|
|
307 | tmp_iv = newSViv (0); |
|
|
308 | |
239 | sql_exec = gv_fetchpv ("PApp::SQL::sql_exec", TRUE, SVt_PV); |
309 | sql_exec = gv_fetchpv ("PApp::SQL::sql_exec", TRUE, SVt_PV); |
240 | DBH = gv_fetchpv ("PApp::SQL::DBH" , TRUE, SVt_PV); |
310 | DBH = gv_fetchpv ("PApp::SQL::DBH" , TRUE, SVt_PV); |
241 | |
|
|
242 | if (!sv_prepare) |
|
|
243 | { |
|
|
244 | sv_prepare = newconstpv ("prepare"); |
|
|
245 | sv_execute = newconstpv ("execute"); |
|
|
246 | sv_bind_columns = newconstpv ("bind_columns"); |
|
|
247 | sv_fetchrow_arrayref = newconstpv ("fetchrow_arrayref"); |
|
|
248 | sv_fetchall_arrayref = newconstpv ("fetchall_arrayref"); |
|
|
249 | sv_finish = newconstpv ("finish"); |
|
|
250 | } |
|
|
251 | |
311 | |
252 | /* apache might BOOT: twice :( */ |
312 | /* apache might BOOT: twice :( */ |
253 | if (lru_size) |
313 | if (lru_size) |
254 | lru_cachesize (0); |
314 | lru_cachesize (0); |
255 | |
315 | |
256 | lru_init (); |
316 | lru_init (); |
257 | lru_cachesize (50); |
317 | lru_cachesize (100); |
258 | } |
318 | } |
|
|
319 | |
|
|
320 | void |
|
|
321 | boot2 (SV *t_str, SV *t_int, SV *t_dbl) |
|
|
322 | CODE: |
|
|
323 | sql_varchar = newSVsv (t_str); |
|
|
324 | sql_integer = newSVsv (t_int); |
|
|
325 | sql_double = newSVsv (t_dbl); |
259 | |
326 | |
260 | int |
327 | int |
261 | cachesize(size = -1) |
328 | cachesize(size = -1) |
262 | int size |
329 | int size |
263 | CODE: |
330 | CODE: |
… | |
… | |
268 | |
335 | |
269 | void |
336 | void |
270 | sql_exec(...) |
337 | sql_exec(...) |
271 | ALIAS: |
338 | ALIAS: |
272 | sql_uexec = 1 |
339 | sql_uexec = 1 |
273 | sql_fetch = 2 |
340 | sql_fetch = 2 |
274 | sql_ufetch = 3 |
341 | sql_ufetch = 3 |
275 | sql_fetchall = 4 |
342 | sql_fetchall = 4 |
276 | sql_ufetchall = 5 |
343 | sql_ufetchall = 5 |
277 | sql_exists = 6 |
344 | sql_exists = 6 |
278 | sql_uexists = 7 |
345 | sql_uexists = 7 |
… | |
… | |
280 | { |
347 | { |
281 | if (items == 0) |
348 | if (items == 0) |
282 | croak ("Usage: sql_exec [database-handle,] [bind-var-refs,... ] \"sql-statement\", [arguments, ...]"); |
349 | croak ("Usage: sql_exec [database-handle,] [bind-var-refs,... ] \"sql-statement\", [arguments, ...]"); |
283 | else |
350 | else |
284 | { |
351 | { |
|
|
352 | int i; |
285 | int arg = 0; |
353 | int arg = 0; |
286 | int bind_first, bind_last; |
354 | int bind_first, bind_last; |
287 | int count; |
355 | int count; |
|
|
356 | lru_node *lru; |
288 | SV *dbh = ST(0); |
357 | SV *dbh = ST(0); |
289 | SV *sth; |
358 | SV *sth; |
290 | SV *sql; |
359 | SV *sql; |
291 | SV *execute; |
360 | SV *execute; |
|
|
361 | mc_node *mc; |
292 | STRLEN dc, dd; /* dummy */ |
362 | STRLEN dc, dd; /* dummy */ |
|
|
363 | I32 orig_stack = SP - PL_stack_base; |
293 | |
364 | |
294 | /* save our arguments against destruction through function calls */ |
365 | /* save our arguments against destruction through function calls */ |
295 | SP += items; |
366 | SP += items; |
296 | |
367 | |
297 | /* first check wether we should use an explicit db handle */ |
368 | /* first check wether we should use an explicit db handle */ |
… | |
… | |
337 | sv_catpv (neu, " limit 1"); |
408 | sv_catpv (neu, " limit 1"); |
338 | sql = neu; |
409 | sql = neu; |
339 | ix -= 4; /* sql_fetch */ |
410 | ix -= 4; /* sql_fetch */ |
340 | } |
411 | } |
341 | |
412 | |
|
|
413 | /* now prepare all parameters, by unmagicalising them and upgrading them */ |
|
|
414 | for (i = arg; i < items; ++i) |
|
|
415 | { |
|
|
416 | SV *sv = ST (i); |
|
|
417 | |
|
|
418 | /* we sv_mortalcopy magical values since DBI seems to have a memory |
|
|
419 | * leak when magical values are passed into execute(). |
|
|
420 | */ |
|
|
421 | if (SvMAGICAL (sv)) |
|
|
422 | ST (i) = sv = sv_mortalcopy (sv); |
|
|
423 | |
|
|
424 | if ((ix & 1) && SvPOKp (sv) && !SvUTF8 (sv)) |
|
|
425 | { |
|
|
426 | ST (i) = sv = sv_mortalcopy (sv); |
|
|
427 | sv_utf8_upgrade (sv); |
|
|
428 | } |
|
|
429 | } |
|
|
430 | |
342 | /* check cache for existing statement handle */ |
431 | /* check cache for existing statement handle */ |
|
|
432 | lru = SvCUR (sql) <= MAX_CACHED_STATEMENT_SIZE |
343 | sth = lru_fetch (dbh, sql); |
433 | ? lru_fetch (dbh, sql) |
|
|
434 | : 0; |
344 | if (!sth) |
435 | if (!lru) |
345 | { |
436 | { |
|
|
437 | mc = mc_find (SvSTASH (SvRV (dbh))); |
|
|
438 | |
|
|
439 | if (!mc->prepare) |
|
|
440 | mc_cache (mc, prepare); |
|
|
441 | |
346 | PUSHMARK (SP); |
442 | PUSHMARK (SP); |
347 | EXTEND (SP, 2); |
443 | EXTEND (SP, 2); |
348 | PUSHs (dbh); |
444 | PUSHs (dbh); |
349 | PUSHs (sql); |
445 | PUSHs (sql); |
350 | PUTBACK; |
446 | PUTBACK; |
351 | count = call_sv (sv_prepare, G_METHOD | G_SCALAR); |
447 | count = call_sv (mc->prepare, G_SCALAR); |
352 | SPAGAIN; |
448 | SPAGAIN; |
353 | |
449 | |
354 | if (count != 1) |
450 | if (count != 1) |
355 | croak ("sql_exec: unable to prepare() statement '%s': %s", |
451 | croak ("sql_exec: unable to prepare() statement '%s': %s", |
356 | SvPV (sql, dc), |
452 | SvPV (sql, dc), |
357 | SvPV (get_sv ("DBI::errstr", TRUE), dd)); |
453 | SvPV (get_sv ("DBI::errstr", TRUE), dd)); |
358 | |
454 | |
359 | sth = POPs; |
455 | sth = POPs; |
360 | |
456 | |
|
|
457 | if (!SvROK (sth)) |
|
|
458 | croak ("sql_exec: buggy DBD driver, prepare returned non-reference for '%s': %s", |
|
|
459 | SvPV (sql, dc), |
|
|
460 | SvPV (get_sv ("DBI::errstr", TRUE), dd)); |
|
|
461 | |
|
|
462 | mc = mc_find (SvSTASH (SvRV (sth))); |
|
|
463 | |
|
|
464 | if (!mc->bind_param) |
|
|
465 | { |
|
|
466 | mc_cache (mc, bind_param); |
|
|
467 | mc_cache (mc, execute); |
|
|
468 | mc_cache (mc, finish); |
|
|
469 | } |
|
|
470 | |
361 | if (SvLEN (sql) < MAX_CACHED_STATEMENT_SIZE) |
471 | if (SvCUR (sql) <= MAX_CACHED_STATEMENT_SIZE) |
362 | lru_store (dbh, sql, sth); |
472 | lru_store (dbh, sql, sth, mc); |
|
|
473 | |
|
|
474 | /* on first execution we unfortunately need to use bind_param |
|
|
475 | * to mark any numeric parameters as such. |
363 | } |
476 | */ |
|
|
477 | SvIV_set (tmp_iv, 0); |
364 | |
478 | |
365 | PUSHMARK (SP); |
|
|
366 | EXTEND (SP, items - arg + 1); |
|
|
367 | PUSHs (sth); |
|
|
368 | while (items > arg) |
479 | while (items > arg) |
|
|
480 | { |
|
|
481 | SV *sv = ST (arg); |
|
|
482 | /* we sv_mortalcopy magical values since DBI seems to have a memory |
|
|
483 | * leak when magical values are passed into execute(). |
|
|
484 | */ |
|
|
485 | |
|
|
486 | PUSHMARK (SP); |
|
|
487 | EXTEND (SP, 4); |
|
|
488 | PUSHs (sth); |
|
|
489 | SvIVX (tmp_iv)++; |
|
|
490 | SvIOK_only (tmp_iv); |
|
|
491 | PUSHs (tmp_iv); |
|
|
492 | PUSHs (sv); |
|
|
493 | |
|
|
494 | PUSHs ( |
|
|
495 | SvPOKp (sv) ? sql_varchar |
|
|
496 | : SvNOKp (sv) ? sql_double |
|
|
497 | : SvIOKp (sv) ? sql_integer |
|
|
498 | : sql_varchar |
|
|
499 | ); |
|
|
500 | |
|
|
501 | PUTBACK; |
|
|
502 | call_sv (mc->bind_param, G_VOID); |
|
|
503 | SPAGAIN; |
|
|
504 | |
|
|
505 | arg++; |
|
|
506 | } |
|
|
507 | |
|
|
508 | /* now use execute without any arguments */ |
|
|
509 | PUSHMARK (SP); |
|
|
510 | EXTEND (SP, 1); |
|
|
511 | PUSHs (sth); |
|
|
512 | } |
|
|
513 | else |
369 | { |
514 | { |
370 | SV *sv = ST(arg); |
515 | sth = sv_2mortal (SvREFCNT_inc (lru->sth)); |
371 | /* we sv_mortalcopy magical values since DBI seems to have a memory |
516 | mc = lru->mc; |
372 | * leak when magical values are passed into execute(). |
517 | |
|
|
518 | /* we have previously executed this statement, so we |
|
|
519 | * use the cached types and use execute with arguments. |
373 | */ |
520 | */ |
374 | PUSHs (maybe_upgrade_utf8 (ix & 1, SvMAGICAL(sv) ? sv_mortalcopy(sv) : sv)); |
521 | |
|
|
522 | PUSHMARK (SP); |
|
|
523 | EXTEND (SP, items - arg + 1); |
|
|
524 | PUSHs (sth); |
|
|
525 | while (items > arg) |
|
|
526 | { |
|
|
527 | SV *sv = ST (arg); |
|
|
528 | PUSHs (sv); |
375 | arg++; |
529 | arg++; |
|
|
530 | } |
376 | } |
531 | } |
377 | |
532 | |
378 | PUTBACK; |
533 | PUTBACK; |
379 | /* { static GV *execute; |
534 | /* { static GV *execute; |
380 | if (!execute) execute = gv_fetchmethod_autoload(SvSTASH(SvRV(sth)), "execute", 0); |
535 | if (!execute) execute = gv_fetchmethod_autoload(SvSTASH(SvRV(sth)), "execute", 0); |
381 | count = call_sv(GvCV(execute), G_SCALAR); |
536 | count = call_sv(GvCV(execute), G_SCALAR); |
382 | }*/ |
537 | }*/ |
383 | count = call_sv (sv_execute, G_METHOD | G_SCALAR); |
538 | count = call_sv (mc->execute, G_SCALAR); |
384 | SPAGAIN; |
539 | SPAGAIN; |
385 | |
540 | |
386 | if (count != 1) |
541 | if (count != 1) |
387 | croak ("sql_exec: execute() didn't return any value ('%s'): %s", |
542 | croak ("sql_exec: execute() didn't return any value ('%s'): %s", |
388 | SvPV (sql, dc), |
543 | SvPV (sql, dc), |
… | |
… | |
393 | if (!SvTRUE (execute)) |
548 | if (!SvTRUE (execute)) |
394 | croak ("sql_exec: unable to execute statement '%s' (%s)", |
549 | croak ("sql_exec: unable to execute statement '%s' (%s)", |
395 | SvPV (sql, dc), |
550 | SvPV (sql, dc), |
396 | SvPV (get_sv ("DBI::errstr", TRUE), dd)); |
551 | SvPV (get_sv ("DBI::errstr", TRUE), dd)); |
397 | |
552 | |
398 | sv_setsv (GvSV(sql_exec), execute); |
553 | sv_setsv (GvSV (sql_exec), execute); |
399 | |
554 | |
400 | if (bind_first != bind_last) |
555 | if (bind_first != bind_last) |
401 | { |
556 | { |
402 | PUSHMARK (SP); |
557 | PUSHMARK (SP); |
403 | EXTEND (SP, bind_last - bind_first + 2); |
558 | EXTEND (SP, bind_last - bind_first + 2); |
… | |
… | |
410 | PUSHs (ST(bind_first)); |
565 | PUSHs (ST(bind_first)); |
411 | bind_first++; |
566 | bind_first++; |
412 | } while (bind_first != bind_last); |
567 | } while (bind_first != bind_last); |
413 | |
568 | |
414 | PUTBACK; |
569 | PUTBACK; |
|
|
570 | |
|
|
571 | if (!mc->bind_columns) |
|
|
572 | mc_cache (mc, bind_columns); |
|
|
573 | |
415 | count = call_sv (sv_bind_columns, G_METHOD | G_SCALAR); |
574 | count = call_sv (mc->bind_columns, G_SCALAR); |
|
|
575 | |
416 | SPAGAIN; |
576 | SPAGAIN; |
417 | |
577 | |
418 | if (count != 1) |
578 | if (count != 1) |
419 | croak ("sql_exec: bind_columns() didn't return any value ('%s'): %s", |
579 | croak ("sql_exec: bind_columns() didn't return any value ('%s'): %s", |
420 | SvPV (sql, dc), |
580 | SvPV (sql, dc), |
… | |
… | |
426 | SvPV (get_sv ("DBI::errstr", TRUE), dd)); |
586 | SvPV (get_sv ("DBI::errstr", TRUE), dd)); |
427 | |
587 | |
428 | POPs; |
588 | POPs; |
429 | } |
589 | } |
430 | |
590 | |
431 | /* restore our arguments again */ |
|
|
432 | SP -= items; |
|
|
433 | |
|
|
434 | if ((ix & ~1) == 2) |
591 | if ((ix & ~1) == 2) |
435 | { /* sql_fetch */ |
592 | { /* sql_fetch */ |
436 | SV *row; |
593 | SV *row; |
437 | |
594 | |
438 | PUSHMARK (SP); |
595 | PUSHMARK (SP); |
439 | XPUSHs (sth); |
596 | XPUSHs (sth); |
440 | PUTBACK; |
597 | PUTBACK; |
|
|
598 | |
|
|
599 | if (!mc->fetchrow_arrayref) |
|
|
600 | mc_cache (mc, fetchrow_arrayref); |
|
|
601 | |
441 | count = call_sv (sv_fetchrow_arrayref, G_METHOD | G_SCALAR); |
602 | count = call_sv (mc->fetchrow_arrayref, G_SCALAR); |
442 | SPAGAIN; |
603 | SPAGAIN; |
443 | |
604 | |
444 | if (count != 1) |
605 | if (count != 1) |
445 | abort (); |
606 | abort (); |
446 | |
607 | |
447 | row = POPs; |
608 | row = POPs; |
|
|
609 | |
|
|
610 | SP = PL_stack_base + orig_stack; |
448 | |
611 | |
449 | if (SvROK (row)) |
612 | if (SvROK (row)) |
450 | { |
613 | { |
451 | AV *av; |
614 | AV *av; |
452 | |
615 | |
… | |
… | |
455 | case G_VOID: |
618 | case G_VOID: |
456 | /* no thing */ |
619 | /* no thing */ |
457 | break; |
620 | break; |
458 | case G_SCALAR: |
621 | case G_SCALAR: |
459 | /* the first element */ |
622 | /* the first element */ |
460 | XPUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, *av_fetch ((AV *)SvRV (row), 0, 1))); |
623 | XPUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY ((AV *)SvRV (row))[0])); |
|
|
624 | count = 1; |
461 | break; |
625 | break; |
462 | case G_ARRAY: |
626 | case G_ARRAY: |
463 | av = (AV *)SvRV (row); |
627 | av = (AV *)SvRV (row); |
464 | count = AvFILL (av) + 1; |
628 | count = AvFILL (av) + 1; |
465 | EXTEND (SP, count); |
629 | EXTEND (SP, count); |
… | |
… | |
477 | SV *rows; |
641 | SV *rows; |
478 | |
642 | |
479 | PUSHMARK (SP); |
643 | PUSHMARK (SP); |
480 | XPUSHs (sth); |
644 | XPUSHs (sth); |
481 | PUTBACK; |
645 | PUTBACK; |
|
|
646 | |
|
|
647 | if (!mc->fetchall_arrayref) |
|
|
648 | mc_cache (mc, fetchall_arrayref); |
|
|
649 | |
482 | count = call_sv (sv_fetchall_arrayref, G_METHOD | G_SCALAR); |
650 | count = call_sv (mc->fetchall_arrayref, G_SCALAR); |
483 | SPAGAIN; |
651 | SPAGAIN; |
484 | |
652 | |
485 | if (count != 1) |
653 | if (count != 1) |
486 | abort (); |
654 | abort (); |
487 | |
655 | |
488 | rows = POPs; |
656 | rows = POPs; |
|
|
657 | |
|
|
658 | SP = PL_stack_base + orig_stack; |
489 | |
659 | |
490 | if (SvROK (rows)) |
660 | if (SvROK (rows)) |
491 | { |
661 | { |
492 | AV *av = (AV *)SvRV (rows); |
662 | AV *av = (AV *)SvRV (rows); |
493 | count = AvFILL (av) + 1; |
663 | count = AvFILL (av) + 1; |
… | |
… | |
505 | PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); |
675 | PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); |
506 | } |
676 | } |
507 | } |
677 | } |
508 | } |
678 | } |
509 | else |
679 | else |
|
|
680 | { |
|
|
681 | SP = PL_stack_base + orig_stack; |
510 | XPUSHs (sth); |
682 | XPUSHs (sth); |
|
|
683 | } |
511 | |
684 | |
512 | if (ix > 1 || GIMME_V == G_VOID) |
685 | if (ix > 1 || GIMME_V == G_VOID) |
513 | { |
686 | { |
|
|
687 | orig_stack = SP - PL_stack_base; |
|
|
688 | |
514 | PUSHMARK (SP); |
689 | PUSHMARK (SP); |
515 | XPUSHs (sth); |
690 | XPUSHs (sth); |
516 | PUTBACK; |
691 | PUTBACK; |
|
|
692 | |
|
|
693 | if (!mc->finish) |
|
|
694 | mc_cache (mc, finish); |
|
|
695 | |
517 | (void) call_sv (sv_finish, G_METHOD | G_DISCARD); |
696 | call_sv (mc->finish, G_DISCARD); |
518 | SPAGAIN; |
697 | SPAGAIN; |
|
|
698 | |
|
|
699 | SP = PL_stack_base + orig_stack; |
519 | } |
700 | } |
520 | } |
701 | } |
521 | } |
702 | } |
522 | |
703 | |
523 | |
704 | |