lib/DBIx/AnyDBD.pm

filter: Statementstmt 17.8% 23 / 129 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 41.1% 7 / 17 Podpod 57.1% 4 / 7 totaltotal 14.1% 34 / 240 CC 38 SCAR 47.9
CC
38
Cov
11%
rebless
308.1
new
56.0
__ANON__
56.0
CRAP 120.1
↑ Summary
1 # $Id: AnyDBD.pm,v 1.15 2002/09/04 12:05:03 matt Exp $
2  
3 package DBIx::AnyDBD;
4 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw/$AUTOLOAD $VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 DBIx::AnyDBD - DBD independent class
12  
13 =head1 VERSION
14  
15 Version 2.03
16  
17 =cut
18  
19 1 our $VERSION = '2.03';
20  
21 =head1 SYNOPSIS
22  
23 This class provides application developers with an abstraction class
24 a level away from DBI, that allows them to write an application that
25 works on multiple database platforms. The idea isn't to take away the
26 responsibility for coding different SQL on different platforms, but
27 to simply provide a platform that uses the right class at the right
28 time for whatever DB is currently in use.
29  
30  
31 use DBIx::AnyDBD;
32
33 my $db = DBIx::AnyDBD->connect("dbi:Oracle:sid1",
34 "user", "pass", {}, "MyClass");
35  
36 my $foo = $db->foo;
37 my $blee = $db->blee;
38  
39 That doesn't really tell you much... Because you have to implement a
40 bit more than that. Underneath you have to have a module
41 MyClass::Oracle that has methods foo() and blee in it. If those
42 methods don't exist in MyClass::Oracle, it will check in MyClass::Default,
43 allowing you to implement code that doesn't need to be driver
44 dependent in the same module. The foo() and blee() methods will receive
45 the DBIx::AnyDBD instance as thier first parameter, and any parameters
46 you pass just go as parameters.
47  
48 See the example Default.pm and Sybase.pm classes in the AnyDBD directory
49 for an example.
50  
51 Underneath it's all implemented using the ISA hierarchy, which is modified
52 when you connect to your database. The inheritance tree ensures that the
53 right functions get called at the right time. There is also an AUTOLOADer
54 that steps in if the function doesn't exist and tries to call the function
55 on the database handle (i.e. in the DBI class). The sub-classing uses
56 C<ucfirst($dbh->{Driver}->{Name})> (along with some clever fiddling for
57 ODBC and ADO) to get the super-class, so if you don't know what to name
58 your class (see the list below first) then check that.
59  
60 =head1 SUBROUTINES/METHODS
61  
62 =head2 new
63  
64 dsn => $dsn,
65 user => $user,
66 pass => $pass,
67 attr => $attr,
68 package => $package
69  
70 new() is a named parameter call that connects and creates a new db object
71 for your use. The named parameters are dsn, user, pass, attr and package.
72 The first 4 are just the parameters passed to DBI->connect, and package
73 contains the package prefix for your database dependent modules, for example,
74 if package was "MyPackage", the AUTOLOADer would look for
75 MyPackage::Oracle::func, and then MyPackage::Default::func. Beware that the
76 DBD driver will be ucfirst'ed, because lower case package names are reserved
77 as pragmas in perl. See the known DBD package mappings below.
78  
79 If package parameter is undefined then the package name used to call
80 the constructor is used. This will usually be DBIx::AnyDBD. This, in
81 itself, is not very useful but is convenient if you subclass
82 DBIx::AnyDBD.
83  
84 If attr is undefined then the default attributes are:
85  
86 AutoCommit => 1
87 PrintError => 0
88 RaiseError => 1
89  
90 So be aware if you don't want your application dying to either eval{} all
91 db sections and catch the exception, or pass in a different attr parameter.
92  
93 After re-blessing the object into the database specific object, DBIx::AnyDBD
94 will call the _init() method on the object, if it exists. This allows you
95 to perform some driver specific post-initialization.
96  
97 =cut
98  
99 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
100 0 my $proto = shift;
101 0 my %args = @_;
102  
103 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
104  
105 0 ▶ return unless(defined($class));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 0 0
106  
107 my $dbh = DBI->connect(
108 $args{dsn},
109 $args{user},
110 $args{pass},
111 ($args{attr} ||
112 {
113 0 ▶ AutoCommit => 1,
Condition0%0/2
$args{'attr'} || {'AutoCommit', 1, 'PrintError', 0, 'RaiseError', 1}
l!l
00
MC/DC0%0/1
$args{'attr'} || {'AutoCommit', 1, 'PrintError', 0, 'RaiseError', 1}
$args{'attr'}
Truth table0%0/2
A
A= $args{'attr'}
Aresult
11
00
114 PrintError => 0,
115 RaiseError => 1,
116 })
117 );
118 0 ▶ die "Can't connect: " . DBI->errstr unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 0 0
119 0 ▶ my $package = $args{'package'} || $class;
Condition0%0/3
$args{'package'} || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'package'} || $class
$args{'package'} $class
Truth table0%0/3
A || B
A= $args{'package'}B= $class
ABresult
1X1
011
000
120 0 my $self = bless { 'package' => $package, dbh => $dbh }, $class;
121 0 $self->rebless;
122 0 ▶ $self->_init if $self->can('_init');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("_init") 0 0 0
123 0 return $self;
124 }
125  
126 =head2 new_with_dbh
127  
128 Instantiate an object around an existing DBI database handle.
129  
130 =cut
131  
132 # RT#5661
133 0 ▶sub new_with_dbh {
Subroutine new_with_dbh: not called CC=2 SCAR=17.9
Pod documented
134 0 my ($class, $dbh, $package) = @_;
135 0 my $self = bless { 'package' => $package, 'dbh' => $dbh }, $class;
136  
137 0 $self->rebless;
138 0 ▶ $self->_init if $self->can('_init');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("_init") 0 0 0
139
140 0 return $self;
141 }
142  
143 =head2 connect($dsn, $user, $pass, $attr, $package)
144  
145 connect() is very similar to DBI->connect, taking exactly the same first
146 4 parameters. The 5th parameter is the package prefix, as above.
147  
148 connect() doesn't try and default attributes for you if you don't pass them.
149  
150 After re-blessing the object into the database specific object, DBIx::AnyDBD
151 will call the _init() method on the object, if it exists. This allows you
152 to perform some driver specific post-initialization.
153  
154 =cut
155  
156 0 ▶sub connect {
Subroutine connect: not called CC=4 SCAR=30.0
Pod documented
157 0 my $class = shift;
158 0 my ($dsn, $user, $pass, $args, $package) = @_;
159 0 my $dbh = DBI->connect($dsn, $user, $pass, $args);
160 0 ▶ return undef unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 0 0
161 0 ▶ $package ||= $class;
Condition0%0/3
$package ||= $class
l!l&&r!l&&!r
000
MC/DC0%0/2
$package ||= $class
$package $class
Truth table0%0/3
A ||= B
A= $packageB= $class
ABresult
1X1
011
000
162 0 my $self = bless { 'package' => $package, 'dbh' => $dbh }, $class;
163 0 $self->rebless;
164 0 ▶ $self->_init if $self->can('_init');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("_init") 0 0 0
165 0 return $self;
166 }
167  
168 0 ▶sub rebless {
Subroutine rebless: not called CC=18 SCAR=57.3
Pod undocumented
169 0 my $self = shift;
170 0 my $driver = ucfirst($self->{dbh}->{Driver}->{Name});
171 0 ▶ if ( $driver eq 'Proxy' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($driver eq "Proxy") 0 0 0
172 # Looking into the internals of DBD::Proxy is maybe a little questionable
173 0 ($driver) = $self->{dbh}->{proxy_client}->{application} =~ /^DBI:(.+?):/;
174 }
175 0 my $class = $self->{'package'};
176 0 my ($odbc, $ado) = ($driver eq 'ODBC', $driver eq 'ADO');
177 0 ▶ if ($odbc || $ado) {
Branch: 1 branch
BranchTrueFalseTotal
if ($odbc or $ado) 0 0 0
Condition0%0/3
$odbc or $ado
l!l&&r!l&&!r
000
MC/DC0%0/2
$odbc or $ado
$odbc $ado
Truth table0%0/3
A or B
A= $odbcB= $ado
ABresult
1X1
011
000
178 0 my $name;
179
180 0 ▶ if ($odbc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($odbc) { } 0 0 0
elsif ($ado) { } 0 0 0
181 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
182 0 $name = $self->{dbh}->func(17, GetInfo);
183 }
184 elsif ($ado) {
185 0 $name = $self->{dbh}->{ado_conn}->Properties->Item('DBMS Name')->Value;
186 }
187 else {
188 0 die "Can't determine driver name!\n";
189 }
190
191 0 ▶ if ($name eq 'Microsoft SQL Server') {
Branch: 5 branches
BranchTrueFalseTotal
if ($name eq 'Microsoft SQL Server') { } 0 0 0
elsif ($name eq 'SQL Server') { } 0 0 0
elsif ($name =~ /Oracle/) { } 0 0 0
elsif ($name eq 'Adaptive Server Anywhere') { } 0 0 0
elsif ($name eq 'ADABAS D') { } 0 0 0
192 0 $driver = 'MSSQL';
193 }
194 elsif ($name eq 'SQL Server') {
195 0 $driver = 'Sybase';
196 }
197 elsif ($name =~ /Oracle/) {
198 0 $driver = 'Oracle';
199 }
200 # elsif ($name eq 'ACCESS') {
201 # $driver = 'Access';
202 # }
203 # elsif ($name eq 'Informix') {
204 # $driver = 'Informix'; # caught by "else" condition below
205 # }
206 elsif ($name eq 'Adaptive Server Anywhere') {
207 0 $driver = 'ASAny';
208 } elsif ($name eq 'ADABAS D') {
209 0 $driver = 'AdabasD';
210 }
211 else { # this should catch Access and Informix
212 0 $driver = lc($name);
213 0 $driver =~ s/\b(\w)/uc($1)/eg;
214 0 $driver =~ s/\s+/_/g;
215 }
216 }
217
218 0 my $dir;
219 0 ($dir = $self->{package}) =~ s/::/\//g;
220 0 ▶ load_module("$dir/Default.pm") or die "Cannot find $dir/Default.pm module in \@INC for $self->{package}!";
Branch: 1 branch
BranchTrueFalseTotal
load_module("$dir/Default.pm") or die qq[Cannot find $dir/Default.pm module in \@INC for $self->{"package"}!] 0 0 0
221  
222 0 ▶ if (!load_module("$dir/$driver.pm")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not load_module("$dir/$driver.pm")) { } 0 0 0
223 # no package for driver - use Default instead
224 0 bless $self, "${class}::Default";
225 # make Default -> DBIx::AnyDBD hierarchy
226 0 add_isa("${class}::Default", 'DBIx::AnyDBD');
227 }
228 else {
229 # package OK...
230
231 0 bless $self, "${class}::${driver}";
232
233 0 ▶ if ($ado) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ado) 0 0 0
234 0 ▶ if (load_module("$dir/ADO.pm")) {
Branch: 1 branch
BranchTrueFalseTotal
if (load_module("$dir/ADO.pm")) 0 0 0
235 0 ▶ if (!load_module("$dir/ODBC.pm")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not load_module("$dir/ODBC.pm")) { } 0 0 0
236 0 add_isa("${class}::${driver}", "${class}::ADO");
237 0 add_isa("${class}::ADO", "${class}::Default");
238 }
239 else {
240 0 add_isa("${class}::${driver}", "${class}::ADO");
241 0 add_isa("${class}::ADO", "${class}::ODBC");
242 0 add_isa("${class}::ODBC", "${class}::Default");
243 }
244 0 return;
245 }
246 }
247
248 0 ▶ if ($odbc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($odbc) 0 0 0
249 0 ▶ if (load_module("$dir/ODBC.pm")) {
Branch: 1 branch
BranchTrueFalseTotal
if (load_module("$dir/ODBC.pm")) 0 0 0
250 0 add_isa("${class}::${driver}", "${class}::ODBC");
251 0 add_isa("${class}::ODBC", "${class}::Default");
252 0 return;
253 }
254 }
255
256 # make Default -> DBIx::AnyDBD hierarchy
257 0 add_isa("${class}::Default", 'DBIx::AnyDBD');
258
259 # make Driver -> Default hierarchy
260 0 add_isa("${class}::${driver}", "${class}::Default");
261 }
262
263 }
264  
265 0 ▶sub add_isa {
Subroutine add_isa: not called CC=2 SCAR=10.9
Pod undocumented
266 0 my ($class, $newval) = @_;
267 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
268 0 ▶ unshift @{"${class}::ISA"}, $newval unless $class->isa($newval);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa($newval) 0 0 0
269 }
270  
271 0 ▶sub load_module {
Subroutine load_module: not called CC=3 SCAR=24.8
Pod undocumented
272 0 my $module = shift;
273
274 0 eval {
275 0 require $module;
276 };
277 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
278 0 ▶ if ($@ =~ /^Can't locate $module in \@INC/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /^Can't locate $module in \@INC/) { } 0 0 0
279 0 undef $@;
280 0 return 0;
281 }
282 else {
283 0 die $@;
284 }
285 }
286
287 0 return 1;
288 }
289  
290 =head2 $db->get_dbh()
291  
292 This method is mainly for the DB dependent modules to use, it returns the
293 underlying DBI database handle. There will probably have code added here
294 to check the db is still connected, so it may be wise to always use this
295 method rather than trying to retrieve $self->{dbh} directly.
296  
297 =cut
298  
299 0 ▶sub get_dbh {
Subroutine get_dbh: not called CC=1 SCAR=6.9
Pod documented
300 # maybe add code here to check connection status.
301 # or maybe add check once every 10 get_dbh's...
302 0 return shift->{dbh};
303 }
304  
305 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
306 0 my $self = shift;
307 0 my $dbh;
308 0 ▶ if ($dbh = $self->get_dbh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh = $self->get_dbh) 0 0 0
309 0 $dbh->disconnect;
310 }
311 }
312  
313 =head2 Controlling error propagation from AUTOLOADed DBI methods
314  
315 Typicially the implementation packages will make calls to DBI methods
316 as though they were methods of the DBIx::AnyDBD object. If one of
317 these methods reports an error in DBI::AnyDBD then the error is caught
318 and rethrown by DBIx::AnyDBD so that the error is reported as occuring
319 in the implementation module. It does this by calling Carp::croak()
320 with the current package set to DBIx::AnyDBD::Carp.
321  
322 Usually this the the right thing to do but sometimes you may want to
323 report the error in the line containing the original method call on
324 the DBIx::AnyDBD object. In this case you should temporarily set
325 @DBIx::AnyDBD::Carp::ISA.
326  
327 my $db = DBIx::AnyDBD->connect("dbi:Oracle:sid1",
328 "user", "pass", {}, "MyClass");
329  
330 my $foo = $db->foo;
331 my $blee = $db->blee("too few arguments"); # Error reported here
332  
333 package MyClass::Oracle;
334
335 sub foo {
336 shift->prepare("Invalid SQL"); # Error reported here
337 }
338  
339 sub blee {
340 local @DBIx::AnyDBD::Carp::ISA = __PACKAGE__;
341 shift->selectall_arrayref(BLEE_STATEMENT,{},@_); # Error not reported here
342 }
343  
344 =cut
345  
346 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=1 SCAR=5.6
347 0 (my $func = $AUTOLOAD) =~ s/.*:://;
348 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
349 # The following is much more elegant but doesn't work!
350 # *$func = sub {
351 # unshift @_ => shift->get_dbh;
352 # goto &{$_[0]->can($func)};
353 # };
354 0 ▶ *$func = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
355 0 my $dbh = shift->get_dbh;
356 0 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
elsif (defined wantarray) { } 0 0 0
357 0 my @r = eval { $dbh->$func(@_) };
358 0 ▶ return @r unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
359 } elsif (defined wantarray) {
360 0 my $r = eval { $dbh->$func(@_) };
361 0 ▶ return $r unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
362 } else {
363 0 eval { $dbh->$func(@_) };
364 0 ▶ return unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
365 };
366 0 ▶ if ( $@ =~ /(.*) at ${\__FILE__} .*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ m[(.*) at ${\"lib/DBIx/AnyDBD.pm";} .*$]) 0 0 0
367 # We want croak() to report errors as occouring
368 # in the implementation class even though we are related
369 package DBIx::AnyDBD::Carp;
370 0 require Carp;
371 0 Carp::croak "$1";
372 }
373 0 die; # Unreachable ?
374 0 };
375 0 goto &$func;
376 }
377  
378 1 1;
379  
380 __END__
↑ Summary