| 1 | package DBIx::SimpleDBI; | ||
| 2 | |||
| 3 | # Copyright (C) 2003 Matt Knopp <mhat@cpan.org> | ||
| 4 | # This library is free software released under the GNU Lesser General Public | ||
| 5 | # License, Version 2.1. Please read the important licensing and disclaimer | ||
| 6 | # information included in the LICENSE file included with this distribution. | ||
| 7 | |||
| 8 | use DBI; | ||
| 9 | use PNDI; | ||
| 10 | use Error qw(:try); | ||
| 11 | use Error::Better ; | ||
| 12 | use Exporter; | ||
| 13 | |||
| 14 | @DBIx::SimpleDBI::ISA = qw (Exporter); | ||
| 15 | @DBIx::SimpleDBI::EXPORT = qw (query getLastInsertID); | ||
| 16 | |||
| 17 | sub _getDbHandle { | ||
| 18 | |||
| 19 | my $dsn = "dbi:"; | ||
| 20 | my $user = ""; | ||
| 21 | my $pass = ""; | ||
| 22 | |||
| 23 | try { | ||
| 24 | $dsn .= PNDI->lookup(name => 'Database.Default.TYPE') . ':' ; | ||
| 25 | $dsn .= PNDI->lookup(name => 'Database.Default.NAME') . ':' ; | ||
| 26 | $dsn .= PNDI->lookup(name => 'Database.Default.HOST') . ';' ; | ||
| 27 | $dsn .= PNDI->lookup(name => 'Database.Default.PORT') ; | ||
| 28 | |||
| 29 | $user = PNDI->lookup(name => 'Database.Default.USER'); | ||
| 30 | $pass = PNDI->lookup(name => 'Database.Default.PASS'); | ||
| 31 | } | ||
| 32 | otherwise { | ||
| 33 | throw Error::Better::OperationFailed("Cannot build DSN!"); | ||
| 34 | }; | ||
| 35 | |||
| 36 | #try { | ||
| 37 | $dbh = DBI->connect_cached ( | ||
| 38 | $dsn, $user, $pass, | ||
| 39 | { | ||
| 40 | #RaiseError => 1, | ||
| 41 | PrintError => 1, | ||
| 42 | AutoCommit => 0 }); | ||
| 43 | |||
| 44 | return($dbh); | ||
| 45 | #}; | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | sub query { | ||
| 51 | my $QueryStr = shift(); | ||
| 52 | my $Bindings = shift(); | ||
| 53 | $SimpleDBI::LastQuery = $QueryStr; | ||
| 54 | |||
| 55 | my $dbh = _getDbHandle(); | ||
| 56 | my $sth = $dbh->prepare($QueryStr); | ||
| 57 | my $rv = $sth->execute(@{$Bindings}); | ||
| 58 | |||
| 59 | if ($rv != 0) { | ||
| 60 | if ($QueryStr =~ m/^SELECT/i) { | ||
| 61 | return($sth->fetchall_arrayref()); | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | return(1); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | return(0); | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | sub getLastInsertID { | ||
| 73 | my $aref = query("SELECT LAST_INSERT_ID()"); | ||
| 74 | |||
| 75 | if(defined($aref->[0]->[0])) { | ||
| 76 | return($aref->[0]->[0]); | ||
| 77 | } | ||
| 78 | return(0); | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | ## | ||
| 84 | 1; |