line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Tao::DBI::db; |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
14
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
5
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
19
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
71
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Exporter; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
11
|
|
|
|
|
|
|
our @EXPORT = qw(); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
2431
|
use DBI; |
|
1
|
|
|
|
|
18551
|
|
|
1
|
|
|
|
|
87
|
|
16
|
1
|
|
|
1
|
|
704
|
use Tao::DBI::st; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
73
|
|
17
|
1
|
|
|
1
|
|
517
|
use Tao::DBI::st_deep; |
|
1
|
|
|
|
|
21
|
|
|
1
|
|
|
|
|
403
|
|
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
|
|
|
0
|
1
|
|
my ($proto, $args) = @_; |
28
|
0
|
|
0
|
|
|
|
my $class = ref($proto) || $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
|
|
|
0
|
0
|
|
my ($self, $args) = @_; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
$self->{NAME} = $args->{name}; |
45
|
0
|
|
0
|
|
|
|
$self->{URL} = $args->{dsn} || $args->{url}; |
46
|
0
|
|
|
|
|
|
$self->{USER} = $args->{user}; |
47
|
0
|
|
|
|
|
|
$self->{PASS} = $args->{password}; |
48
|
0
|
0
|
|
|
|
|
unless (exists $args->{FetchHashKeyName}) { |
49
|
0
|
|
|
|
|
|
$args->{FetchHashKeyName} = 'NAME_lc'; |
50
|
|
|
|
|
|
|
} |
51
|
0
|
0
|
|
|
|
|
unless (exists $args->{AutoCommit}) { |
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
|
0
|
|
|
|
|
if ($dbh) { |
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
|
|
|
0
|
0
|
|
my $self = shift; |
70
|
0
|
|
|
|
|
|
my $sql; |
71
|
0
|
0
|
|
|
|
|
$sql = shift unless ref $_[0]; # first non-ref arg |
72
|
0
|
|
0
|
|
|
|
my $args = shift || {}; |
73
|
0
|
|
0
|
|
|
|
my $st_type = $args->{type} || 'plain'; |
74
|
0
|
0
|
|
|
|
|
if ($st_type eq 'plain') { |
|
|
0
|
|
|
|
|
|
75
|
|
|
|
|
|
|
return new Tao::DBI::st({ |
76
|
|
|
|
|
|
|
($sql ? (sql => $sql) : ()), |
77
|
0
|
0
|
|
|
|
|
dbh => $self->{DBH}, %$args }); |
78
|
|
|
|
|
|
|
} elsif ($st_type eq 'deep') { |
79
|
|
|
|
|
|
|
return new Tao::DBI::st_deep({ |
80
|
|
|
|
|
|
|
($sql ? (sql => $sql) : ()), |
81
|
0
|
0
|
|
|
|
|
dbh => $self->{DBH}, %$args }); |
82
|
|
|
|
|
|
|
} else { |
83
|
0
|
|
|
|
|
|
die "unknown statement type: $st_type\n"; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
1
|
|
|
1
|
|
5
|
use vars qw ($AUTOLOAD); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
127
|
|
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
|
|
|
0
|
|
|
my $self = shift; |
96
|
0
|
|
|
|
|
|
my $meth = $AUTOLOAD; |
97
|
0
|
|
|
|
|
|
$meth =~ s/.*:://; |
98
|
0
|
|
|
|
|
|
return $self->{DBH}->$meth(@_); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
0
|
|
|
sub DESTROY {} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# May 29, 2003 |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
__END__ |