]> mj.ucw.cz Git - pciids.git/blob - PciIds/DBQAny.pm
Output what the sub-item really is
[pciids.git] / PciIds / DBQAny.pm
1 package PciIds::DBQAny;
2 use strict;
3 use warnings;
4 use DBI;
5
6 sub new( $$ ) {
7         my( $dbh, $queries ) = @_;
8         my %qs;
9         foreach( keys %{$queries} ) {
10                 $qs{$_} = $dbh->prepare( $queries->{$_} );
11         }
12         return bless {
13                 "dbh" => $dbh,
14                 "queries" => \%qs
15         };
16 }
17
18 sub queryAll( $$$$ ) {
19         my( $self, $name, $params, $fetch ) = @_;
20         my $q = $self->{'queries'}->{$name};
21         $q->execute( @{$params} );#Will die automatically
22         if( $fetch ) {
23                 my @result = @{$q->fetchall_arrayref()};#Copy the array, finish() deletes the content
24                 $q->finish();
25                 return \@result;
26         }
27 }
28
29 sub query( $$$ ) {
30         my( $self, $name, $params ) = @_;
31         return queryAll( $self, $name, $params, 1 );
32 }
33
34 sub command( $$$ ) {
35         my( $self, $name, $params ) = @_;
36         queryAll( $self, $name, $params, 0 );
37 }
38
39 sub commit( $ ) {
40         shift->{'dbh'}->commit();
41 }
42
43 sub rollback( $ ) {
44         shift->{'dbh'}->rollback();
45 }
46
47 sub last( $ ) {
48         return shift->{'dbh'}->last_insert_id( undef, undef, undef, undef );
49 }
50
51 sub dbh( $ ) { return shift->{'dbh'}; }
52
53 1;