line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::SQLHandler; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
33
|
use warnings; |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
186
|
|
4
|
6
|
|
|
6
|
|
34
|
use strict; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
221
|
|
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
269
|
use Abstract::Meta::Class ':all'; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
794
|
|
7
|
6
|
|
|
6
|
|
36
|
use vars qw($VERSION); |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
4779
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
$VERSION = 0.03; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
DBIx::SQLHandler - Sql statement handler. |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use DBIx::SQLHandler |
18
|
|
|
|
|
|
|
my $sql_handler = new DBIx::SQLHandler( |
19
|
|
|
|
|
|
|
name => 'emp_ins', |
20
|
|
|
|
|
|
|
connection => $connection, |
21
|
|
|
|
|
|
|
sql => "INSERT INTO emp(empno, ename) VALUES(?, ?)" |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
$sql_handler->execute(1, 'Smith'); |
24
|
|
|
|
|
|
|
$sql_handler->execute(2, 'Witek'); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $sql_handler = new DBIx::SQLHandler( |
28
|
|
|
|
|
|
|
name => 'emp_upd', |
29
|
|
|
|
|
|
|
connection => $connection, |
30
|
|
|
|
|
|
|
sql => "UPDATE emp SET ename = ? WHERE empno = ?" |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
$sql_handler->execute('Smith2',1); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
or |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
use DBIx::Connection; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
... |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $sql_handler = $connection->sql_handler( |
41
|
|
|
|
|
|
|
name => 'emp_ins' |
42
|
|
|
|
|
|
|
sql => "INSERT INTO emp(empno, ename) VALUES(?, ?)", |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
$sql_handler->execute(1, 'Smith'); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 DESCRIPTION |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Represents wrapper for sql statments. |
50
|
|
|
|
|
|
|
It manages sql statement's information within its state. |
51
|
|
|
|
|
|
|
If an error occurs on any stage (prepare, execute, binding) you will get full information |
52
|
|
|
|
|
|
|
including connection name, sql, binding variables. |
53
|
|
|
|
|
|
|
Caching statements (if using name). |
54
|
|
|
|
|
|
|
It gathers performance statistics (optionaly). |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head1 EXPORT |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
None. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head2 ATTRIBUTES |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=over |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item name |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Statement name. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=cut |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
has '$.name'; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=item connection |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Database connection. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
has '$.connection' => (associated_class => 'DBIx::Connection'); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=item sth |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Cursor handler. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
has '$.sth'; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=item sql |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
SQL text for cursor. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=cut |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
has '$.sql'; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=back |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head2 METHODS |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=over |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=item initialise |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Prepares the sql statement. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub initialise { |
112
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
113
|
0
|
|
|
|
|
|
$self->prepare(); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=item prepare |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Prepare the statement. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub prepare { |
124
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
125
|
0
|
|
|
|
|
|
my $connection = $self->connection; |
126
|
0
|
|
|
|
|
|
$connection->record_action_start_time; |
127
|
0
|
0
|
|
|
|
|
my $sth = $connection->dbh->prepare($self->sql) |
128
|
|
|
|
|
|
|
or $self->error_handler; |
129
|
0
|
|
|
|
|
|
$connection->record_action_end_time($self->sql); |
130
|
0
|
|
|
|
|
|
$self->set_sth($sth); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item execute |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Executes the statement. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub execute { |
141
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
142
|
0
|
|
|
|
|
|
my $connection = $self->connection; |
143
|
0
|
|
|
|
|
|
my $sth = $self->sth; |
144
|
0
|
|
|
|
|
|
$connection->record_action_start_time; |
145
|
0
|
0
|
|
|
|
|
$sth->execute(@_) |
146
|
|
|
|
|
|
|
or $self->error_handler(\@_); |
147
|
0
|
|
|
|
|
|
$connection->record_action_end_time($self->sql); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item bind_columns |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Binds column to the statement. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=cut |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub bind_columns { |
158
|
0
|
|
|
0
|
1
|
|
my ($self, $bind_results) = @_; |
159
|
0
|
0
|
|
|
|
|
if ($bind_results) { |
160
|
0
|
|
|
|
|
|
my $sth = $self->sth; |
161
|
0
|
|
|
|
|
|
$sth->bind_columns( |
162
|
|
|
|
|
|
|
ref($bind_results) eq 'HASH' |
163
|
0
|
|
|
|
|
|
? \(@{$bind_results}{@{$sth->{NAME_lc}}}) |
|
0
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
: ref($bind_results) eq 'ARRAY' |
165
|
0
|
0
|
|
|
|
|
? \(@{$bind_results}[0..$#{$sth->{NAME_lc}}]) |
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
166
|
|
|
|
|
|
|
: () |
167
|
|
|
|
|
|
|
) or $self->error_handler; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item bind_params_inout |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Bind parameters to the statement. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub bind_params_inout { |
179
|
0
|
|
|
0
|
1
|
|
my ($self, $params) = @_; |
180
|
0
|
|
|
|
|
|
my $sth = $self->sth; |
181
|
0
|
0
|
|
|
|
|
eval { |
182
|
|
|
|
|
|
|
$sth->bind_param_inout(":".$_, \$params->{$_}, 32000) |
183
|
0
|
|
|
|
|
|
for keys %$params; |
184
|
0
|
|
|
|
|
|
$self; |
185
|
|
|
|
|
|
|
} or $self->error_handler; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=item bind_params |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Bind parameters to the statement. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=cut |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub bind_params { |
196
|
0
|
|
|
0
|
1
|
|
my ($self, $params) = @_; |
197
|
0
|
|
|
|
|
|
my $sth = $self->sth; |
198
|
0
|
0
|
|
|
|
|
eval { |
199
|
|
|
|
|
|
|
$sth->bind_param(":".$_, $params->{$_}) |
200
|
0
|
|
|
|
|
|
for keys %$params; |
201
|
0
|
|
|
|
|
|
$self; |
202
|
|
|
|
|
|
|
} or $self->error_handler; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=item error_handler |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Returns error messagem, takes error message, and optionally bind variables. |
210
|
|
|
|
|
|
|
If bind variables are passed in the sql's place holders are replaced with the bind_variables. |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=cut |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
sub error_handler { |
215
|
0
|
|
|
0
|
1
|
|
my ($self, $bind_params) = @_; |
216
|
0
|
|
|
|
|
|
my $connection = $self->connection; |
217
|
0
|
|
|
|
|
|
my $sql = $self->sql; |
218
|
0
|
0
|
|
|
|
|
if (defined($bind_params)) { |
219
|
0
|
0
|
|
|
|
|
$sql =~ s/\?/'$_'/ |
|
|
0
|
|
|
|
|
|
220
|
0
|
|
|
|
|
|
for map {ref($_) eq 'CODE' ? $_->() : defined $_ ? $_ : ''} @$bind_params |
221
|
|
|
|
|
|
|
} |
222
|
0
|
|
|
|
|
|
$connection->error_handler($sql); |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=item cleanup |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=cut |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
sub cleanup { |
231
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
232
|
0
|
|
|
|
|
|
$self->finish; |
233
|
0
|
|
|
|
|
|
$self->set_sth; |
234
|
0
|
|
|
|
|
|
$self->set_dbh; |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
1; |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
__END__ |