blib/lib/DBIx/AbstractLite.pm

filter: Statementstmt 17.6% 21 / 119 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 27.7% 5 / 18 Podpod 0.0% 0 / 11 totaltotal 12.6% 26 / 205 CC 29 SCAR 49.3
CC
29
Cov
12%
select
342.0
DESTROY
20.0
new
12.0
CRAP 138.5
↑ Summary
1 package DBIx::AbstractLite;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 DBIx::AbstractLite - Lightweight DBI SQL abstraction in a hybrid interface
8  
9 =head1 SYNOPSIS
10  
11 use Project::DB;
12  
13 my $DB = new Project::DB; # connect to DB
14 $DB->setWhere('date >= sysdate-1'); # global condition for all queries to follow
15 my $sth = $DB->select({
16 fields => [ 'user', 'email' ],
17 table => 'users',
18 where => { 'user' => [ 'like', 'me%' ],
19 'length(email)' => [ '>', '20' ],
20 },
21 }) or die $DB->error();
22 print $sth->fetchrow_array();
23  
24 $DB->query('SELECT user, email FROM users WHERE user like ?', 'me%')
25 or die $DB->error();
26 my $userEmail = $DB->fetch_hash();
27 print "someuser's email is: ", $userEmail->{someuser}, "\n";
28  
29 $DB->query('SELECT email FROM users WHERE user = ?', 'me')
30 or die $DB->error();
31 print "my email is ", $DB->fetch_col();
32  
33  
34 package Project::DB;
35  
36 use DBIx::AbstractLite;
37 use vars qw (@ISA);
38 @ISA = qw(DBIx::AbstractLite);
39  
40 sub _initMembers {
41 my ($self) = @_;
42  
43 $self->{DSN} = "dbi:Oracle:$ENV{ORACLE_SID}";
44 $self->{USER} = 'username';
45 $self->{PASS} = 'password';
46 }
47  
48 =head1 DESCRIPTION
49  
50 This module is based on DBIx::Abstract, but is much simpler.
51 It also doesn't deviate from the DBI interface as much as DBIx::Abstract does.
52 The main similarity between DBIx::AbstractLite and DBIx::Abstract
53 is in the select method.
54 Unlike Abstract, AbstractLite is not 100% abstract in that it still allows
55 conventional access to the DBI interface,
56 using plain SQL and the DBI statement handle methods.
57  
58 CGI::LogCarp is used internally to trace the queries sent to DBI.
59 To see the trace statements, add this statement at the beginning of your program:
60 use CGI::LogCarp qw(:STDBUG);
61  
62  
63  
64 MORE DOCUMENTATION TBD...
65  
66 =cut
67  
68 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use Error::Dumb;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use CGI::LogCarp qw(:STDBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 1 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 1 @ISA = qw(Error::Dumb);
75 1 $VERSION = do { my @r=(q$Revision: 1.5 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r };
76  
77  
78 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
79 0 my ($class) = @_;
80
81 0 my $self= {};
82 0 $self->{WHERE} = undef; # a list of where conditions
83  
84 0 bless $self, $class;
85  
86 0 ▶ $self->_initMembers() if $self->can('_initMembers');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("_initMembers") 0 0 0
87  
88 0 ▶ $self->{DBH} = $self->connect() or die $DBI::errstr;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DBH"} = $self->connect or die $DBI::errstr 0 0 0
89 0 trace "$self->{DSN} successfully connected\n";
90  
91 0 return $self;
92 }
93  
94 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=4 SCAR=30.0
95 0 my ($self) = @_;
96  
97 0 ▶ $self->{STH}->finish() if $self->{STH};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"STH"} 0 0 0
98  
99 # don't disconnect if running under mod_perl and Apache::DBI
100 0 ▶ return if $ENV{MOD_PERL};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MOD_PERL"} 0 0 0
101  
102 0 ▶ $self->disconnect() if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 0 0 0
103 }
104  
105 0 ▶sub connect {
Subroutine connect: not called CC=1 SCAR=6.9
Pod undocumented
106 0 my ($self) = @_;
107  
108 return DBI->connect($self->{DSN}, $self->{USER}, $self->{PASS},
109 0 { ChopBlanks => 1 } );
110 }
111  
112 1 my %aliases = (
113 group => 'group_by',
114 order => 'order_by',
115 );
116  
117 0 ▶sub select {
Subroutine select: not called CC=18 SCAR=58.3
Pod undocumented
118 0 my ($self, $args) = @_;
119  
120 0 my @bind = ();
121 0 my @where = ();
122  
123 0 $args->{fields_global} = $self->{FIELDS};
124  
125 # convert aliases
126 0 ▶ while ( my ($alias, $real) = each %aliases ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %aliases) 0 0 0
127 0 ▶ if ( defined $args->{$alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{$alias}) 0 0 0
128 0 $args->{$real} = $args->{$alias};
129 0 delete $args->{$alias};
130 }
131 }
132  
133 # "join" arg is a special case. add it to @where list directly,
134 0 ▶ if ( $args->{join} && ref $args->{join} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"join"} and ref $args->{"join"} eq "ARRAY") 0 0 0
Condition0%0/3
$args->{"join"} and ref $args->{"join"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"join"} and ref $args->{"join"} eq "ARRAY"
$args->{"join"} ref $args->{"join"} eq "ARRAY"
Truth table0%0/3
A and B
A= $args->{"join"}B= ref $args->{"join"} eq "ARRAY"
ABresult
0X0
100
111
135 0 push @where, @{ $args->{join} };
136 0 delete $args->{join};
137 }
138  
139 # convert scalar to arrayref,
140 # then convert arrayref to comma-separated string.
141 # this is to accomodate a choice of input: either arrayref or scalar
142 0 foreach my $key ( keys %$args ) {
143 0 ▶ unless ( ref $args->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args->{$key}) 0 0 0
144 0 $args->{$key} = [ $args->{$key} ];
145 }
146 0 ▶ if ( ref $args->{$key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{$key} eq "ARRAY") 0 0 0
147 0 $args->{$key} = join ', ', @{ $args->{$key} };
148 }
149 }
150  
151 0 ▶ $args->{fields} .= ", $args->{fields_global}" if $args->{fields_global};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"fields_global"} 0 0 0
152 0 my $query = "SELECT $args->{fields} FROM $args->{table} ";
153 0 ▶ if ( $args->{where} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"where"}) 0 0 0
154 0 ▶ while ( my ($key, $value) = each %{ $args->{where} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$args{"where"};}) 0 0 0
155 0 my ($operator, $targetValue) = @$value;
156 0 push @where, "($key $operator ?)";
157 0 push @bind, $targetValue;
158 }
159 }
160 0 ▶ if ( $args->{where_raw} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"where_raw"}) 0 0 0
161 0 ▶ while ( my ($key, $value) = each %{ $args->{where_raw} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$args{"where_raw"};}) 0 0 0
162 0 my ($operator, $targetValue) = @$value;
163 0 push @where, "($key $operator $targetValue)";
164 }
165 }
166 0 ▶ push @where, @{ $self->{WHERE} } if $self->{WHERE};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"WHERE"} 0 0 0
167 0 ▶ if ( @where ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@where) 0 0 0
168 0 $query .= 'WHERE ';
169 0 $query .= join ' AND ', @where;
170 }
171 0 ▶ if ( $args->{group_by} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"group_by"}) 0 0 0
172 0 $query .= " GROUP BY $args->{group_by}";
173 }
174 0 ▶ if ( $args->{order_by} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"order_by"}) 0 0 0
175 0 $query .= " ORDER BY $args->{order_by}";
176 }
177 0 ▶ if ( $args->{extra} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"extra"}) 0 0 0
178 0 $query .= " $args->{extra}";
179 }
180 0 $self->{QUERY} = $query;
181 0 $self->{BIND} = \@bind;
182 0 return $self->_query();
183 }
184  
185 0 ▶sub query {
Subroutine query: not called CC=1 SCAR=6.9
Pod undocumented
186 0 my ($self, $query, @args) = @_;
187  
188 0 $self->{QUERY} = $query;
189 0 $self->{BIND} = \@args;
190 0 return $self->_query();
191 }
192  
193 0 ▶sub _query {
Subroutine _query: not called CC=3 SCAR=24.8
194 0 my ($self) = @_;
195  
196 0 my @args = @{ $self->{BIND} };
197 0 my $args = join ',', @args;
198 0 trace "$self->{DSN} query: $self->{QUERY}; args: $args\n";
199 $self->{STH} = $self->{DBH}->prepare($self->{QUERY})
200 0 ▶ or return $self->_setError($self->{DBH}->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"STH"} = $self->{"DBH"}->prepare($self->{"QUERY"}) or return $self->_setError($self->{"DBH"}->errstr) 0 0 0
201 0 ▶ $self->{STH}->execute(@args) or return $self->_setError($self->{STH}->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"STH"}->execute(@args) or return $self->_setError($self->{"STH"}->errstr) 0 0 0
202
203 0 return $self->{STH};
204 }
205  
206 0 ▶sub disconnect {
Subroutine disconnect: not called CC=2 SCAR=17.9
Pod undocumented
207 0 my ($self) = @_;
208  
209 0 ▶ $self->{DBH}->disconnect() if $self->{DBH};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DBH"} 0 0 0
210 }
211  
212 0 ▶sub fetch_col {
Subroutine fetch_col: not called CC=1 SCAR=6.9
Pod undocumented
213 0 my ($self) = @_;
214  
215 0 my ($col) = $self->{STH}->fetchrow_array();
216 0 return $col;
217 }
218  
219 0 ▶sub fetch_hash {
Subroutine fetch_hash: not called CC=2 SCAR=17.9
Pod undocumented
220 0 my ($self) = @_;
221  
222 0 my $hash = {};
223 0 ▶ while ( my ($key, $value) = $self->{STH}->fetchrow_array() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->{"STH"}->fetchrow_array) 0 0 0
224 0 $hash->{$key} = $value;
225 }
226  
227 0 return $hash;
228 }
229  
230 0 ▶sub setWhere {
Subroutine setWhere: not called CC=1 SCAR=6.9
Pod undocumented
231 0 my ($self, $where) = @_;
232  
233 0 push @{ $self->{WHERE} }, $where;
234 }
235  
236 0 ▶sub getWhere {
Subroutine getWhere: not called CC=2 SCAR=17.9
Pod undocumented
237 0 my ($self) = @_;
238  
239 0 ▶ if ( defined $self->{WHERE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'WHERE'}) { } 0 0 0
240 0 return ' WHERE ' . join (' AND ', @{ $self->{WHERE} }) . ' ';
241 }
242 else {
243 0 return '';
244 }
245 }
246  
247 0 ▶sub setFields {
Subroutine setFields: not called CC=1 SCAR=6.9
Pod undocumented
248 0 my ($self, $field) = @_;
249  
250 0 push @{ $self->{FIELDS} }, $field;
251 }
252  
253 0 ▶sub getDistinct {
Subroutine getDistinct: not called CC=2 SCAR=17.9
Pod undocumented
254 0 my ($self, $colname, $table) = @_;
255  
256 0 my @cols = ();
257 0 my $sth = $self->select({
258 fields => "distinct($colname)",
259 table => $table
260 });
261 0 ▶ while ( my ($col) = $sth->fetchrow_array() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($col) = $sth->fetchrow_array) 0 0 0
262 0 push @cols, $col;
263 }
264 0 return \@cols;
265 }
266  
267 1 1;
268  
269 __END__
↑ Summary