… | |
… | |
24 | #use PApp::Exception; # not yet used |
24 | #use PApp::Exception; # not yet used |
25 | |
25 | |
26 | BEGIN { |
26 | BEGIN { |
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 | |
51 | Connect to the database given by C<($dsn,$user,$pass)>, while using the |
51 | Connect to the database given by C<($dsn,$user,$pass)>, while using the |
52 | flags from C<$flags>. These are just the same arguments as given to |
52 | flags from C<$flags>. These are just the same arguments as given to |
53 | C<DBI->connect>. |
53 | C<DBI->connect>. |
54 | |
54 | |
55 | The database handle will be cached under the unique id C<$id>. If the same |
55 | The database handle will be cached under the unique id |
56 | id is requested later, the cached handle will be checked (using ping), and |
56 | C<$id|$dsn|$user|$pass>. If the same id is requested later, the |
|
|
57 | cached handle will be checked (using ping), and the connection will |
57 | the connection will be re-established if necessary (be sure to prefix your |
58 | be re-established if necessary (be sure to prefix your application or |
58 | application or module name to the id to make it "more" unique. Things like |
59 | module 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 | |
|
|
62 | The reason C<$id> is necessary is that you might specify special connect |
|
|
63 | arguments or special flags, or you might want to configure your $DBH |
|
|
64 | differently than maybe other applications requesting the same database |
|
|
65 | connection. If none of this is becessary for your application you can |
|
|
66 | leave $id empty (i.e. ""). |
60 | |
67 | |
61 | If specified, C<$connect> is a callback (e.g. a coderef) that will be |
68 | If specified, C<$connect> is a callback (e.g. a coderef) that will be |
62 | called each time a new connection is being established, with the new |
69 | called each time a new connection is being established, with the new |
63 | C<$dbh> as first argument. |
70 | C<$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 | |
|
|
200 | Returns the last automatically created key value. It must be executed |
|
|
201 | directly after executing the insert statement that created it. This is |
|
|
202 | what is actually returned for various databases. If your database is |
|
|
203 | missing, please send me an e-mail on how to implement this ;) |
|
|
204 | |
|
|
205 | mysql: first C<AUTO_INCREMENT> column set to NULL |
|
|
206 | postgres: C<oid> column (is there a way to get the last SERIAL?) |
|
|
207 | sybase: C<IDENTITY> column of the last insert (slow) |
|
|
208 | informix: C<SERIAL> or C<SERIAL8> column of the last insert |
|
|
209 | |
|
|
210 | Except for sybase, this does not require a server access. |
|
|
211 | |
|
|
212 | =cut |
|
|
213 | |
192 | sub sql_insertid { |
214 | sub sql_insertid($) { |
193 | $DBH->{mysql_insertid}; |
215 | my $sth = shift or die "sql_insertid requires a statement handle"; |
|
|
216 | my $dbh = $sth->{Database}; |
|
|
217 | my $driver = $dbh->{Driver}{Name}; |
|
|
218 | |
|
|
219 | $driver eq "mysql" and return $sth->{mysql_insertid}; |
|
|
220 | $driver eq "Pg" and return $sth->{pg_oid_status}; |
|
|
221 | $driver eq "Sybase" and return sql_fetch($dbh, 'SELECT @@IDENTITY'); |
|
|
222 | $driver eq "Informix" and return $sth->{ix_sqlerrd}[1]; |
|
|
223 | |
|
|
224 | die "sql_insertid does not spport the dbd driver '$driver', please see PApp::SQL::sql_insertid"; |
194 | } |
225 | } |
195 | |
226 | |
196 | =item [old-size] = cachesize [new-size] |
227 | =item [old-size] = cachesize [new-size] |
197 | |
228 | |
198 | Returns (and possibly changes) the LRU cache size used by C<sql_exec>. The |
229 | Returns (and possibly changes) the LRU cache size used by C<sql_exec>. The |
… | |
… | |
210 | |
241 | |
211 | =cut |
242 | =cut |
212 | |
243 | |
213 | =item reinitialize [not exported] |
244 | =item reinitialize [not exported] |
214 | |
245 | |
215 | Clears any internal caches (statement cache, database handle cache). |
246 | Clears any internal caches (statement cache, database handle |
|
|
247 | cache). Should be called after C<fork> and other accidents that invalidate |
|
|
248 | database handles. |
216 | |
249 | |
217 | =cut |
250 | =cut |
218 | |
251 | |
219 | sub reinitialize { |
252 | sub reinitialize { |
220 | cachesize cachesize 0; |
253 | cachesize cachesize 0; |
… | |
… | |
226 | |
259 | |
227 | =back |
260 | =back |
228 | |
261 | |
229 | =cut |
262 | =cut |
230 | |
263 | |
|
|
264 | reinitialize; |
|
|
265 | |
231 | package PApp::SQL::Database; |
266 | package PApp::SQL::Database; |
232 | |
267 | |
233 | =head2 THE DATABASE CLASS |
268 | =head2 THE DATABASE CLASS |
234 | |
269 | |
235 | Again (sigh) the problem of persistency. What do you do when you have to serialize on object |
270 | Again (sigh) the problem of persistency. What do you do when you have to serialize on object |
… | |
… | |
286 | |
321 | |
287 | =cut |
322 | =cut |
288 | |
323 | |
289 | sub dsn($) { |
324 | sub dsn($) { |
290 | my $self = shift; |
325 | my $self = shift; |
291 | $self->[1][1]; |
326 | (split /\x00/, $self->[0])[1]; |
292 | } |
327 | } |
293 | |
328 | |
294 | =back |
329 | =back |
295 | |
330 | |
296 | =cut |
331 | =cut |
297 | |
|
|
298 | reinitialize; |
|
|
299 | |
332 | |
300 | 1; |
333 | 1; |
301 | |
334 | |
302 | =head1 BUGS |
335 | =head1 BUGS |
303 | |
336 | |