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.9 by root, Mon Dec 31 03:05:03 2001 UTC vs.
Revision 1.27 by root, Thu Sep 20 09:00:44 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
9#endif 53#endif
10 54
11#if (PERL_VERSION < 5) || ((PERL_VERSION == 5) && (PERL_SUBVERSION <= 5)) 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 *
26sql_force_utf8 (SV *sv) 72mortalcopy_and_maybe_force_utf8(int utf8, SV *sv)
27{ 73{
74 sv = sv_mortalcopy (sv);
28#if CAN_UTF8 75#if CAN_UTF8
29 if (SvPOK (sv)) 76 if (utf8 && SvPOKp (sv))
30 SvUTF8_on (sv); 77 SvUTF8_on (sv);
31#endif 78#endif
32 return sv; 79 return sv;
33} 80}
34 81
35#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))
36#define maybe_force_utf8(utf8,sv) ((utf8) ? sql_force_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:
206 334
207void 335void
208sql_exec(...) 336sql_exec(...)
209 ALIAS: 337 ALIAS:
210 sql_uexec = 1 338 sql_uexec = 1
211 sql_fetch = 2 339 sql_fetch = 2
212 sql_ufetch = 3 340 sql_ufetch = 3
213 sql_fetchall = 4 341 sql_fetchall = 4
214 sql_ufetchall = 5 342 sql_ufetchall = 5
215 sql_exists = 6 343 sql_exists = 6
216 sql_uexists = 7 344 sql_uexists = 7
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;
224 int bind_first, bind_last; 353 int bind_first, bind_last;
225 int count; 354 int count;
355 lru_node *lru;
226 SV *dbh = ST(0); 356 SV *dbh = ST(0);
227 SV *sth; 357 SV *sth;
228 SV *sql; 358 SV *sql;
229 SV *execute; 359 SV *execute;
360 mc_node *mc;
230 STRLEN dc, dd; /* dummy */ 361 STRLEN dc, dd; /* dummy */
362 I32 orig_stack = SP - PL_stack_base;
231 363
232 /* save our arguments against destruction through function calls */ 364 /* save our arguments against destruction through function calls */
233 SP += items; 365 SP += items;
234 366
235 /* first check wether we should use an explicit db handle */ 367 /* first check wether we should use an explicit db handle */
236 if (!is_dbh (dbh)) 368 if (!is_dbh (dbh))
237 { 369 {
370 /* the next line doesn't work - check why later maybe */
238 dbh = get_sv ("DBH", FALSE); 371 /* dbh = get_sv ("DBH", FALSE);
239 if (!is_dbh (dbh)) 372 if (!is_dbh (dbh))
240 { 373 {*/
241 dbh = GvSV(DBH); 374 dbh = GvSV (DBH);
242 if (!is_dbh (dbh)) 375 if (!is_dbh (dbh))
376 croak ("sql_exec: no $DBH argument and no fallback in $PApp::SQL::DBH");
243 croak ("sql_exec: no $DBH found in current package or in PApp::SQL::"); 377 /*croak ("sql_exec: no $DBH found in current package or in PApp::SQL::");
244 } 378 }*/
245 } 379 }
246 else 380 else
247 arg++; /* we consumed one argument */ 381 arg++; /* we consumed one argument */
382
383 /* be more Coro-friendly by keeping a copy, so different threads */
384 /* can replace their global handles */
385 dbh = sv_2mortal (newSVsv (dbh));
248 386
249 /* count the remaining references (for bind_columns) */ 387 /* count the remaining references (for bind_columns) */
250 bind_first = arg; 388 bind_first = arg;
251 while (items > arg && SvROK (ST(arg))) 389 while (items > arg && SvROK (ST(arg)))
252 arg++; 390 arg++;
269 sv_catpv (neu, " limit 1"); 407 sv_catpv (neu, " limit 1");
270 sql = neu; 408 sql = neu;
271 ix -= 4; /* sql_fetch */ 409 ix -= 4; /* sql_fetch */
272 } 410 }
273 411
412 /* now prepare all parameters, by unmagicalising them and upgrading them */
413 for (i = arg; i < items; ++i)
414 {
415 SV *sv = ST (i);
416
417 /* we sv_mortalcopy magical values since DBI seems to have a memory
418 * leak when magical values are passed into execute().
419 */
420 if (SvMAGICAL (sv))
421 ST (i) = sv = sv_mortalcopy (sv);
422
423 if ((ix & 1) && SvPOKp (sv) && !SvUTF8 (sv))
424 {
425 ST (i) = sv = sv_mortalcopy (sv);
426 sv_utf8_upgrade (sv);
427 }
428 }
429
274 /* check cache for existing statement handle */ 430 /* check cache for existing statement handle */
431 lru = SvCUR (sql) <= MAX_CACHED_STATEMENT_SIZE
275 sth = lru_fetch (dbh, sql); 432 ? lru_fetch (dbh, sql)
433 : 0;
276 if (!sth) 434 if (!lru)
277 { 435 {
436 mc = mc_find (SvSTASH (SvRV (dbh)));
437
438 if (!mc->prepare)
439 mc_cache (mc, prepare);
440
278 PUSHMARK (SP); 441 PUSHMARK (SP);
279 EXTEND (SP, 2); 442 EXTEND (SP, 2);
280 PUSHs (dbh); 443 PUSHs (dbh);
281 PUSHs (sql); 444 PUSHs (sql);
282 PUTBACK; 445 PUTBACK;
283 count = call_sv (sv_prepare, G_METHOD | G_SCALAR); 446 count = call_sv (mc->prepare, G_SCALAR);
284 SPAGAIN; 447 SPAGAIN;
285 448
286 if (count != 1) 449 if (count != 1)
287 croak ("sql_exec: unable to prepare() statement '%s': %s", 450 croak ("sql_exec: unable to prepare() statement '%s': %s",
288 SvPV (sql, dc), 451 SvPV (sql, dc),
289 SvPV (get_sv ("DBI::errstr", TRUE), dd)); 452 SvPV (get_sv ("DBI::errstr", TRUE), dd));
290 453
291 sth = POPs; 454 sth = POPs;
292 455
456 if (!SvROK (sth))
457 croak ("sql_exec: buggy DBD driver, prepare returned non-reference for '%s': %s",
458 SvPV (sql, dc),
459 SvPV (get_sv ("DBI::errstr", TRUE), dd));
460
461 mc = mc_find (SvSTASH (SvRV (sth)));
462
463 if (!mc->bind_param)
464 {
465 mc_cache (mc, bind_param);
466 mc_cache (mc, execute);
467 mc_cache (mc, finish);
468 }
469
470 if (SvCUR (sql) <= MAX_CACHED_STATEMENT_SIZE)
293 lru_store (dbh, sql, sth); 471 lru_store (dbh, sql, sth, mc);
472
473 /* on first execution we unfortunately need to use bind_param
474 * to mark any numeric parameters as such.
294 } 475 */
476 SvIV_set (tmp_iv, 0);
295 477
296 PUSHMARK (SP);
297 EXTEND (SP, items - arg + 1);
298 PUSHs (sth);
299 while (items > arg) 478 while (items > arg)
479 {
480 SV *sv = ST (arg);
481 /* we sv_mortalcopy magical values since DBI seems to have a memory
482 * leak when magical values are passed into execute().
483 */
484
485 PUSHMARK (SP);
486 EXTEND (SP, 4);
487 PUSHs (sth);
488 SvIVX (tmp_iv)++;
489 PUSHs (tmp_iv);
490 PUSHs (sv);
491
492 PUSHs (
493 SvPOKp (sv) ? sql_varchar
494 : SvNOKp (sv) ? sql_double
495 : SvIOKp (sv) ? sql_integer
496 : sql_varchar
497 );
498
499 PUTBACK;
500 call_sv (mc->bind_param, G_VOID);
501 SPAGAIN;
502
503 arg++;
504 }
505
506 /* now use execute without any arguments */
507 PUSHMARK (SP);
508 EXTEND (SP, 1);
509 PUSHs (sth);
510 }
511 else
300 { 512 {
301 PUSHs (maybe_upgrade_utf8 (ix & 1, ST(arg))); 513 sth = sv_2mortal (SvREFCNT_inc (lru->sth));
514 mc = lru->mc;
515
516 /* we have previously executed this statement, so we
517 * use the cached types and use execute with arguments.
518 */
519
520 PUSHMARK (SP);
521 EXTEND (SP, items - arg + 1);
522 PUSHs (sth);
523 while (items > arg)
524 {
525 SV *sv = ST (arg);
526 PUSHs (sv);
302 arg++; 527 arg++;
528 }
303 } 529 }
304 530
305 PUTBACK; 531 PUTBACK;
306 /* { static GV *execute; 532 /* { static GV *execute;
307 if (!execute) execute = gv_fetchmethod_autoload(SvSTASH(SvRV(sth)), "execute", 0); 533 if (!execute) execute = gv_fetchmethod_autoload(SvSTASH(SvRV(sth)), "execute", 0);
308 count = call_sv(GvCV(execute), G_SCALAR); 534 count = call_sv(GvCV(execute), G_SCALAR);
309 }*/ 535 }*/
310 count = call_sv (sv_execute, G_METHOD | G_SCALAR); 536 count = call_sv (mc->execute, G_SCALAR);
311 SPAGAIN; 537 SPAGAIN;
312 538
313 if (count != 1) 539 if (count != 1)
314 croak ("sql_exec: execute() didn't return any value ('%s'): %s", 540 croak ("sql_exec: execute() didn't return any value ('%s'): %s",
315 SvPV (sql, dc), 541 SvPV (sql, dc),
316 SvPV (get_sv ("DBI::errstr", TRUE), dc)); 542 SvPV (get_sv ("DBI::errstr", TRUE), dd));
317 543
318 execute = POPs; 544 execute = POPs;
319 545
320 if (!SvTRUE (execute)) 546 if (!SvTRUE (execute))
321 croak ("sql_exec: unable to execute statement '%s' (%s)", 547 croak ("sql_exec: unable to execute statement '%s' (%s)",
322 SvPV (sql, dc), 548 SvPV (sql, dc),
323 SvPV (get_sv ("DBI::errstr", TRUE), dc)); 549 SvPV (get_sv ("DBI::errstr", TRUE), dd));
324 550
325 sv_setsv (GvSV(sql_exec), execute); 551 sv_setsv (GvSV (sql_exec), execute);
326 552
327 if (bind_first != bind_last) 553 if (bind_first != bind_last)
328 { 554 {
329 PUSHMARK (SP); 555 PUSHMARK (SP);
330 EXTEND (SP, bind_last - bind_first + 2); 556 EXTEND (SP, bind_last - bind_first + 2);
331 PUSHs (sth); 557 PUSHs (sth);
332 do { 558 do {
559#if CAN_UTF8
560 if (ix & 1)
561 SvUTF8_on (SvRV(ST(bind_first)));
562#endif
333 PUSHs (ST(bind_first)); 563 PUSHs (ST(bind_first));
334 bind_first++; 564 bind_first++;
335 } while (bind_first != bind_last); 565 } while (bind_first != bind_last);
336 566
337 PUTBACK; 567 PUTBACK;
568
569 if (!mc->bind_columns)
570 mc_cache (mc, bind_columns);
571
338 count = call_sv (sv_bind_columns, G_METHOD | G_SCALAR); 572 count = call_sv (mc->bind_columns, G_SCALAR);
573
339 SPAGAIN; 574 SPAGAIN;
340 575
341 if (count != 1) 576 if (count != 1)
342 croak ("sql_exec: bind_columns() didn't return any value ('%s'): %s", 577 croak ("sql_exec: bind_columns() didn't return any value ('%s'): %s",
343 SvPV (sql, dc), 578 SvPV (sql, dc),
344 SvPV (get_sv ("DBI::errstr", TRUE), dc)); 579 SvPV (get_sv ("DBI::errstr", TRUE), dd));
345 580
346 if (!SvOK (POPs)) 581 if (!SvOK (TOPs))
347 croak ("sql_exec: bind_columns() didn't return a true ('%s'): %s", 582 croak ("sql_exec: bind_columns() didn't return a true ('%s'): %s",
348 SvPV (sql, dc), 583 SvPV (sql, dc),
349 SvPV (get_sv ("DBI::errstr", TRUE), dc)); 584 SvPV (get_sv ("DBI::errstr", TRUE), dd));
585
350 } 586 POPs;
351 587 }
352 /* free our arguments from the stack */
353 SP -= items;
354 588
355 if ((ix & ~1) == 2) 589 if ((ix & ~1) == 2)
356 { /* sql_fetch */ 590 { /* sql_fetch */
357 SV *row; 591 SV *row;
358 592
359 PUSHMARK (SP); 593 PUSHMARK (SP);
360 XPUSHs (sth); 594 XPUSHs (sth);
361 PUTBACK; 595 PUTBACK;
596
597 if (!mc->fetchrow_arrayref)
598 mc_cache (mc, fetchrow_arrayref);
599
362 count = call_sv (sv_fetchrow_arrayref, G_METHOD | G_SCALAR); 600 count = call_sv (mc->fetchrow_arrayref, G_SCALAR);
363 SPAGAIN; 601 SPAGAIN;
364 602
365 if (count != 1) 603 if (count != 1)
366 abort (); 604 abort ();
367 605
368 row = POPs; 606 row = POPs;
607
608 SP = PL_stack_base + orig_stack;
369 609
370 if (SvROK (row)) 610 if (SvROK (row))
371 { 611 {
372 AV *av; 612 AV *av;
373 613
376 case G_VOID: 616 case G_VOID:
377 /* no thing */ 617 /* no thing */
378 break; 618 break;
379 case G_SCALAR: 619 case G_SCALAR:
380 /* the first element */ 620 /* the first element */
381 XPUSHs (maybe_force_utf8 (ix & 1, *av_fetch ((AV *)SvRV (row), 0, 1))); 621 XPUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, *av_fetch ((AV *)SvRV (row), 0, 1)));
622 count = 1;
382 break; 623 break;
383 case G_ARRAY: 624 case G_ARRAY:
384 av = (AV *)SvRV (row); 625 av = (AV *)SvRV (row);
385 count = AvFILL (av) + 1; 626 count = AvFILL (av) + 1;
386 EXTEND (SP, count); 627 EXTEND (SP, count);
387 for (arg = 0; arg < count; arg++) 628 for (arg = 0; arg < count; arg++)
388 PUSHs (maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); 629 PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY (av)[arg]));
389 630
390 break; 631 break;
391 default: 632 default:
392 abort (); 633 abort ();
393 } 634 }
398 SV *rows; 639 SV *rows;
399 640
400 PUSHMARK (SP); 641 PUSHMARK (SP);
401 XPUSHs (sth); 642 XPUSHs (sth);
402 PUTBACK; 643 PUTBACK;
644
645 if (!mc->fetchall_arrayref)
646 mc_cache (mc, fetchall_arrayref);
647
403 count = call_sv (sv_fetchall_arrayref, G_METHOD | G_SCALAR); 648 count = call_sv (mc->fetchall_arrayref, G_SCALAR);
404 SPAGAIN; 649 SPAGAIN;
405 650
406 if (count != 1) 651 if (count != 1)
407 abort (); 652 abort ();
408 653
409 rows = POPs; 654 rows = POPs;
655
656 SP = PL_stack_base + orig_stack;
410 657
411 if (SvROK (rows)) 658 if (SvROK (rows))
412 { 659 {
413 AV *av = (AV *)SvRV (rows); 660 AV *av = (AV *)SvRV (rows);
414 count = AvFILL (av) + 1; 661 count = AvFILL (av) + 1;
418 int columns = AvFILL ((AV *) SvRV (AvARRAY (av)[0])) + 1; /* columns? */ 665 int columns = AvFILL ((AV *) SvRV (AvARRAY (av)[0])) + 1; /* columns? */
419 666
420 EXTEND (SP, count); 667 EXTEND (SP, count);
421 if (columns == 1) 668 if (columns == 1)
422 for (arg = 0; arg < count; arg++) 669 for (arg = 0; arg < count; arg++)
423 PUSHs (maybe_force_utf8 (ix & 1, AvARRAY ((AV *)SvRV (AvARRAY (av)[arg]))[0])); 670 PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY ((AV *)SvRV (AvARRAY (av)[arg]))[0]));
424 else 671 else
425 for (arg = 0; arg < count; arg++) 672 for (arg = 0; arg < count; arg++)
426 PUSHs (maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); 673 PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY (av)[arg]));
427 } 674 }
428 } 675 }
429 } 676 }
430 else 677 else
678 {
679 SP = PL_stack_base + orig_stack;
431 XPUSHs (sth); 680 XPUSHs (sth);
681 }
432 682
433 if (ix > 1 || GIMME_V == G_VOID) 683 if (ix > 1 || GIMME_V == G_VOID)
434 { 684 {
685 orig_stack = SP - PL_stack_base;
686
435 PUSHMARK (SP); 687 PUSHMARK (SP);
436 XPUSHs (sth); 688 XPUSHs (sth);
437 PUTBACK; 689 PUTBACK;
690
691 if (!mc->finish)
692 mc_cache (mc, finish);
693
438 (void) call_sv (sv_finish, G_METHOD | G_DISCARD); 694 call_sv (mc->finish, G_DISCARD);
439 SPAGAIN; 695 SPAGAIN;
696
697 SP = PL_stack_base + orig_stack;
440 } 698 }
441 } 699 }
442} 700}
443 701
444 702

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines