… | |
… | |
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.11; |
48 | $VERSION = 0.122; |
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 | |
… | |
… | |
179 | |
228 | |
180 | =item sql_exists "<table> where ...", args... |
229 | =item sql_exists "<table> where ...", args... |
181 | |
230 | |
182 | Check wether the result of the sql-statement "select xxx from |
231 | Check wether the result of the sql-statement "select xxx from |
183 | $first_argument" would be empty or not (that is, imagine the string |
232 | $first_argument" would be empty or not (that is, imagine the string |
184 | "select from" were prepended to your statement (it isn't)). Should work |
233 | "select * from" were prepended to your statement (it isn't)). Should work |
185 | with every database but can be quite slow, except on mysql, where this |
234 | with every database but can be quite slow, except on mysql, where this |
186 | should be quite fast. |
235 | should be quite fast. |
187 | |
236 | |
188 | Examples: |
237 | Examples: |
189 | |
238 | |
… | |
… | |
250 | =cut |
299 | =cut |
251 | |
300 | |
252 | sub reinitialize { |
301 | sub reinitialize { |
253 | cachesize cachesize 0; |
302 | cachesize cachesize 0; |
254 | for (values %dbcache) { |
303 | for (values %dbcache) { |
255 | eval { $_->disconnect }; |
304 | eval { $_->{InactiveDestroy} = 1 }; |
256 | } |
305 | } |
257 | undef %dbcache; |
306 | undef %dbcache; |
258 | } |
307 | } |
259 | |
308 | |
260 | =back |
309 | =back |
… | |
… | |
317 | |
366 | |
318 | =item $db->dsn |
367 | =item $db->dsn |
319 | |
368 | |
320 | Return the DSN (L<DBI>) fo the database object (e.g. for error messages). |
369 | Return the DSN (L<DBI>) fo the database object (e.g. for error messages). |
321 | |
370 | |
|
|
371 | =item $db->login |
|
|
372 | |
|
|
373 | Return the login name. |
|
|
374 | |
|
|
375 | =item $db->password |
|
|
376 | |
|
|
377 | Return the password (emphasizing the fact that the apssword is stored plaintext ;) |
|
|
378 | |
322 | =cut |
379 | =cut |
323 | |
380 | |
324 | sub dsn($) { |
381 | sub dsn($) { |
325 | my $self = shift; |
382 | my $self = shift; |
326 | (split /\x00/, $self->[0])[1]; |
383 | (split /\x00/, $self->[0])[1]; |
327 | } |
384 | } |
328 | |
385 | |
|
|
386 | sub login($) { |
|
|
387 | my $self = shift; |
|
|
388 | (split /\x00/, $self->[0])[2]; |
|
|
389 | } |
|
|
390 | |
|
|
391 | sub password($) { |
|
|
392 | my $self = shift; |
|
|
393 | (split /\x00/, $self->[0])[3]; |
|
|
394 | } |
|
|
395 | |
329 | =back |
396 | =back |
330 | |
397 | |
331 | =cut |
398 | =cut |
332 | |
399 | |
333 | 1; |
400 | 1; |
334 | |
|
|
335 | =head1 BUGS |
|
|
336 | |
|
|
337 | As of this writing, sql_fetch and sql_fetchall are not very well tested |
|
|
338 | (they were just re-written in C). |
|
|
339 | |
|
|
340 | sql_exists could be faster (it is written very ugly to not change the |
|
|
341 | current package). |
|
|
342 | |
401 | |
343 | =head1 SEE ALSO |
402 | =head1 SEE ALSO |
344 | |
403 | |
345 | L<PApp>. |
404 | L<PApp>. |
346 | |
405 | |