| 1 | |||||||||||||||||||||
| 2 | package Tao::DBI::db; | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 1 | ▶ | use 5.006; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | require Exporter; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||
| 11 | 1 | our @EXPORT = qw(); | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | our $VERSION = '0.011'; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | ▶ | use DBI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use Tao::DBI::st; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 1 | ▶ | use Tao::DBI::st_deep; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # the instance variables: | ||||||||||||||||||||
| 20 | # DBH | ||||||||||||||||||||
| 21 | # NAME | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | # usage: | ||||||||||||||||||||
| 24 | # $dbh = new Tao::DBI::db($args); | ||||||||||||||||||||
| 25 | sub new { | ||||||||||||||||||||
| 26 | 0 | ▶ | my ( $proto, $args ) = @_; | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 27 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 28 | 0 | my $obj = bless {}, $class; | |||||||||||||||||||
| 29 | 0 | $obj->initialize($args); | |||||||||||||||||||
| 30 | 0 | return $obj; | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # usage: | ||||||||||||||||||||
| 34 | # $dbh->initialize($args); | ||||||||||||||||||||
| 35 | # where $args is | ||||||||||||||||||||
| 36 | # { name => , dsn|url => , user => , password =>, options passed to DBI->connect } | ||||||||||||||||||||
| 37 | # | ||||||||||||||||||||
| 38 | # | ||||||||||||||||||||
| 39 | # protected | ||||||||||||||||||||
| 40 | sub initialize { | ||||||||||||||||||||
| 41 | 0 | ▶ | my ( $self, $args ) = @_; | ||||||||||||||||||
|
Subroutine
initialize: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 0 | $self->{NAME} = $args->{name}; | |||||||||||||||||||
| 44 | 0 | ▶ | $self->{URL} = $args->{dsn} || $args->{url}; | ||||||||||||||||||
|
Condition0%0/3
$args->{'dsn'} || $args->{'url'}
MC/DC0%0/2
$args->{'dsn'} || $args->{'url'}
$args->{'dsn'}
$args->{'url'}
Truth table0%0/3
A || B
A= $args->{'dsn'}B= $args->{'url'}
| |||||||||||||||||||||
| 45 | 0 | $self->{USER} = $args->{user}; | |||||||||||||||||||
| 46 | 0 | $self->{PASS} = $args->{password}; | |||||||||||||||||||
| 47 | 0 | ▶ | unless ( exists $args->{FetchHashKeyName} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | 0 | $args->{FetchHashKeyName} = 'NAME_lc'; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | 0 | ▶ | unless ( exists $args->{AutoCommit} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 0 | $args->{AutoCommit} = 0; | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | my $dbh = DBI->connect( $self->{URL}, $self->{USER}, $self->{PASS}, $args ) | |||||||||||||||||||
| 55 | ; # remaining arguments can be passed to DBI->connect (needs improving!) | ||||||||||||||||||||
| 56 | 0 | ▶ | if ($dbh) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 0 | $self->{DBH} = $dbh; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||
| 60 | 0 | die "Can't establish connection '$self->{NAME}': $DBI::errstr\n"; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # $stmt = $dbh->prepare($sql, $args); | ||||||||||||||||||||
| 65 | # $stmt = $dbh->prepare($args); # $args is hashref | ||||||||||||||||||||
| 66 | sub prepare { | ||||||||||||||||||||
| 67 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
prepare: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||
| 68 | 0 | my $sql; | |||||||||||||||||||
| 69 | 0 | ▶ | $sql = shift unless ref $_[0]; # first non-ref arg | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | ▶ | my $args = shift || {}; | ||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 71 | 0 | ▶ | my $st_type = $args->{type} || 'plain'; | ||||||||||||||||||
|
Condition0%0/2
$args->{'type'} || 'plain'
MC/DC0%0/1
$args->{'type'} || 'plain'
$args->{'type'}
Truth table0%0/2
A
A= $args->{'type'}
| |||||||||||||||||||||
| 72 | 0 | ▶ | if ( $st_type eq 'plain' ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 73 | return new Tao::DBI::st( | ||||||||||||||||||||
| 74 | { | ||||||||||||||||||||
| 75 | ( $sql ? ( sql => $sql ) : () ), | ||||||||||||||||||||
| 76 | dbh => $self->{DBH}, | ||||||||||||||||||||
| 77 | 0 | ▶ | %$args | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | elsif ( $st_type eq 'deep' ) { | ||||||||||||||||||||
| 82 | return new Tao::DBI::st_deep( | ||||||||||||||||||||
| 83 | { | ||||||||||||||||||||
| 84 | ( $sql ? ( sql => $sql ) : () ), | ||||||||||||||||||||
| 85 | dbh => $self->{DBH}, | ||||||||||||||||||||
| 86 | 0 | ▶ | %$args | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||
| 91 | 0 | die "unknown statement type: $st_type\n"; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 1 | ▶ | use vars qw ($AUTOLOAD); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | # If method wasn't found, delegates to DBH instance variable. | ||||||||||||||||||||
| 98 | # This way, instances of this class behaves like DBI connections. | ||||||||||||||||||||
| 99 | # | ||||||||||||||||||||
| 100 | # this needs improvement: non existent methods will provoke weird | ||||||||||||||||||||
| 101 | # messages from DBI objects, instead of Tao::DBI::db | ||||||||||||||||||||
| 102 | sub AUTOLOAD { | ||||||||||||||||||||
| 103 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 104 | 0 | my $meth = $AUTOLOAD; | |||||||||||||||||||
| 105 | 0 | $meth =~ s/.*:://; | |||||||||||||||||||
| 106 | 0 | return $self->{DBH}->$meth(@_); | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | ▶ | sub DESTROY { } | |||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 1 | 1; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | # May 29, 2003 | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||