ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/PApp-SQL/SQL.pm
(Generate patch)

Comparing PApp-SQL/SQL.pm (file contents):
Revision 1.24 by root, Thu Apr 11 01:02:10 2002 UTC vs.
Revision 1.36 by root, Fri Mar 3 14:12:22 2006 UTC

44use DBI (); 44use DBI ();
45 45
46BEGIN { 46BEGIN {
47 use base qw(Exporter DynaLoader); 47 use base qw(Exporter DynaLoader);
48 48
49 $VERSION = 0.131; 49 $VERSION = '1.02';
50 @EXPORT = qw( 50 @EXPORT = qw(
51 sql_exec sql_fetch sql_fetchall sql_exists sql_insertid $sql_exec 51 sql_exec sql_fetch sql_fetchall sql_exists sql_insertid $sql_exec
52 sql_uexec sql_ufetch sql_ufetchall sql_uexists 52 sql_uexec sql_ufetch sql_ufetchall sql_uexists
53 ); 53 );
54 @EXPORT_OK = qw( 54 @EXPORT_OK = qw(
62our $DBH; # the default database handle 62our $DBH; # the default database handle
63our $Database; # the current SQL::Database object, if applicable 63our $Database; # the current SQL::Database object, if applicable
64 64
65our %dbcache; 65our %dbcache;
66 66
67=head2 GLOBAL VARIABLES 67=head2 Global Variables
68 68
69=over 4 69=over 4
70 70
71=item $sql_exec 71=item $sql_exec
72 72
88be nice as a placeholder for the database object that corresponds to 88be nice as a placeholder for the database object that corresponds to
89$PApp::SQL::DBH. 89$PApp::SQL::DBH.
90 90
91=back 91=back
92 92
93=head2 FUNCTIONS 93=head2 Functions
94 94
95=over 4 95=over 4
96 96
97=item $dbh = connect_cached $id, $dsn, $user, $pass, $flags, $connect 97=item $dbh = connect_cached $id, $dsn, $user, $pass, $flags, $connect
98 98
186package-global (and exported) variable C<$sql_exec>. 186package-global (and exported) variable C<$sql_exec>.
187 187
188If any error occurs C<sql_exec> will throw an exception. 188If any error occurs C<sql_exec> will throw an exception.
189 189
190C<sql_uexec> is similar to C<sql_exec> but upgrades all input arguments to 190C<sql_uexec> is similar to C<sql_exec> but upgrades all input arguments to
191utf8 before calling the C<execute> method. 191UTF-8 before calling the C<execute> method.
192 192
193Examples: 193Examples:
194 194
195 # easy one 195 # easy one
196 my $st = sql_exec "select name, id from table where id = ?", $id; 196 my $st = sql_exec "select name, id from table where id = ?", $id;
230 my($name, $amount) = sql_fetch "select ...", args... 230 my($name, $amount) = sql_fetch "select ...", args...
231 231
232... and it's still quite fast unless you fetch large amounts of data. 232... and it's still quite fast unless you fetch large amounts of data.
233 233
234C<sql_ufetch> is similar to C<sql_fetch> but upgrades all input values to 234C<sql_ufetch> is similar to C<sql_fetch> but upgrades all input values to
235utf8 and forces all result values to utf8 (this does I<not> include result 235UTF-8 and forces all result values to UTF-8 (this does I<not> include result
236parameters, only return values. Using bind variables in cinjunction with 236parameters, only return values. Using bind variables in conjunction with
237sql_u* functions results in undefined behaviour). 237sql_u* functions might result in undefined behaviour - we use UTF-8 on
238bind-variables at execution time and it seems to work on DBD::mysql as it
239ignores the UTF-8 bit completely. Which just means that that DBD-driver is
240broken).
238 241
239=item sql_fetchall <see sql_exec> 242=item sql_fetchall <see sql_exec>
240 243
241=item sql_ufetchall <see sql_uexec> 244=item sql_ufetchall <see sql_uexec>
242 245
259 for (sql_fetchall "select name, age, place from user") { 262 for (sql_fetchall "select name, age, place from user") {
260 my ($name, $age, $place) = @$_; 263 my ($name, $age, $place) = @$_;
261 } 264 }
262 265
263C<sql_ufetchall> is similar to C<sql_fetchall> but upgrades all input 266C<sql_ufetchall> is similar to C<sql_fetchall> but upgrades all input
264values to utf8 and forces all result values to utf8 (see the caveats in 267values to UTF-8 and forces all result values to UTF-8 (see the caveats in
265the description of C<sql_ufetch>, though). 268the description of C<sql_ufetch>, though).
266 269
267=item sql_exists "<table_references> where <where_condition>...", args... 270=item sql_exists "<table_references> where <where_condition>...", args...
268 271
269=item sql_uexists <see sql_exists> 272=item sql_uexists <see sql_exists>
273"select * from" were prepended to your statement (it isn't)). Should work 276"select * from" were prepended to your statement (it isn't)). Should work
274with every database but can be quite slow, except on mysql, where this 277with every database but can be quite slow, except on mysql, where this
275should be quite fast. 278should be quite fast.
276 279
277C<sql_uexists> is similar to C<sql_exists> but upgrades all parameters to 280C<sql_uexists> is similar to C<sql_exists> but upgrades all parameters to
278utf8. 281UTF-8.
279 282
280Examples: 283Examples:
281 284
282 print "user 7 exists!\n" 285 print "user 7 exists!\n"
283 if sql_exists "user where id = ?", 7; 286 if sql_exists "user where id = ?", 7;
296 299
297 mysql: first C<AUTO_INCREMENT> column set to NULL 300 mysql: first C<AUTO_INCREMENT> column set to NULL
298 postgres: C<oid> column (is there a way to get the last SERIAL?) 301 postgres: C<oid> column (is there a way to get the last SERIAL?)
299 sybase: C<IDENTITY> column of the last insert (slow) 302 sybase: C<IDENTITY> column of the last insert (slow)
300 informix: C<SERIAL> or C<SERIAL8> column of the last insert 303 informix: C<SERIAL> or C<SERIAL8> column of the last insert
304 sqlite: C<last_insert_rowid()>
301 305
302Except for sybase, this does not require a server access. 306Except for sybase, this does not require a server access.
303 307
304=cut 308=cut
305 309
310 314
311 $driver eq "mysql" and return $sth->{mysql_insertid}; 315 $driver eq "mysql" and return $sth->{mysql_insertid};
312 $driver eq "Pg" and return $sth->{pg_oid_status}; 316 $driver eq "Pg" and return $sth->{pg_oid_status};
313 $driver eq "Sybase" and return sql_fetch($dbh, 'SELECT @@IDENTITY'); 317 $driver eq "Sybase" and return sql_fetch($dbh, 'SELECT @@IDENTITY');
314 $driver eq "Informix" and return $sth->{ix_sqlerrd}[1]; 318 $driver eq "Informix" and return $sth->{ix_sqlerrd}[1];
319 $driver eq "SQLite" and return sql_fetch($dbh, 'SELECT last_insert_rowid ()');
315 320
316 die "sql_insertid does not spport the dbd driver '$driver', please see PApp::SQL::sql_insertid"; 321 die "sql_insertid does not spport the dbd driver '$driver', please see PApp::SQL::sql_insertid";
317} 322}
318 323
319=item [old-size] = cachesize [new-size] 324=item [old-size] = cachesize [new-size]
320 325
321Returns (and possibly changes) the LRU cache size used by C<sql_exec>. The 326Returns (and possibly changes) the LRU cache size used by C<sql_exec>. The
322default is somewhere around 50 (= the 50 last recently used statements 327default is somewhere around 50 (= the 50 last recently used statements
323will be cached). It shouldn't be too large, since a simple linear listed 328will be cached). It shouldn't be too large, since a simple linear list
324is used for the cache at the moment (which, for small (<100) cache sizes 329is used for the cache at the moment (which, for small (<100) cache sizes
325is actually quite fast). 330is actually quite fast).
326 331
327The function always returns the cache size in effect I<before> the call, 332The function always returns the cache size in effect I<before> the call,
328so, to nuke the cache (for example, when a database connection has died 333so, to nuke the cache (for example, when a database connection has died
355 360
356reinitialize; 361reinitialize;
357 362
358package PApp::SQL::Database; 363package PApp::SQL::Database;
359 364
360=head2 THE DATABASE CLASS 365=head2 The Database Class
361 366
362Again (sigh) the problem of persistency. What do you do when you have 367Again (sigh) the problem of persistency. What do you do when you have
363to serialize on object that contains (or should contain) a database 368to serialize on object that contains (or should contain) a database
364handle? Short answer: you don't. Long answer: you can embed the necessary 369handle? Short answer: you don't. Long answer: you can embed the necessary
365information to recreate the dbh when needed. 370information to recreate the dbh when needed.
403 408
404sub checked_dbh($) { 409sub checked_dbh($) {
405 my $dbh = $dbcache{$_[0][0]}; 410 my $dbh = $dbcache{$_[0][0]};
406 $dbh && $dbh->ping 411 $dbh && $dbh->ping
407 ? $dbh 412 ? $dbh
408 : PApp::SQL::connect_cached((split /\x00/, $_[0][0]), $_[0][1], $_[0][2]); 413 : PApp::SQL::connect_cached((split /\x00/, $_[0][0], 4), $_[0][1], $_[0][2]);
409} 414}
410 415
411=item $db->dsn 416=item $db->dsn
412 417
413Return the DSN (L<DBI>) fo the database object (e.g. for error messages). 418Return the DSN (L<DBI>) fo the database object (e.g. for error messages).
416 421
417Return the login name. 422Return the login name.
418 423
419=item $db->password 424=item $db->password
420 425
421Return the password (emphasizing the fact that the apssword is stored plaintext ;) 426Return the password (emphasizing the fact that the password is stored plaintext ;)
422 427
423=cut 428=cut
424 429
425sub dsn($) { 430sub dsn($) {
426 my $self = shift; 431 my $self = shift;
447 452
448L<PApp>. 453L<PApp>.
449 454
450=head1 AUTHOR 455=head1 AUTHOR
451 456
452 Marc Lehmann <pcg@goof.com> 457 Marc Lehmann <schmorp@schmorp.de>
453 http://www.goof.com/pcg/marc/ 458 http://home.schmorp.de/
454 459
455=cut 460=cut
456 461

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines