Quiq/Sql/Script/Reader.pm

filter: Statementstmt 32.7% 19 / 58 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 6 / 9 Podpod 100% 3 / 3 totaltotal 28.2% 28 / 99 CC 14 SCAR 49.0
CC
14
Cov
22%
nextStmt
210.0
new
2.0
close
2.0
CRAP 134.1
« Quiq/PlotlyJs/XY/DiagramGroup.pm ↑ Summary Quiq/TimeLapse/Directory.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Sql::Script::Reader - Leser von SQL-Skripten
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 SYNOPSIS
14  
15 use Quiq::Sql::Script::Reader;
16
17 my $rd = Quiq::Sql::Script::Reader->new($dbms,$file);
18 while (my $stmt = $rd->nextStmt) {
19 # irgendwas mit dem SQL-Statement machen
20 }
21 $rd->close;
22  
23 =head1 DESCRIPTION
24  
25 Die Klasse implementiert einen Leser von SQL-Skripten. Ein SQL-Skript
26 ist eine Folge von SQL-Statements, die mit Semikolon I<am Ende einer
27 Zeile> (also vor einem Newline) voneinander getrennt sind. Eine Instanz
28 der Klasse liefert nacheinander die einzelnen Statements, die ausgeführt
29 oder anderweitig verarbeitet werden können. Da das Skript sukzessive
30 gelesen wird, können auch sehr große SQL-Skripte, z.B. von
31 Datenbank-Dumps, durch die Klasse verarbeitet werden.
32  
33 Leerzeilen am Anfang eines Statements werden entfernt, außerdem
34 das abschließende Semikolon und darauffolgender Whitespace bis
35 zum Zeilenende.
36  
37 =head1 CAVEATS
38  
39 Mehrere SQL-Statements I<auf einer Zeile> beherrscht die Klasse nicht.
40  
41 =cut
42  
43 # -----------------------------------------------------------------------------
44  
45 package Quiq::Sql::Script::Reader;
46 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 1 our $VERSION = '1.240';
53  
54 1 ▶use Quiq::Sql::Analyzer;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use Quiq::FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 # -----------------------------------------------------------------------------
58  
59 =head1 METHODS
60  
61 =head2 Klassenmethoden
62  
63 =head3 new() - Konstruktor
64  
65 =head4 Synopsis
66  
67 $rd = $class->new($dbms,$file,@opt);
68 $rd = $class->new($dbms,\$str,@opt);
69  
70 =head4 Arguments
71  
72 =over 4
73  
74 =item $dbms
75  
76 Name des DBMS, das die SQL-Statements des Skripts ausführen kann.
77  
78 =item $file
79  
80 Dateipfad des SQL-Skripts. Im Falle von '-' wird von STDIN gelesen.
81  
82 =item $str
83  
84 Das SQL-Skript als Zeichenkette.
85  
86 =back
87  
88 =head4 Options
89  
90 =over 4
91  
92 =item -separator => $regex (Default: qr/;\s*$/)
93  
94 Statement-Separator. Per Default ein Semikolon (;) am Zeilenende.
95 Andere Variante (geeigneter, wenn Prozeduren vorkommen können),
96 Semikolon allein auf Zeile: qr/^;\s*$/.
97  
98 =back
99  
100 =head4 Returns
101  
102 Reader-Objekt
103  
104 =head4 Description
105  
106 Instantiiere ein Reader-Objekt für DBMS $dbms und Datei $file bzw.
107 Zeichenkette $str und liefere eine Referenz auf dieses Objekt zurück.
108  
109 =cut
110  
111 # -----------------------------------------------------------------------------
112  
113 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
114 0 my ($class,$dbms,$input) = splice @_,0,3;
115  
116 0 my $separator = qr/\s*;$/;
117  
118 0 $class->parameters(\@_,
119 -separator => \$separator,
120 );
121  
122 # SQL-Analyzer instantiieren (mit Namensprüfung)
123 0 my $aly = Quiq::Sql::Analyzer->new($dbms);
124  
125 0 my $fh = Quiq::FileHandle->new('<',$input);
126 0 $fh->setEncoding('utf-8');
127  
128 0 return $class->SUPER::new(
129 analyzer => $aly,
130 fh => $fh,
131 separator => $separator,
132 );
133 }
134  
135 # -----------------------------------------------------------------------------
136  
137 =head2 Objektmethoden
138  
139 =head3 close() - Beende Nutzung des Readers
140  
141 =head4 Synopsis
142  
143 $rd->close;
144  
145 =head4 Description
146  
147 Beende die die Nutzung des Reader-Objekts. Nach Aufruf der Methode
148 kann der Reader nicht mehr genutzt werden.
149  
150 =cut
151  
152 # -----------------------------------------------------------------------------
153  
154 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
155 0 $_[0]->{'fh'}->close;
156 0 $_[0] = undef;
157 }
158  
159 # -----------------------------------------------------------------------------
160  
161 =head3 nextStmt() - Nächstes Statement
162  
163 =head4 Synopsis
164  
165 $stmt = $rd->nextStmt;
166  
167 =head4 Returns
168  
169 SQL-Statemt (String)
170  
171 =head4 Description
172  
173 Liefere das nächste SQL-Statement des Skripts. Leere Statements werden
174 übergangen. Ist das Ende erreicht, liefere C<undef>.
175  
176 =cut
177  
178 # -----------------------------------------------------------------------------
179  
180 0 ▶sub nextStmt {
Subroutine nextStmt: not called CC=14 SCAR=53.5
Pod documented
181 0 my $self = shift;
182  
183 0 my ($fh,$aly,$separator) = $self->get('fh','analyzer','separator');
184  
185 0 my $stmt;
186 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
187 0 ▶ if (s/^---+$//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^---+$//) 0 0 0
188 # Eine Trennlinie übergehen wir
189 0 next;
190 }
191 0 $stmt .= $_;
192 0 ▶ if (/$separator/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$separator/) 0 0 0
193 0 ▶ if ($stmt =~ /^[\s;]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ /^[\s;]*$/) 0 0 0
194 # Leere Statements übergehen wir
195 0 $stmt = undef;
196 0 next;
197 }
198  
199 0 ▶ if ($aly->isCreateFunction($stmt)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aly->isCreateFunction($stmt)) 0 0 0
200  
201 # Im Falle von CREATE FUNCTION oder CREATE OR REPLACE
202 # FUNCTION bei PostgreSQL endet das Statement nicht unbedingt
203 # mit dem ersten Semikolon am Zeilenende. Ggf. müssen wir
204 # den Inhalt zwischen den Begrenzern $STR$ ... $STR$
205 # (STR ist ein frei gewählter Bezeichner) überlesen.
206 # Die Begrenzer sind aber optional.
207  
208 # Begrenzer ermitteln
209 0 my ($as) = $stmt =~ /AS\s+(\$.*?\$)/i;
210 0 ▶ if (!$as) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($as) 0 0 0
211 # Kein Begrenzer, also Ende des Statement
212 0 last;
213 }
214  
215 # Anzahl der bereits gelesenen Begrenzer ermitteln
216  
217 0 my $i = 0;
218 0 ▶ while ($stmt =~ /\Q$as/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ /\Q$as\E/g) 0 0 0
219 0 $i++;
220 }
221  
222 # Haben wir erst einen Begrenzer gelesen, müssen wir
223 # bis zum zweiten Begrenzer und darüber hinaus bis
224 # zum Semikolon lesen.
225  
226 0 ▶ if ($i == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 1) 0 0 0
227 0 my $asFound = 0;
228 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
229 0 $stmt .= $_;
230 0 ▶ if ($asFound && /;\s*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($asFound and /;\s*$/) { } 0 0 0
elsif (/\Q$as\E/) { } 0 0 0
Condition0%0/3
$asFound and /;\s*$/
!ll&&!rl&&r
000
MC/DC0%0/2
$asFound and /;\s*$/
$asFound /;\s*$/
Truth table0%0/3
A and B
A= $asFoundB= /;\s*$/
ABresult
0X0
100
111
231 0 last;
232 }
233 elsif (/\Q$as/) {
234 0 $asFound = 1;
235 }
236 }
237 }
238 }
239 0 last;
240 }
241 }
242  
243 0 ▶ if (defined $stmt) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stmt) 0 0 0
244 0 $stmt =~ s/^\n+//; # Leerzeilen am Anfang entfernen
245 0 $stmt =~ s/[;\s]*$//; # Whitespace und Semikolon am Ende entfernen
246 }
247  
248 0 return $stmt;
249 }
250  
251 # -----------------------------------------------------------------------------
252  
253 =head1 VERSION
254  
255 1.240
256  
257 =head1 AUTHOR
258  
259 Frank Seitz, L<http://fseitz.de/>
260  
261 =head1 COPYRIGHT
262  
263 Copyright (C) 2026 Frank Seitz
264  
265 =head1 LICENSE
266  
267 This code is free software; you can redistribute it and/or modify
268 it under the same terms as Perl itself.
269  
270 =cut
271  
272 # -----------------------------------------------------------------------------
273  
274 1 1;
275  
276 # eof
« Quiq/PlotlyJs/XY/DiagramGroup.pm ↑ Summary Quiq/TimeLapse/Directory.pm »