Importer/DBI.pm untested

filter: Statementstmt 0.0% 0 / 40 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 70 CC 6 SCAR 17.2
CC
6
Cov
0%
next
12.0
fetch
6.0
rows
6.0
CRAP 5.6
1 package Bot::Infobot::Importer::DBI;
2 use DBI;
3 use Carp qw(croak);
4 use strict;
5  
6 sub handle {
7 return $_[0] =~ m!^dbi:!;
8 }
9  
10 sub new {
11 my $class = shift;
12  
13 my $dbi = DBI->connect( @_ ) || die "DB connection not made - $DBI::errstr\n";
14  
15 my $self = { dbh => $dbi, table_prefix => 't' };
16  
17 return bless $self, $class;
18 }
19  
20 sub fetch {
21 my ($self, $table, @keys) = @_;
22 my $table_prefix = $self->{table_prefix};
23 my $sql = "SELECT * FROM ${table_prefix}_${table}";
24 if (@keys) {
25 $sql .= " where key = ?";
26 }
27 $self->{sth} = $self->do_db($sql, @keys);
28  
29 return $self->{sth};
30 }
31  
32 sub rows {
33 my $self = shift;
34 croak "No statement set up" unless $self->{sth};
35 return $self->{sth}->rows;
36  
37 }
38  
39  
40  
41 sub next {
42 my $self = shift;
43 return unless $self->{sth};
44 my $return = $self->{sth}->fetchrow_hashref;
45 return unless $return->{pkey};
46 # munge pkey and pvalue to right names
47 for (qw(key value)) {
48 $return->{$_} = $return->{"p$_"};
49 delete $return->{"p$_"};
50 }
51 return $return;
52 }
53  
54 sub finish {
55 my ($self) = @_;
56 $self->{sth}->finish;
57 delete $self->{sth};
58 }
59  
60  
61 sub do_db {
62 my ($self, $sql, @values) = @_;
63 my $sth = $self->{dbh}->prepare($sql);
64 my $rv = $sth->execute(@values);
65 die "Error executing '$sql' : ".$self->{dbh}->errstr."\n" unless $rv;
66  
67 return $sth;
68 }
69  
70  
71 1;