… | |
… | |
3 | PApp::SQL - absolutely easy yet fast and powerful sql access |
3 | PApp::SQL - absolutely easy yet fast and powerful sql access |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use PApp::SQL; |
7 | use PApp::SQL; |
8 | # to be written |
8 | |
|
|
9 | my $st = sql_exec $DBH, "select ... where a = ?", $a; |
|
|
10 | |
|
|
11 | local $DBH = <database handle>; |
|
|
12 | my $st = sql_exec \my($bind_a, $bind_b), "select a,b ..."; |
|
|
13 | my $st = sql_insertid |
|
|
14 | sql_exec "insert into ... values (?, ?)", $v1, $v2; |
|
|
15 | my $a = sql_fetch "select a from ..."; |
|
|
16 | sql_fetch \my($a, $b), "select a,b ..."; |
|
|
17 | |
|
|
18 | sql_exists "name from table where name like 'a%'" |
|
|
19 | or die "a* required but not existent"; |
|
|
20 | |
|
|
21 | my $db = new PApp::SQL::Database "", "DBI:mysql:test", "user", "pass"; |
|
|
22 | local $PApp::SQL::DBH = $db->checked_dbh; # does 'ping' |
|
|
23 | |
|
|
24 | sql_exec $db->dbh, "select ..."; |
9 | |
25 | |
10 | =head1 DESCRIPTION |
26 | =head1 DESCRIPTION |
11 | |
27 | |
12 | This module provides you with easy-to-use functions to execute sql |
28 | This module provides you with easy-to-use functions to execute sql |
13 | commands (using DBI). Despite being easy to use, they are also quite |
29 | commands (using DBI). Despite being easy to use, they are also quite |
14 | efficient and allow you to write faster programs in less lines of code. |
30 | efficient and allow you to write faster programs in less lines of code. It |
|
|
31 | should work with anything from perl-5.004_01 onwards, but I only support |
|
|
32 | 5.005+. |
15 | |
33 | |
16 | =over 4 |
34 | If the descriptions here seem terse or if you always wanted to know |
|
|
35 | what PApp is then have a look at the PApp module which uses this module |
|
|
36 | extensively but also provides you with a lot more gimmicks to play around |
|
|
37 | with to help you create cool applications ;) |
17 | |
38 | |
18 | =cut |
39 | =cut |
19 | |
40 | |
20 | package PApp::SQL; |
41 | package PApp::SQL; |
21 | |
42 | |
22 | use DBI; |
43 | use DBI (); |
23 | |
|
|
24 | #use PApp::Exception; # not yet used |
|
|
25 | |
44 | |
26 | BEGIN { |
45 | BEGIN { |
27 | use base Exporter; |
46 | use base qw(Exporter DynaLoader); |
28 | |
47 | |
29 | $VERSION = 0.1; |
48 | $VERSION = 0.12; |
30 | @EXPORT = qw( |
49 | @EXPORT = qw( |
31 | sql_exec sql_fetch sql_fetchall sql_exists sql_insertid $sql_exec |
50 | sql_exec sql_fetch sql_fetchall sql_exists sql_insertid $sql_exec |
32 | ); |
51 | ); |
33 | @EXPORT_OK = qw( |
52 | @EXPORT_OK = qw( |
34 | connect_cached |
53 | connect_cached |
35 | ); |
54 | ); |
36 | |
55 | |
37 | require XSLoader; |
56 | bootstrap PApp::SQL $VERSION; |
38 | XSLoader::load PApp::SQL, $VERSION; |
|
|
39 | } |
57 | } |
40 | |
58 | |
41 | our $sql_exec; # last result of sql_exec's execute call |
59 | our $sql_exec; # last result of sql_exec's execute call |
42 | our $DBH; # the default database handle |
60 | our $DBH; # the default database handle |
43 | our $database; # the current SQL::Database object, if applicable |
61 | our $Database; # the current SQL::Database object, if applicable |
44 | |
62 | |
45 | our %dbcache; |
63 | our %dbcache; |
|
|
64 | |
|
|
65 | =head2 GLOBAL VARIABLES |
|
|
66 | |
|
|
67 | =over 4 |
|
|
68 | |
|
|
69 | =item $sql_exec |
|
|
70 | |
|
|
71 | Since the C<sql_exec> family of functions return a statement handle there |
|
|
72 | must eb another way to test the return value of the C<execute> call. This |
|
|
73 | global variable contains the result of the most recent call to C<execute> |
|
|
74 | done by this module. |
|
|
75 | |
|
|
76 | =item $PApp::SQL::DBH |
|
|
77 | |
|
|
78 | The default database handle used by this module if no C<$DBH> was |
|
|
79 | specified as argument and no C<$DBH> is found in the current package. See |
|
|
80 | C<sql_exec> for a discussion. |
|
|
81 | |
|
|
82 | =item $PApp::SQL::Database |
|
|
83 | |
|
|
84 | The current default C<PApp::SQL::Database>-object. Future versions might |
|
|
85 | automatically fall back on this database and create database handles from |
|
|
86 | it if neccessary. At the moment this is not used by this module but might |
|
|
87 | be nice as a placeholder for the database object that corresponds to |
|
|
88 | $PApp::SQL::DBH. |
|
|
89 | |
|
|
90 | =back |
|
|
91 | |
|
|
92 | =head2 FUNCTIONS |
|
|
93 | |
|
|
94 | =over 4 |
46 | |
95 | |
47 | =item $dbh = connect_cached $id, $dsn, $user, $pass, $flags, $connect |
96 | =item $dbh = connect_cached $id, $dsn, $user, $pass, $flags, $connect |
48 | |
97 | |
49 | (not exported by by default) |
98 | (not exported by by default) |
50 | |
99 | |
51 | Connect to the database given by C<($dsn,$user,$pass)>, while using the |
100 | 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 |
101 | flags from C<$flags>. These are just the same arguments as given to |
53 | C<DBI->connect>. |
102 | C<DBI->connect>. |
54 | |
103 | |
55 | The database handle will be cached under the unique id C<$id>. If the same |
104 | The database handle will be cached under the unique id |
56 | id is requested later, the cached handle will be checked (using ping), and |
105 | C<$id|$dsn|$user|$pass>. If the same id is requested later, the |
|
|
106 | 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 |
107 | 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 |
108 | module name to the id to make it "more" unique. Things like __PACKAGE__ . |
59 | __PACKAGE__ . __LINE__ work fine as well). |
109 | __LINE__ work fine as well). |
|
|
110 | |
|
|
111 | The reason C<$id> is necessary is that you might specify special connect |
|
|
112 | arguments or special flags, or you might want to configure your $DBH |
|
|
113 | differently than maybe other applications requesting the same database |
|
|
114 | connection. If none of this is becessary for your application you can |
|
|
115 | leave $id empty (i.e. ""). |
60 | |
116 | |
61 | If specified, C<$connect> is a callback (e.g. a coderef) that will be |
117 | 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 |
118 | called each time a new connection is being established, with the new |
63 | C<$dbh> as first argument. |
119 | C<$dbh> as first argument. |
64 | |
120 | |
… | |
… | |
73 | my ($id, $dsn, $user, $pass, $flags, $connect) = @_; |
129 | my ($id, $dsn, $user, $pass, $flags, $connect) = @_; |
74 | # the following line is duplicated in PApp::SQL::Database::new |
130 | # the following line is duplicated in PApp::SQL::Database::new |
75 | $id = "$id\0$dsn\0$user\0$pass"; |
131 | $id = "$id\0$dsn\0$user\0$pass"; |
76 | unless ($dbcache{$id} && $dbcache{$id}->ping) { |
132 | unless ($dbcache{$id} && $dbcache{$id}->ping) { |
77 | #warn "connecting to ($dsn|$user|$pass|$flags)\n";#d# |
133 | #warn "connecting to ($dsn|$user|$pass|$flags)\n";#d# |
78 | # first, nuke our cache (sooory ;) |
134 | # first, nuke our statement cache (sooory ;) |
79 | cachesize cachesize 0; |
135 | cachesize cachesize 0; |
80 | # then connect anew |
136 | # then connect anew |
81 | $dbcache{$id} = |
137 | $dbcache{$id} = |
82 | eval { DBI->connect($dsn, $user, $pass, $flags) } |
138 | eval { DBI->connect($dsn, $user, $pass, $flags) } |
83 | || eval { DBI->connect($dsn, $user, $pass, $flags) } |
139 | || eval { DBI->connect($dsn, $user, $pass, $flags) } |
84 | || die "$DBI::errstr\n"; |
140 | || die "unable to connect to database $dsn: $DBI::errstr\n"; |
85 | $connect->($dbcache{$id}) if $connect; |
141 | $connect->($dbcache{$id}) if $connect; |
86 | } |
142 | } |
87 | $dbcache{$id}; |
143 | $dbcache{$id}; |
88 | } |
144 | } |
89 | |
145 | |
… | |
… | |
188 | |
244 | |
189 | =cut |
245 | =cut |
190 | |
246 | |
191 | =item $lastid = sql_insertid $sth |
247 | =item $lastid = sql_insertid $sth |
192 | |
248 | |
193 | Returns the last automatically created key value (e.g. for mysql |
249 | Returns the last automatically created key value. It must be executed |
194 | AUTO_INCREMENT or sybase IDENTITY fields). It must be executed directly |
|
|
195 | after executing the insert statement that created it. |
250 | directly after executing the insert statement that created it. This is |
|
|
251 | what is actually returned for various databases. If your database is |
|
|
252 | missing, please send me an e-mail on how to implement this ;) |
|
|
253 | |
|
|
254 | mysql: first C<AUTO_INCREMENT> column set to NULL |
|
|
255 | postgres: C<oid> column (is there a way to get the last SERIAL?) |
|
|
256 | sybase: C<IDENTITY> column of the last insert (slow) |
|
|
257 | informix: C<SERIAL> or C<SERIAL8> column of the last insert |
|
|
258 | |
|
|
259 | Except for sybase, this does not require a server access. |
196 | |
260 | |
197 | =cut |
261 | =cut |
198 | |
262 | |
199 | sub sql_insertid($) { |
263 | sub sql_insertid($) { |
200 | my $sth = shift or die "sql_insertid requires a statement handle"; |
264 | my $sth = shift or die "sql_insertid requires a statement handle"; |
201 | my $dbh = $sth->{Database}; |
265 | my $dbh = $sth->{Database}; |
202 | my $driver = $dbh->{Driver}{Name}; |
266 | my $driver = $dbh->{Driver}{Name}; |
203 | |
267 | |
204 | $driver eq "mysql" and return $sth->{mysql_insertid}; |
268 | $driver eq "mysql" and return $sth->{mysql_insertid}; |
|
|
269 | $driver eq "Pg" and return $sth->{pg_oid_status}; |
205 | $driver eq "Sybase" and return sql_fetch($dbh, 'SELECT @@IDENTITY'); |
270 | $driver eq "Sybase" and return sql_fetch($dbh, 'SELECT @@IDENTITY'); |
206 | $driver eq "Informix" and return $sth->{ix_sqlerrd}[1]; |
271 | $driver eq "Informix" and return $sth->{ix_sqlerrd}[1]; |
207 | |
272 | |
208 | die "sql_insertid does not spport the dbd driver '$driver', please see PApp::SQL::sql_insertid"; |
273 | die "sql_insertid does not spport the dbd driver '$driver', please see PApp::SQL::sql_insertid"; |
209 | } |
274 | } |
210 | |
275 | |
… | |
… | |
225 | |
290 | |
226 | =cut |
291 | =cut |
227 | |
292 | |
228 | =item reinitialize [not exported] |
293 | =item reinitialize [not exported] |
229 | |
294 | |
230 | Clears any internal caches (statement cache, database handle cache). |
295 | Clears any internal caches (statement cache, database handle |
|
|
296 | cache). Should be called after C<fork> and other accidents that invalidate |
|
|
297 | database handles. |
231 | |
298 | |
232 | =cut |
299 | =cut |
233 | |
300 | |
234 | sub reinitialize { |
301 | sub reinitialize { |
235 | cachesize cachesize 0; |
302 | cachesize cachesize 0; |
… | |
… | |
241 | |
308 | |
242 | =back |
309 | =back |
243 | |
310 | |
244 | =cut |
311 | =cut |
245 | |
312 | |
|
|
313 | reinitialize; |
|
|
314 | |
246 | package PApp::SQL::Database; |
315 | package PApp::SQL::Database; |
247 | |
316 | |
248 | =head2 THE DATABASE CLASS |
317 | =head2 THE DATABASE CLASS |
249 | |
318 | |
250 | Again (sigh) the problem of persistency. What do you do when you have to serialize on object |
319 | Again (sigh) the problem of persistency. What do you do when you have to serialize on object |
… | |
… | |
301 | |
370 | |
302 | =cut |
371 | =cut |
303 | |
372 | |
304 | sub dsn($) { |
373 | sub dsn($) { |
305 | my $self = shift; |
374 | my $self = shift; |
306 | $self->[1][1]; |
375 | (split /\x00/, $self->[0])[1]; |
307 | } |
376 | } |
308 | |
377 | |
309 | =back |
378 | =back |
310 | |
379 | |
311 | =cut |
380 | =cut |
312 | |
381 | |
313 | reinitialize; |
|
|
314 | |
|
|
315 | 1; |
382 | 1; |
316 | |
|
|
317 | =head1 BUGS |
|
|
318 | |
|
|
319 | As of this writing, sql_fetch and sql_fetchall are not very well tested |
|
|
320 | (they were just re-written in C). |
|
|
321 | |
|
|
322 | sql_exists could be faster (it is written very ugly to not change the |
|
|
323 | current package). |
|
|
324 | |
383 | |
325 | =head1 SEE ALSO |
384 | =head1 SEE ALSO |
326 | |
385 | |
327 | L<PApp>. |
386 | L<PApp>. |
328 | |
387 | |