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.10 by root, Mon Dec 31 03:07:57 2001 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 <= 5))
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 *
26sql_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
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 */
218 if (!SvPOK (ST(arg))) 260 if (!SvPOK (ST(arg)))
219 croak ("sql_exec: sql-statement must be a string"); 261 croak ("sql_exec: sql-statement must be a string");
220 262
221 sql = ST(arg); arg++; 263 sql = ST(arg); arg++;
222 264
223 if (ix == 4) 265 if ((ix & ~1) == 6)
224 { 266 {
225 SV *neu = sv_2mortal (newSVpv ("select count(*) > 0 from ", 0)); 267 SV *neu = sv_2mortal (newSVpv ("select count(*) > 0 from ", 0));
226 sv_catsv (neu, sql); 268 sv_catsv (neu, sql);
227 sv_catpv (neu, " limit 1"); 269 sv_catpv (neu, " limit 1");
228 sql = neu; 270 sql = neu;
229 ix = 1; /* sql_fetch */ 271 ix -= 4; /* sql_fetch */
230 } 272 }
231 273
232 /* check cache for existing statement handle */ 274 /* check cache for existing statement handle */
233 sth = lru_fetch (dbh, sql); 275 sth = lru_fetch (dbh, sql);
234 if (!sth) 276 if (!sth)
236 PUSHMARK (SP); 278 PUSHMARK (SP);
237 EXTEND (SP, 2); 279 EXTEND (SP, 2);
238 PUSHs (dbh); 280 PUSHs (dbh);
239 PUSHs (sql); 281 PUSHs (sql);
240 PUTBACK; 282 PUTBACK;
241 count = call_method ("prepare", G_SCALAR); 283 count = call_sv (sv_prepare, G_METHOD | G_SCALAR);
242 SPAGAIN; 284 SPAGAIN;
243 285
244 if (count != 1) 286 if (count != 1)
245 croak ("sql_exec: unable to prepare() statement '%s': %s", 287 croak ("sql_exec: unable to prepare() statement '%s': %s",
246 SvPV (sql, dc), 288 SvPV (sql, dc),
247 SvPV (get_sv ("DBI::errstr", TRUE), dc)); 289 SvPV (get_sv ("DBI::errstr", TRUE), dd));
248 290
249 sth = POPs; 291 sth = POPs;
250 292
251 lru_store (dbh, sql, sth); 293 lru_store (dbh, sql, sth);
252 } 294 }
254 PUSHMARK (SP); 296 PUSHMARK (SP);
255 EXTEND (SP, items - arg + 1); 297 EXTEND (SP, items - arg + 1);
256 PUSHs (sth); 298 PUSHs (sth);
257 while (items > arg) 299 while (items > arg)
258 { 300 {
259 PUSHs (ST(arg)); 301 PUSHs (maybe_upgrade_utf8 (ix & 1, ST(arg)));
260 arg++; 302 arg++;
261 } 303 }
262 304
263 PUTBACK; 305 PUTBACK;
264 /* { static GV *execute; 306 /* { static GV *execute;
265 if (!execute) execute = gv_fetchmethod_autoload(SvSTASH(SvRV(sth)), "execute", 0); 307 if (!execute) execute = gv_fetchmethod_autoload(SvSTASH(SvRV(sth)), "execute", 0);
266 count = call_sv(GvCV(execute), G_SCALAR); 308 count = call_sv(GvCV(execute), G_SCALAR);
267 }*/ 309 }*/
268 count = call_method ("execute", G_SCALAR); 310 count = call_sv (sv_execute, G_METHOD | G_SCALAR);
269 SPAGAIN; 311 SPAGAIN;
270 312
271 if (count != 1) 313 if (count != 1)
272 croak ("sql_exec: execute() didn't return any value ('%s'): %s", 314 croak ("sql_exec: execute() didn't return any value ('%s'): %s",
273 SvPV (sql, dc), 315 SvPV (sql, dc),
274 SvPV (get_sv ("DBI::errstr", TRUE), dc)); 316 SvPV (get_sv ("DBI::errstr", TRUE), dd));
275 317
276 execute = POPs; 318 execute = POPs;
277 319
278 if (!SvTRUE (execute)) 320 if (!SvTRUE (execute))
279 croak ("sql_exec: unable to execute statement '%s' (%s)", 321 croak ("sql_exec: unable to execute statement '%s' (%s)",
280 SvPV (sql, dc), 322 SvPV (sql, dc),
281 SvPV (get_sv ("DBI::errstr", TRUE), dc)); 323 SvPV (get_sv ("DBI::errstr", TRUE), dd));
282 324
283 sv_setsv (GvSV(sql_exec), execute); 325 sv_setsv (GvSV(sql_exec), execute);
284 326
285 if (bind_first != bind_last) 327 if (bind_first != bind_last)
286 { 328 {
291 PUSHs (ST(bind_first)); 333 PUSHs (ST(bind_first));
292 bind_first++; 334 bind_first++;
293 } while (bind_first != bind_last); 335 } while (bind_first != bind_last);
294 336
295 PUTBACK; 337 PUTBACK;
296 count = call_method ("bind_columns", G_SCALAR); 338 count = call_sv (sv_bind_columns, G_METHOD | G_SCALAR);
297 SPAGAIN; 339 SPAGAIN;
298 340
299 if (count != 1) 341 if (count != 1)
300 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",
301 SvPV (sql, dc), 343 SvPV (sql, dc),
302 SvPV (get_sv ("DBI::errstr", TRUE), dc)); 344 SvPV (get_sv ("DBI::errstr", TRUE), dd));
303 345
304 if (!SvOK (POPs)) 346 if (!SvOK (POPs))
305 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",
306 SvPV (sql, dc), 348 SvPV (sql, dc),
307 SvPV (get_sv ("DBI::errstr", TRUE), dc)); 349 SvPV (get_sv ("DBI::errstr", TRUE), dd));
308 } 350 }
309 351
310 /* free our arguments from the stack */ 352 /* free our arguments from the stack */
311 SP -= items; 353 SP -= items;
312 354
313 if (ix == 1) 355 if ((ix & ~1) == 2)
314 { /* sql_fetch */ 356 { /* sql_fetch */
315 SV *row; 357 SV *row;
316 358
317 PUSHMARK (SP); 359 PUSHMARK (SP);
318 XPUSHs (sth); 360 XPUSHs (sth);
319 PUTBACK; 361 PUTBACK;
320 count = call_method ("fetchrow_arrayref", G_SCALAR); 362 count = call_sv (sv_fetchrow_arrayref, G_METHOD | G_SCALAR);
321 SPAGAIN; 363 SPAGAIN;
322 364
323 if (count != 1) 365 if (count != 1)
324 abort (); 366 abort ();
325 367
334 case G_VOID: 376 case G_VOID:
335 /* no thing */ 377 /* no thing */
336 break; 378 break;
337 case G_SCALAR: 379 case G_SCALAR:
338 /* the first element */ 380 /* the first element */
339 XPUSHs (*av_fetch ((AV *)SvRV (row), 0, 1)); 381 XPUSHs (maybe_force_utf8 (ix & 1, *av_fetch ((AV *)SvRV (row), 0, 1)));
340 break; 382 break;
341 case G_ARRAY: 383 case G_ARRAY:
342 av = (AV *)SvRV (row); 384 av = (AV *)SvRV (row);
343 count = AvFILL (av) + 1; 385 count = AvFILL (av) + 1;
344 EXTEND (SP, count); 386 EXTEND (SP, count);
345 for (arg = 0; arg < count; arg++) 387 for (arg = 0; arg < count; arg++)
346 PUSHs (AvARRAY (av)[arg]); 388 PUSHs (maybe_force_utf8 (ix & 1, AvARRAY (av)[arg]));
347 389
348 break; 390 break;
349 default: 391 default:
350 abort (); 392 abort ();
351 } 393 }
352 } 394 }
353 } 395 }
354 else if (ix == 2) 396 else if ((ix & ~1) == 4)
355 { /* sql_fetchall */ 397 { /* sql_fetchall */
356 SV *rows; 398 SV *rows;
357 399
358 PUSHMARK (SP); 400 PUSHMARK (SP);
359 XPUSHs (sth); 401 XPUSHs (sth);
360 PUTBACK; 402 PUTBACK;
361 count = call_method ("fetchall_arrayref", G_SCALAR); 403 count = call_sv (sv_fetchall_arrayref, G_METHOD | G_SCALAR);
362 SPAGAIN; 404 SPAGAIN;
363 405
364 if (count != 1) 406 if (count != 1)
365 abort (); 407 abort ();
366 408
371 AV *av = (AV *)SvRV (rows); 413 AV *av = (AV *)SvRV (rows);
372 count = AvFILL (av) + 1; 414 count = AvFILL (av) + 1;
373 415
374 if (count) 416 if (count)
375 { 417 {
376 int columns = AvFILL ((AV *)SvRV (AvARRAY(av)[0])) + 1; /* columns? */ 418 int columns = AvFILL ((AV *) SvRV (AvARRAY (av)[0])) + 1; /* columns? */
377 419
378 EXTEND (SP, count); 420 EXTEND (SP, count);
379 if (columns == 1) 421 if (columns == 1)
380 for (arg = 0; arg < count; arg++) 422 for (arg = 0; arg < count; arg++)
381 PUSHs (AvARRAY ((AV *)SvRV (AvARRAY (av)[arg]))[0]); 423 PUSHs (maybe_force_utf8 (ix & 1, AvARRAY ((AV *)SvRV (AvARRAY (av)[arg]))[0]));
382 else 424 else
383 for (arg = 0; arg < count; arg++) 425 for (arg = 0; arg < count; arg++)
384 PUSHs (AvARRAY (av)[arg]); 426 PUSHs (maybe_force_utf8 (ix & 1, AvARRAY (av)[arg]));
385 } 427 }
386 } 428 }
387 } 429 }
388 else 430 else
389 XPUSHs (sth); 431 XPUSHs (sth);
390 432
391 if (ix || GIMME_V == G_VOID) 433 if (ix > 1 || GIMME_V == G_VOID)
392 { 434 {
393 PUSHMARK (SP); 435 PUSHMARK (SP);
394 XPUSHs (sth); 436 XPUSHs (sth);
395 PUTBACK; 437 PUTBACK;
396 (void) call_method ("finish", G_DISCARD); 438 (void) call_sv (sv_finish, G_METHOD | G_DISCARD);
397 SPAGAIN; 439 SPAGAIN;
398 } 440 }
399 } 441 }
400} 442}
401 443

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines