Quiq/SqlPlus.pm

filter: Statementstmt 95.3% 62 / 65 Branchbran 50.0% 9 / 18 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 81.1% 82 / 101 CC 12 SCAR 22.2
CC
12
Cov
82%
script
14.0
BEGIN
1.0
BEGIN
1.0
CRAP 9.2
« Quiq/Html/Util.pm ↑ Summary Quiq/Converter.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::SqlPlus - Erzeuge Code für SQL*Plus
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 EXAMPLE
14  
15 Der Aufruf
16  
17 my $script = Quiq::SqlPlus->script('test.sql',q|
18 SELECT
19 *
20 FROM
21 all_users
22 ORDER BY
23 username
24 ;
25 |,
26 -before => q|
27 SELECT
28 SYSDATE AS t0
29 FROM
30 dual
31 ;
32 |,
33 -after => q|
34 SELECT
35 SYSDATE AS t1
36 FROM
37 dual
38 ;
39 |,
40 -author => 'Frank Seitz',
41 -description => q|
42 Dies ist ein Test-Skript.
43 |,
44 );
45  
46 erzeugt
47  
48 -- NAME
49 -- test.sql
50 --
51 -- DESCRIPTION
52 -- Dies ist ein Test-Skript.
53 --
54 -- AUTHOR
55 -- Frank Seitz
56
57 COLUMN tempdatum NEW_VALUE startdatum NOPRINT
58
59 SELECT
60 TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') AS tempdatum
61 FROM
62 dual;
63
64 SPOOL test.sql.&&startdatum..log
65
66 SET ECHO ON
67 SET FEEDBACK ON
68 SET VERIFY OFF
69 SET HEADING ON
70 SET TAB OFF
71 SET PAGESIZE 0
72 SET TRIMSPOOL ON
73 SET LINESIZE 10000
74 SET SERVEROUTPUT ON SIZE 10000
75 SET SQLBLANKLINES ON
76 SET NULL '~'
77 SET TIMING ON
78
79 WHENEVER OSERROR EXIT FAILURE ROLLBACK
80 WHENEVER SQLERROR EXIT FAILURE ROLLBACK
81
82 ALTER SESSION SET NLS_NUMERIC_CHARACTERS=",.";
83 ALTER SESSION set NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
84
85 -- ZUSTAND ZUVOR
86
87 SELECT
88 SYSDATE AS t0
89 FROM
90 dual
91 ;
92
93 -- OPERATION
94
95 SELECT
96 *
97 FROM
98 all_users
99 ORDER BY
100 username
101 ;
102
103 -- ZUSTAND DANACH
104
105 SELECT
106 SYSDATE AS t1
107 FROM
108 dual
109 ;
110
111 ROLLBACK;
112
113 EXIT
114
115 -- eof
116  
117 =cut
118  
119 # -----------------------------------------------------------------------------
120  
121 package Quiq::SqlPlus;
122 1 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 1 our $VERSION = '1.240';
129  
130 1 ▶use Quiq::Unindent;
Subroutine BEGIN: called CC=1 SCAR=0.0
131 1 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 1 ▶use Quiq::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
133  
134 # -----------------------------------------------------------------------------
135  
136 =head1 METHODS
137  
138 =head2 Klassenmethoden
139  
140 =head3 script() - Erzeuge SQL*Plus-Skript
141  
142 =head4 Synopsis
143  
144 $script = $class->script($name,$sql,@opt);
145  
146 =head4 Arguments
147  
148 =over 4
149  
150 =item $name
151  
152 Name des Skripts. Der Name wird in einen Kommentar an den Anfang
153 des Skripts gesetzt und für die Benennung der Logdatei genutzt
154  
155 NAME-YYYYMMDDHHMMSS.log
156  
157 wobei der Zeitanteil beim Aufruf des Skripts gesetzt wird.
158  
159 =item $sql
160  
161 Der SQL*Plus-Code, der in den Rumpf des Skripts eingesetzt wird.
162  
163 =back
164  
165 =head4 Options
166  
167 =over 4
168  
169 =item -author => $author
170  
171 Name des Skript-Autors, z.B. "Frank Seitz".
172  
173 =item -description => $description
174  
175 Beschreibung des Skripts. Darf mehrzeilig sein.
176  
177 =item -commit => $bool (Default: 0)
178  
179 Wenn diese Option gesetzt ist, wird COMMIT ans Ende des
180 Skripots gesetzt, sonst ROLLBACK.
181  
182 =item -before => $sql
183  
184 SQL*Plus-Code, der I<vor> $sql ausgeführt wird.
185  
186 =item -beforeAndAfter => $sql
187  
188 SQL*Plus-Code, der vor I<und> nach $sql ausgeführt wird.
189  
190 =item -after => $sql
191  
192 SQL*Plus-Code, der I<nach> $sql ausgeführt wird.
193  
194 =back
195  
196 =cut
197  
198 # -----------------------------------------------------------------------------
199  
200 1 ▶sub script {
Subroutine script: called CC=12 SCAR=26.4
Pod documented
201 1 my $class = shift;
202 1 my $name = shift;
203 1 my $sql = Quiq::Unindent->trim(shift);
204 # @_: @opt
205  
206 1 my $author = undef;
207 1 my $description = undef;
208 1 my $commit = 0;
209 1 my $before = undef;
210 1 my $beforeAndAfter = undef;
211 1 my $after = undef;
212 1 my $help = 0;
213  
214 1 Quiq::Option->extract(\@_,
215 -author => \$author,
216 -description => \$description,
217 -commit => \$commit,
218 -before => \$before,
219 -beforeAndAfter => \$beforeAndAfter,
220 -after => \$after,
221 -help => \$help,
222 );
223  
224 1 my $template = Quiq::Unindent->string(q|
225 __COMMENT__
226  
227 COLUMN tempdatum NEW_VALUE startdatum NOPRINT
228  
229 SELECT
230 TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') AS tempdatum
231 FROM
232 dual;
233  
234 SPOOL __NAME__.&&startdatum..log
235  
236 SET ECHO ON
237 SET FEEDBACK ON
238 SET VERIFY OFF
239 SET HEADING ON
240 SET TAB OFF
241 SET PAGESIZE 0
242 SET TRIMSPOOL ON
243 SET LINESIZE 2100
244 SET SQLBLANKLINES ON
245 SET NULL '~'
246 SET TIMING ON
247  
248 WHENEVER OSERROR EXIT FAILURE ROLLBACK
249 WHENEVER SQLERROR EXIT FAILURE ROLLBACK
250  
251 ALTER SESSION SET NLS_NUMERIC_CHARACTERS=",.";
252 ALTER SESSION set NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
253  
254 __SQL__
255  
256 EXIT
257  
258 -- eof
259 |);
260  
261 1 my $tpl = Quiq::Template->new('text',\$template);
262  
263 1 my $comment = Quiq::Unindent->string("
264 -- NAME
265 -- $name
266 ");
267 1 ▶ if ($description) {
Branch: 1 branch
BranchTrueFalseTotal
if ($description) 1 0 1
268 1 $description = Quiq::Unindent->trim($description);
269 1 $description =~ s/^/-- /mg;
270 1 $comment .= "--\n-- DESCRIPTION\n$description\n";
271 }
272 1 ▶ if ($author) {
Branch: 1 branch
BranchTrueFalseTotal
if ($author) 1 0 1
273 1 $author = Quiq::Unindent->trim($author);
274 1 $author =~ s/^/-- /mg;
275 1 $comment .= "--\n-- AUTHOR\n$author\n";
276 }
277  
278 1 ▶ if ($before || $after || $beforeAndAfter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before or $after or $beforeAndAfter) 1 0 1
Condition33%2/6
$before or $after or $beforeAndAfter
l!l&&r!l&&!r
010
$before or $after
l!l&&r!l&&!r
001
MC/DC0%0/3
$before or $after or $beforeAndAfter
$before $after $beforeAndAfter
Truth table25%1/4
A or B or C
A= $beforeB= $afterC= $beforeAndAfter
ABCresult
1XX1
01X1
0011
0000
279 1 $sql = "-- OPERATION\n\n$sql";
280 }
281 1 ▶ if ($beforeAndAfter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($beforeAndAfter) 1 0 1
282 1 $beforeAndAfter = Quiq::Unindent->trim($beforeAndAfter);
283 1 ▶ if ($before) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before) 0 1 1
284 0 $before .= "\n";
285 }
286 1 $before .= $beforeAndAfter;
287 1 ▶ if ($after) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after) 0 1 1
288 0 $after .= "\n";
289 }
290 1 $after .= $beforeAndAfter;
291 }
292 1 ▶ if ($before) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before) 1 0 1
293 1 $before = Quiq::Unindent->trim($before);
294 1 $sql =
295 "-- ZUSTAND VORHER\n\n$before\n\n$sql";
296 }
297 1 ▶ if ($after) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after) 1 0 1
298 1 $after = Quiq::Unindent->trim($after);
299 1 $sql =
300 "$sql\n\n-- ZUSTAND NACHHER\n\n$after\n";
301 }
302 1 ▶ if ($commit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($commit) { } 0 1 1
303 0 $sql .= "\nCOMMIT;\n";
304 }
305 else {
306 1 $sql .= "\nROLLBACK;\n";
307 }
308  
309 1 $tpl->replace(
310 __COMMENT__ => $comment,
311 __NAME__ => $name,
312 __SQL__ => $sql,
313 );
314  
315 1 return $tpl->asStringNL;
316 }
317
318  
319 # -----------------------------------------------------------------------------
320  
321 =head1 VERSION
322  
323 1.240
324  
325 =head1 AUTHOR
326  
327 Frank Seitz, L<http://fseitz.de/>
328  
329 =head1 COPYRIGHT
330  
331 Copyright (C) 2026 Frank Seitz
332  
333 =head1 LICENSE
334  
335 This code is free software; you can redistribute it and/or modify
336 it under the same terms as Perl itself.
337  
338 =cut
339  
340 # -----------------------------------------------------------------------------
341  
342 1 1;
343  
344 # eof
« Quiq/Html/Util.pm ↑ Summary Quiq/Converter.pm »