Connection/MySQL/PLSQL.pm

filter: Statementstmt 34.2% 24 / 70 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 37.5% 6 / 16 Podpod 100% 10 / 10 totaltotal 39.2% 40 / 102 CC 6 SCAR 12.8
CC
6
Cov
32%
initialise_plsql_block
6.0
initialise_sql
6.0
execute
6.0
CRAP 3.6
« Connection/PostgreSQL/PLSQL.pm ↑ Summary Connection/Oracle/PLSQL.pm »
1 package DBIx::Connection::MySQL::PLSQL;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Abstract::Meta::Class ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use base qw(DBIx::PLSQLHandler);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 $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 1 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 0 ▶ sub sql_defintion {
Subroutine sql_defintion: not called CC=1 SCAR=6.9
Pod documented
81 0 my ($self, $name) = @_;
82 0 $SQL{$name};
83 }
84 }
85  
86  
87 =item prepare
88  
89 Prepares plsql block
90  
91 =cut
92  
93 1 ▶sub prepare {
Subroutine prepare: not called CC=1 SCAR=3.5
Pod documented
94 0 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 0 ▶sub initialise_plsql_block {
Subroutine initialise_plsql_block: not called CC=2 SCAR=17.9
Pod documented
105 0 my ($self) = @_;
106 0 my $connection = $self->connection;
107 0 ▶ if($self->is_block_changed($connection->username, $self->plsql_block_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_block_changed($connection->username, $self->plsql_block_name)) 0 0 0
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 0 ▶sub drop_plsql_block {
Subroutine drop_plsql_block: not called CC=1 SCAR=6.9
Pod documented
120 0 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 0 ▶sub plsql_block_wrapper {
Subroutine plsql_block_wrapper: not called CC=1 SCAR=6.9
Pod documented
132 0 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 0 ▶sub initialise_sql {
Subroutine initialise_sql: not called CC=2 SCAR=17.9
Pod documented
143 0 my ($self) = @_;
144 0 my @binded_out_variables = $self->binded_out_variables;
145 0 my $result = join (",", map { '@' . $_ . ' AS ' . $_ } @binded_out_variables);
146 0 ▶ $self->set_sql(@binded_out_variables ? "SELECT $result" : '');
Branch: 1 branch
BranchTrueFalseTotal
@binded_out_variables ? : 0 0 0
147 }
148  
149  
150 =item execute
151  
152 Executes plsql block
153  
154 =cut
155  
156 0 ▶sub execute {
Subroutine execute: not called CC=2 SCAR=17.9
Pod documented
157 0 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 ▶ return $connection->record($sql) if $sql ;
Branch: 1 branch
BranchTrueFalseTotal
if $sql 0 0 0
162 }
163  
164  
165 =item bind_parameters
166  
167 =cut
168  
169 0 ▶sub bind_parameters {
Subroutine bind_parameters: not called CC=2 SCAR=17.9
Pod documented
170 0 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));
178 0 my @bind_variables = map { $bind_variables->{$_} } @bind_in_variables;
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 0 ▶sub parsed_plsql {
Subroutine parsed_plsql: not called CC=2 SCAR=17.9
Pod documented
192 0 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 1 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 0 ▶ sub get_type {
Subroutine get_type: not called CC=1 SCAR=6.9
Pod documented
236 0 my ($class, $type) = @_;
237 0 $type_map{$type};
238 }
239 }
240  
241  
242  
243 1 1;
244  
245 __END__
« Connection/PostgreSQL/PLSQL.pm ↑ Summary Connection/Oracle/PLSQL.pm »