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 | #if (PERL_VERSION < 5) || ((PERL_VERSION == 5) && (PERL_SUBVERSION <= 6)) |
|
|
6 | # define get_sv perl_get_sv |
|
|
7 | # define call_method perl_call_method |
|
|
8 | # define call_sv perl_call_sv |
|
|
9 | #endif |
|
|
10 | |
|
|
11 | #if (PERL_VERSION < 5) || ((PERL_VERSION == 5) && (PERL_SUBVERSION <= 5)) |
|
|
12 | # define CAN_UTF8 1 |
|
|
13 | #endif |
|
|
14 | |
|
|
15 | static SV * |
|
|
16 | sql_upgrade_utf8 (SV *sv) |
|
|
17 | { |
|
|
18 | #if CAN_UTF8 |
|
|
19 | if (SvPOK (sv)) |
|
|
20 | sv_utf8_upgrade (sv); |
|
|
21 | #endif |
|
|
22 | return sv; |
|
|
23 | } |
|
|
24 | |
|
|
25 | static SV * |
|
|
26 | sql_force_utf8 (SV *sv) |
|
|
27 | { |
|
|
28 | #if CAN_UTF8 |
|
|
29 | if (SvPOK (sv)) |
|
|
30 | SvUTF8_on (sv); |
|
|
31 | #endif |
|
|
32 | return sv; |
|
|
33 | } |
|
|
34 | |
|
|
35 | #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)) |
4 | |
37 | |
5 | #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")) |
6 | |
39 | |
7 | typedef struct lru_node { |
40 | typedef struct lru_node { |
8 | struct lru_node *next; |
41 | struct lru_node *next; |
… | |
… | |
14 | SV *sth; |
47 | SV *sth; |
15 | #if 0 /* method cache */ |
48 | #if 0 /* method cache */ |
16 | GV *execute; |
49 | GV *execute; |
17 | GV *bind_columns; |
50 | GV *bind_columns; |
18 | GV *fetch; |
51 | GV *fetch; |
|
|
52 | GV *finish; |
19 | #endif |
53 | #endif |
20 | } lru_node; |
54 | } lru_node; |
21 | |
55 | |
22 | static lru_node lru_list; |
56 | static lru_node lru_list; |
23 | static int lru_size; |
57 | static int lru_size; |
… | |
… | |
44 | char *statement = SvPV (sql, len); |
78 | char *statement = SvPV (sql, len); |
45 | |
79 | |
46 | dbh = SvRV (dbh); |
80 | dbh = SvRV (dbh); |
47 | |
81 | |
48 | lru_hash; |
82 | lru_hash; |
49 | |
|
|
50 | /*fprintf (stderr, "F: %08lx %s\n", hash, SvPV_nolen (sql));/*D*/ |
|
|
51 | |
83 | |
52 | n = &lru_list; |
84 | n = &lru_list; |
53 | do { |
85 | do { |
54 | n = n->next; |
86 | n = n->next; |
55 | if (!n->hash) |
87 | if (!n->hash) |
… | |
… | |
78 | n = lru_list.prev; |
110 | n = lru_list.prev; |
79 | |
111 | |
80 | lru_list.prev = n->prev; |
112 | lru_list.prev = n->prev; |
81 | n->prev->next = &lru_list; |
113 | n->prev->next = &lru_list; |
82 | |
114 | |
83 | /*fprintf (stderr, "N: %s\n", SvPV_nolen (n->sql));/*D*/ |
|
|
84 | |
|
|
85 | SvREFCNT_dec (n->dbh); |
115 | SvREFCNT_dec (n->dbh); |
86 | SvREFCNT_dec (n->sql); |
116 | SvREFCNT_dec (n->sql); |
87 | SvREFCNT_dec (n->sth); |
117 | SvREFCNT_dec (n->sth); |
88 | Safefree (n); |
118 | Safefree (n); |
89 | |
119 | |
… | |
… | |
101 | |
131 | |
102 | dbh = SvRV (dbh); |
132 | dbh = SvRV (dbh); |
103 | |
133 | |
104 | lru_hash; |
134 | lru_hash; |
105 | |
135 | |
106 | /*fprintf (stderr, "S: %08lx %s\n", hash, SvPV_nolen (sql));/*D*/ |
|
|
107 | |
|
|
108 | lru_size++; |
136 | lru_size++; |
109 | if (lru_size > lru_maxsize) |
137 | if (lru_size > lru_maxsize) |
110 | lru_nukeone (); |
138 | lru_nukeone (); |
111 | |
139 | |
112 | New (0, n, 1, lru_node); |
140 | New (0, n, 1, lru_node); |
… | |
… | |
132 | } |
160 | } |
133 | } |
161 | } |
134 | |
162 | |
135 | static GV *sql_exec; |
163 | static GV *sql_exec; |
136 | static GV *DBH; |
164 | static GV *DBH; |
|
|
165 | static SV *sv_prepare, *sv_execute, *sv_bind_columns, |
|
|
166 | *sv_fetchrow_arrayref, *sv_fetchall_arrayref, |
|
|
167 | *sv_finish; |
|
|
168 | |
|
|
169 | #define newconstpv(str) newSVpvn ((str), sizeof (str)) |
137 | |
170 | |
138 | MODULE = PApp::SQL PACKAGE = PApp::SQL |
171 | MODULE = PApp::SQL PACKAGE = PApp::SQL |
139 | |
172 | |
140 | PROTOTYPES: DISABLE |
173 | PROTOTYPES: DISABLE |
141 | |
174 | |
142 | BOOT: |
175 | BOOT: |
143 | { |
176 | { |
144 | sql_exec = gv_fetchpv ("PApp::SQL::sql_exec", TRUE, SVt_PV); |
177 | sql_exec = gv_fetchpv ("PApp::SQL::sql_exec", TRUE, SVt_PV); |
145 | DBH = gv_fetchpv ("PApp::SQL::DBH" , TRUE, SVt_PV); |
178 | 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 | } |
146 | |
189 | |
147 | /* apache might BOOT: twice :( */ |
190 | /* apache might BOOT: twice :( */ |
148 | if (lru_size) |
191 | if (lru_size) |
149 | lru_cachesize (0); |
192 | lru_cachesize (0); |
150 | |
193 | |
… | |
… | |
162 | RETVAL |
205 | RETVAL |
163 | |
206 | |
164 | void |
207 | void |
165 | sql_exec(...) |
208 | sql_exec(...) |
166 | ALIAS: |
209 | ALIAS: |
|
|
210 | sql_uexec = 1 |
167 | sql_fetch = 1 |
211 | sql_fetch = 2 |
|
|
212 | sql_ufetch = 3 |
168 | sql_fetchall = 2 |
213 | sql_fetchall = 4 |
|
|
214 | sql_ufetchall = 5 |
169 | sql_exists = 4 |
215 | sql_exists = 6 |
|
|
216 | sql_uexists = 7 |
170 | PPCODE: |
217 | PPCODE: |
171 | { |
218 | { |
172 | if (items == 0) |
219 | if (items == 0) |
173 | croak ("Usage: sql_exec [database-handle,] [bind-var-refs,... ] \"sql-statement\", [arguments, ...]"); |
220 | croak ("Usage: sql_exec [database-handle,] [bind-var-refs,... ] \"sql-statement\", [arguments, ...]"); |
174 | else |
221 | else |
… | |
… | |
178 | int count; |
225 | int count; |
179 | SV *dbh = ST(0); |
226 | SV *dbh = ST(0); |
180 | SV *sth; |
227 | SV *sth; |
181 | SV *sql; |
228 | SV *sql; |
182 | SV *execute; |
229 | SV *execute; |
|
|
230 | STRLEN dc, dd; /* dummy */ |
183 | |
231 | |
184 | /* save our arguments against destruction through function calls */ |
232 | /* save our arguments against destruction through function calls */ |
185 | SP += items; |
233 | SP += items; |
186 | |
234 | |
187 | /* first check wether we should use an explicit db handle */ |
235 | /* first check wether we should use an explicit db handle */ |
… | |
… | |
212 | if (!SvPOK (ST(arg))) |
260 | if (!SvPOK (ST(arg))) |
213 | croak ("sql_exec: sql-statement must be a string"); |
261 | croak ("sql_exec: sql-statement must be a string"); |
214 | |
262 | |
215 | sql = ST(arg); arg++; |
263 | sql = ST(arg); arg++; |
216 | |
264 | |
217 | if (ix == 4) |
265 | if ((ix & ~1) == 6) |
218 | { |
266 | { |
219 | SV *neu = sv_2mortal (newSVpv ("select count(*) > 0 from ", 0)); |
267 | SV *neu = sv_2mortal (newSVpv ("select count(*) > 0 from ", 0)); |
220 | sv_catsv (neu, sql); |
268 | sv_catsv (neu, sql); |
221 | sv_catpv (neu, " limit 1"); |
269 | sv_catpv (neu, " limit 1"); |
222 | sql = neu; |
270 | sql = neu; |
223 | ix = 1; /* sql_fetch */ |
271 | ix -= 4; /* sql_fetch */ |
224 | } |
272 | } |
225 | |
273 | |
226 | /* check cache for existing statement handle (NYI) */ |
274 | /* check cache for existing statement handle */ |
227 | sth = lru_fetch (dbh, sql); |
275 | sth = lru_fetch (dbh, sql); |
228 | if (!sth) |
276 | if (!sth) |
229 | { |
277 | { |
230 | PUSHMARK (SP); |
278 | PUSHMARK (SP); |
231 | EXTEND (SP, 2); |
279 | EXTEND (SP, 2); |
232 | PUSHs (dbh); |
280 | PUSHs (dbh); |
233 | PUSHs (sql); |
281 | PUSHs (sql); |
234 | PUTBACK; |
282 | PUTBACK; |
235 | count = call_method ("prepare", G_SCALAR); |
283 | count = call_sv (sv_prepare, G_METHOD | G_SCALAR); |
236 | SPAGAIN; |
284 | SPAGAIN; |
237 | |
285 | |
238 | if (count != 1) |
286 | if (count != 1) |
239 | croak ("sql_exec: unable to prepare() statement '%s': %s", |
287 | croak ("sql_exec: unable to prepare() statement '%s': %s", |
240 | SvPV_nolen (sql), |
288 | SvPV (sql, dc), |
241 | SvPV_nolen (get_sv ("DBI::errstr", TRUE))); |
289 | SvPV (get_sv ("DBI::errstr", TRUE), dd)); |
242 | |
290 | |
243 | sth = POPs; |
291 | sth = POPs; |
244 | |
292 | |
245 | lru_store (dbh, sql, sth); |
293 | lru_store (dbh, sql, sth); |
246 | } |
294 | } |
… | |
… | |
248 | PUSHMARK (SP); |
296 | PUSHMARK (SP); |
249 | EXTEND (SP, items - arg + 1); |
297 | EXTEND (SP, items - arg + 1); |
250 | PUSHs (sth); |
298 | PUSHs (sth); |
251 | while (items > arg) |
299 | while (items > arg) |
252 | { |
300 | { |
253 | PUSHs (ST(arg)); |
301 | PUSHs (maybe_upgrade_utf8 (ix & 1, ST(arg))); |
254 | arg++; |
302 | arg++; |
255 | } |
303 | } |
256 | |
304 | |
257 | PUTBACK; |
305 | PUTBACK; |
258 | /* { static GV *execute; |
306 | /* { static GV *execute; |
259 | if (!execute) execute = gv_fetchmethod_autoload(SvSTASH(SvRV(sth)), "execute", 0); |
307 | if (!execute) execute = gv_fetchmethod_autoload(SvSTASH(SvRV(sth)), "execute", 0); |
260 | count = call_sv(GvCV(execute), G_SCALAR); |
308 | count = call_sv(GvCV(execute), G_SCALAR); |
261 | }*/ |
309 | }*/ |
262 | count = call_method ("execute", G_SCALAR); |
310 | count = call_sv (sv_execute, G_METHOD | G_SCALAR); |
263 | SPAGAIN; |
311 | SPAGAIN; |
264 | |
312 | |
265 | if (count != 1) |
313 | if (count != 1) |
266 | croak ("sql_exec: execute() didn't return any value ('%s'): %s", |
314 | croak ("sql_exec: execute() didn't return any value ('%s'): %s", |
267 | SvPV_nolen (sql), |
315 | SvPV (sql, dc), |
268 | SvPV_nolen (get_sv ("DBI::errstr", TRUE))); |
316 | SvPV (get_sv ("DBI::errstr", TRUE), dc)); |
269 | |
317 | |
270 | execute = POPs; |
318 | execute = POPs; |
271 | |
319 | |
272 | if (!SvTRUE (execute)) |
320 | if (!SvTRUE (execute)) |
273 | croak ("sql_exec: unable to execute statement '%s' (%s)", |
321 | croak ("sql_exec: unable to execute statement '%s' (%s)", |
274 | SvPV_nolen (sql), |
322 | SvPV (sql, dc), |
275 | SvPV_nolen (get_sv ("DBI::errstr", TRUE))); |
323 | SvPV (get_sv ("DBI::errstr", TRUE), dc)); |
276 | |
324 | |
277 | sv_setsv (GvSV(sql_exec), execute); |
325 | sv_setsv (GvSV(sql_exec), execute); |
278 | |
326 | |
279 | if (bind_first != bind_last) |
327 | if (bind_first != bind_last) |
280 | { |
328 | { |
… | |
… | |
285 | PUSHs (ST(bind_first)); |
333 | PUSHs (ST(bind_first)); |
286 | bind_first++; |
334 | bind_first++; |
287 | } while (bind_first != bind_last); |
335 | } while (bind_first != bind_last); |
288 | |
336 | |
289 | PUTBACK; |
337 | PUTBACK; |
290 | count = call_method ("bind_columns", G_SCALAR); |
338 | count = call_sv (sv_bind_columns, G_METHOD | G_SCALAR); |
291 | SPAGAIN; |
339 | SPAGAIN; |
292 | |
340 | |
293 | if (count != 1) |
341 | if (count != 1) |
294 | croak ("sql_exec: bind_columns() didn't return any value ('%s'): %s", |
342 | croak ("sql_exec: bind_columns() didn't return any value ('%s'): %s", |
295 | SvPV_nolen (sql), |
343 | SvPV (sql, dc), |
296 | SvPV_nolen (get_sv ("DBI::errstr", TRUE))); |
344 | SvPV (get_sv ("DBI::errstr", TRUE), dc)); |
297 | |
345 | |
298 | if (!SvOK (POPs)) |
346 | if (!SvOK (POPs)) |
299 | croak ("sql_exec: bind_columns() didn't return a true ('%s'): %s", |
347 | croak ("sql_exec: bind_columns() didn't return a true ('%s'): %s", |
300 | SvPV_nolen (sql), |
348 | SvPV (sql, dc), |
301 | SvPV_nolen (get_sv ("DBI::errstr", TRUE))); |
349 | SvPV (get_sv ("DBI::errstr", TRUE), dc)); |
302 | } |
350 | } |
303 | |
351 | |
304 | /* free our arguments from the stack */ |
352 | /* free our arguments from the stack */ |
305 | SP -= items; |
353 | SP -= items; |
306 | |
354 | |
307 | if (ix == 1) |
355 | if ((ix & ~1) == 2) |
308 | { /* sql_fetch */ |
356 | { /* sql_fetch */ |
309 | SV *row; |
357 | SV *row; |
310 | |
358 | |
311 | PUSHMARK (SP); |
359 | PUSHMARK (SP); |
312 | XPUSHs (sth); |
360 | XPUSHs (sth); |
313 | PUTBACK; |
361 | PUTBACK; |
314 | count = call_method ("fetchrow_arrayref", G_SCALAR); |
362 | count = call_sv (sv_fetchrow_arrayref, G_METHOD | G_SCALAR); |
315 | SPAGAIN; |
363 | SPAGAIN; |
316 | |
364 | |
317 | if (count != 1) |
365 | if (count != 1) |
318 | abort (); |
366 | abort (); |
319 | |
367 | |
… | |
… | |
328 | case G_VOID: |
376 | case G_VOID: |
329 | /* no thing */ |
377 | /* no thing */ |
330 | break; |
378 | break; |
331 | case G_SCALAR: |
379 | case G_SCALAR: |
332 | /* the first element */ |
380 | /* the first element */ |
333 | XPUSHs (*av_fetch ((AV *)SvRV (row), 0, 1)); |
381 | XPUSHs (maybe_force_utf8 (ix & 1, *av_fetch ((AV *)SvRV (row), 0, 1))); |
334 | break; |
382 | break; |
335 | case G_ARRAY: |
383 | case G_ARRAY: |
336 | av = (AV *)SvRV (row); |
384 | av = (AV *)SvRV (row); |
337 | count = AvFILL (av) + 1; |
385 | count = AvFILL (av) + 1; |
338 | EXTEND (SP, count); |
386 | EXTEND (SP, count); |
339 | for (arg = 0; arg < count; arg++) |
387 | for (arg = 0; arg < count; arg++) |
340 | PUSHs (AvARRAY (av)[arg]); |
388 | PUSHs (maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); |
341 | |
389 | |
342 | break; |
390 | break; |
343 | default: |
391 | default: |
344 | abort (); |
392 | abort (); |
345 | } |
393 | } |
346 | } |
394 | } |
347 | } |
395 | } |
348 | else if (ix == 2) |
396 | else if ((ix & ~1) == 4) |
349 | { /* sql_fetchall */ |
397 | { /* sql_fetchall */ |
350 | SV *rows; |
398 | SV *rows; |
351 | |
399 | |
352 | PUSHMARK (SP); |
400 | PUSHMARK (SP); |
353 | XPUSHs (sth); |
401 | XPUSHs (sth); |
354 | PUTBACK; |
402 | PUTBACK; |
355 | count = call_method ("fetchall_arrayref", G_SCALAR); |
403 | count = call_sv (sv_fetchall_arrayref, G_METHOD | G_SCALAR); |
356 | SPAGAIN; |
404 | SPAGAIN; |
357 | |
405 | |
358 | if (count != 1) |
406 | if (count != 1) |
359 | abort (); |
407 | abort (); |
360 | |
408 | |
… | |
… | |
365 | AV *av = (AV *)SvRV (rows); |
413 | AV *av = (AV *)SvRV (rows); |
366 | count = AvFILL (av) + 1; |
414 | count = AvFILL (av) + 1; |
367 | |
415 | |
368 | if (count) |
416 | if (count) |
369 | { |
417 | { |
370 | int columns = AvFILL ((AV *)SvRV (AvARRAY(av)[0])) + 1; /* columns? */ |
418 | int columns = AvFILL ((AV *) SvRV (AvARRAY (av)[0])) + 1; /* columns? */ |
371 | |
419 | |
372 | EXTEND (SP, count); |
420 | EXTEND (SP, count); |
373 | if (columns == 1) |
421 | if (columns == 1) |
374 | for (arg = 0; arg < count; arg++) |
422 | for (arg = 0; arg < count; arg++) |
375 | PUSHs (AvARRAY ((AV *)SvRV (AvARRAY (av)[arg]))[0]); |
423 | PUSHs (maybe_force_utf8 (ix & 1, AvARRAY ((AV *)SvRV (AvARRAY (av)[arg]))[0])); |
376 | else |
424 | else |
377 | for (arg = 0; arg < count; arg++) |
425 | for (arg = 0; arg < count; arg++) |
378 | PUSHs (AvARRAY (av)[arg]); |
426 | PUSHs (maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); |
379 | } |
427 | } |
380 | } |
428 | } |
381 | } |
429 | } |
382 | else |
430 | else |
383 | XPUSHs (sth); |
431 | XPUSHs (sth); |
384 | |
432 | |
385 | if (ix || GIMME_V == G_VOID) |
433 | if (ix > 1 || GIMME_V == G_VOID) |
386 | { |
434 | { |
387 | PUSHMARK (SP); |
435 | PUSHMARK (SP); |
388 | XPUSHs (sth); |
436 | XPUSHs (sth); |
389 | PUTBACK; |
437 | PUTBACK; |
390 | (void) call_method ("finish", G_DISCARD); |
438 | (void) call_sv (sv_finish, G_METHOD | G_DISCARD); |
391 | SPAGAIN; |
439 | SPAGAIN; |
392 | } |
440 | } |
393 | } |
441 | } |
394 | } |
442 | } |
395 | |
443 | |