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