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.1 by root, Sat Oct 21 19:00:53 2000 UTC vs.
Revision 1.7 by root, Mon Jan 15 00:19:55 2001 UTC

24#use PApp::Exception; # not yet used 24#use PApp::Exception; # not yet used
25 25
26BEGIN { 26BEGIN {
27 use base Exporter; 27 use base Exporter;
28 28
29 $VERSION = 0.1; 29 $VERSION = 0.11;
30 @EXPORT = qw( 30 @EXPORT = qw(
31 sql_exec sql_fetch sql_fetchall sql_exists sql_insertid $sql_exec 31 sql_exec sql_fetch sql_fetchall sql_exists sql_insertid $sql_exec
32 ); 32 );
33 @EXPORT_OK = qw( 33 @EXPORT_OK = qw(
34 connect_cached 34 connect_cached
50 50
51Connect to the database given by C<($dsn,$user,$pass)>, while using the 51Connect to the database given by C<($dsn,$user,$pass)>, while using the
52flags from C<$flags>. These are just the same arguments as given to 52flags from C<$flags>. These are just the same arguments as given to
53C<DBI->connect>. 53C<DBI->connect>.
54 54
55The database handle will be cached under the unique id C<$id>. If the same 55The database handle will be cached under the unique id
56id is requested later, the cached handle will be checked (using ping), and 56C<$id|$dsn|$user|$pass>. If the same id is requested later, the
57cached handle will be checked (using ping), and the connection will
57the connection will be re-established if necessary (be sure to prefix your 58be re-established if necessary (be sure to prefix your application or
58application or module name to the id to make it "more" unique. Things like 59module name to the id to make it "more" unique. Things like __PACKAGE__ .
59__PACKAGE__ . __LINE__ work fine as well). 60__LINE__ work fine as well).
61
62The reason C<$id> is necessary is that you might specify special connect
63arguments or special flags, or you might want to configure your $DBH
64differently than maybe other applications requesting the same database
65connection. If none of this is becessary for your application you can
66leave $id empty (i.e. "").
60 67
61If specified, C<$connect> is a callback (e.g. a coderef) that will be 68If specified, C<$connect> is a callback (e.g. a coderef) that will be
62called each time a new connection is being established, with the new 69called each time a new connection is being established, with the new
63C<$dbh> as first argument. 70C<$dbh> as first argument.
64 71
73 my ($id, $dsn, $user, $pass, $flags, $connect) = @_; 80 my ($id, $dsn, $user, $pass, $flags, $connect) = @_;
74 # the following line is duplicated in PApp::SQL::Database::new 81 # the following line is duplicated in PApp::SQL::Database::new
75 $id = "$id\0$dsn\0$user\0$pass"; 82 $id = "$id\0$dsn\0$user\0$pass";
76 unless ($dbcache{$id} && $dbcache{$id}->ping) { 83 unless ($dbcache{$id} && $dbcache{$id}->ping) {
77 #warn "connecting to ($dsn|$user|$pass|$flags)\n";#d# 84 #warn "connecting to ($dsn|$user|$pass|$flags)\n";#d#
78 # first, nuke our cache (sooory ;) 85 # first, nuke our statement cache (sooory ;)
79 cachesize cachesize 0; 86 cachesize cachesize 0;
80 # then connect anew 87 # then connect anew
81 $dbcache{$id} = 88 $dbcache{$id} =
82 eval { DBI->connect($dsn, $user, $pass, $flags) } 89 eval { DBI->connect($dsn, $user, $pass, $flags) }
83 || eval { DBI->connect($dsn, $user, $pass, $flags) } 90 || eval { DBI->connect($dsn, $user, $pass, $flags) }
84 || die $DBI::errstr; 91 || die "unable to connect to database $dsn: $DBI::errstr\n";
85 $connect->($dbcache{$id}) if $connect; 92 $connect->($dbcache{$id}) if $connect;
86 } 93 }
87 $dbcache{$id}; 94 $dbcache{$id};
88} 95}
89 96
186 die "duplicate key" 193 die "duplicate key"
187 if sql_exists "user where name = ? and pass = ?", "stefan", "geheim"; 194 if sql_exists "user where name = ? and pass = ?", "stefan", "geheim";
188 195
189=cut 196=cut
190 197
191# uncodumented, since unportable (only works with DBH even!). yet it is exported (aaargh!) 198=item $lastid = sql_insertid $sth
199
200Returns the last automatically created key value (e.g. for mysql
201AUTO_INCREMENT or sybase IDENTITY fields). It must be executed directly
202after executing the insert statement that created it.
203
204=cut
205
192sub sql_insertid { 206sub sql_insertid($) {
193 $DBH->{mysql_insertid}; 207 my $sth = shift or die "sql_insertid requires a statement handle";
208 my $dbh = $sth->{Database};
209 my $driver = $dbh->{Driver}{Name};
210
211 $driver eq "mysql" and return $sth->{mysql_insertid};
212 $driver eq "Sybase" and return sql_fetch($dbh, 'SELECT @@IDENTITY');
213 $driver eq "Informix" and return $sth->{ix_sqlerrd}[1];
214
215 die "sql_insertid does not spport the dbd driver '$driver', please see PApp::SQL::sql_insertid";
194} 216}
195 217
196=item [old-size] = cachesize [new-size] 218=item [old-size] = cachesize [new-size]
197 219
198Returns (and possibly changes) the LRU cache size used by C<sql_exec>. The 220Returns (and possibly changes) the LRU cache size used by C<sql_exec>. The
226 248
227=back 249=back
228 250
229=cut 251=cut
230 252
253reinitialize;
254
231package PApp::SQL::Database; 255package PApp::SQL::Database;
232 256
233=head2 THE DATABASE CLASS 257=head2 THE DATABASE CLASS
234 258
235Again (sigh) the problem of persistency. What do you do when you have to serialize on object 259Again (sigh) the problem of persistency. What do you do when you have to serialize on object
293 317
294=back 318=back
295 319
296=cut 320=cut
297 321
298reinitialize;
299
3001; 3221;
301 323
302=head1 BUGS 324=head1 BUGS
303 325
304As of this writing, sql_fetch and sql_fetchall are not very well tested 326As of this writing, sql_fetch and sql_fetchall are not very well tested

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines