… | |
… | |
6 | # define get_sv perl_get_sv |
6 | # define get_sv perl_get_sv |
7 | # define call_method perl_call_method |
7 | # define call_method perl_call_method |
8 | # define call_sv perl_call_sv |
8 | # define call_sv perl_call_sv |
9 | #endif |
9 | #endif |
10 | |
10 | |
11 | #if (PERL_VERSION < 5) || ((PERL_VERSION == 5) && (PERL_SUBVERSION <= 5)) |
11 | #if (PERL_VERSION > 5) || ((PERL_VERSION == 5) && (PERL_SUBVERSION >= 6)) |
12 | # define CAN_UTF8 1 |
12 | # define CAN_UTF8 1 |
13 | #endif |
13 | #endif |
14 | |
14 | |
15 | static SV * |
15 | static SV * |
16 | sql_upgrade_utf8 (SV *sv) |
16 | sql_upgrade_utf8 (SV *sv) |
… | |
… | |
21 | #endif |
21 | #endif |
22 | return sv; |
22 | return sv; |
23 | } |
23 | } |
24 | |
24 | |
25 | static SV * |
25 | static SV * |
26 | sql_force_utf8 (SV *sv) |
26 | mortalcopy_and_maybe_force_utf8(int utf8, SV *sv) |
27 | { |
27 | { |
|
|
28 | sv = sv_mortalcopy (sv); |
28 | #if CAN_UTF8 |
29 | #if CAN_UTF8 |
29 | if (SvPOK (sv)) |
30 | if (utf8 && SvPOK (sv)) |
30 | SvUTF8_on (sv); |
31 | SvUTF8_on (sv); |
31 | #endif |
32 | #endif |
32 | return sv; |
33 | return sv; |
33 | } |
34 | } |
34 | |
35 | |
35 | #define maybe_upgrade_utf8(utf8,sv) ((utf8) ? sql_upgrade_utf8 (sv) : (sv)) |
36 | #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 | |
37 | |
38 | #define is_dbh(sv) ((sv) && sv_isobject (sv) && sv_derived_from ((sv), "DBI::db")) |
38 | #define is_dbh(sv) ((sv) && sv_isobject (sv) && sv_derived_from ((sv), "DBI::db")) |
39 | |
39 | |
40 | typedef struct lru_node { |
40 | typedef struct lru_node { |
41 | struct lru_node *next; |
41 | struct lru_node *next; |
… | |
… | |
79 | |
79 | |
80 | dbh = SvRV (dbh); |
80 | dbh = SvRV (dbh); |
81 | |
81 | |
82 | lru_hash; |
82 | lru_hash; |
83 | |
83 | |
84 | /*fprintf (stderr, "F: %08lx %s\n", hash, SvPV_nolen (sql));/*D*/ |
|
|
85 | |
|
|
86 | n = &lru_list; |
84 | n = &lru_list; |
87 | do { |
85 | do { |
88 | n = n->next; |
86 | n = n->next; |
89 | if (!n->hash) |
87 | if (!n->hash) |
90 | return 0; |
88 | return 0; |
… | |
… | |
112 | n = lru_list.prev; |
110 | n = lru_list.prev; |
113 | |
111 | |
114 | lru_list.prev = n->prev; |
112 | lru_list.prev = n->prev; |
115 | n->prev->next = &lru_list; |
113 | n->prev->next = &lru_list; |
116 | |
114 | |
117 | /*fprintf (stderr, "N: %s\n", SvPV_nolen (n->sql));/*D*/ |
|
|
118 | |
|
|
119 | SvREFCNT_dec (n->dbh); |
115 | SvREFCNT_dec (n->dbh); |
120 | SvREFCNT_dec (n->sql); |
116 | SvREFCNT_dec (n->sql); |
121 | SvREFCNT_dec (n->sth); |
117 | SvREFCNT_dec (n->sth); |
122 | Safefree (n); |
118 | Safefree (n); |
123 | |
119 | |
… | |
… | |
134 | char *statement = SvPV (sql, len); |
130 | char *statement = SvPV (sql, len); |
135 | |
131 | |
136 | dbh = SvRV (dbh); |
132 | dbh = SvRV (dbh); |
137 | |
133 | |
138 | lru_hash; |
134 | lru_hash; |
139 | |
|
|
140 | /*fprintf (stderr, "S: %08lx %s\n", hash, SvPV_nolen (sql));/*D*/ |
|
|
141 | |
135 | |
142 | lru_size++; |
136 | lru_size++; |
143 | if (lru_size > lru_maxsize) |
137 | if (lru_size > lru_maxsize) |
144 | lru_nukeone (); |
138 | lru_nukeone (); |
145 | |
139 | |
… | |
… | |
231 | int count; |
225 | int count; |
232 | SV *dbh = ST(0); |
226 | SV *dbh = ST(0); |
233 | SV *sth; |
227 | SV *sth; |
234 | SV *sql; |
228 | SV *sql; |
235 | SV *execute; |
229 | SV *execute; |
236 | STRLEN dc; |
230 | STRLEN dc, dd; /* dummy */ |
237 | |
231 | |
238 | /* save our arguments against destruction through function calls */ |
232 | /* save our arguments against destruction through function calls */ |
239 | SP += items; |
233 | SP += items; |
240 | |
234 | |
241 | /* first check wether we should use an explicit db handle */ |
235 | /* first check wether we should use an explicit db handle */ |
242 | if (!is_dbh (dbh)) |
236 | if (!is_dbh (dbh)) |
243 | { |
237 | { |
|
|
238 | /* the next line doesn't work - check why later maybe */ |
244 | dbh = get_sv ("DBH", FALSE); |
239 | /* dbh = get_sv ("DBH", FALSE); |
245 | if (!is_dbh (dbh)) |
240 | if (!is_dbh (dbh)) |
246 | { |
241 | {*/ |
247 | dbh = GvSV(DBH); |
242 | dbh = GvSV(DBH); |
248 | if (!is_dbh (dbh)) |
243 | if (!is_dbh (dbh)) |
|
|
244 | croak ("sql_exec: no $DBH argument and no fallback in $PApp::SQL::DBH"); |
249 | croak ("sql_exec: no $DBH found in current package or in PApp::SQL::"); |
245 | /*croak ("sql_exec: no $DBH found in current package or in PApp::SQL::"); |
250 | } |
246 | }*/ |
251 | } |
247 | } |
252 | else |
248 | else |
253 | arg++; /* we consumed one argument */ |
249 | arg++; /* we consumed one argument */ |
254 | |
250 | |
255 | /* count the remaining references (for bind_columns) */ |
251 | /* count the remaining references (for bind_columns) */ |
… | |
… | |
272 | { |
268 | { |
273 | SV *neu = sv_2mortal (newSVpv ("select count(*) > 0 from ", 0)); |
269 | SV *neu = sv_2mortal (newSVpv ("select count(*) > 0 from ", 0)); |
274 | sv_catsv (neu, sql); |
270 | sv_catsv (neu, sql); |
275 | sv_catpv (neu, " limit 1"); |
271 | sv_catpv (neu, " limit 1"); |
276 | sql = neu; |
272 | sql = neu; |
277 | ix -= 6; /* sql_fetch */ |
273 | ix -= 4; /* sql_fetch */ |
278 | } |
274 | } |
279 | |
275 | |
280 | /* check cache for existing statement handle */ |
276 | /* check cache for existing statement handle */ |
281 | sth = lru_fetch (dbh, sql); |
277 | sth = lru_fetch (dbh, sql); |
282 | if (!sth) |
278 | if (!sth) |
… | |
… | |
290 | SPAGAIN; |
286 | SPAGAIN; |
291 | |
287 | |
292 | if (count != 1) |
288 | if (count != 1) |
293 | croak ("sql_exec: unable to prepare() statement '%s': %s", |
289 | croak ("sql_exec: unable to prepare() statement '%s': %s", |
294 | SvPV (sql, dc), |
290 | SvPV (sql, dc), |
295 | SvPV (get_sv ("DBI::errstr", TRUE), dc)); |
291 | SvPV (get_sv ("DBI::errstr", TRUE), dd)); |
296 | |
292 | |
297 | sth = POPs; |
293 | sth = POPs; |
298 | |
294 | |
299 | lru_store (dbh, sql, sth); |
295 | lru_store (dbh, sql, sth); |
300 | } |
296 | } |
… | |
… | |
317 | SPAGAIN; |
313 | SPAGAIN; |
318 | |
314 | |
319 | if (count != 1) |
315 | if (count != 1) |
320 | croak ("sql_exec: execute() didn't return any value ('%s'): %s", |
316 | croak ("sql_exec: execute() didn't return any value ('%s'): %s", |
321 | SvPV (sql, dc), |
317 | SvPV (sql, dc), |
322 | SvPV (get_sv ("DBI::errstr", TRUE), dc)); |
318 | SvPV (get_sv ("DBI::errstr", TRUE), dd)); |
323 | |
319 | |
324 | execute = POPs; |
320 | execute = POPs; |
325 | |
321 | |
326 | if (!SvTRUE (execute)) |
322 | if (!SvTRUE (execute)) |
327 | croak ("sql_exec: unable to execute statement '%s' (%s)", |
323 | croak ("sql_exec: unable to execute statement '%s' (%s)", |
328 | SvPV (sql, dc), |
324 | SvPV (sql, dc), |
329 | SvPV (get_sv ("DBI::errstr", TRUE), dc)); |
325 | SvPV (get_sv ("DBI::errstr", TRUE), dd)); |
330 | |
326 | |
331 | sv_setsv (GvSV(sql_exec), execute); |
327 | sv_setsv (GvSV(sql_exec), execute); |
332 | |
328 | |
333 | if (bind_first != bind_last) |
329 | if (bind_first != bind_last) |
334 | { |
330 | { |
… | |
… | |
345 | SPAGAIN; |
341 | SPAGAIN; |
346 | |
342 | |
347 | if (count != 1) |
343 | if (count != 1) |
348 | croak ("sql_exec: bind_columns() didn't return any value ('%s'): %s", |
344 | croak ("sql_exec: bind_columns() didn't return any value ('%s'): %s", |
349 | SvPV (sql, dc), |
345 | SvPV (sql, dc), |
350 | SvPV (get_sv ("DBI::errstr", TRUE), dc)); |
346 | SvPV (get_sv ("DBI::errstr", TRUE), dd)); |
351 | |
347 | |
352 | if (!SvOK (POPs)) |
348 | if (!SvOK (POPs)) |
353 | croak ("sql_exec: bind_columns() didn't return a true ('%s'): %s", |
349 | croak ("sql_exec: bind_columns() didn't return a true ('%s'): %s", |
354 | SvPV (sql, dc), |
350 | SvPV (sql, dc), |
355 | SvPV (get_sv ("DBI::errstr", TRUE), dc)); |
351 | SvPV (get_sv ("DBI::errstr", TRUE), dd)); |
356 | } |
352 | } |
357 | |
353 | |
358 | /* free our arguments from the stack */ |
354 | /* free our arguments from the stack */ |
359 | SP -= items; |
355 | SP -= items; |
360 | |
356 | |
… | |
… | |
382 | case G_VOID: |
378 | case G_VOID: |
383 | /* no thing */ |
379 | /* no thing */ |
384 | break; |
380 | break; |
385 | case G_SCALAR: |
381 | case G_SCALAR: |
386 | /* the first element */ |
382 | /* the first element */ |
387 | XPUSHs (maybe_force_utf8 (ix & 1, *av_fetch ((AV *)SvRV (row), 0, 1))); |
383 | XPUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, *av_fetch ((AV *)SvRV (row), 0, 1))); |
388 | break; |
384 | break; |
389 | case G_ARRAY: |
385 | case G_ARRAY: |
390 | av = (AV *)SvRV (row); |
386 | av = (AV *)SvRV (row); |
391 | count = AvFILL (av) + 1; |
387 | count = AvFILL (av) + 1; |
392 | EXTEND (SP, count); |
388 | EXTEND (SP, count); |
393 | for (arg = 0; arg < count; arg++) |
389 | for (arg = 0; arg < count; arg++) |
394 | PUSHs (maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); |
390 | PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); |
395 | |
391 | |
396 | break; |
392 | break; |
397 | default: |
393 | default: |
398 | abort (); |
394 | abort (); |
399 | } |
395 | } |
… | |
… | |
424 | int columns = AvFILL ((AV *) SvRV (AvARRAY (av)[0])) + 1; /* columns? */ |
420 | int columns = AvFILL ((AV *) SvRV (AvARRAY (av)[0])) + 1; /* columns? */ |
425 | |
421 | |
426 | EXTEND (SP, count); |
422 | EXTEND (SP, count); |
427 | if (columns == 1) |
423 | if (columns == 1) |
428 | for (arg = 0; arg < count; arg++) |
424 | for (arg = 0; arg < count; arg++) |
429 | PUSHs (maybe_force_utf8 (ix & 1, AvARRAY ((AV *)SvRV (AvARRAY (av)[arg]))[0])); |
425 | PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY ((AV *)SvRV (AvARRAY (av)[arg]))[0])); |
430 | else |
426 | else |
431 | for (arg = 0; arg < count; arg++) |
427 | for (arg = 0; arg < count; arg++) |
432 | PUSHs (maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); |
428 | PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); |
433 | } |
429 | } |
434 | } |
430 | } |
435 | } |
431 | } |
436 | else |
432 | else |
437 | XPUSHs (sth); |
433 | XPUSHs (sth); |