blib/lib/SQLite/DB.pm

filter: Statementstmt 91.6% 142 / 155 Branchbran 71.8% 46 / 64 Conditioncond 64.2% 9 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 91.3% 21 / 23 Podpod 86.6% 13 / 15 totaltotal 83.8% 234 / 279 CC 38 SCAR 15.6
CC
38
Cov
85%
get_sql_type
8.3
exec
8.2
commit
6.0
CRAP 4.7
↑ Summary
1 # For Emacs: -*- mode:cperl; mode:folding -*-
2  
3 package SQLite::DB;
4  
5 # {{{ BEGIN
6 #
7 8 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 require Exporter;
9 }
10 # }}}
11 # {{{ use block
12 #
13 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use DBD::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 8 ▶use constant { INVALID => 0, # SQL Statment types
Subroutine BEGIN: called CC=1 SCAR=0.0
19 SELECT => 1,
20 INSERT => 2,
21 UPDATE => 3,
22 DELETE => 4,
23 CREATE => 5,
24 8 DROP => 6,};
25  
26 8 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # }}}
29 # {{{ variable declarations
30  
31 8 our @ISA = ('Exporter');
32 8 our @EXPORT = qw(connect disconnect transaction_mode commit exec
33 select get_dboptionlist get_dblist get_error);
34  
35 8 our $VERSION = '0.04';
36  
37 8 my $last_rowid = undef;
38 8 my $affected_rows = 0;
39 8 my @transaction_errors;
40  
41 # }}}
42  
43 # {{{ new Object Constructor
44 #
45 # Creates object based on given dbfile.
46 #
47 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
48 7 my $class = shift;
49 7 my $self = { dbfile => shift,
50 dbconn => undef,
51 dberror => undef,};
52  
53 7 bless $self,$class;
54  
55 7 return $self;
56 }
57 # }}}
58 # {{{ connect Connect to the database
59 #
60 # Returns 1 if sucessfull, 0 otherwise.
61 #
62 7 ▶sub connect {
Subroutine connect: called CC=3 SCAR=11.7
Pod documented
63 7 my $this = shift;
64 7 my $db = DBI->connect("dbi:SQLite:dbname=".$this->{dbfile},"","");
65  
66 7 $db->{PrintError} = 0;
67 7 $db->{RaiseError} = 0;
68  
69 7 ▶ return 0 if ($this->check_error(__PACKAGE__."::connect - Error while connecting to DBI :"));
Branch: 1 branch
BranchTrueFalseTotal
if $this->check_error("SQLite::DB::connect - Error while connecting to DBI :") 0 7 7
70  
71 7 ▶ if (!defined $db) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $db) 0 7 7
72 0 $this->{dberror} = "Cannot connect to databases: $DBI::errstr\n";
73 0 return 0;
74 }
75  
76 7 $this->{dbconn} = $db;
77  
78 7 return 1;
79 }
80 # }}}
81 # {{{ disconnect Diconnect to the database
82 #
83 7 ▶sub disconnect {
Subroutine disconnect: called CC=2 SCAR=7.0
Pod documented
84 7 my $this = shift;
85  
86 7 ▶ $this->{dbconn}->disconnect || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->{"dbconn"}->disconnect or return 0 7 0 7
87 7 $this->{dbconn} = undef;
88  
89 7 return 1;
90 }
91 # }}}
92 # {{{ transaction_mode Start transaction mode
93 #
94 3 ▶sub transaction_mode {
Subroutine transaction_mode: called CC=1 SCAR=0.0
Pod documented
95 3 my $this = shift;
96  
97 3 @transaction_errors = ();
98 3 $this->{dbconn}->{AutoCommit} = 0;
99  
100 }
101 # }}}
102 # {{{ commit Commit transaction
103 #
104 3 ▶sub commit {
Subroutine commit: called CC=6 SCAR=17.9
Pod documented
105 3 my $this = shift;
106 3 my $result = 1;
107 3 my $error;
108 3 ▶ return 1 if ($this->{dbconn}->{AutoCommit});
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"dbconn"}{"AutoCommit"} 1 2 3
109  
110 2 ▶ eval { $this->{dbconn}->commit } if !(@transaction_errors>0);
Branch: 1 branch
BranchTrueFalseTotal
unless @transaction_errors > 0 1 1 2
111  
112 2 ▶ if ($@ || @transaction_errors>0) { # Check if errors occurred in the transaction
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or @transaction_errors > 0) 1 1 2
Condition66%2/3
$@ or @transaction_errors > 0
l!l&&r!l&&!r
011
MC/DC50%1/2
$@ or @transaction_errors > 0
$@ @transaction_errors > 0
Truth table66%2/3
A or B
A= $@B= @transaction_errors > 0
ABresult
1X1
011
000
113 1 $result = 0;
114 1 ▶ $error = ($@) ? $@ : join "\n",@transaction_errors;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 1 1
115 1 $this->{dberror} = "DBSqlite::DB::commit - Error in transaction because $error";
116 1 eval {$this->{dbconn}->rollback};
117 }
118  
119 2 $this->{dbconn}->{AutoCommit} = 1;
120  
121 2 return $result;
122 }
123  
124 # }}}
125 # {{{ rollback Rollback transaction
126 #
127 2 ▶sub rollback {
Subroutine rollback: called CC=3 SCAR=11.3
Pod documented
128 2 my $this = shift;
129 2 my $result = 1;
130  
131 2 ▶ return 1 if ($this->{dbconn}->{AutoCommit});
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"dbconn"}{"AutoCommit"} 1 1 2
132  
133 1 $this->{dbconn}->rollback;
134  
135 1 ▶ if ($@) { # Check if errors occurred in the transaction
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
136 0 $result = 0;
137 0 $this->{dberror} = "DBSqlite::DB::rollback - Error in transaction because $@\n ";
138 }
139  
140 1 $this->{dbconn}->{AutoCommit} = 1;
141  
142 1 return $result;
143 }
144  
145 # }}}
146 # {{{ exec Execute an query
147 #
148 # You can pass bind array as arguments.
149 #
150 36 ▶sub exec {
Subroutine exec: called CC=8 SCAR=21.0
Pod documented
151 36 my $this = shift;
152 36 my $query = shift;
153 36 my @bind = @_;
154 36 my $type = get_sql_type($query);
155 36 my $sth;
156 my $rv;
157  
158 36 $last_rowid = undef;
159 36 $affected_rows = 0;
160  
161 36 ▶ if (!defined $this->{dbconn}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"dbconn"}) 0 36 36
162 0 $this->{dberror} = __PACKAGE__."::exec_query - DB handle not defined";
163 0 return 0;
164 }
165  
166 36 $sth = $this->{dbconn}->prepare($query);
167 36 ▶ return 0 if ($this->check_error(__PACKAGE__."::exec_query - Error while preparing :"));
Branch: 1 branch
BranchTrueFalseTotal
if $this->check_error("SQLite::DB::exec_query - Error while preparing :") 2 34 36
168  
169 34 $rv = $sth->execute(@bind);
170 34 ▶ return 0 if ($this->check_error(__PACKAGE__."::exec_query - Error while executing :"));
Branch: 1 branch
BranchTrueFalseTotal
if $this->check_error("SQLite::DB::exec_query - Error while executing :") 3 31 34
171  
172 31 ▶ $last_rowid = $this->{dbconn}->func('last_insert_rowid') if ($type == INSERT);
Branch: 1 branch
BranchTrueFalseTotal
if $type == 2 23 8 31
173  
174 31 ▶ if (($type == DELETE || $type == UPDATE) && $rv != 0E0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 4 || $type == 3 and $rv != 0) 1 30 31
Condition66%4/6
$type == 4 || $type == 3 and $rv != 0
!ll&&!rl&&r
3001
$type == 4 or $type == 3
l!l&&r!l&&!r
1030
MC/DC33%1/3
$type == 4 || $type == 3 and $rv != 0
$type == 4 $type == 3 $rv != 0
Truth table40%2/5
A or B and C
A= $type == 4B= $type == 3C= $rv != 0
ABCresult
00X0
1X00
0100
1X11
0111
175 1 $affected_rows = $rv;
176 }
177  
178 31 $sth->finish;
179  
180 31 return 1;
181 }
182 # }}}
183 # {{{ last_insert_rowid Returns the last inserted row id
184 #
185 0 ▶sub last_insert_rowid {
Subroutine last_insert_rowid: not called CC=1 SCAR=6.9
Pod undocumented
186 0 return $last_rowid;
187 }
188 # }}}
189 # {{{ get_affected_rows Returns the number of affected rows from last exec
190 #
191 1 ▶sub get_affected_rows {
Subroutine get_affected_rows: called CC=1 SCAR=0.0
Pod undocumented
192 1 return $affected_rows;
193 }
194 # }}}
195 # {{{ select Execute an select query
196 #
197 # You can pass bind arguments.
198 #
199 9 ▶sub select {
Subroutine select: called CC=5 SCAR=16.6
Pod documented
200 9 my $this = shift;
201 9 my $query = shift;
202 9 my $funcptr = shift;
203 9 my @bind = @_;
204 9 my $sth;
205  
206 9 ▶ if (!defined $this->{dbconn}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"dbconn"}) 0 9 9
207 0 $this->{dberror} = __PACKAGE__."::select_query - DB handle not defined";
208 0 return 0;
209 }
210  
211 9 $sth = $this->{dbconn}->prepare($query);
212 9 ▶ return 0 if ($this->check_error(__PACKAGE__."::select_query - Error while preparing :"));
Branch: 1 branch
BranchTrueFalseTotal
if $this->check_error("SQLite::DB::select_query - Error while preparing :") 1 8 9
213  
214 8 $sth->execute(@bind);
215 8 ▶ return 0 if ($this->check_error(__PACKAGE__."::select_query - Error while executing :"));
Branch: 1 branch
BranchTrueFalseTotal
if $this->check_error("SQLite::DB::select_query - Error while executing :") 0 8 8
216  
217 8 ▶ $funcptr->($sth) if (defined $funcptr);
Branch: 1 branch
BranchTrueFalseTotal
if defined $funcptr 8 0 8
218  
219 8 $sth->finish;
220  
221 8 return 1;
222 }
223 # }}}
224 # {{{ select_one_row Execute an select query returnin only one row as an hash
225 #
226 4 ▶sub select_one_row {
Subroutine select_one_row: called CC=1 SCAR=0.0
Pod documented
227 4 my $this = shift;
228 4 my $query = shift;
229 4 my @bind = @_;
230 4 my $result = undef;
231  
232 $this->select($query,
233 4 ▶ sub { my $sth = (defined $_[0]) ? shift : return;
Subroutine __ANON__: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 4 0 4
234 4 ▶ $result = $sth->fetchrow_hashref || return;},
Condition50%1/2
$sth->fetchrow_hashref || (return)
l!l
40
MC/DC0%0/1
$sth->fetchrow_hashref || (return)
$sth->fetchrow_hashref
Truth table50%1/2
A
A= $sth->fetchrow_hashref
Aresult
11
00
235 4 @bind);
236  
237 4 return $result;
238 }
239 # }}}
240 # {{{ get_dblist Returns an array representing the resultset
241 #
242 # Each result contains key and value
243 #
244 1 ▶sub get_dblist {
Subroutine get_dblist: called CC=5 SCAR=16.4
Pod documented
245 1 my $this = shift;
246 1 my $query = shift;
247 1 my $displayfield = shift;
248 1 my $keyfield = shift;
249 1 my $result = [];
250 1 my $id = 0;
251 1 my $sth;
252  
253 1 ▶ if (!defined $this->{dbconn}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"dbconn"}) 0 1 1
254 0 $this->{dberror} = __PACKAGE__."::get_dblist - DB handle not defined";
255 0 return 0;
256 }
257  
258 1 $sth = $this->{dbconn}->prepare($query);
259 1 ▶ return 0 if ($this->check_error(__PACKAGE__."::get_dblist - Error while preparing :"));
Branch: 1 branch
BranchTrueFalseTotal
if $this->check_error("SQLite::DB::get_dblist - Error while preparing :") 0 1 1
260  
261 1 $sth->execute;
262 1 ▶ return 0 if ($this->check_error(__PACKAGE__."::get_dblist - Error while executing :"));
Branch: 1 branch
BranchTrueFalseTotal
if $this->check_error("SQLite::DB::get_dblist - Error while executing :") 0 1 1
263  
264 1 ▶ while (my $data = $sth->fetchrow_hashref) { # Go through the records
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $sth->fetchrow_hashref) 3 1 4
265 3 $result->[$id]->{id} = $$data{$keyfield};
266 3 $result->[$id]->{value} = $$data{$displayfield};
267 3 $id++;
268 }
269  
270 1 $sth->finish;
271  
272 1 return $result;
273 }
274 # }}}
275 # {{{ get_error Return last db error
276 #
277 0 ▶sub get_error {
Subroutine get_error: not called CC=1 SCAR=6.9
Pod documented
278 0 my $this = shift;
279  
280 0 return $this->{dberror};
281 }
282 # }}}
283 # {{{ check_error Check if an error occured
284 #
285 # Save the error in dberror var.
286 #
287 96 ▶sub check_error {
Subroutine check_error: called CC=4 SCAR=13.9
Pod documented
288 96 my $this = shift;
289 96 my $err_id = shift;
290 96 my $query = shift;
291  
292 96 ▶ if ($DBI::err || $DBI::errstr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DBI::err or $DBI::errstr) 6 90 96
Condition66%2/3
$DBI::err or $DBI::errstr
l!l&&r!l&&!r
6090
MC/DC50%1/2
$DBI::err or $DBI::errstr
$DBI::err $DBI::errstr
Truth table66%2/3
A or B
A= $DBI::errB= $DBI::errstr
ABresult
1X1
011
000
293 6 $this->{dberror} = $err_id."\n";
294 6 ▶ $this->{dberror} .= "SQL : ".$query."\n" if (defined $query);
Branch: 1 branch
BranchTrueFalseTotal
if defined $query 0 6 6
295 6 $this->{dberror} .= "Errors: $DBI::err, $DBI::errstr\n";
296 6 push @transaction_errors,$this->{dberror};
297 6 return 1;
298 }
299  
300 90 return 0;
301 }
302 # }}}
303 # {{{ get_sql_type Get the type of an sql statment
304 #
305 36 ▶sub get_sql_type {
Subroutine get_sql_type: called CC=8 SCAR=21.2
Pod documented
306 36 ▶ my $this = (ref $_[0]) ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 36 36
307 36 my $query = shift;
308  
309 36 ▶ return SELECT if ($query =~ /^\s*SeLeCt/i);
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /^\s*SeLeCt/i 0 36 36
310 36 ▶ return INSERT if ($query =~ /^\s*InSeRt/i);
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /^\s*InSeRt/i 27 9 36
311 9 ▶ return UPDATE if ($query =~ /^\s*UpDaTe/i);
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /^\s*UpDaTe/i 0 9 9
312 9 ▶ return DELETE if ($query =~ /^\s*DeLeTe/i);
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /^\s*DeLeTe/i 1 8 9
313 8 ▶ return CREATE if ($query =~ /^\s*CrEaTe/i);
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /^\s*CrEaTe/i 7 1 8
314 1 ▶ return DROP if ($query =~ /^\s*DrOp/i);
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /^\s*DrOp/i 0 1 1
315  
316 1 return INVALID;
317 }
318 # }}}
319  
320 8 1;
321  
322 # {{{ module documentation
323  
324 __END__
↑ Summary