line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#********************************************************************* |
2
|
|
|
|
|
|
|
#*** lib/ResourcePool/Command/DBI/Common.pm |
3
|
|
|
|
|
|
|
#*** Copyright (c) 2004 by Markus Winand |
4
|
|
|
|
|
|
|
#*** $Id: Common.pm,v 1.3 2004/05/02 07:48:55 mws Exp $ |
5
|
|
|
|
|
|
|
#********************************************************************* |
6
|
|
|
|
|
|
|
package ResourcePool::Command::DBI::Common; |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
93
|
use ResourcePool::Command; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
69
|
|
9
|
3
|
|
|
3
|
|
15
|
use ResourcePool::Command::NoFailoverException; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
52
|
|
10
|
3
|
|
|
3
|
|
15
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
76
|
|
11
|
3
|
|
|
3
|
|
4901
|
use DBI; |
|
3
|
|
|
|
|
44805
|
|
|
3
|
|
|
|
|
175
|
|
12
|
3
|
|
|
3
|
|
30
|
use vars qw($VERSION); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
3221
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
$VERSION = "1.0101"; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new($$;$@) { |
17
|
31
|
|
|
31
|
0
|
5055
|
my $proto = shift; |
18
|
31
|
|
33
|
|
|
139
|
my $class = ref($proto) || $proto; |
19
|
31
|
|
|
|
|
49
|
my $self = {}; |
20
|
31
|
|
|
|
|
48
|
my $sql = shift; |
21
|
|
|
|
|
|
|
|
22
|
31
|
|
|
|
|
85
|
bless($self, $class); |
23
|
31
|
|
|
|
|
102
|
$self->_setOptions({}); |
24
|
|
|
|
|
|
|
|
25
|
31
|
100
|
66
|
|
|
166
|
if (defined $sql && $sql ne '') { |
26
|
25
|
100
|
100
|
|
|
316
|
if (scalar(@_) == 0 || ref($_[0]) eq 'HASH' || scalar(@_) %2 == 0) { |
|
|
|
100
|
|
|
|
|
27
|
|
|
|
|
|
|
# if these things are given, the first argument is a SQL |
28
|
24
|
|
|
|
|
212
|
$self->_setSQL($sql); |
29
|
|
|
|
|
|
|
|
30
|
24
|
100
|
|
|
|
60
|
if (defined $_[0]) { |
31
|
11
|
100
|
|
|
|
26
|
if (ref($_[0]) eq 'HASH') { |
32
|
6
|
|
|
|
|
29
|
$self->_setBindArgs(shift); |
33
|
6
|
100
|
|
|
|
17
|
if (defined $_[0]) { |
34
|
5
|
|
|
|
|
21
|
$self->_setOptions({@_}); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
} else { |
37
|
5
|
|
|
|
|
216
|
$self->_setOptions({@_}); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} else { |
41
|
|
|
|
|
|
|
# otherwise, its part of an option |
42
|
1
|
|
|
|
|
4
|
$self->_setOptions({($sql, @_)}); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
31
|
|
|
|
|
98
|
return $self; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _setSQL($$) { |
51
|
24
|
|
|
24
|
|
36
|
my ($self, $sql) = @_; |
52
|
24
|
|
|
|
|
103
|
$self->{sql} = $sql; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub getSQL($) { |
56
|
7
|
|
|
7
|
0
|
1901
|
my ($self) = @_; |
57
|
7
|
|
|
|
|
32
|
return $self->{sql}; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub _setBindArgs($$) { |
61
|
6
|
|
|
6
|
|
10
|
my ($self, $bindargs) = @_; |
62
|
6
|
|
|
|
|
14
|
$self->{bindargs} = $bindargs; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
sub _getBindArgs($) { |
65
|
7
|
|
|
7
|
|
15
|
my ($self, $sql) = @_; |
66
|
7
|
|
|
|
|
36
|
return $self->{bindargs}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _setOptions($$) { |
70
|
42
|
|
|
42
|
|
64
|
my ($self, $options) = @_; |
71
|
|
|
|
|
|
|
# the defaults |
72
|
42
|
|
|
|
|
431
|
my %options = ( |
73
|
|
|
|
|
|
|
prepare_cached => 0 |
74
|
|
|
|
|
|
|
); |
75
|
42
|
|
|
|
|
316
|
%options = ((%options), %{$options}); |
|
42
|
|
|
|
|
367
|
|
76
|
42
|
|
|
|
|
165
|
$self->{options} = \%options; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _getOptions($) { |
80
|
7
|
|
|
7
|
|
44
|
my ($self) = @_; |
81
|
7
|
|
|
|
|
64
|
return $self->{options}; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _getOptPrepareCached($) { |
85
|
7
|
|
|
7
|
|
16
|
my ($self) = @_; |
86
|
7
|
|
|
|
|
35
|
return $self->{options}->{prepare_cached}; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub getSQLfromargs($$) { |
90
|
0
|
|
|
0
|
0
|
|
my ($self, $argsref) = @_; |
91
|
0
|
|
|
|
|
|
my $sql = $self->getSQL(); |
92
|
|
|
|
|
|
|
|
93
|
0
|
0
|
0
|
|
|
|
if (! defined $sql && ! ref($argsref->[0])) { |
94
|
0
|
|
|
|
|
|
$sql = shift @{$argsref}; |
|
0
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
|
if (! defined $sql) { |
98
|
0
|
|
|
|
|
|
die ResourcePool::Command::NoFailoverException->new( |
99
|
|
|
|
|
|
|
ref($self) . ': ' |
100
|
|
|
|
|
|
|
. 'you have to specify a SQL statement' |
101
|
|
|
|
|
|
|
); |
102
|
|
|
|
|
|
|
} |
103
|
0
|
|
|
|
|
|
return $sql; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub prepare($$) { |
107
|
0
|
|
|
0
|
0
|
|
my ($self, $dbh, $sql) = @_; |
108
|
0
|
|
|
|
|
|
my $sth; |
109
|
|
|
|
|
|
|
|
110
|
0
|
0
|
|
|
|
|
if ($self->_getOptPrepareCached()) { |
111
|
0
|
|
|
|
|
|
$sth = $dbh->prepare_cached($sql); |
112
|
|
|
|
|
|
|
} else { |
113
|
0
|
|
|
|
|
|
$sth = $dbh->prepare($sql); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
return $sth; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub bind($$) { |
120
|
0
|
|
|
0
|
0
|
|
my ($self, $sth, $argsref) = @_; |
121
|
|
|
|
|
|
|
|
122
|
0
|
0
|
|
|
|
|
if (scalar(@{$argsref}) > 0) { |
|
0
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
my $argshash; |
124
|
0
|
0
|
|
|
|
|
if (ref($argsref->[0]) eq 'HASH') { |
125
|
|
|
|
|
|
|
# named args syntax |
126
|
0
|
|
|
|
|
|
$argshash = $argsref->[0]; |
127
|
|
|
|
|
|
|
} else { |
128
|
|
|
|
|
|
|
# ordered args syntax |
129
|
0
|
|
|
|
|
|
my %argshash; |
130
|
0
|
|
|
|
|
|
$argshash = {}; |
131
|
0
|
|
|
|
|
|
my $arg; |
132
|
0
|
|
|
|
|
|
my $i = 1; |
133
|
0
|
|
|
|
|
|
foreach $arg (@{$argsref}) { |
|
0
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
$argshash{$i} = $arg; |
135
|
0
|
|
|
|
|
|
++$i; |
136
|
|
|
|
|
|
|
} |
137
|
0
|
|
|
|
|
|
$argshash = \%argshash; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# bind parameters |
141
|
0
|
|
|
|
|
|
my $bindargs = $self->_getBindArgs(); |
142
|
0
|
|
|
|
|
|
my ($name); |
143
|
0
|
|
|
|
|
|
foreach $name (keys(%{$argshash})) { |
|
0
|
|
|
|
|
|
|
144
|
0
|
0
|
|
|
|
|
if (defined $bindargs->{$name}->{max_len}) { |
145
|
|
|
|
|
|
|
# in that case, $value is required to be a ref |
146
|
0
|
|
|
|
|
|
$sth->bind_param_inout($name |
147
|
|
|
|
|
|
|
, $argshash->{$name} |
148
|
|
|
|
|
|
|
, $bindargs->{$name}->{max_len} |
149
|
|
|
|
|
|
|
, $bindargs->{$name}->{type} |
150
|
|
|
|
|
|
|
); |
151
|
|
|
|
|
|
|
} else { |
152
|
0
|
|
|
|
|
|
$sth->bind_param($name |
153
|
|
|
|
|
|
|
, $argshash->{$name} |
154
|
|
|
|
|
|
|
, $bindargs->{$name}->{type}); |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub info($) { |
161
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
162
|
0
|
|
|
|
|
|
my $sql = $self->getSQL(); |
163
|
0
|
0
|
|
|
|
|
if (defined $sql) { |
164
|
0
|
|
|
|
|
|
return ref($self) . ": '" . $sql . "'"; |
165
|
|
|
|
|
|
|
} else { |
166
|
0
|
|
|
|
|
|
return ref($self) . ": no SQL pre-declared"; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
1; |