Shark/DBI.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 49 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 260 CC 27 SCAR 30.8
CC
27
Cov
0%
db_sql
56.0
db_exec
30.0
db_exec_fetchall_args
30.0
CRAP 21.8
1 #
2 # Data::Shark::DBI.pm
3 #
4 # Copyright (C) 2007 William Walz. All Rights Reserved
5 # This program is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
7 #
8 #
9  
10 package Data::Shark::DBI;
11  
12 use version; our $VERSION = qv('2.1');
13  
14 use strict;
15 use base qw( Exporter );
16  
17 use DBI;
18  
19 our @EXPORT = qw( );
20 our @EXPORT_OK = qw( );
21 our %EXPORT_TAGS = ( );
22  
23 # private
24 my %desc_hash = ();
25  
26 # constructor for DBI
27 sub new {
28  
29 my ( $class, $dsn, $user, $password, $options ) = @_;
30  
31 my $dbh;
32 my $errstr;
33 my $own;
34  
35 # check for dbh handle passed as first argument
36 if (ref $dsn) {
37 $dbh = $dsn;
38 $own = 0;
39 } else {
40 $dbh = DBI->connect($dsn, $user, $password, $options);
41 $errstr = $DBI::errstr;
42 $own = 1;
43 }
44
45 #
46 # create the object
47 my $self = {
48 'dsn' => $dsn,
49 'user' => $user,
50 'pass' => $password,
51 'dbh' => $dbh,
52 'own' => $own,
53 'errstr' => $errstr,
54 'log' => undef,
55 'log_fetch' => '0', # don't log fetched values by default
56 'log_fetchall' => '0', # don't log fetchall values by default
57 };
58  
59 bless ( $self, $class );
60  
61 return( $self );
62 }
63 sub DESTROY {
64 my ($self) = @_;
65 # disconnect if we have a handle and its ours;
66 $self->{'dbh'}->disconnect() if $self->{'dbh'} && $self->{'own'};
67 }
68  
69 #-------------------------------------------------------------------------
70 #
71 # Data::Shark::DBI DBI FUNCTIONS
72 #
73 #-------------------------------------------------------------------------
74  
75 #
76 # Setup Log Function
77 #
78 sub db_log {
79 my ($self, $logfunc, $log_fetch, $log_fetchall) = @_;
80  
81 $log_fetch ||= '0';
82 $log_fetchall ||= '0';
83  
84 $self->{'log'} = $logfunc;
85 $self->{'log_fetch'} = $log_fetch;
86 $self->{'log_fetchall'} = $log_fetchall;
87 }
88 #
89 # Prepare SQL
90 #
91 sub db_prep {
92 my ($self, $sql) = @_;
93  
94 $self->{'log'}->('prepare: ' . $sql) if $self->{'log'};
95 # prepare
96 my $sth = $self->{'dbh'}->prepare($sql) or ($self->{'log'} && $self->{'log'}->('FAILED: prepare ' . $DBI::errstr));
97 return $sth;
98 }
99 #
100 # Execute SQL
101 #
102 sub db_exec {
103 my ($self, $sth, @args) = @_;
104  
105 $self->{'log'}->('execute: (' . (@args > 0 ? join(',', @args) : '') . ')') if $self->{'log'};
106 my $c = 1;
107 foreach my $arg (@args) {
108 ref($arg)?
109 $sth->bind_param_inout($c++,$arg,256)
110 :
111 $sth->bind_param($c++,$arg);
112 }
113 $sth->execute() or ($self->{'log'} && $self->{'log'}->('FAILED: execute: (' . (@args > 0 ? join(',', @args) : '') . ') ' . $DBI::errstr));
114 }
115 #
116 # Prepare/Execute SQL
117 #
118 sub db_sql {
119 my ($self, $sql, @args) = @_;
120  
121 $self->{'log'}->('prepare: ' . $sql) if $self->{'log'};
122 # prepare
123 my $sth = $self->{'dbh'}->prepare($sql) or ($self->{'log'} && $self->{'log'}->('FAILED: prepare ' . $DBI::errstr));
124 if ($sth) {
125 $self->{'log'}->('execute: (' . (@args > 0 ? join(',', @args) : '') . ')') if $self->{'log'};
126 my $c = 1;
127 foreach my $arg (@args) {
128 ref($arg) ?
129 $sth->bind_param_inout($c++,$arg,256)
130 :
131 $sth->bind_param($c++,$arg);
132 }
133 $sth->execute() or do {
134 $self->{'log'}->('FAILED: execute: (' .
135 join(',', @args) . ') ' .
136 $DBI::errstr) if $self->{'log'};
137 $self->{'errstr'} = 'FAILED: execute: (' .
138 join(',', @args) . ') ' .
139 $DBI::errstr;
140 $sth->finish();
141 undef $sth;
142 };
143 }
144  
145 return $sth;
146 }
147 #
148 # Fetch
149 #
150 sub db_fetch {
151 my ($self, $sth) = @_;
152
153 my @rows;
154  
155 @rows = $sth->fetchrow() if $sth;
156  
157 $self->{'log'}->('fetch: (' .
158 join(',', @rows) .')') if $sth && $self->{'log'} && $self->{'log_fetchall'};
159  
160 return @rows;
161 }
162 #
163 # Fetch Array Ref
164 #
165 sub db_fetch_arrayref {
166 my ($self, $sth) = @_;
167  
168 my $rows;
169  
170 $rows = $sth->fetchrow_arrayref() if $sth;
171  
172 $self->{'log'}->('fetch_arrayref: (' .
173 join(',', @{$rows}) .')') if $sth && $self->{'log'} && $self->{'log_fetchall'};
174  
175 return $rows;
176 }
177 #
178 # Fetch All
179 #
180 sub db_fetchall {
181 my ($self, $sth) = @_;
182  
183 my $allrows = $sth->fetchall_arrayref() if $sth;
184  
185 if ($sth && $self->{'log'} && $self->{'log_fetch'}) {
186 if ($allrows) {
187 foreach my $row (@{$allrows}) {
188 $self->{'log'}->('fetch: (' . join(',', @{$row}) .')');
189 }
190 } else {
191 $self->{'log'}->('fetch: ()');
192 }
193 }
194  
195 return $allrows;
196 }
197 #
198 # Fetch All
199 #
200 sub db_exec_fetchall_args {
201 my ($self, $sql, @args) = @_;
202  
203 if ($self->{'log'}) {
204 $self->{'log'}->('prepare: ' . $sql);
205 $self->{'log'}->('execute: (' . (@args > 0 ? join(',', @args) : '') . ')');
206 }
207  
208 my $rows = $self->{'dbh'}->selectall_arrayref($sql, {Slice => {}}, @args);
209  
210 if ($self->{'log'} && $self->{'log_fetch'}) {
211 if ($rows) {
212 foreach my $row (@{$rows}) {
213 $self->{'log'}->('fetch: (' . join(',', values %{$row}) .')');
214 }
215 } else {
216 $self->{'log'}->('fetch: ()');
217 }
218 }
219  
220 return $rows;
221 }
222 #
223 # Rows
224 #
225 sub db_rows {
226 my ($self, $sth) = @_;
227  
228 return $sth->rows() if $sth;
229 }
230 #
231 # Finish
232 #
233 sub db_done {
234 my ($self, $sth) = @_;
235  
236 $sth->finish() if $sth;
237 }
238  
239 # Autoload methods go after =cut, and are processed by the autosplit program.
240  
241 1;
242 __END__