--- PApp-SQL/SQL.pm 2001/03/11 14:54:21 1.14 +++ PApp-SQL/SQL.pm 2002/04/07 16:22:56 1.22 @@ -1,6 +1,6 @@ =head1 NAME -PApp::SQL - absolutely easy yet fast and powerful sql access +PApp::SQL - absolutely easy yet fast and powerful sql access. =head1 SYNOPSIS @@ -15,7 +15,7 @@ my $a = sql_fetch "select a from ..."; sql_fetch \my($a, $b), "select a,b ..."; - sql_exists "name from table where name like 'a%'" + sql_exists "table where name like 'a%'" or die "a* required but not existent"; my $db = new PApp::SQL::Database "", "DBI:mysql:test", "user", "pass"; @@ -29,7 +29,8 @@ commands (using DBI). Despite being easy to use, they are also quite efficient and allow you to write faster programs in less lines of code. It should work with anything from perl-5.004_01 onwards, but I only support -5.005+. +5.005+. UTF8 handling (the C family of functions) will only be +effective with perl version 5.006 and beyond. If the descriptions here seem terse or if you always wanted to know what PApp is then have a look at the PApp module which uses this module @@ -45,9 +46,10 @@ BEGIN { use base qw(Exporter DynaLoader); - $VERSION = 0.122; + $VERSION = 0.13; @EXPORT = qw( - sql_exec sql_fetch sql_fetchall sql_exists sql_insertid $sql_exec + sql_exec sql_fetch sql_fetchall sql_exists sql_insertid $sql_exec + sql_uexec sql_ufetch sql_ufetchall sql_uexists ); @EXPORT_OK = qw( connect_cached @@ -69,7 +71,7 @@ =item $sql_exec Since the C family of functions return a statement handle there -must eb another way to test the return value of the C call. This +must be another way to test the return value of the C call. This global variable contains the result of the most recent call to C done by this module. @@ -111,8 +113,8 @@ The reason C<$id> is necessary is that you might specify special connect arguments or special flags, or you might want to configure your $DBH differently than maybe other applications requesting the same database -connection. If none of this is becessary for your application you can -leave $id empty (i.e. ""). +connection. If none of this is necessary for your application you can +leave C<$id> empty (i.e. ""). If specified, C<$connect> is a callback (e.g. a coderef) that will be called each time a new connection is being established, with the new @@ -123,6 +125,9 @@ # try your luck opening the papp database without access info $dbh = connect_cached __FILE__, "DBI:mysql:papp"; +Mysql-specific behaviour: The default setting of +C is TRUE, you can overwrite this, though. + =cut sub connect_cached { @@ -130,9 +135,14 @@ # the following line is duplicated in PApp::SQL::Database::new $id = "$id\0$dsn\0$user\0$pass"; unless ($dbcache{$id} && $dbcache{$id}->ping) { - #warn "connecting to ($dsn|$user|$pass|$flags)\n";#d# # first, nuke our statement cache (sooory ;) cachesize cachesize 0; + + # then make mysql behave more standardly by default + $dsn =~ /^[Dd][Bb][Ii]:mysql:/ + and $dsn !~ /;mysql_client_found_rows/ + and $dsn .= ";mysql_client_found_rows=1"; + # then connect anew $dbcache{$id} = eval { DBI->connect($dsn, $user, $pass, $flags) } @@ -145,6 +155,8 @@ =item $sth = sql_exec [dbh,] [bind-vals...,] "sql-statement", [arguments...] +=item $sth = sql_uexec + C is the most important and most-used function in this module. Runs the given sql command with the given parameters and returns the @@ -165,6 +177,9 @@ If any error occurs C will throw an exception. +C is similar to C but upgrades all input arguments to +utf8 before calling the C method. + Examples: # easy one @@ -183,7 +198,9 @@ =item sql_fetch -Execute a sql-statement and fetch the first row of results. Depending on +=item sql_ufetch + +Execute an sql-statement and fetch the first row of results. Depending on the caller context the row will be returned as a list (array context), or just the first columns. In table form: @@ -204,8 +221,15 @@ ... and it's still quite fast unless you fetch large amounts of data. +C is similar to C but upgrades all input values to +utf8 and forces all result values to utf8 (this does I include result +parameters, only return values. Using bind variables in cinjunction with +sql_u* functions results in undefined behaviour). + =item sql_fetchall +=item sql_ufetchall + Similarly to C, but all result rows will be fetched (this is of course inefficient for large results!). The context is ignored (only list context makes sense), but the result still depends on the number of @@ -226,7 +250,13 @@ my ($name, $age, $place) = @$_; } -=item sql_exists " where ...", args... +C is similar to C but upgrades all input +values to utf8 and forces all result values to utf8 (see the caveats in +the description of C, though). + +=item sql_exists " where ...", args... + +=item sql_uexists Check wether the result of the sql-statement "select xxx from $first_argument" would be empty or not (that is, imagine the string @@ -234,6 +264,9 @@ with every database but can be quite slow, except on mysql, where this should be quite fast. +C is similar to C but upgrades all parameters to +utf8. + Examples: print "user 7 exists!\n" @@ -316,9 +349,10 @@ =head2 THE DATABASE CLASS -Again (sigh) the problem of persistency. What do you do when you have to serialize on object -that contains (or should contain) a database handle? Short answer: you don't. Long answer: -you can embed the necessary information to recreate the dbh when needed. +Again (sigh) the problem of persistency. What do you do when you have +to serialize on object that contains (or should contain) a database +handle? Short answer: you don't. Long answer: you can embed the necessary +information to recreate the dbh when needed. The C class does that, in a relatively efficient fashion: the overhead is currently a single method call per access (you