line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::MySperqlOO; |
2
|
1
|
|
|
1
|
|
26812
|
use Class::Std; |
|
1
|
|
|
|
|
18630
|
|
|
1
|
|
|
|
|
11
|
|
3
|
1
|
|
|
1
|
|
1182
|
use Class::Std::Utils; |
|
1
|
|
|
|
|
3294
|
|
|
1
|
|
|
|
|
5
|
|
4
|
1
|
|
|
1
|
|
33
|
use strict; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
23
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
18
|
|
6
|
1
|
|
|
1
|
|
2145
|
use DBI; |
|
1
|
|
|
|
|
17135
|
|
|
1
|
|
|
|
|
66
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
10
|
use version; our $VERSION = qv('1.0.1'); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
{ |
11
|
|
|
|
|
|
|
my %dbh_of : ATTR(); |
12
|
|
|
|
|
|
|
my %sth_of : ATTR(); |
13
|
|
|
|
|
|
|
my %dbd_of : ATTR(); |
14
|
|
|
|
|
|
|
my %db_of : ATTR(); |
15
|
|
|
|
|
|
|
my %host_of : ATTR(); |
16
|
|
|
|
|
|
|
my %user_of : ATTR(); |
17
|
|
|
|
|
|
|
my %pass_of : ATTR(); |
18
|
|
|
|
|
|
|
my %dsn_of : ATTR(); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub BUILD { |
21
|
0
|
|
|
0
|
0
|
|
my ( $self, $ident, $arg_ref ) = @_; |
22
|
|
|
|
|
|
|
|
23
|
0
|
0
|
|
|
|
|
$dbd_of{$ident} = $arg_ref->{dbd} ? $arg_ref->{dbd} : 'mysql'; |
24
|
0
|
0
|
|
|
|
|
$db_of{$ident} = $arg_ref->{db} ? $arg_ref->{db} : $arg_ref->{database}; |
25
|
0
|
0
|
|
|
|
|
$host_of{$ident} = $arg_ref->{host} ? $arg_ref->{host} : 'localhost'; |
26
|
0
|
0
|
|
|
|
|
$user_of{$ident} = $arg_ref->{user} ? $arg_ref->{user} : ''; |
27
|
0
|
0
|
|
|
|
|
$pass_of{$ident} = $arg_ref->{pass} ? $arg_ref->{pass} : ''; |
28
|
0
|
0
|
|
|
|
|
$dsn_of{$ident} = $arg_ref->{dsn} ? $self->_build_dsn() : ''; |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
$self->connect(); |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
return; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
0
|
0
|
|
sub set_dbd { my ($self, $value) = @_; $dbd_of{ident $self} = $value; $self->_build_dsn(); return $self; } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
36
|
0
|
|
|
0
|
0
|
|
sub set_db { my ($self, $value) = @_; $db_of{ident $self} = $value; $self->_build_dsn(); return $self; } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
37
|
0
|
|
|
0
|
0
|
|
sub set_host { my ($self, $value) = @_; $host_of{ident $self} = $value; $self->_build_dsn(); return $self; } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
38
|
0
|
|
|
0
|
0
|
|
sub set_user { my ($self, $value) = @_; $user_of{ident $self} = $value; $self->_build_dsn(); return $self; } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
39
|
0
|
|
|
0
|
0
|
|
sub set_pass { my ($self, $value) = @_; $pass_of{ident $self} = $value; $self->_build_dsn(); return $self; } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
40
|
0
|
|
|
0
|
0
|
|
sub set_dsn { my ($self, $value) = @_; $dsn_of{ident $self} = $value; return $self; } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
0
|
0
|
|
sub get_user { my ($self) = @_; return $user_of{ident $self}; } |
|
0
|
|
|
|
|
|
|
43
|
0
|
|
|
0
|
0
|
|
sub get_pass { my ($self) = @_; return $pass_of{ident $self}; } |
|
0
|
|
|
|
|
|
|
44
|
0
|
0
|
|
0
|
0
|
|
sub get_dsn { my ($self) = @_; my ($ident) = ident $self; if (! $dsn_of{$ident} ) { $self->_build_dsn(); } return $dsn_of{$ident}; } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
0
|
|
|
sub _build_dsn { my ($self) = @_; my ($ident) = ident $self; $dsn_of{$ident} = 'DBI:' . $dbd_of{$ident} . ':database=' . $db_of{$ident} . ';host=' . $host_of{$ident}; } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub connect { |
49
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
50
|
0
|
|
|
|
|
|
my ($ident) = ident $self; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Either connect or quit with error |
53
|
0
|
|
|
|
|
|
$dbh_of{$ident} = DBI->connect( $self->get_dsn(), $self->get_user(), $self->get_pass() ); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Emit error string if connect failed |
56
|
0
|
0
|
|
|
|
|
if ($DBI::err) { warn "ERROR: $DBI::err : $DBI::errstr [DBConnect failed]"; } |
|
0
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
return $dbh_of{$ident}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub sqlexec { |
62
|
0
|
|
|
0
|
1
|
|
my ( $self, $sql, $type, $parameters ) = @_; |
63
|
|
|
|
|
|
|
|
64
|
0
|
0
|
|
|
|
|
$type = $type ? $type : '$sth'; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# Execute the sql with the given params |
67
|
0
|
|
|
|
|
|
$sth_of{ident $self} = $self->_sqlexecute( $sql, $parameters ); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# Check for type of return |
70
|
0
|
0
|
|
|
|
|
if ($type =~ m/sth/) { |
71
|
|
|
|
|
|
|
# Return the handle |
72
|
0
|
|
|
|
|
|
return $sth_of{ident $self}; |
73
|
|
|
|
|
|
|
} else { |
74
|
|
|
|
|
|
|
# Return the data per type |
75
|
0
|
|
|
|
|
|
return $self->_sqlfetch( $type ); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _sqlexecute { |
80
|
0
|
|
|
0
|
|
|
my ( $self, $sql, $parameters ) = @_; |
81
|
0
|
|
|
|
|
|
my $ident = ident($self); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Check parameters and prepare |
84
|
0
|
|
|
|
|
|
$sth_of{$ident} = $self->_sqlparse( $sql, $parameters ); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Execute |
87
|
0
|
0
|
|
|
|
|
$sth_of{$ident}->execute() or return "ERROR: [database.pl:_sqlexecute]$DBI::err : $DBI::errstr [_sqlexecute failed]"; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# Return statement handle |
90
|
0
|
|
|
|
|
|
return $sth_of{ident $self}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _sqlparse { |
94
|
0
|
|
|
0
|
|
|
my ( $self, $test, $parameters) = @_; |
95
|
0
|
|
|
|
|
|
my $ident = ident($self); |
96
|
|
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
|
if ($self->_is_handle($test)) { |
98
|
|
|
|
|
|
|
# Handle |
99
|
0
|
0
|
|
|
|
|
$sth_of{$ident} = $dbh_of{$ident}->prepare($test->{Statement}) or return "ERROR: Could not prepare handle [_sqlparse failed]"; |
100
|
|
|
|
|
|
|
} else { |
101
|
|
|
|
|
|
|
# SQL string |
102
|
|
|
|
|
|
|
# Get database handle |
103
|
0
|
0
|
|
|
|
|
if (!($dbh_of{$ident}->{Active})) { $dbh_of{$ident} = &DBConnect; } |
|
0
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# Prepare the statement |
106
|
0
|
0
|
|
|
|
|
$sth_of{$ident} = $dbh_of{$ident}->prepare($test) or return "ERROR: Could not prepare statement [_sqlparse failed]"; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
return $sth_of{ident $self}; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub _sqlfetch { |
113
|
0
|
|
|
0
|
|
|
my ( $self, $type ) = @_; |
114
|
|
|
|
|
|
|
|
115
|
0
|
0
|
|
|
|
|
if ($type eq '\@@') { $self->_fetch_all_array_ref(); } |
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
elsif ($type eq '\@') { $self->_fetch_row_array_ref(); } |
117
|
0
|
|
|
|
|
|
elsif ($type eq '@') { $self->_fetch_row_array(); } |
118
|
0
|
|
|
|
|
|
elsif ($type eq '\%') { $self->_fetch_row_hash_ref(); } |
119
|
0
|
|
|
|
|
|
else { return "ERROR: type($type) [_sqlfetch failed]"; } |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub _fetch_all_array_ref { |
123
|
0
|
|
|
0
|
|
|
my ( $self ) = @_; |
124
|
0
|
|
|
|
|
|
my ($ref) = $sth_of{ident $self}->fetchall_arrayref; |
125
|
0
|
0
|
|
|
|
|
if ($DBI::err) { return "ERROR: Fetching Error($DBI::err) $DBI::errstr [_fetch_all_array_ref failed]"; } |
|
0
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
return $ref; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub _fetch_row_array_ref { |
130
|
0
|
|
|
0
|
|
|
my ( $self ) = @_; |
131
|
0
|
|
|
|
|
|
my ($ref) = $sth_of{ident $self}->fetchrow_arrayref; |
132
|
0
|
0
|
|
|
|
|
if ($DBI::err) { return "ERROR: Fetching Error($DBI::err) $DBI::errstr [_fetch_row_array_ref failed]"; } |
|
0
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
return $ref; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub _fetch_row_array { |
137
|
0
|
|
|
0
|
|
|
my ( $self ) = @_; |
138
|
0
|
|
|
|
|
|
my (@array) = $sth_of{ident $self}->fetchrow_array; |
139
|
0
|
0
|
|
|
|
|
if ($DBI::err) { return "ERROR: Fetching Error($DBI::err) $DBI::errstr [_fetch_row_array failed]"; } |
|
0
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
return @array; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _fetch_row_hash_ref { |
144
|
0
|
|
|
0
|
|
|
my ( $self ) = @_; |
145
|
0
|
|
|
|
|
|
my ($ref) = $sth_of{ident $self}->fetchrow_hashref; |
146
|
0
|
0
|
|
|
|
|
if ($DBI::err) { return "ERROR: Fetching Error($DBI::err) $DBI::errstr [_fetch_row_hash_ref failed]"; } |
|
0
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
|
return $ref; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub get_row_count { |
151
|
0
|
|
|
0
|
1
|
|
my $self = shift @_; |
152
|
0
|
|
|
|
|
|
my $ref = $_[0]; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# Check to see if it is a non array reference |
155
|
0
|
0
|
0
|
|
|
|
if (($ref =~ /SCALAR/) or ($ref =~ /HASH/)) { |
156
|
0
|
0
|
|
|
|
|
if ($ref =~ /HASH/) { |
157
|
|
|
|
|
|
|
# It is a reference to a hash |
158
|
|
|
|
|
|
|
# Return the number of keys for hash |
159
|
0
|
|
|
|
|
|
return keys(%$ref); |
160
|
|
|
|
|
|
|
} else { |
161
|
|
|
|
|
|
|
# It is a scalar |
162
|
|
|
|
|
|
|
# Return 1 for scalar |
163
|
0
|
|
|
|
|
|
return 1; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
} else { |
166
|
0
|
0
|
|
|
|
|
if ($ref =~ /ARRAY/) { |
167
|
0
|
0
|
|
|
|
|
if (@{ $ref->[0] }) { |
|
0
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
return @$ref; |
169
|
|
|
|
|
|
|
} else { |
170
|
0
|
|
|
|
|
|
return 0; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} else { |
173
|
|
|
|
|
|
|
# It is a simple array or scalar |
174
|
0
|
|
|
|
|
|
return 1; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub get_col_count { |
180
|
0
|
|
|
0
|
0
|
|
my $self = shift @_; |
181
|
0
|
|
|
|
|
|
my $ref = $_[0]; |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
# Check to see if it is a non array reference |
184
|
0
|
0
|
0
|
|
|
|
if (($ref =~ /SCALAR/) or ($ref =~ /HASH/)) { |
185
|
0
|
0
|
|
|
|
|
if ($ref =~ /HASH/) { |
186
|
|
|
|
|
|
|
# It is a reference to a hash |
187
|
|
|
|
|
|
|
# Return the number of keys for hash |
188
|
0
|
|
|
|
|
|
return keys(%$ref); |
189
|
|
|
|
|
|
|
} else { |
190
|
|
|
|
|
|
|
# It is a scalar |
191
|
|
|
|
|
|
|
# Return 1 for scalar |
192
|
0
|
|
|
|
|
|
return 1; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
} else { |
195
|
|
|
|
|
|
|
# Evaluates the length of the first row |
196
|
|
|
|
|
|
|
# Only non zero for actual array of arrays |
197
|
0
|
0
|
|
|
|
|
if (@{ $ref->[0] }) { |
|
0
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
# It is a reference to an array of arrays |
199
|
|
|
|
|
|
|
# Return the length of the first row |
200
|
0
|
|
|
|
|
|
return @{ $ref->[0] }; |
|
0
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
} else { |
202
|
|
|
|
|
|
|
# Regular arrays, array references, and |
203
|
|
|
|
|
|
|
# scalars land here |
204
|
|
|
|
|
|
|
# An array would have at least two elements |
205
|
|
|
|
|
|
|
# A reference or scalar would have one |
206
|
0
|
0
|
|
|
|
|
if (@_ > 1) { |
207
|
|
|
|
|
|
|
# It is an array |
208
|
|
|
|
|
|
|
# Return the length of the input array |
209
|
0
|
|
|
|
|
|
return @_; |
210
|
|
|
|
|
|
|
} else { |
211
|
0
|
0
|
|
|
|
|
if (@$ref) { |
212
|
|
|
|
|
|
|
# It is a reference to an array |
213
|
|
|
|
|
|
|
# Return the length of the input array |
214
|
0
|
|
|
|
|
|
return @$ref; |
215
|
|
|
|
|
|
|
} else { |
216
|
|
|
|
|
|
|
# It is a scalar |
217
|
|
|
|
|
|
|
# Return 1 for scalar |
218
|
0
|
|
|
|
|
|
return 1; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
0
|
|
|
0
|
1
|
|
sub get_field_names { my ( $self ) = @_; my $ref = $sth_of{ident $self}->{NAME}; return @$ref; } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
|
227
|
0
|
|
|
0
|
|
|
sub _is_handle { my $self = shift @_; return ($_[0] =~ m/^DBI/); } |
|
0
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
1; # Magic true value required at end of module |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
__END__ |