blib/lib/QWizard/Storage/SQL.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 75 CC 5 SCAR 17.6
CC
5
Cov
0%
new
12.0
create_table
6.0
set
6.0
CRAP 5.8
1 package QWizard::Storage::SQL;
2  
3 use strict;
4 use QWizard::Storage::Base;
5  
6 our @ISA = qw(QWizard::Storage::Base);
7  
8 our $VERSION = '3.15';
9  
10 our %default_opts =
11 (
12 table => 'QWStorage',
13 namecol => 'name',
14 valcol => 'value',
15 );
16  
17 sub new {
18 my $class = shift;
19 my $self;
20 %$self = @_;
21 map { $self->{$_} = $default_opts{$_} if (!exists($self->{$_})) } (keys(%default_opts));
22 return undef if (!$self->{'dbh'});
23 my $dbh = $self->{'dbh'};
24 $self->{'geth'} = $dbh->prepare("select $self->{valcol} from $self->{table} where $self->{namecol} = ?");
25 $self->{'allh'} = $dbh->prepare("select $self->{namecol}, $self->{valcol} from $self->{table}");
26 $self->{'cnth'} = $dbh->prepare("select count($self->{valcol}) from $self->{table} where $self->{namecol} = ?");
27 $self->{'insh'} = $dbh->prepare("insert into $self->{table} ($self->{namecol}, $self->{valcol}) values(?, ?)");
28 $self->{'updh'} = $dbh->prepare("update $self->{table} set $self->{valcol} = ? where $self->{namecol} = ?");
29 $self->{'delh'} = $dbh->prepare("delete from $self->{table}");
30 bless $self, $class;
31 }
32  
33 sub create_table {
34 my $self = shift;
35  
36 my $xret; # Return code from execute().
37  
38 # Table existence check command.
39 my $chk = $self->{'dbh'}->prepare("select * from $self->{table}");
40  
41 # Table creation command.
42 my $ct = $self->{'dbh'}->prepare("create table $self->{table} (id int, $self->{namecol} varchar(255), $self->{valcol} varchar(65536))");
43  
44 #
45 # Return without doing anything if the specified table exists.
46 #
47 $xret = $chk->execute();
48 return if($xret == 1);
49  
50 #
51 # Create the table.
52 #
53 $ct->execute();
54 }
55  
56 sub get_all {
57 my $self = shift;
58 my $ret;
59  
60 $self->{'allh'}->execute();
61 while (my $vals = $self->{'allh'}->fetchrow_arrayref()) {
62 $ret->{$vals->[0]} = $vals->[1];
63 }
64 $self->{'allh'}->finish;
65  
66 return $ret;
67 }
68  
69 sub set {
70 my ($self, $it, $value) = @_;
71  
72 # check to see if it exists
73 $self->{'cnth'}->execute($it);
74 my $vals = $self->{'cnth'}->fetchrow_arrayref();
75 $self->{'cnth'}->finish;
76 if ($vals->[0] == 0) {
77 $self->{'insh'}->execute($it, $value);
78 } else {
79 $self->{'updh'}->execute($value, $it);
80 }
81  
82 return $value;
83 }
84  
85 sub get {
86 my ($self, $it) = @_;
87 $self->{'geth'}->execute($it);
88 my $vals = $self->{'geth'}->fetchrow_arrayref();
89 $self->{'geth'}->finish;
90 return $vals->[0];
91 }
92  
93 sub reset {
94 my $self = shift;
95 $self->{'delh'}->execute();
96 }
97  
98 1;
99  
100 =pod
101  
102 =head1 NAME
103  
104 QWizard::Storage::SQL - Stores data in a SQL database
105  
106 =head1 SYNOPSIS
107  
108 my $st = new QWizard::Storage::SQL(dbh => $DBI_reference);
109 $st->create_table();
110 $st->set('var', 'value');
111 $st->get('var');
112  
113 =head1 DESCRIPTION
114  
115 Stores data passed to and from a database table through a passed in
116 database handle reference.
117  
118 =head1 AUTHOR
119  
120 Wes Hardaker, hardaker@users.sourceforge.net
121  
122 =head1 SEE ALSO
123  
124 perl(1)
125  
126 Net-Policy: http://net-policy.sourceforge.net/
127  
128 =cut