]> mj.ucw.cz Git - pciids.git/blob - PciIds/DBQAny.pm
Revision of the help texts.
[pciids.git] / PciIds / DBQAny.pm
1 #       PciIds web database
2 #       Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
3 #
4 #       This program is free software; you can redistribute it and/or modify
5 #       it under the terms of the GNU General Public License as published by
6 #       he Free Software Foundation; either version 2 of the License, or
7 #       (at your option) any later version.
8 #
9 #       This program is distributed in the hope that it will be useful,
10 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #
13 #       GNU General Public License for more details.
14 #
15 #       You should have received a copy of the GNU General Public License
16 #       along with this program; if not, write to the Free Software
17 #       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19 package PciIds::DBQAny;
20 use strict;
21 use warnings;
22 use DBI;
23
24 sub new( $$ ) {
25         my( $dbh, $queries ) = @_;
26         my %qs;
27         foreach( keys %{$queries} ) {
28                 $qs{$_} = $dbh->prepare( $queries->{$_} );
29         }
30         return bless {
31                 "dbh" => $dbh,
32                 "queries" => \%qs
33         };
34 }
35
36 sub queryAll( $$$$ ) {
37         my( $self, $name, $params, $fetch ) = @_;
38         my $q = $self->{'queries'}->{$name};
39         $q->execute( @{$params} );#Will die automatically
40         if( $fetch ) {
41                 my @result = @{$q->fetchall_arrayref()};#Copy the array, finish() deletes the content
42                 $q->finish();
43                 return \@result;
44         }
45 }
46
47 sub query( $$$ ) {
48         my( $self, $name, $params ) = @_;
49         return queryAll( $self, $name, $params, 1 );
50 }
51
52 sub command( $$$ ) {
53         my( $self, $name, $params ) = @_;
54         queryAll( $self, $name, $params, 0 );
55 }
56
57 sub commit( $ ) {
58         shift->{'dbh'}->commit();
59 }
60
61 sub rollback( $ ) {
62         shift->{'dbh'}->rollback();
63 }
64
65 sub last( $ ) {
66         return shift->{'dbh'}->last_insert_id( undef, undef, undef, undef );
67 }
68
69 sub dbh( $ ) { return shift->{'dbh'}; }
70
71 1;