--- PApp-SQL/SQL.pm 2000/11/26 21:04:22 1.4 +++ PApp-SQL/SQL.pm 2001/02/03 17:48:02 1.9 @@ -52,11 +52,18 @@ flags from C<$flags>. These are just the same arguments as given to Cconnect>. -The database handle will be cached under the unique id C<$id>. If the same -id is requested later, the cached handle will be checked (using ping), and -the connection will be re-established if necessary (be sure to prefix your -application or module name to the id to make it "more" unique. Things like -__PACKAGE__ . __LINE__ work fine as well). +The database handle will be cached under the unique id +C<$id|$dsn|$user|$pass>. If the same id is requested later, the +cached handle will be checked (using ping), and the connection will +be re-established if necessary (be sure to prefix your application or +module name to the id to make it "more" unique. Things like __PACKAGE__ . +__LINE__ work fine as well). + +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. ""). 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 @@ -75,13 +82,13 @@ $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 cache (sooory ;) + # first, nuke our statement cache (sooory ;) cachesize cachesize 0; # then connect anew $dbcache{$id} = eval { DBI->connect($dsn, $user, $pass, $flags) } || eval { DBI->connect($dsn, $user, $pass, $flags) } - || die "$DBI::errstr\n"; + || die "unable to connect to database $dsn: $DBI::errstr\n"; $connect->($dbcache{$id}) if $connect; } $dbcache{$id}; @@ -190,9 +197,17 @@ =item $lastid = sql_insertid $sth -Returns the last automatically created key value (e.g. for mysql -AUTO_INCREMENT or sybase IDENTITY fields). It must be executed directly -after executing the insert statement that created it. +Returns the last automatically created key value. It must be executed +directly after executing the insert statement that created it. This is +what is actually returned for various databases. If your database is +missing, please send me an e-mail on how to implement this ;) + + mysql: first C column set to NULL + postgres: C column (is there a way to get the last SERIAL?) + sybase: C column of the last insert (slow) + informix: C or C column of the last insert + +Except for sybase, this does not require a server access. =cut @@ -201,8 +216,9 @@ my $dbh = $sth->{Database}; my $driver = $dbh->{Driver}{Name}; - $driver eq "mysql" and return $sth->{mysql_insertid}; - $driver eq "Sybase" and return sql_fetch($dbh, 'SELECT @@IDENTITY'); + $driver eq "mysql" and return $sth->{mysql_insertid}; + $driver eq "Pg" and return $sth->{pg_oid_status}; + $driver eq "Sybase" and return sql_fetch($dbh, 'SELECT @@IDENTITY'); $driver eq "Informix" and return $sth->{ix_sqlerrd}[1]; die "sql_insertid does not spport the dbd driver '$driver', please see PApp::SQL::sql_insertid"; @@ -227,7 +243,9 @@ =item reinitialize [not exported] -Clears any internal caches (statement cache, database handle cache). +Clears any internal caches (statement cache, database handle +cache). Should be called after C and other accidents that invalidate +database handles. =cut @@ -243,6 +261,8 @@ =cut +reinitialize; + package PApp::SQL::Database; =head2 THE DATABASE CLASS @@ -303,15 +323,13 @@ sub dsn($) { my $self = shift; - $self->[1][1]; + (split /\x00/, $self->[0])[1]; } =back =cut -reinitialize; - 1; =head1 BUGS