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 | |
4 | |
5 | #if (PERL_VERSION < 5) || ((PERL_VERSION == 5) && (PERL_SUBVERSION <= 6)) |
5 | #if (PERL_VERSION < 5) || ((PERL_VERSION == 5) && (PERL_SUBVERSION <= 6)) |
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 | #endif |
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)) |
9 | |
37 | |
10 | #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")) |
11 | |
39 | |
12 | typedef struct lru_node { |
40 | typedef struct lru_node { |
13 | struct lru_node *next; |
41 | struct lru_node *next; |
… | |
… | |
19 | SV *sth; |
47 | SV *sth; |
20 | #if 0 /* method cache */ |
48 | #if 0 /* method cache */ |
21 | GV *execute; |
49 | GV *execute; |
22 | GV *bind_columns; |
50 | GV *bind_columns; |
23 | GV *fetch; |
51 | GV *fetch; |
|
|
52 | GV *finish; |
24 | #endif |
53 | #endif |
25 | } lru_node; |
54 | } lru_node; |
26 | |
55 | |
27 | static lru_node lru_list; |
56 | static lru_node lru_list; |
28 | static int lru_size; |
57 | static int lru_size; |
… | |
… | |
137 | } |
166 | } |
138 | } |
167 | } |
139 | |
168 | |
140 | static GV *sql_exec; |
169 | static GV *sql_exec; |
141 | static GV *DBH; |
170 | static GV *DBH; |
|
|
171 | static SV *sv_prepare, *sv_execute, *sv_bind_columns, |
|
|
172 | *sv_fetchrow_arrayref, *sv_fetchall_arrayref, |
|
|
173 | *sv_finish; |
|
|
174 | |
|
|
175 | #define newconstpv(str) newSVpvn ((str), sizeof (str)) |
142 | |
176 | |
143 | MODULE = PApp::SQL PACKAGE = PApp::SQL |
177 | MODULE = PApp::SQL PACKAGE = PApp::SQL |
144 | |
178 | |
145 | PROTOTYPES: DISABLE |
179 | PROTOTYPES: DISABLE |
146 | |
180 | |
147 | BOOT: |
181 | BOOT: |
148 | { |
182 | { |
149 | sql_exec = gv_fetchpv ("PApp::SQL::sql_exec", TRUE, SVt_PV); |
183 | sql_exec = gv_fetchpv ("PApp::SQL::sql_exec", TRUE, SVt_PV); |
150 | DBH = gv_fetchpv ("PApp::SQL::DBH" , TRUE, SVt_PV); |
184 | DBH = gv_fetchpv ("PApp::SQL::DBH" , TRUE, SVt_PV); |
|
|
185 | |
|
|
186 | if (!sv_prepare) |
|
|
187 | { |
|
|
188 | sv_prepare = newconstpv ("prepare"); |
|
|
189 | sv_execute = newconstpv ("execute"); |
|
|
190 | sv_bind_columns = newconstpv ("bind_columns"); |
|
|
191 | sv_fetchrow_arrayref = newconstpv ("fetchrow_arrayref"); |
|
|
192 | sv_fetchall_arrayref = newconstpv ("fetchall_arrayref"); |
|
|
193 | sv_finish = newconstpv ("finish"); |
|
|
194 | } |
151 | |
195 | |
152 | /* apache might BOOT: twice :( */ |
196 | /* apache might BOOT: twice :( */ |
153 | if (lru_size) |
197 | if (lru_size) |
154 | lru_cachesize (0); |
198 | lru_cachesize (0); |
155 | |
199 | |
… | |
… | |
167 | RETVAL |
211 | RETVAL |
168 | |
212 | |
169 | void |
213 | void |
170 | sql_exec(...) |
214 | sql_exec(...) |
171 | ALIAS: |
215 | ALIAS: |
|
|
216 | sql_uexec = 1 |
172 | sql_fetch = 1 |
217 | sql_fetch = 2 |
|
|
218 | sql_ufetch = 3 |
173 | sql_fetchall = 2 |
219 | sql_fetchall = 4 |
|
|
220 | sql_ufetchall = 5 |
174 | sql_exists = 4 |
221 | sql_exists = 6 |
|
|
222 | sql_uexists = 7 |
175 | PPCODE: |
223 | PPCODE: |
176 | { |
224 | { |
177 | if (items == 0) |
225 | if (items == 0) |
178 | croak ("Usage: sql_exec [database-handle,] [bind-var-refs,... ] \"sql-statement\", [arguments, ...]"); |
226 | croak ("Usage: sql_exec [database-handle,] [bind-var-refs,... ] \"sql-statement\", [arguments, ...]"); |
179 | else |
227 | else |
… | |
… | |
218 | if (!SvPOK (ST(arg))) |
266 | if (!SvPOK (ST(arg))) |
219 | croak ("sql_exec: sql-statement must be a string"); |
267 | croak ("sql_exec: sql-statement must be a string"); |
220 | |
268 | |
221 | sql = ST(arg); arg++; |
269 | sql = ST(arg); arg++; |
222 | |
270 | |
223 | if (ix == 4) |
271 | if ((ix & ~1) == 6) |
224 | { |
272 | { |
225 | SV *neu = sv_2mortal (newSVpv ("select count(*) > 0 from ", 0)); |
273 | SV *neu = sv_2mortal (newSVpv ("select count(*) > 0 from ", 0)); |
226 | sv_catsv (neu, sql); |
274 | sv_catsv (neu, sql); |
227 | sv_catpv (neu, " limit 1"); |
275 | sv_catpv (neu, " limit 1"); |
228 | sql = neu; |
276 | sql = neu; |
229 | ix = 1; /* sql_fetch */ |
277 | ix -= 6; /* sql_fetch */ |
230 | } |
278 | } |
231 | |
279 | |
232 | /* check cache for existing statement handle (NYI) */ |
280 | /* check cache for existing statement handle */ |
233 | sth = lru_fetch (dbh, sql); |
281 | sth = lru_fetch (dbh, sql); |
234 | if (!sth) |
282 | if (!sth) |
235 | { |
283 | { |
236 | PUSHMARK (SP); |
284 | PUSHMARK (SP); |
237 | EXTEND (SP, 2); |
285 | EXTEND (SP, 2); |
238 | PUSHs (dbh); |
286 | PUSHs (dbh); |
239 | PUSHs (sql); |
287 | PUSHs (sql); |
240 | PUTBACK; |
288 | PUTBACK; |
241 | count = call_method ("prepare", G_SCALAR); |
289 | count = call_sv (sv_prepare, G_METHOD | G_SCALAR); |
242 | SPAGAIN; |
290 | SPAGAIN; |
243 | |
291 | |
244 | if (count != 1) |
292 | if (count != 1) |
245 | croak ("sql_exec: unable to prepare() statement '%s': %s", |
293 | croak ("sql_exec: unable to prepare() statement '%s': %s", |
246 | SvPV (sql, dc), |
294 | SvPV (sql, dc), |
… | |
… | |
254 | PUSHMARK (SP); |
302 | PUSHMARK (SP); |
255 | EXTEND (SP, items - arg + 1); |
303 | EXTEND (SP, items - arg + 1); |
256 | PUSHs (sth); |
304 | PUSHs (sth); |
257 | while (items > arg) |
305 | while (items > arg) |
258 | { |
306 | { |
259 | PUSHs (ST(arg)); |
307 | PUSHs (maybe_upgrade_utf8 (ix & 1, ST(arg))); |
260 | arg++; |
308 | arg++; |
261 | } |
309 | } |
262 | |
310 | |
263 | PUTBACK; |
311 | PUTBACK; |
264 | /* { static GV *execute; |
312 | /* { static GV *execute; |
265 | if (!execute) execute = gv_fetchmethod_autoload(SvSTASH(SvRV(sth)), "execute", 0); |
313 | if (!execute) execute = gv_fetchmethod_autoload(SvSTASH(SvRV(sth)), "execute", 0); |
266 | count = call_sv(GvCV(execute), G_SCALAR); |
314 | count = call_sv(GvCV(execute), G_SCALAR); |
267 | }*/ |
315 | }*/ |
268 | count = call_method ("execute", G_SCALAR); |
316 | count = call_sv (sv_execute, G_METHOD | G_SCALAR); |
269 | SPAGAIN; |
317 | SPAGAIN; |
270 | |
318 | |
271 | if (count != 1) |
319 | if (count != 1) |
272 | croak ("sql_exec: execute() didn't return any value ('%s'): %s", |
320 | croak ("sql_exec: execute() didn't return any value ('%s'): %s", |
273 | SvPV (sql, dc), |
321 | SvPV (sql, dc), |
… | |
… | |
291 | PUSHs (ST(bind_first)); |
339 | PUSHs (ST(bind_first)); |
292 | bind_first++; |
340 | bind_first++; |
293 | } while (bind_first != bind_last); |
341 | } while (bind_first != bind_last); |
294 | |
342 | |
295 | PUTBACK; |
343 | PUTBACK; |
296 | count = call_method ("bind_columns", G_SCALAR); |
344 | count = call_sv (sv_bind_columns, G_METHOD | G_SCALAR); |
297 | SPAGAIN; |
345 | SPAGAIN; |
298 | |
346 | |
299 | if (count != 1) |
347 | if (count != 1) |
300 | croak ("sql_exec: bind_columns() didn't return any value ('%s'): %s", |
348 | croak ("sql_exec: bind_columns() didn't return any value ('%s'): %s", |
301 | SvPV (sql, dc), |
349 | SvPV (sql, dc), |
… | |
… | |
308 | } |
356 | } |
309 | |
357 | |
310 | /* free our arguments from the stack */ |
358 | /* free our arguments from the stack */ |
311 | SP -= items; |
359 | SP -= items; |
312 | |
360 | |
313 | if (ix == 1) |
361 | if ((ix & ~1) == 2) |
314 | { /* sql_fetch */ |
362 | { /* sql_fetch */ |
315 | SV *row; |
363 | SV *row; |
316 | |
364 | |
317 | PUSHMARK (SP); |
365 | PUSHMARK (SP); |
318 | XPUSHs (sth); |
366 | XPUSHs (sth); |
319 | PUTBACK; |
367 | PUTBACK; |
320 | count = call_method ("fetchrow_arrayref", G_SCALAR); |
368 | count = call_sv (sv_fetchrow_arrayref, G_METHOD | G_SCALAR); |
321 | SPAGAIN; |
369 | SPAGAIN; |
322 | |
370 | |
323 | if (count != 1) |
371 | if (count != 1) |
324 | abort (); |
372 | abort (); |
325 | |
373 | |
… | |
… | |
334 | case G_VOID: |
382 | case G_VOID: |
335 | /* no thing */ |
383 | /* no thing */ |
336 | break; |
384 | break; |
337 | case G_SCALAR: |
385 | case G_SCALAR: |
338 | /* the first element */ |
386 | /* the first element */ |
339 | XPUSHs (*av_fetch ((AV *)SvRV (row), 0, 1)); |
387 | XPUSHs (maybe_force_utf8 (ix & 1, *av_fetch ((AV *)SvRV (row), 0, 1))); |
340 | break; |
388 | break; |
341 | case G_ARRAY: |
389 | case G_ARRAY: |
342 | av = (AV *)SvRV (row); |
390 | av = (AV *)SvRV (row); |
343 | count = AvFILL (av) + 1; |
391 | count = AvFILL (av) + 1; |
344 | EXTEND (SP, count); |
392 | EXTEND (SP, count); |
345 | for (arg = 0; arg < count; arg++) |
393 | for (arg = 0; arg < count; arg++) |
346 | PUSHs (AvARRAY (av)[arg]); |
394 | PUSHs (maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); |
347 | |
395 | |
348 | break; |
396 | break; |
349 | default: |
397 | default: |
350 | abort (); |
398 | abort (); |
351 | } |
399 | } |
352 | } |
400 | } |
353 | } |
401 | } |
354 | else if (ix == 2) |
402 | else if ((ix & ~1) == 4) |
355 | { /* sql_fetchall */ |
403 | { /* sql_fetchall */ |
356 | SV *rows; |
404 | SV *rows; |
357 | |
405 | |
358 | PUSHMARK (SP); |
406 | PUSHMARK (SP); |
359 | XPUSHs (sth); |
407 | XPUSHs (sth); |
360 | PUTBACK; |
408 | PUTBACK; |
361 | count = call_method ("fetchall_arrayref", G_SCALAR); |
409 | count = call_sv (sv_fetchall_arrayref, G_METHOD | G_SCALAR); |
362 | SPAGAIN; |
410 | SPAGAIN; |
363 | |
411 | |
364 | if (count != 1) |
412 | if (count != 1) |
365 | abort (); |
413 | abort (); |
366 | |
414 | |
… | |
… | |
371 | AV *av = (AV *)SvRV (rows); |
419 | AV *av = (AV *)SvRV (rows); |
372 | count = AvFILL (av) + 1; |
420 | count = AvFILL (av) + 1; |
373 | |
421 | |
374 | if (count) |
422 | if (count) |
375 | { |
423 | { |
376 | int columns = AvFILL ((AV *)SvRV (AvARRAY(av)[0])) + 1; /* columns? */ |
424 | int columns = AvFILL ((AV *) SvRV (AvARRAY (av)[0])) + 1; /* columns? */ |
377 | |
425 | |
378 | EXTEND (SP, count); |
426 | EXTEND (SP, count); |
379 | if (columns == 1) |
427 | if (columns == 1) |
380 | for (arg = 0; arg < count; arg++) |
428 | for (arg = 0; arg < count; arg++) |
381 | PUSHs (AvARRAY ((AV *)SvRV (AvARRAY (av)[arg]))[0]); |
429 | PUSHs (maybe_force_utf8 (ix & 1, AvARRAY ((AV *)SvRV (AvARRAY (av)[arg]))[0])); |
382 | else |
430 | else |
383 | for (arg = 0; arg < count; arg++) |
431 | for (arg = 0; arg < count; arg++) |
384 | PUSHs (AvARRAY (av)[arg]); |
432 | PUSHs (maybe_force_utf8 (ix & 1, AvARRAY (av)[arg])); |
385 | } |
433 | } |
386 | } |
434 | } |
387 | } |
435 | } |
388 | else |
436 | else |
389 | XPUSHs (sth); |
437 | XPUSHs (sth); |
390 | |
438 | |
391 | if (ix || GIMME_V == G_VOID) |
439 | if (ix > 1 || GIMME_V == G_VOID) |
392 | { |
440 | { |
393 | PUSHMARK (SP); |
441 | PUSHMARK (SP); |
394 | XPUSHs (sth); |
442 | XPUSHs (sth); |
395 | PUTBACK; |
443 | PUTBACK; |
396 | (void) call_method ("finish", G_DISCARD); |
444 | (void) call_sv (sv_finish, G_METHOD | G_DISCARD); |
397 | SPAGAIN; |
445 | SPAGAIN; |
398 | } |
446 | } |
399 | } |
447 | } |
400 | } |
448 | } |
401 | |
449 | |