Connection/PostgreSQL/PLSQL.pm

filter: Statementstmt 34.7% 24 / 69 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 40.0% 6 / 15 Podpod 100% 9 / 9 totaltotal 35.4% 39 / 110 CC 8 SCAR 19.6
CC
8
Cov
29%
drop_plsql_block
20.0
initialise_plsql_block
12.0
initialise_sql
6.0
CRAP 7.1
« QueryCursor.pm ↑ Summary Connection/MySQL/PLSQL.pm »
1 package DBIx::Connection::PostgreSQL::PLSQL;
2  
3  
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Abstract::Meta::Class ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use base qw(DBIx::PLSQLHandler);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 $VERSION = 0.02;
14  
15 =head1 NAME
16  
17 DBIx::Connection::PostgreSQL::PLSQL - PLSQL block wrapper for PostgreSQL
18  
19 =head1 SYNOPSIS
20  
21 use DBIx::PLSQLHandler;
22  
23 my $plsql_handler = new DBIx::PLSQLHandler(
24 name => 'test_proc',
25 connection => $connection,
26 plsql => "
27 DECLARE
28 var1 INT;
29 BEGIN
30 var1 := :var2 + :var3;
31 END;",
32 bind_variables => {
33 var2 => {type => 'SQL_INTEGER'},
34 var3 => {type => 'SQL_INTEGER'}
35 }
36 );
37 $plsql_handler->execute(var2 => 12, var3 => 8);
38  
39 or
40  
41 use DBIx::Connection;
42  
43  
44 my $plsql_handler = $connection->plsql_handler(
45 name => 'test_proc',
46 connection => $connection,
47 plsql => "
48 DECLARE
49 var1 INT;
50 BEGIN
51 :var1 := :var2 + :var3;
52 END;",
53 bind_variables => {
54 var1 => {type => 'SQL_INTEGER'},
55 var2 => {type => 'SQL_INTEGER'},
56 var3 => {type => 'SQL_INTEGER'}
57 }
58 );
59  
60 my $result_set = $plsql_handler->execute(var2 => 12, var3 => 8);
61  
62  
63 =head1 DESCRIPTION
64  
65 This class creates and invokes plsql function dynamicly that wraps
66 defined plsql block. This module check if body of plsql block has been changed
67 then it recreated wraper function for changed plsql block
68  
69 =cut
70  
71 =head2 METHODS
72  
73 =over
74  
75 =cut
76  
77 {
78 1 my %SQL = (
79 find_function => 'SELECT prosrc AS routine_definition FROM pg_proc WHERE proname = ? ',
80 function_args => 'SELECT t.typname, t.oid, p.proargtypes FROM pg_proc p JOIN pg_type t ON t.oid = ANY (p.proallargtypes) WHERE p.proname = ? '
81 );
82  
83 =item sql_defintion
84  
85 Return sql statement defintion. Takes sql name.
86  
87 =cut
88  
89 0 ▶ sub sql_defintion {
Subroutine sql_defintion: not called CC=1 SCAR=6.9
Pod documented
90 0 my ($self, $name) = @_;
91 0 $SQL{$name};
92 }
93 }
94  
95  
96 =item prepare
97  
98 Prepares plsql block
99  
100 =cut
101  
102 1 ▶sub prepare {
Subroutine prepare: not called CC=1 SCAR=3.5
Pod documented
103 0 my ($self) = @_;
104 0 $self->initialise_plsql_block();
105 0 $self->initialise_sql();
106 }
107  
108  
109 =item initialise_plsql_block
110  
111 Initialises plsql block, checks for changes, recreated if necessary.
112  
113 =cut
114  
115 0 ▶sub initialise_plsql_block {
Subroutine initialise_plsql_block: not called CC=3 SCAR=24.8
Pod documented
116 0 my ($self) = @_;
117 0 my @binded_out_variables = $self->binded_out_variables;
118 0 ▶ unless (@binded_out_variables) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@binded_out_variables) 0 0 0
119 0 $self->push_bind_out_variables('result');
120 0 $self->bind_variable(result => $self->default_variable_info(binding => 'out'));
121 }
122 0 my $plsql_block_wrapper = $self->plsql_block_wrapper;
123 0 ▶ if($self->is_block_changed($self->plsql_block_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_block_changed($self->plsql_block_name)) 0 0 0
124 0 $self->connection->do($plsql_block_wrapper);
125 }
126 }
127  
128 =item drop_plsql_block
129  
130 Removes existing function that acts as plsql block wrapper.
131  
132 =cut
133  
134 0 ▶sub drop_plsql_block {
Subroutine drop_plsql_block: not called CC=4 SCAR=30.0
Pod documented
135 0 my ($self) = @_;
136 0 my $connection = $self->connection;
137 0 my $cursor = $connection->query_cursor(sql => $self->sql_defintion('function_args'));
138 0 $cursor->execute([$self->plsql_block_name]);
139 0 my $args;
140 0 ▶ while (my ($typname, $oid, $proargtypes) = $cursor->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $cursor->fetch) 0 0 0
141 0 ▶ $args ||= join (",", split /\s+/, $proargtypes);
Condition0%0/3
$args ||= join(",", split(/\s+/, $proargtypes, 0))
l!l&&r!l&&!r
000
MC/DC0%0/2
$args ||= join(",", split(/\s+/, $proargtypes, 0))
$args join(",", split(/\s+/, $proargtypes, 0))
Truth table0%0/3
A ||= B
A= $argsB= join(",", split(/\s+/, $proargtypes, 0))
ABresult
1X1
011
000
142 0 ▶ $args =~ s/$oid/$typname/g if $oid;
Branch: 1 branch
BranchTrueFalseTotal
if $oid 0 0 0
143 }
144 0 $connection->do("DROP FUNCTION " . $self->plsql_block_name . "($args)" );
145 }
146  
147  
148 =item plsql_block_wrapper
149  
150 Returns plsql block weapper as plsql function
151  
152 =cut
153  
154 0 ▶sub plsql_block_wrapper {
Subroutine plsql_block_wrapper: not called CC=1 SCAR=6.9
Pod documented
155 0 my ($self) = @_;
156 0 'CREATE FUNCTION ' . $self->plsql_block_name . '(' . $self->plsql_block_declaration . ') AS $$'
157 . "\n" . $self->block_source . "\n"
158 . '$$ LANGUAGE plpgsql;';
159 }
160  
161  
162  
163 =item initialise_sql
164  
165 Initialises sql that will be used to invoke postgres function (plsql block)
166  
167 =cut
168  
169 0 ▶sub initialise_sql {
Subroutine initialise_sql: not called CC=2 SCAR=17.9
Pod documented
170 0 my ($self) = @_;
171 0 my @bind_in_variables = $self->binded_in_variables;
172 0 my @bind_out_variables = $self->binded_out_variables;
173 $self->set_sql(scalar(@bind_out_variables) == 1
174 0 ? "SELECT " . $self->plsql_block_name . '(' . join (",", ,map {'?'} @bind_in_variables) . ') AS ' . $bind_out_variables[0]
175 0 ▶ : "SELECT " . (join ",", (map { '(f.func).' . $_ } @bind_out_variables)) . " FROM (SELECT " . $self->plsql_block_name . '(' . join (",", ,map {'?'} @bind_in_variables) . ') AS func) f');
Branch: 1 branch
BranchTrueFalseTotal
scalar @bind_out_variables == 1 ? : 0 0 0
176 }
177  
178  
179 =item execute
180  
181 Binds and executes plsql block.
182  
183 =cut
184  
185 0 ▶sub execute {
Subroutine execute: not called CC=2 SCAR=17.9
Pod documented
186 0 my ($self, %bind_variables) = @_;
187 0 my @bind_in_variables = $self->binded_in_variables;
188 0 my $connection = $self->connection;
189 0 $connection->no_cache(1);
190 0 my $result_set;
191 0 eval {$result_set = $self->connection->record($self->sql, map {$bind_variables{$_}} @bind_in_variables);};
192 0 $connection->no_cache(0);
193 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
194 0 $result_set ;
195 }
196  
197  
198 =item type_precision
199  
200 Returns variable precision.
201  
202 =cut
203  
204 0 ▶sub type_precision {''}
Subroutine type_precision: not called CC=1 SCAR=6.9
Pod documented
205  
206  
207 {
208 =item type_map
209  
210 Mapping between DBI and specyfic postgres types.
211 The following mapping is defined:
212  
213 SQL_DECIMAL => 'numeric',
214 SQL_VARCHAR => 'varchar',
215 SQL_DATE =>'date',
216 SQL_CHAR =>'varchar',
217 SQL_DOUBLE =>'float8',
218 SQL_INTEGER =>'int4',
219 SQL_BOOLEAN =>'boolean',
220  
221 =cut
222  
223 1 my %type_map = (
224 SQL_DECIMAL => 'numeric',
225 SQL_VARCHAR => 'varchar',
226 SQL_DATE =>'date',
227 SQL_CHAR =>'varchar',
228 SQL_DOUBLE =>'float8',
229 SQL_INTEGER =>'int4',
230 SQL_BOOLEAN =>'boolean',
231 );
232  
233  
234 =item get_type
235  
236 Returns
237  
238 =cut
239  
240 0 ▶ sub get_type {
Subroutine get_type: not called CC=1 SCAR=6.9
Pod documented
241 0 my ($class, $type) = @_;
242 0 $type_map{$type};
243 }
244 }
245  
246  
247 1 1;
248  
249 __END__
« QueryCursor.pm ↑ Summary Connection/MySQL/PLSQL.pm »