OpenInteract/DBI.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 127 CC 9 SCAR 35.7
CC
9
Cov
0%
connect
72.0
_w
6.0
BEGIN
2.0
CRAP 35.4
1 package OpenInteract::DBI;
2  
3 # $Id: DBI.pm,v 1.11 2002/05/08 12:02:17 lachoy Exp $
4  
5 use strict;
6 use Carp qw( croak );
7 use Data::Dumper qw( Dumper );
8 use DBI ();
9  
10 @OpenInteract::DBI::ISA = qw();
11 $OpenInteract::DBI::VERSION = sprintf("%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/);
12  
13 use constant DEBUG => 0;
14  
15 use constant DEFAULT_READ_LEN => 32768;
16 use constant DEFAULT_TRUNC_OK => 0;
17  
18 sub connect {
19 my ( $class, $db_info, $p ) = @_;
20 $p ||= {};
21  
22 # Allow callback to modify the connection info; note that we
23 # dereference $db_info for a reason here -- so we don't mess up the
24 # original information, say, in the config hashref
25  
26 if ( ref $p->{pre_connect} eq 'CODE' ) {
27 DEBUG && _w( 2, "before pre_connect code: ", Dumper( $db_info ) );
28 my $new_db_info = $p->{pre_connect}->( \%{ $db_info } );
29 $db_info = $new_db_info if ( $new_db_info->{dsn} );
30 }
31  
32 # Make the actual connection -- let the 'die' trickle up to our
33 # caller if it happens
34  
35 my $dsn = "DBI:$db_info->{driver_name}:$db_info->{dsn}";
36 my $username = $db_info->{username};
37 my $password = $db_info->{password};
38 DEBUG && _w( 2, "Connecting with ($dsn) ($username) ($password)" );
39 my $db = eval { DBI->connect( $dsn, $username, $password ) };
40 croak "Connect failed: $@\n" if ( $@ );
41 DEBUG && _w( 1, "DBI::connect >> Connected ok" );
42  
43 # If we have specified a 'db_name', go ahead and 'use' that
44  
45 if ( $db_info->{db_name} ) {
46 DEBUG && _w( 1, "Use right database ($db_info->{db_name})." );
47 my $rv = $db->do( "use $db_info->{db_name}" );
48 unless ( $rv ) {
49 my $msg = $DBI::errstr;
50 $db->disconnect;
51 die "Use database failed: $msg\n";
52 }
53 }
54  
55 # We don't set this until here so we can control the format of the
56 # error...
57  
58 $db->{RaiseError} = 1;
59 $db->{PrintError} = 0;
60 $db->{ChopBlanks} = 1;
61 $db->{AutoCommit} = 1;
62 $db->{LongReadLen} = $db_info->{long_read_len} || DEFAULT_READ_LEN;
63 $db->{LongTruncOk} = $db_info->{long_trunc_ok} || DEFAULT_TRUNC_OK;
64  
65 $db->trace( $db_info->{trace_level} ) if ( $db_info->{trace_level} );
66  
67 # Allow callback to do something with the database handle along with
68 # the parameters used to connect to it.
69  
70 if ( ref $p->{post_connect} eq 'CODE' ) {
71 DEBUG && _w( 1, "Calling post_connect code with handle and info" );
72 $p->{post_connect}->( \%{ $db_info }, $db );
73 }
74 return $db;
75 }
76  
77 sub _w {
78 return unless ( DEBUG >= shift );
79 my ( $pkg, $file, $line ) = caller;
80 my @ci = caller(1);
81 warn "$ci[3] ($line) >> ", join( ' ', @_ ), "\n";
82 }
83  
84 1;
85  
86 __END__