… | |
… | |
44 | use DBI (); |
44 | use DBI (); |
45 | |
45 | |
46 | BEGIN { |
46 | BEGIN { |
47 | use base qw(Exporter DynaLoader); |
47 | use base qw(Exporter DynaLoader); |
48 | |
48 | |
49 | $VERSION = 0.143; |
49 | $VERSION = '1.01'; |
50 | @EXPORT = qw( |
50 | @EXPORT = qw( |
51 | sql_exec sql_fetch sql_fetchall sql_exists sql_insertid $sql_exec |
51 | sql_exec sql_fetch sql_fetchall sql_exists sql_insertid $sql_exec |
52 | sql_uexec sql_ufetch sql_ufetchall sql_uexists |
52 | sql_uexec sql_ufetch sql_ufetchall sql_uexists |
53 | ); |
53 | ); |
54 | @EXPORT_OK = qw( |
54 | @EXPORT_OK = qw( |
… | |
… | |
62 | our $DBH; # the default database handle |
62 | our $DBH; # the default database handle |
63 | our $Database; # the current SQL::Database object, if applicable |
63 | our $Database; # the current SQL::Database object, if applicable |
64 | |
64 | |
65 | our %dbcache; |
65 | our %dbcache; |
66 | |
66 | |
67 | =head2 GLOBAL VARIABLES |
67 | =head2 Global Variables |
68 | |
68 | |
69 | =over 4 |
69 | =over 4 |
70 | |
70 | |
71 | =item $sql_exec |
71 | =item $sql_exec |
72 | |
72 | |
… | |
… | |
88 | be nice as a placeholder for the database object that corresponds to |
88 | be nice as a placeholder for the database object that corresponds to |
89 | $PApp::SQL::DBH. |
89 | $PApp::SQL::DBH. |
90 | |
90 | |
91 | =back |
91 | =back |
92 | |
92 | |
93 | =head2 FUNCTIONS |
93 | =head2 Functions |
94 | |
94 | |
95 | =over 4 |
95 | =over 4 |
96 | |
96 | |
97 | =item $dbh = connect_cached $id, $dsn, $user, $pass, $flags, $connect |
97 | =item $dbh = connect_cached $id, $dsn, $user, $pass, $flags, $connect |
98 | |
98 | |
… | |
… | |
186 | package-global (and exported) variable C<$sql_exec>. |
186 | package-global (and exported) variable C<$sql_exec>. |
187 | |
187 | |
188 | If any error occurs C<sql_exec> will throw an exception. |
188 | If any error occurs C<sql_exec> will throw an exception. |
189 | |
189 | |
190 | C<sql_uexec> is similar to C<sql_exec> but upgrades all input arguments to |
190 | C<sql_uexec> is similar to C<sql_exec> but upgrades all input arguments to |
191 | utf8 before calling the C<execute> method. |
191 | UTF-8 before calling the C<execute> method. |
192 | |
192 | |
193 | Examples: |
193 | Examples: |
194 | |
194 | |
195 | # easy one |
195 | # easy one |
196 | my $st = sql_exec "select name, id from table where id = ?", $id; |
196 | my $st = sql_exec "select name, id from table where id = ?", $id; |
… | |
… | |
230 | my($name, $amount) = sql_fetch "select ...", args... |
230 | my($name, $amount) = sql_fetch "select ...", args... |
231 | |
231 | |
232 | ... and it's still quite fast unless you fetch large amounts of data. |
232 | ... and it's still quite fast unless you fetch large amounts of data. |
233 | |
233 | |
234 | C<sql_ufetch> is similar to C<sql_fetch> but upgrades all input values to |
234 | C<sql_ufetch> is similar to C<sql_fetch> but upgrades all input values to |
235 | utf8 and forces all result values to utf8 (this does I<not> include result |
235 | UTF-8 and forces all result values to UTF-8 (this does I<not> include result |
236 | parameters, only return values. Using bind variables in cinjunction with |
236 | parameters, only return values. Using bind variables in conjunction with |
237 | sql_u* functions results in undefined behaviour - we use utf8_on on |
237 | sql_u* functions might result in undefined behaviour - we use UTF-8 on |
238 | bind-variables and it seems to work on DBD::mysql which just means |
238 | bind-variables at execution time and it seems to work on DBD::mysql as it |
239 | that that DBD-driver is broken). |
239 | ignores the UTF-8 bit completely. Which just means that that DBD-driver is |
|
|
240 | broken). |
240 | |
241 | |
241 | =item sql_fetchall <see sql_exec> |
242 | =item sql_fetchall <see sql_exec> |
242 | |
243 | |
243 | =item sql_ufetchall <see sql_uexec> |
244 | =item sql_ufetchall <see sql_uexec> |
244 | |
245 | |
… | |
… | |
261 | for (sql_fetchall "select name, age, place from user") { |
262 | for (sql_fetchall "select name, age, place from user") { |
262 | my ($name, $age, $place) = @$_; |
263 | my ($name, $age, $place) = @$_; |
263 | } |
264 | } |
264 | |
265 | |
265 | C<sql_ufetchall> is similar to C<sql_fetchall> but upgrades all input |
266 | C<sql_ufetchall> is similar to C<sql_fetchall> but upgrades all input |
266 | values to utf8 and forces all result values to utf8 (see the caveats in |
267 | values to UTF-8 and forces all result values to UTF-8 (see the caveats in |
267 | the description of C<sql_ufetch>, though). |
268 | the description of C<sql_ufetch>, though). |
268 | |
269 | |
269 | =item sql_exists "<table_references> where <where_condition>...", args... |
270 | =item sql_exists "<table_references> where <where_condition>...", args... |
270 | |
271 | |
271 | =item sql_uexists <see sql_exists> |
272 | =item sql_uexists <see sql_exists> |
… | |
… | |
275 | "select * from" were prepended to your statement (it isn't)). Should work |
276 | "select * from" were prepended to your statement (it isn't)). Should work |
276 | with every database but can be quite slow, except on mysql, where this |
277 | with every database but can be quite slow, except on mysql, where this |
277 | should be quite fast. |
278 | should be quite fast. |
278 | |
279 | |
279 | C<sql_uexists> is similar to C<sql_exists> but upgrades all parameters to |
280 | C<sql_uexists> is similar to C<sql_exists> but upgrades all parameters to |
280 | utf8. |
281 | UTF-8. |
281 | |
282 | |
282 | Examples: |
283 | Examples: |
283 | |
284 | |
284 | print "user 7 exists!\n" |
285 | print "user 7 exists!\n" |
285 | if sql_exists "user where id = ?", 7; |
286 | if sql_exists "user where id = ?", 7; |
… | |
… | |
298 | |
299 | |
299 | mysql: first C<AUTO_INCREMENT> column set to NULL |
300 | mysql: first C<AUTO_INCREMENT> column set to NULL |
300 | postgres: C<oid> column (is there a way to get the last SERIAL?) |
301 | postgres: C<oid> column (is there a way to get the last SERIAL?) |
301 | sybase: C<IDENTITY> column of the last insert (slow) |
302 | sybase: C<IDENTITY> column of the last insert (slow) |
302 | informix: C<SERIAL> or C<SERIAL8> column of the last insert |
303 | informix: C<SERIAL> or C<SERIAL8> column of the last insert |
|
|
304 | sqlite: C<last_insert_rowid()> |
303 | |
305 | |
304 | Except for sybase, this does not require a server access. |
306 | Except for sybase, this does not require a server access. |
305 | |
307 | |
306 | =cut |
308 | =cut |
307 | |
309 | |
… | |
… | |
312 | |
314 | |
313 | $driver eq "mysql" and return $sth->{mysql_insertid}; |
315 | $driver eq "mysql" and return $sth->{mysql_insertid}; |
314 | $driver eq "Pg" and return $sth->{pg_oid_status}; |
316 | $driver eq "Pg" and return $sth->{pg_oid_status}; |
315 | $driver eq "Sybase" and return sql_fetch($dbh, 'SELECT @@IDENTITY'); |
317 | $driver eq "Sybase" and return sql_fetch($dbh, 'SELECT @@IDENTITY'); |
316 | $driver eq "Informix" and return $sth->{ix_sqlerrd}[1]; |
318 | $driver eq "Informix" and return $sth->{ix_sqlerrd}[1]; |
|
|
319 | $driver eq "SQLite" and return sql_fetch($dbh, 'SELECT last_insert_rowid ()'); |
317 | |
320 | |
318 | die "sql_insertid does not spport the dbd driver '$driver', please see PApp::SQL::sql_insertid"; |
321 | die "sql_insertid does not spport the dbd driver '$driver', please see PApp::SQL::sql_insertid"; |
319 | } |
322 | } |
320 | |
323 | |
321 | =item [old-size] = cachesize [new-size] |
324 | =item [old-size] = cachesize [new-size] |
… | |
… | |
357 | |
360 | |
358 | reinitialize; |
361 | reinitialize; |
359 | |
362 | |
360 | package PApp::SQL::Database; |
363 | package PApp::SQL::Database; |
361 | |
364 | |
362 | =head2 THE DATABASE CLASS |
365 | =head2 The Database Class |
363 | |
366 | |
364 | Again (sigh) the problem of persistency. What do you do when you have |
367 | Again (sigh) the problem of persistency. What do you do when you have |
365 | to serialize on object that contains (or should contain) a database |
368 | to serialize on object that contains (or should contain) a database |
366 | handle? Short answer: you don't. Long answer: you can embed the necessary |
369 | handle? Short answer: you don't. Long answer: you can embed the necessary |
367 | information to recreate the dbh when needed. |
370 | information to recreate the dbh when needed. |
… | |
… | |
405 | |
408 | |
406 | sub checked_dbh($) { |
409 | sub checked_dbh($) { |
407 | my $dbh = $dbcache{$_[0][0]}; |
410 | my $dbh = $dbcache{$_[0][0]}; |
408 | $dbh && $dbh->ping |
411 | $dbh && $dbh->ping |
409 | ? $dbh |
412 | ? $dbh |
410 | : PApp::SQL::connect_cached((split /\x00/, $_[0][0]), $_[0][1], $_[0][2]); |
413 | : PApp::SQL::connect_cached((split /\x00/, $_[0][0], 4), $_[0][1], $_[0][2]); |
411 | } |
414 | } |
412 | |
415 | |
413 | =item $db->dsn |
416 | =item $db->dsn |
414 | |
417 | |
415 | Return the DSN (L<DBI>) fo the database object (e.g. for error messages). |
418 | Return the DSN (L<DBI>) fo the database object (e.g. for error messages). |
… | |
… | |
449 | |
452 | |
450 | L<PApp>. |
453 | L<PApp>. |
451 | |
454 | |
452 | =head1 AUTHOR |
455 | =head1 AUTHOR |
453 | |
456 | |
454 | Marc Lehmann <pcg@goof.com> |
457 | Marc Lehmann <schmorp@schmorp.de> |
455 | http://www.goof.com/pcg/marc/ |
458 | http://home.schmorp.de/ |
456 | |
459 | |
457 | =cut |
460 | =cut |
458 | |
461 | |