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.1 by root, Sat Oct 21 19:00:53 2000 UTC vs.
Revision 1.13 by root, Wed Jun 26 03:26:39 2002 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 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 >= 6))
12# define CAN_UTF8 1
13#endif
14
15static SV *
16sql_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
25static SV *
26mortalcopy_and_maybe_force_utf8(int utf8, SV *sv)
27{
28 sv = sv_mortalcopy (sv);
29#if CAN_UTF8
30 if (utf8 && SvPOK (sv))
31 SvUTF8_on (sv);
32#endif
33 return sv;
34}
35
36#define maybe_upgrade_utf8(utf8,sv) ((utf8) ? sql_upgrade_utf8 (sv) : (sv))
37
5#define is_dbh(sv) (sv && SvROK (sv) && sv_derived_from (sv, "DBI::db")) 38#define is_dbh(sv) ((sv) && sv_isobject (sv) && sv_derived_from ((sv), "DBI::db"))
6 39
7typedef struct lru_node { 40typedef struct lru_node {
8 struct lru_node *next; 41 struct lru_node *next;
9 struct lru_node *prev; 42 struct lru_node *prev;
10 U32 hash; 43 U32 hash;
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
22static lru_node lru_list; 56static lru_node lru_list;
23static int lru_size; 57static 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
135static GV *sql_exec; 163static GV *sql_exec;
136static GV *DBH; 164static GV *DBH;
165static 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
138MODULE = PApp::SQL PACKAGE = PApp::SQL 171MODULE = PApp::SQL PACKAGE = PApp::SQL
139 172
140PROTOTYPES: DISABLE 173PROTOTYPES: DISABLE
141 174
142BOOT: 175BOOT:
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
164void 207void
165sql_exec(...) 208sql_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 */
188 if (!is_dbh (dbh)) 236 if (!is_dbh (dbh))
189 { 237 {
238 /* the next line doesn't work - check why later maybe */
190 dbh = get_sv ("DBH", FALSE); 239 /* dbh = get_sv ("DBH", FALSE);
191 if (!is_dbh (dbh)) 240 if (!is_dbh (dbh))
192 { 241 {*/
193 dbh = GvSV(DBH); 242 dbh = GvSV(DBH);
194 if (!is_dbh (dbh)) 243 if (!is_dbh (dbh))
244 croak ("sql_exec: no $DBH argument and no fallback in $PApp::SQL::DBH");
195 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::");
196 } 246 }*/
197 } 247 }
198 else 248 else
199 arg++; /* we consumed one argument */ 249 arg++; /* we consumed one argument */
200 250
201 /* count the remaining references (for bind_columns) */ 251 /* count the remaining references (for bind_columns) */
212 if (!SvPOK (ST(arg))) 262 if (!SvPOK (ST(arg)))
213 croak ("sql_exec: sql-statement must be a string"); 263 croak ("sql_exec: sql-statement must be a string");
214 264
215 sql = ST(arg); arg++; 265 sql = ST(arg); arg++;
216 266
217 if (ix == 4) 267 if ((ix & ~1) == 6)
218 { 268 {
219 SV *neu = sv_2mortal (newSVpv ("select count(*) > 0 from ", 0)); 269 SV *neu = sv_2mortal (newSVpv ("select count(*) > 0 from ", 0));
220 sv_catsv (neu, sql); 270 sv_catsv (neu, sql);
221 sv_catpv (neu, " limit 1"); 271 sv_catpv (neu, " limit 1");
222 sql = neu; 272 sql = neu;
223 ix = 1; /* sql_fetch */ 273 ix -= 4; /* sql_fetch */
224 } 274 }
225 275
226 /* check cache for existing statement handle (NYI) */ 276 /* check cache for existing statement handle */
227 sth = lru_fetch (dbh, sql); 277 sth = lru_fetch (dbh, sql);
228 if (!sth) 278 if (!sth)
229 { 279 {
230 PUSHMARK (SP); 280 PUSHMARK (SP);
231 EXTEND (SP, 2); 281 EXTEND (SP, 2);
232 PUSHs (dbh); 282 PUSHs (dbh);
233 PUSHs (sql); 283 PUSHs (sql);
234 PUTBACK; 284 PUTBACK;
235 count = call_method ("prepare", G_SCALAR); 285 count = call_sv (sv_prepare, G_METHOD | G_SCALAR);
236 SPAGAIN; 286 SPAGAIN;
237 287
238 if (count != 1) 288 if (count != 1)
239 croak ("sql_exec: unable to prepare() statement '%s': %s", 289 croak ("sql_exec: unable to prepare() statement '%s': %s",
240 SvPV_nolen (sql), 290 SvPV (sql, dc),
241 SvPV_nolen (get_sv ("DBI::errstr", TRUE))); 291 SvPV (get_sv ("DBI::errstr", TRUE), dd));
242 292
243 sth = POPs; 293 sth = POPs;
244 294
245 lru_store (dbh, sql, sth); 295 lru_store (dbh, sql, sth);
246 } 296 }
248 PUSHMARK (SP); 298 PUSHMARK (SP);
249 EXTEND (SP, items - arg + 1); 299 EXTEND (SP, items - arg + 1);
250 PUSHs (sth); 300 PUSHs (sth);
251 while (items > arg) 301 while (items > arg)
252 { 302 {
253 PUSHs (ST(arg)); 303 PUSHs (maybe_upgrade_utf8 (ix & 1, ST(arg)));
254 arg++; 304 arg++;
255 } 305 }
256 306
257 PUTBACK; 307 PUTBACK;
258 /* { static GV *execute; 308 /* { static GV *execute;
259 if (!execute) execute = gv_fetchmethod_autoload(SvSTASH(SvRV(sth)), "execute", 0); 309 if (!execute) execute = gv_fetchmethod_autoload(SvSTASH(SvRV(sth)), "execute", 0);
260 count = call_sv(GvCV(execute), G_SCALAR); 310 count = call_sv(GvCV(execute), G_SCALAR);
261 }*/ 311 }*/
262 count = call_method ("execute", G_SCALAR); 312 count = call_sv (sv_execute, G_METHOD | G_SCALAR);
263 SPAGAIN; 313 SPAGAIN;
264 314
265 if (count != 1) 315 if (count != 1)
266 croak ("sql_exec: execute() didn't return any value ('%s'): %s", 316 croak ("sql_exec: execute() didn't return any value ('%s'): %s",
267 SvPV_nolen (sql), 317 SvPV (sql, dc),
268 SvPV_nolen (get_sv ("DBI::errstr", TRUE))); 318 SvPV (get_sv ("DBI::errstr", TRUE), dd));
269 319
270 execute = POPs; 320 execute = POPs;
271 321
272 if (!SvTRUE (execute)) 322 if (!SvTRUE (execute))
273 croak ("sql_exec: unable to execute statement '%s' (%s)", 323 croak ("sql_exec: unable to execute statement '%s' (%s)",
274 SvPV_nolen (sql), 324 SvPV (sql, dc),
275 SvPV_nolen (get_sv ("DBI::errstr", TRUE))); 325 SvPV (get_sv ("DBI::errstr", TRUE), dd));
276 326
277 sv_setsv (GvSV(sql_exec), execute); 327 sv_setsv (GvSV(sql_exec), execute);
278 328
279 if (bind_first != bind_last) 329 if (bind_first != bind_last)
280 { 330 {
285 PUSHs (ST(bind_first)); 335 PUSHs (ST(bind_first));
286 bind_first++; 336 bind_first++;
287 } while (bind_first != bind_last); 337 } while (bind_first != bind_last);
288 338
289 PUTBACK; 339 PUTBACK;
290 count = call_method ("bind_columns", G_SCALAR); 340 count = call_sv (sv_bind_columns, G_METHOD | G_SCALAR);
291 SPAGAIN; 341 SPAGAIN;
292 342
293 if (count != 1) 343 if (count != 1)
294 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",
295 SvPV_nolen (sql), 345 SvPV (sql, dc),
296 SvPV_nolen (get_sv ("DBI::errstr", TRUE))); 346 SvPV (get_sv ("DBI::errstr", TRUE), dd));
297 347
298 if (!SvOK (POPs)) 348 if (!SvOK (POPs))
299 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",
300 SvPV_nolen (sql), 350 SvPV (sql, dc),
301 SvPV_nolen (get_sv ("DBI::errstr", TRUE))); 351 SvPV (get_sv ("DBI::errstr", TRUE), dd));
302 } 352 }
303 353
304 /* free our arguments from the stack */ 354 /* free our arguments from the stack */
305 SP -= items; 355 SP -= items;
306 356
307 if (ix == 1) 357 if ((ix & ~1) == 2)
308 { /* sql_fetch */ 358 { /* sql_fetch */
309 SV *row; 359 SV *row;
310 360
311 PUSHMARK (SP); 361 PUSHMARK (SP);
312 XPUSHs (sth); 362 XPUSHs (sth);
313 PUTBACK; 363 PUTBACK;
314 count = call_method ("fetchrow_arrayref", G_SCALAR); 364 count = call_sv (sv_fetchrow_arrayref, G_METHOD | G_SCALAR);
315 SPAGAIN; 365 SPAGAIN;
316 366
317 if (count != 1) 367 if (count != 1)
318 abort (); 368 abort ();
319 369
328 case G_VOID: 378 case G_VOID:
329 /* no thing */ 379 /* no thing */
330 break; 380 break;
331 case G_SCALAR: 381 case G_SCALAR:
332 /* the first element */ 382 /* the first element */
333 XPUSHs (*av_fetch ((AV *)SvRV (row), 0, 1)); 383 XPUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, *av_fetch ((AV *)SvRV (row), 0, 1)));
334 break; 384 break;
335 case G_ARRAY: 385 case G_ARRAY:
336 av = (AV *)SvRV (row); 386 av = (AV *)SvRV (row);
337 count = AvFILL (av) + 1; 387 count = AvFILL (av) + 1;
338 EXTEND (SP, count); 388 EXTEND (SP, count);
339 for (arg = 0; arg < count; arg++) 389 for (arg = 0; arg < count; arg++)
340 PUSHs (AvARRAY (av)[arg]); 390 PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY (av)[arg]));
341 391
342 break; 392 break;
343 default: 393 default:
344 abort (); 394 abort ();
345 } 395 }
346 } 396 }
347 } 397 }
348 else if (ix == 2) 398 else if ((ix & ~1) == 4)
349 { /* sql_fetchall */ 399 { /* sql_fetchall */
350 SV *rows; 400 SV *rows;
351 401
352 PUSHMARK (SP); 402 PUSHMARK (SP);
353 XPUSHs (sth); 403 XPUSHs (sth);
354 PUTBACK; 404 PUTBACK;
355 count = call_method ("fetchall_arrayref", G_SCALAR); 405 count = call_sv (sv_fetchall_arrayref, G_METHOD | G_SCALAR);
356 SPAGAIN; 406 SPAGAIN;
357 407
358 if (count != 1) 408 if (count != 1)
359 abort (); 409 abort ();
360 410
365 AV *av = (AV *)SvRV (rows); 415 AV *av = (AV *)SvRV (rows);
366 count = AvFILL (av) + 1; 416 count = AvFILL (av) + 1;
367 417
368 if (count) 418 if (count)
369 { 419 {
370 int columns = AvFILL ((AV *)SvRV (AvARRAY(av)[0])) + 1; /* columns? */ 420 int columns = AvFILL ((AV *) SvRV (AvARRAY (av)[0])) + 1; /* columns? */
371 421
372 EXTEND (SP, count); 422 EXTEND (SP, count);
373 if (columns == 1) 423 if (columns == 1)
374 for (arg = 0; arg < count; arg++) 424 for (arg = 0; arg < count; arg++)
375 PUSHs (AvARRAY ((AV *)SvRV (AvARRAY (av)[arg]))[0]); 425 PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY ((AV *)SvRV (AvARRAY (av)[arg]))[0]));
376 else 426 else
377 for (arg = 0; arg < count; arg++) 427 for (arg = 0; arg < count; arg++)
378 PUSHs (AvARRAY (av)[arg]); 428 PUSHs (mortalcopy_and_maybe_force_utf8 (ix & 1, AvARRAY (av)[arg]));
379 } 429 }
380 } 430 }
381 } 431 }
382 else 432 else
383 XPUSHs (sth); 433 XPUSHs (sth);
384 434
385 if (ix || GIMME_V == G_VOID) 435 if (ix > 1 || GIMME_V == G_VOID)
386 { 436 {
387 PUSHMARK (SP); 437 PUSHMARK (SP);
388 XPUSHs (sth); 438 XPUSHs (sth);
389 PUTBACK; 439 PUTBACK;
390 (void) call_method ("finish", G_DISCARD); 440 (void) call_sv (sv_finish, G_METHOD | G_DISCARD);
391 SPAGAIN; 441 SPAGAIN;
392 } 442 }
393 } 443 }
394} 444}
395 445

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines