ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/PApp-SQL/SQL.xs
(Generate patch)

Comparing PApp-SQL/SQL.xs (file contents):
Revision 1.13 by root, Wed Jun 26 03:26:39 2002 UTC vs.
Revision 1.26 by root, Thu Sep 6 10:48:25 2012 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4
5/* import some stuff from DBIXS.h and DBI.xs */
6#define DBIXS_VERSION 93
7#define DBI_MAGIC '~'
8
9#define DBISTATE_PERLNAME "DBI::_dbistate"
10#define DBISTATE_ADDRSV (perl_get_sv (DBISTATE_PERLNAME, 0x05))
11#define DBIS_PUBLISHED_LVALUE (*(INT2PTR(dbistate_t**, &SvIVX(DBISTATE_ADDRSV))))
12
13static SV *sql_varchar, *sql_integer, *sql_double;
14static SV *tmp_iv;
15
16struct dbistate_st {
17#define DBISTATE_VERSION 94 /* Must change whenever dbistate_t does */
18 /* this must be the first member in structure */
19 void (*check_version) _((const char *name,
20 int dbis_cv, int dbis_cs, int need_dbixs_cv,
21 int drc_s, int dbc_s, int stc_s, int fdc_s));
22
23 /* version and size are used to check for DBI/DBD version mis-match */
24 U16 version; /* version of this structure */
25 U16 size;
26 U16 xs_version; /* version of the overall DBIXS / DBD interface */
27 U16 spare_pad;
28};
29typedef struct dbistate_st dbistate_t;
30
31#define DBIcf_ACTIVE 0x000004 /* needs finish/disconnect before clear */
32
33typedef U32 imp_sth;
34
35/* not strictly part of the API... */
36static imp_sth *
37sth_get_imp (SV *sth)
38{
39 MAGIC *mg = mg_find (SvRV (sth), PERL_MAGIC_tied);
40 sth = mg->mg_obj;
41 mg = mg_find (SvRV (sth), DBI_MAGIC);
42 return (imp_sth *)SvPVX (mg->mg_obj);
43}
44
45#define DBI_STH_ACTIVE(imp) (*(imp) & DBIcf_ACTIVE)
46
47/* end of import section */
4 48
5#if (PERL_VERSION < 5) || ((PERL_VERSION == 5) && (PERL_SUBVERSION <= 6)) 49#if (PERL_VERSION < 5) || ((PERL_VERSION == 5) && (PERL_SUBVERSION <= 6))
6# define get_sv perl_get_sv 50# define get_sv perl_get_sv
7# define call_method perl_call_method 51# define call_method perl_call_method
8# define call_sv perl_call_sv 52# define call_sv perl_call_sv
10 54
11#if (PERL_VERSION > 5) || ((PERL_VERSION == 5) && (PERL_SUBVERSION >= 6)) 55#if (PERL_VERSION > 5) || ((PERL_VERSION == 5) && (PERL_SUBVERSION >= 6))
12# define CAN_UTF8 1 56# define CAN_UTF8 1
13#endif 57#endif
14 58
59#define MAX_CACHED_STATEMENT_SIZE 2048
60
15static SV * 61static SV *
16sql_upgrade_utf8 (SV *sv) 62sql_upgrade_utf8 (SV *sv)
17{ 63{
18#if CAN_UTF8 64#if CAN_UTF8
19 if (SvPOK (sv)) 65 if (SvPOKp (sv))
20 sv_utf8_upgrade (sv); 66 sv_utf8_upgrade (sv);
21#endif 67#endif
22 return sv; 68 return sv;
23} 69}
24 70
25static SV * 71static SV *
26mortalcopy_and_maybe_force_utf8(int utf8, SV *sv) 72mortalcopy_and_maybe_force_utf8(int utf8, SV *sv)
27{ 73{
28 sv = sv_mortalcopy (sv); 74 sv = sv_mortalcopy (sv);
29#if CAN_UTF8 75#if CAN_UTF8
30 if (utf8 && SvPOK (sv)) 76 if (utf8 && SvPOKp (sv))
31 SvUTF8_on (sv); 77 SvUTF8_on (sv);
32#endif 78#endif
33 return sv; 79 return sv;
34} 80}
35 81
36#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))
37 83
38#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"))
39 85
86typedef 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
104static mc_node *first;
105
106static mc_node *
107mc_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
143static void
144mc_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, pelase report.", name);
150}
151
152#define mc_cache(mc, method) mc_cache ((mc), &((mc)->method), # method)
153
40typedef struct lru_node { 154typedef struct lru_node
155{
41 struct lru_node *next; 156 struct lru_node *next;
42 struct lru_node *prev; 157 struct lru_node *prev;
158
43 U32 hash; 159 U32 hash;
44 SV *dbh; 160 SV *dbh;
45 SV *sql; 161 SV *sql;
46 162
47 SV *sth; 163 SV *sth;
48#if 0 /* method cache */ 164 imp_sth *sth_imp;
49 GV *execute; 165
50 GV *bind_columns; 166 mc_node *mc;
51 GV *fetch;
52 GV *finish;
53#endif
54} lru_node; 167} lru_node;
55 168
56static lru_node lru_list; 169static lru_node lru_list;
57static int lru_size; 170static int lru_size;
58static int lru_maxsize; 171static int lru_maxsize;
59 172
60#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 */
61 174
62/* this is primitive, yet effective */ 175/* this is primitive, yet effective */
63/* 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) */
64#define lru_hash do { \ 177static U32
65 hash = (((U32)dbh)>>2); \ 178lru_hash (SV *dbh, SV *sql)
66 hash += *statement;\ 179{
67 hash += len; \ 180 STRLEN i, l;
68} while (0) 181 char *b = SvPV (sql, l);
182 U32 hash = 2166136261U;
183
184 hash = (hash ^ (U32)dbh) * 16777619U;
185 hash = (hash ^ l) * 16777619U;
186
187 for (i = 7; i < l; i += i >> 2)
188 hash = (hash ^ b [i]) * 16777619U;
189
190 return hash;
191}
69 192
70/* fetch and "use" */ 193/* fetch and "use" */
71/* could be done using a single call (we could call prepare!) */ 194static lru_node *
72static SV *lru_fetch(SV *dbh, SV *sql) 195lru_fetch (SV *dbh, SV *sql)
73{ 196{
74 lru_node *n; 197 lru_node *n;
75
76 U32 hash; 198 U32 hash;
77 STRLEN len;
78 char *statement = SvPV (sql, len);
79 199
80 dbh = SvRV (dbh); 200 dbh = SvRV (dbh);
81 201 hash = lru_hash (dbh, sql);
82 lru_hash;
83 202
84 n = &lru_list; 203 n = &lru_list;
85 do { 204 do {
86 n = n->next; 205 n = n->next;
206
87 if (!n->hash) 207 if (!n->hash)
88 return 0; 208 return 0;
89 } while (n->hash != hash 209 } while (n->hash != hash
210 || DBI_STH_ACTIVE (n->sth_imp)
90 || !sv_eq (n->sql, sql) 211 || !sv_eq (n->sql, sql)
91 || n->dbh != dbh); 212 || n->dbh != dbh);
92 213
93 /* found, so return to the start of the list */ 214 /* found, so return to the start of the list */
94 n->prev->next = n->next; 215 n->prev->next = n->next;
97 n->next = lru_list.next; 218 n->next = lru_list.next;
98 n->prev = &lru_list; 219 n->prev = &lru_list;
99 lru_list.next->prev = n; 220 lru_list.next->prev = n;
100 lru_list.next = n; 221 lru_list.next = n;
101 222
102 return n->sth; 223 return n;
103} 224}
104 225
105static void lru_nukeone(void) 226static void
227lru_trim (void)
228{
229 while (lru_size > lru_maxsize)
230 {
231 /* nuke at the end */
232 lru_node *n = lru_list.prev;
233
234 n = lru_list.prev;
235
236 lru_list.prev = n->prev;
237 n->prev->next = &lru_list;
238
239 SvREFCNT_dec (n->dbh);
240 SvREFCNT_dec (n->sql);
241 SvREFCNT_dec (n->sth);
242 Safefree (n);
243
244 lru_size--;
245 }
246}
247
248/* store a not-yet existing entry(!) */
249static void
250lru_store (SV *dbh, SV *sql, SV *sth, mc_node *mc)
106{ 251{
107 lru_node *n; 252 lru_node *n;
108 /* nuke at the end */ 253 U32 hash;
109 254
110 n = lru_list.prev; 255 if (!lru_maxsize)
111 256 return;
112 lru_list.prev = n->prev;
113 n->prev->next = &lru_list;
114
115 SvREFCNT_dec (n->dbh);
116 SvREFCNT_dec (n->sql);
117 SvREFCNT_dec (n->sth);
118 Safefree (n);
119 257
120 lru_size--;
121}
122
123/* store a not-yet existing entry(!) */
124static void lru_store(SV *dbh, SV *sql, SV *sth)
125{
126 lru_node *n;
127
128 U32 hash;
129 STRLEN len;
130 char *statement = SvPV (sql, len);
131
132 dbh = SvRV (dbh); 258 dbh = SvRV (dbh);
133 259 hash = lru_hash (dbh, sql);
134 lru_hash;
135 260
136 lru_size++; 261 lru_size++;
137 if (lru_size > lru_maxsize) 262 lru_trim ();
138 lru_nukeone ();
139 263
140 New (0, n, 1, lru_node); 264 New (0, n, 1, lru_node);
141 265
142 n->hash = hash; 266 n->hash = hash;
143 n->dbh = dbh; SvREFCNT_inc (dbh); /* note: this is the dbi hash itself, not the reference */ 267 n->dbh = dbh; SvREFCNT_inc (dbh); /* note: this is the dbi hash itself, not the reference */
144 n->sql = newSVsv (sql); 268 n->sql = newSVsv (sql);
145 n->sth = sth; SvREFCNT_inc (sth); 269 n->sth = sth; SvREFCNT_inc (sth);
270 n->sth_imp = sth_get_imp (sth);
271 n->mc = mc;
146 272
147 n->next = lru_list.next; 273 n->next = lru_list.next;
148 n->prev = &lru_list; 274 n->prev = &lru_list;
149 lru_list.next->prev = n; 275 lru_list.next->prev = n;
150 lru_list.next = n; 276 lru_list.next = n;
151} 277}
152 278
279static void
153static void lru_cachesize (int size) 280lru_cachesize (int size)
154{ 281{
155 if (size >= 0) 282 if (size >= 0)
156 { 283 {
157 lru_maxsize = size; 284 lru_maxsize = size;
158 while (lru_size > lru_maxsize) 285 lru_trim ();
159 lru_nukeone ();
160 } 286 }
161} 287}
162 288
163static GV *sql_exec; 289static GV *sql_exec;
164static GV *DBH; 290static GV *DBH;
165static SV *sv_prepare, *sv_execute, *sv_bind_columns,
166 *sv_fetchrow_arrayref, *sv_fetchall_arrayref,
167 *sv_finish;
168 291
169#define newconstpv(str) newSVpvn ((str), sizeof (str)) 292#define newconstpv(str) newSVpvn ((str), sizeof (str))
170 293
171MODULE = PApp::SQL PACKAGE = PApp::SQL 294MODULE = PApp::SQL PACKAGE = PApp::SQL
172 295
173PROTOTYPES: DISABLE 296PROTOTYPES: DISABLE
174 297
175BOOT: 298BOOT:
176{ 299{
300 struct dbistate_st *dbis = DBIS_PUBLISHED_LVALUE;
301
302 /* this is actually wrong, we should call the check member, apparently */
303 assert (dbis->version == DBISTATE_VERSION);
304 assert (dbis->xs_version == DBIXS_VERSION);
305
306 tmp_iv = newSViv (0);
307
177 sql_exec = gv_fetchpv ("PApp::SQL::sql_exec", TRUE, SVt_PV); 308 sql_exec = gv_fetchpv ("PApp::SQL::sql_exec", TRUE, SVt_PV);
178 DBH = gv_fetchpv ("PApp::SQL::DBH" , TRUE, SVt_PV); 309 DBH = gv_fetchpv ("PApp::SQL::DBH" , TRUE, SVt_PV);
179
180 if (!sv_prepare)
181 {
182 sv_prepare = newconstpv ("prepare");
183 sv_execute = newconstpv ("execute");
184 sv_bind_columns = newconstpv ("bind_columns");
185 sv_fetchrow_arrayref = newconstpv ("fetchrow_arrayref");
186 sv_fetchall_arrayref = newconstpv ("fetchall_arrayref");
187 sv_finish = newconstpv ("finish");
188 }
189 310
190 /* apache might BOOT: twice :( */ 311 /* apache might BOOT: twice :( */
191 if (lru_size) 312 if (lru_size)
192 lru_cachesize (0); 313 lru_cachesize (0);
193 314
194 lru_init; 315 lru_init ();
195 lru_cachesize (50); 316 lru_cachesize (100);
196} 317}
318
319void
320boot2 (SV *t_str, SV *t_int, SV *t_dbl)
321 CODE:
322 sql_varchar = newSVsv (t_str);
323 sql_integer = newSVsv (t_int);
324 sql_double = newSVsv (t_dbl);
197 325
198int 326int
199cachesize(size = -1) 327cachesize(size = -1)
200 int size 328 int size
201 CODE: 329 CODE:
218{ 346{
219 if (items == 0) 347 if (items == 0)
220 croak ("Usage: sql_exec [database-handle,] [bind-var-refs,... ] \"sql-statement\", [arguments, ...]"); 348 croak ("Usage: sql_exec [database-handle,] [bind-var-refs,... ] \"sql-statement\", [arguments, ...]");
221 else 349 else
222 { 350 {
351 int i;
223 int arg = 0; 352 int arg = 0;
353 int first_execution = 0;
224 int bind_first, bind_last; 354 int bind_first, bind_last;
225 int count; 355 int count;
356 lru_node *lru;
226 SV *dbh = ST(0); 357 SV *dbh = ST(0);
227 SV *sth; 358 SV *sth;
228 SV *sql; 359 SV *sql;
229 SV *execute; 360 SV *execute;
361 mc_node *mc;
230 STRLEN dc, dd; /* dummy */ 362 STRLEN dc, dd; /* dummy */
363 I32 orig_stack = SP - PL_stack_base;
231 364
232 /* save our arguments against destruction through function calls */ 365 /* save our arguments against destruction through function calls */
233 SP += items; 366 SP += items;
234 367
235 /* first check wether we should use an explicit db handle */ 368 /* first check wether we should use an explicit db handle */
237 { 370 {
238 /* the next line doesn't work - check why later maybe */ 371 /* the next line doesn't work - check why later maybe */
239 /* dbh = get_sv ("DBH", FALSE); 372 /* dbh = get_sv ("DBH", FALSE);
240 if (!is_dbh (dbh)) 373 if (!is_dbh (dbh))
241 {*/ 374 {*/
242 dbh = GvSV(DBH); 375 dbh = GvSV (DBH);
243 if (!is_dbh (dbh)) 376 if (!is_dbh (dbh))
244 croak ("sql_exec: no $DBH argument and no fallback in $PApp::SQL::DBH"); 377 croak ("sql_exec: no $DBH argument and no fallback in $PApp::SQL::DBH");
245 /*croak ("sql_exec: no $DBH found in current package or in PApp::SQL::"); 378 /*croak ("sql_exec: no $DBH found in current package or in PApp::SQL::");
246 }*/ 379 }*/
247 } 380 }
248 else 381 else
249 arg++; /* we consumed one argument */ 382 arg++; /* we consumed one argument */
383
384 /* be more Coro-friendly by keeping a copy, so different threads */
385 /* can replace their global handles */
386 dbh = sv_2mortal (newSVsv (dbh));
250 387
251 /* count the remaining references (for bind_columns) */ 388 /* count the remaining references (for bind_columns) */
252 bind_first = arg; 389 bind_first = arg;
253 while (items > arg && SvROK (ST(arg))) 390 while (items > arg && SvROK (ST(arg)))
254 arg++; 391 arg++;
271 sv_catpv (neu, " limit 1"); 408 sv_catpv (neu, " limit 1");
272 sql = neu; 409 sql = neu;
273 ix -= 4; /* sql_fetch */ 410 ix -= 4; /* sql_fetch */
274 } 411 }
275 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
276 /* check cache for existing statement handle */ 431 /* check cache for existing statement handle */
432 lru = SvCUR (sql) <= MAX_CACHED_STATEMENT_SIZE
277 sth = lru_fetch (dbh, sql); 433 ? lru_fetch (dbh, sql)
434 : 0;
278 if (!sth) 435 if (!lru)
279 { 436 {
437 mc = mc_find (SvSTASH (SvRV (dbh)));
438
439 if (!mc->prepare)
440 mc_cache (mc, prepare);
441
280 PUSHMARK (SP); 442 PUSHMARK (SP);
281 EXTEND (SP, 2); 443 EXTEND (SP, 2);
282 PUSHs (dbh); 444 PUSHs (dbh);
283 PUSHs (sql); 445 PUSHs (sql);
284 PUTBACK; 446 PUTBACK;
285 count = call_sv (sv_prepare, G_METHOD | G_SCALAR); 447 count = call_sv (mc->prepare, G_SCALAR);
286 SPAGAIN; 448 SPAGAIN;
287 449
288 if (count != 1) 450 if (count != 1)
289 croak ("sql_exec: unable to prepare() statement '%s': %s", 451 croak ("sql_exec: unable to prepare() statement '%s': %s",
290 SvPV (sql, dc), 452 SvPV (sql, dc),
291 SvPV (get_sv ("DBI::errstr", TRUE), dd)); 453 SvPV (get_sv ("DBI::errstr", TRUE), dd));
292 454
293 sth = POPs; 455 sth = POPs;
294 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
471 if (SvCUR (sql) <= MAX_CACHED_STATEMENT_SIZE)
295 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.
296 } 476 */
477 SvIV_set (tmp_iv, 0);
297 478
298 PUSHMARK (SP);
299 EXTEND (SP, items - arg + 1);
300 PUSHs (sth);
301 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 PUSHs (tmp_iv);
491 PUSHs (sv);
492
493 PUSHs (
494 SvPOKp (sv) ? sql_varchar
495 : SvNOKp (sv) ? sql_double
496 : SvIOKp (sv) ? sql_integer
497 : sql_varchar
498 );
499
500 PUTBACK;
501 call_sv (mc->bind_param, G_VOID);
502 SPAGAIN;
503
504 arg++;
505 }
506
507 /* now use execute without any arguments */
508 PUSHMARK (SP);
509 EXTEND (SP, 1);
510 PUSHs (sth);
511 }
512 else
302 { 513 {
303 PUSHs (maybe_upgrade_utf8 (ix & 1, ST(arg))); 514 sth = sv_2mortal (SvREFCNT_inc (lru->sth));
515 mc = lru->mc;
516
517 /* we have previously executed this statement, so we
518 * use the cached types and use execute with arguments.
519 */
520
521 PUSHMARK (SP);
522 EXTEND (SP, items - arg + 1);
523 PUSHs (sth);
524 while (items > arg)
525 {
526 SV *sv = ST (arg);
527 PUSHs (ST (arg));
304 arg++; 528 arg++;
529 }
305 } 530 }
306 531
307 PUTBACK; 532 PUTBACK;
308 /* { static GV *execute; 533 /* { static GV *execute;
309 if (!execute) execute = gv_fetchmethod_autoload(SvSTASH(SvRV(sth)), "execute", 0); 534 if (!execute) execute = gv_fetchmethod_autoload(SvSTASH(SvRV(sth)), "execute", 0);
310 count = call_sv(GvCV(execute), G_SCALAR); 535 count = call_sv(GvCV(execute), G_SCALAR);
311 }*/ 536 }*/
312 count = call_sv (sv_execute, G_METHOD | G_SCALAR); 537 count = call_sv (mc->execute, G_SCALAR);
313 SPAGAIN; 538 SPAGAIN;
314 539
315 if (count != 1) 540 if (count != 1)
316 croak ("sql_exec: execute() didn't return any value ('%s'): %s", 541 croak ("sql_exec: execute() didn't return any value ('%s'): %s",
317 SvPV (sql, dc), 542 SvPV (sql, dc),
322 if (!SvTRUE (execute)) 547 if (!SvTRUE (execute))
323 croak ("sql_exec: unable to execute statement '%s' (%s)", 548 croak ("sql_exec: unable to execute statement '%s' (%s)",
324 SvPV (sql, dc), 549 SvPV (sql, dc),
325 SvPV (get_sv ("DBI::errstr", TRUE), dd)); 550 SvPV (get_sv ("DBI::errstr", TRUE), dd));
326 551
327 sv_setsv (GvSV(sql_exec), execute); 552 sv_setsv (GvSV (sql_exec), execute);
328 553
329 if (bind_first != bind_last) 554 if (bind_first != bind_last)
330 { 555 {
331 PUSHMARK (SP); 556 PUSHMARK (SP);
332 EXTEND (SP, bind_last - bind_first + 2); 557 EXTEND (SP, bind_last - bind_first + 2);
333 PUSHs (sth); 558 PUSHs (sth);
334 do { 559 do {
560#if CAN_UTF8
561 if (ix & 1)
562 SvUTF8_on (SvRV(ST(bind_first)));
563#endif
335 PUSHs (ST(bind_first)); 564 PUSHs (ST(bind_first));
336 bind_first++; 565 bind_first++;
337 } while (bind_first != bind_last); 566 } while (bind_first != bind_last);
338 567
339 PUTBACK; 568 PUTBACK;
569
570 if (!mc->bind_columns)
571 mc_cache (mc, bind_columns);
572
340 count = call_sv (sv_bind_columns, G_METHOD | G_SCALAR); 573 count = call_sv (mc->bind_columns, G_SCALAR);
574
341 SPAGAIN; 575 SPAGAIN;
342 576
343 if (count != 1) 577 if (count != 1)
344 croak ("sql_exec: bind_columns() didn't return any value ('%s'): %s", 578 croak ("sql_exec: bind_columns() didn't return any value ('%s'): %s",
345 SvPV (sql, dc), 579 SvPV (sql, dc),
346 SvPV (get_sv ("DBI::errstr", TRUE), dd)); 580 SvPV (get_sv ("DBI::errstr", TRUE), dd));
347 581
348 if (!SvOK (POPs)) 582 if (!SvOK (TOPs))
349 croak ("sql_exec: bind_columns() didn't return a true ('%s'): %s", 583 croak ("sql_exec: bind_columns() didn't return a true ('%s'): %s",
350 SvPV (sql, dc), 584 SvPV (sql, dc),
351 SvPV (get_sv ("DBI::errstr", TRUE), dd)); 585 SvPV (get_sv ("DBI::errstr", TRUE), dd));
586
352 } 587 POPs;
353 588 }
354 /* free our arguments from the stack */
355 SP -= items;
356 589
357 if ((ix & ~1) == 2) 590 if ((ix & ~1) == 2)
358 { /* sql_fetch */ 591 { /* sql_fetch */
359 SV *row; 592 SV *row;
360 593
361 PUSHMARK (SP); 594 PUSHMARK (SP);
362 XPUSHs (sth); 595 XPUSHs (sth);
363 PUTBACK; 596 PUTBACK;
597
598 if (!mc->fetchrow_arrayref)
599 mc_cache (mc, fetchrow_arrayref);
600
364 count = call_sv (sv_fetchrow_arrayref, G_METHOD | G_SCALAR); 601 count = call_sv (mc->fetchrow_arrayref, G_SCALAR);
365 SPAGAIN; 602 SPAGAIN;
366 603
367 if (count != 1) 604 if (count != 1)
368 abort (); 605 abort ();
369 606
370 row = POPs; 607 row = POPs;
608
609 SP = PL_stack_base + orig_stack;
371 610
372 if (SvROK (row)) 611 if (SvROK (row))
373 { 612 {
374 AV *av; 613 AV *av;
375 614
379 /* no thing */ 618 /* no thing */
380 break; 619 break;
381 case G_SCALAR: 620 case G_SCALAR:
382 /* the first element */ 621 /* the first element */
383 XPUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, *av_fetch ((AV *)SvRV (row), 0, 1))); 622 XPUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, *av_fetch ((AV *)SvRV (row), 0, 1)));
623 count = 1;
384 break; 624 break;
385 case G_ARRAY: 625 case G_ARRAY:
386 av = (AV *)SvRV (row); 626 av = (AV *)SvRV (row);
387 count = AvFILL (av) + 1; 627 count = AvFILL (av) + 1;
388 EXTEND (SP, count); 628 EXTEND (SP, count);
400 SV *rows; 640 SV *rows;
401 641
402 PUSHMARK (SP); 642 PUSHMARK (SP);
403 XPUSHs (sth); 643 XPUSHs (sth);
404 PUTBACK; 644 PUTBACK;
645
646 if (!mc->fetchall_arrayref)
647 mc_cache (mc, fetchall_arrayref);
648
405 count = call_sv (sv_fetchall_arrayref, G_METHOD | G_SCALAR); 649 count = call_sv (mc->fetchall_arrayref, G_SCALAR);
406 SPAGAIN; 650 SPAGAIN;
407 651
408 if (count != 1) 652 if (count != 1)
409 abort (); 653 abort ();
410 654
411 rows = POPs; 655 rows = POPs;
656
657 SP = PL_stack_base + orig_stack;
412 658
413 if (SvROK (rows)) 659 if (SvROK (rows))
414 { 660 {
415 AV *av = (AV *)SvRV (rows); 661 AV *av = (AV *)SvRV (rows);
416 count = AvFILL (av) + 1; 662 count = AvFILL (av) + 1;
428 PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); 674 PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY (av)[arg]));
429 } 675 }
430 } 676 }
431 } 677 }
432 else 678 else
679 {
680 SP = PL_stack_base + orig_stack;
433 XPUSHs (sth); 681 XPUSHs (sth);
682 }
434 683
435 if (ix > 1 || GIMME_V == G_VOID) 684 if (ix > 1 || GIMME_V == G_VOID)
436 { 685 {
686 orig_stack = SP - PL_stack_base;
687
437 PUSHMARK (SP); 688 PUSHMARK (SP);
438 XPUSHs (sth); 689 XPUSHs (sth);
439 PUTBACK; 690 PUTBACK;
691
692 if (!mc->finish)
693 mc_cache (mc, finish);
694
440 (void) call_sv (sv_finish, G_METHOD | G_DISCARD); 695 call_sv (mc->finish, G_DISCARD);
441 SPAGAIN; 696 SPAGAIN;
697
698 SP = PL_stack_base + orig_stack;
442 } 699 }
443 } 700 }
444} 701}
445 702
446 703

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines