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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines