line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::Connection::MySQL::PLSQL; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
5164
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
39
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
7
|
use Abstract::Meta::Class ':all'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
203
|
|
7
|
1
|
|
|
1
|
|
7
|
use Carp 'confess'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
63
|
|
8
|
1
|
|
|
1
|
|
5
|
use base qw(DBIx::PLSQLHandler); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
115
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
7
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
825
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$VERSION = 0.02; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
DBIx::Connection::MySQL::PLSQL - PLSQL handler |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use DBIx::PLSQLHandler; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $plsql_handler = new DBIx::PLSQLHandler( |
23
|
|
|
|
|
|
|
name => 'test_proc', |
24
|
|
|
|
|
|
|
connection => $connection, |
25
|
|
|
|
|
|
|
plsql => " |
26
|
|
|
|
|
|
|
DECLARE |
27
|
|
|
|
|
|
|
var1 INT; |
28
|
|
|
|
|
|
|
BEGIN |
29
|
|
|
|
|
|
|
SET var1 := :var2 + :var3; |
30
|
|
|
|
|
|
|
END;", |
31
|
|
|
|
|
|
|
bind_variables => { |
32
|
|
|
|
|
|
|
var2 => {type => 'SQL_INTEGER'}, |
33
|
|
|
|
|
|
|
var3 => {type => 'SQL_INTEGER'} |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
$plsql_handler->execute(var2 => 12, var3 => 8); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
or |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
use DBIx::Connection; |
41
|
|
|
|
|
|
|
.... |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $plsql_handler = $connection->plsql_handler( |
44
|
|
|
|
|
|
|
name => 'test_proc', |
45
|
|
|
|
|
|
|
connection => $connection, |
46
|
|
|
|
|
|
|
plsql => " |
47
|
|
|
|
|
|
|
DECLARE |
48
|
|
|
|
|
|
|
var1 INT; |
49
|
|
|
|
|
|
|
BEGIN |
50
|
|
|
|
|
|
|
:var1 := :var2 + :var3; |
51
|
|
|
|
|
|
|
END;", |
52
|
|
|
|
|
|
|
bind_variables => { |
53
|
|
|
|
|
|
|
var1 => {type => 'SQL_INTEGER'}, |
54
|
|
|
|
|
|
|
var2 => {type => 'SQL_INTEGER'}, |
55
|
|
|
|
|
|
|
var3 => {type => 'SQL_INTEGER'} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $result_set = $plsql_handler->execute(var2 => 12, var3 => 8); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head2 METHODS |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=over |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=cut |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
{ |
69
|
|
|
|
|
|
|
my %SQL = ( |
70
|
|
|
|
|
|
|
find_function => 'SELECT routine_definition FROM information_schema.ROUTINES WHERE routine_schema = ? AND routine_name = ? ', |
71
|
|
|
|
|
|
|
); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item sql_defintion |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Returns sql statment definitio, Takes sql name as parameter. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub sql_defintion { |
81
|
0
|
|
|
0
|
1
|
|
my ($self, $name) = @_; |
82
|
0
|
|
|
|
|
|
$SQL{$name}; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=item prepare |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Prepares plsql block |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub prepare { |
94
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
95
|
0
|
|
|
|
|
|
$self->initialise_plsql_block(); |
96
|
0
|
|
|
|
|
|
$self->initialise_sql(); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=item initialise_plsql_block |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=cut |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub initialise_plsql_block { |
105
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
106
|
0
|
|
|
|
|
|
my $connection = $self->connection; |
107
|
0
|
0
|
|
|
|
|
if($self->is_block_changed($connection->username, $self->plsql_block_name)) { |
108
|
0
|
|
|
|
|
|
my $plsql_block_wrapper = $self->plsql_block_wrapper; |
109
|
0
|
|
|
|
|
|
$self->connection->do($plsql_block_wrapper); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item drop_plsql_block |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Removes plsql block wrapper |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub drop_plsql_block { |
120
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
121
|
0
|
|
|
|
|
|
$self->connection->do("DROP PROCEDURE IF EXISTS " . $self->plsql_block_name); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=item plsql_block_wrapper |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Generates plsql procedure. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub plsql_block_wrapper { |
132
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
133
|
0
|
|
|
|
|
|
"CREATE PROCEDURE " . $self->plsql_block_name . '(' . $self->plsql_block_declaration . ')' |
134
|
|
|
|
|
|
|
. $self->block_source; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item initialise_sql |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=cut |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub initialise_sql { |
143
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
144
|
0
|
|
|
|
|
|
my @binded_out_variables = $self->binded_out_variables; |
145
|
0
|
|
|
|
|
|
my $result = join (",", map { '@' . $_ . ' AS ' . $_ } @binded_out_variables); |
|
0
|
|
|
|
|
|
|
146
|
0
|
0
|
|
|
|
|
$self->set_sql(@binded_out_variables ? "SELECT $result" : ''); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item execute |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Executes plsql block |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub execute { |
157
|
0
|
|
|
0
|
1
|
|
my ($self, %bind_variables) = @_; |
158
|
0
|
|
|
|
|
|
my $connection = $self->connection; |
159
|
0
|
|
|
|
|
|
$self->bind_parameters(\%bind_variables); |
160
|
0
|
|
|
|
|
|
my $sql = $self->sql; |
161
|
0
|
0
|
|
|
|
|
return $connection->record($sql) if $sql ; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=item bind_parameters |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=cut |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub bind_parameters { |
170
|
0
|
|
|
0
|
1
|
|
my ($self, $bind_variables) = @_; |
171
|
0
|
|
|
|
|
|
my $connection = $self->connection; |
172
|
0
|
|
|
|
|
|
my @binded_out_variables = $self->binded_out_variables; |
173
|
0
|
|
|
|
|
|
foreach my $variable (@binded_out_variables) { |
174
|
0
|
|
|
|
|
|
$connection->execute_statement('SET @' . $variable . ' = ?', $bind_variables->{$variable}); |
175
|
|
|
|
|
|
|
} |
176
|
0
|
|
|
|
|
|
my @bind_in_variables = $self->bind_in_variables; |
177
|
0
|
|
|
|
|
|
my $call_params = join(",", (map { '?' } @bind_in_variables), (map { '@' . $_ } @binded_out_variables)); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
my @bind_variables = map { $bind_variables->{$_} } @bind_in_variables; |
|
0
|
|
|
|
|
|
|
179
|
0
|
|
|
|
|
|
my $sql = "CALL " . $self->plsql_block_name . "($call_params)"; |
180
|
0
|
|
|
|
|
|
$connection->execute_statement($sql, @bind_variables); |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=item parsed_plsql |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Parses plsql code and replaces :var to var |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=cut |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub parsed_plsql { |
192
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
193
|
0
|
|
|
|
|
|
my $plsql = $self->plsql; |
194
|
0
|
|
|
|
|
|
my $bind_variables = $self->bind_variables; |
195
|
0
|
|
|
|
|
|
foreach my $variable (sort keys %$bind_variables) { |
196
|
0
|
|
|
|
|
|
$plsql =~ s/:$variable\s*:=/SET $variable :=/g; |
197
|
0
|
|
|
|
|
|
$plsql =~ s/:$variable/$variable/g; |
198
|
|
|
|
|
|
|
} |
199
|
0
|
|
|
|
|
|
$plsql; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
{ |
203
|
|
|
|
|
|
|
=item type_map |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
mapping between DBI and database types. |
206
|
|
|
|
|
|
|
The following mapping is defined: |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
SQL_DECIMAL => 'NUMERIC', |
209
|
|
|
|
|
|
|
SQL_VARCHAR => 'VARCHAR', |
210
|
|
|
|
|
|
|
SQL_DATE =>'DATE', |
211
|
|
|
|
|
|
|
SQL_CHAR =>'CHAR', |
212
|
|
|
|
|
|
|
SQL_DOUBLE =>'NUMERIC', |
213
|
|
|
|
|
|
|
SQL_INTEGER =>'INT', |
214
|
|
|
|
|
|
|
SQL_BOOLEAN =>'BOOLEAN', |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=cut |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
my %type_map = ( |
219
|
|
|
|
|
|
|
SQL_DECIMAL => 'NUMERIC', |
220
|
|
|
|
|
|
|
SQL_VARCHAR => 'VARCHAR', |
221
|
|
|
|
|
|
|
SQL_DATE =>'DATE', |
222
|
|
|
|
|
|
|
SQL_CHAR =>'CHAR', |
223
|
|
|
|
|
|
|
SQL_DOUBLE =>'NUMERIC', |
224
|
|
|
|
|
|
|
SQL_INTEGER =>'INT', |
225
|
|
|
|
|
|
|
SQL_BOOLEAN =>'BOOLEAN', |
226
|
|
|
|
|
|
|
); |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=item get_type |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Returns |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=cut |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
sub get_type { |
236
|
0
|
|
|
0
|
1
|
|
my ($class, $type) = @_; |
237
|
0
|
|
|
|
|
|
$type_map{$type}; |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
1; |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
__END__ |