blib/lib/DBIO/SQL/Util.pm

filter: Statementstmt 100% 252 / 252 Branchbran 97.0% 196 / 202 Conditioncond 87.1% 190 / 218 MC/DCmcdc 73.2% 82 / 112 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 91.9% 732 / 796 CC 182 SCAR 45.4
CC
182
Cov
95%
_scan_statements
132.5
_split_statements
25.0
_drop_statements
17.1
CRAP 93.7
« blib/lib/DBIO/Test/Schema.pm ↑ Summary blib/lib/DBIO/ResultSet.pm »
1 # ABSTRACT: Shared SQL utility functions for DBIO
2  
3 384 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 384 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 384 our $VERSION = '0.900007';
7  
8 package DBIO::SQL::Util;
9  
10 384 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 384 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 384 our @EXPORT_OK = qw(_quote_ident _split_statements);
14  
15  
16 # Quote a SQL identifier.
17 # Unquoted identifiers must match /\A[a-z_][a-z0-9_]*\z/i (case-insensitive).
18 # Embedded double-quotes in quoted identifiers are escaped as "".
19 361 ▶sub _quote_ident {
Subroutine _quote_ident: called CC=2 SCAR=6.9
20 361 my ($name) = @_;
21  
22 # Simple unquoted identifier: starts with letter or underscore,
23 # followed by letters, digits, or underscores (case-insensitive)
24 361 ▶ return $name if $name =~ /\A[a-z_][a-z0-9_]*\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\A[a-z_][a-z0-9_]*\z/i 353 8 361
25  
26 # Double-quote the identifier, escaping embedded double-quotes as ""
27 8 $name =~ s/"/""/g;
28 8 return qq{"$name"};
29 }
30  
31 # Split SQL text into its statements.
32 #
33 # my @statements = _split_statements($sql);
34 # my @statements = _split_statements($sql, { backslash_escapes => 1 });
35 # my @statements = _split_statements($sql, { dialect => 'postgresql' });
36 #
37 # The text is read as SQL, so a semicolon ends a statement only where it
38 # stands as one. It does not inside
39 #
40 # * a string literal '...' (a quote in it is doubled, ''), PostgreSQL's
41 # E'...' and Oracle's q'[...]';
42 # * a quoted identifier "...", `...` or [...] (the closing character in it is
43 # doubled). A [ is no identifier but a subscript or an array where it
44 # stands directly behind a word character, a ] or a ) -- PostgreSQL's
45 # integer[], col[1], ARRAY['a', 'b'] -- and where it opens on a literal or
46 # on another [ -- the lists of ClickHouse and DuckDB, ['a', 'b']. What
47 # stands between such brackets is read like everything else;
48 # * a comment, -- to the end of the line or /* ... */. Block comments do not
49 # nest: the first */ closes. PostgreSQL nests them, the other engines do
50 # not, and this reading never finds fewer statements than the other one --
51 # except under dialect postgresql, which reads /* a /* b */ c */ as one
52 # comment, in the scan and in the comments taken off in front of a statement.
53 # MySQL's # comment is not read, # being an operator elsewhere;
54 # * a dollar-quoted string $$...$$ or $tag$...$tag$, by PostgreSQL's rule:
55 # the tag is empty or an identifier, and a $ directly behind a word
56 # character or another $ belongs to that word (a$b$, Oracle's v$session;
57 # $1 is a placeholder). One that is never closed is ordinary text;
58 # * parentheses. A semicolon between ( and ) -- PostgreSQL's CREATE RULE ...
59 # DO ALSO (INSERT ...; INSERT ...) -- ends nothing, in any dialect. The
60 # depth never goes below zero, so a stray ) costs no boundary;
61 # * the body of a CREATE TRIGGER, from its BEGIN to the END that closes it.
62 # Inside the body CASE opens a further level, and BEGIN does where a
63 # statement can start (behind a semicolon, a label, THEN, ELSE, DO, LOOP
64 # or REPEAT); END closes one. An END that closes a BEGIN level stands
65 # behind a semicolon or directly behind its BEGIN, so a bare end or begin
66 # anywhere else -- SET end = 1, WHERE begin = 1 -- is a column; the END of
67 # a CASE is not asked this, it closes its CASE wherever it stands.
68 # END IF, END LOOP, END WHILE and END REPEAT close a construct whose
69 # opening was not counted, and the CASE of END CASE opens nothing. This
70 # is read ONLY in a statement that begins CREATE [OR REPLACE | OR ALTER]
71 # [TEMP | TEMPORARY] [DEFINER = ...] TRIGGER. The body of a function, a
72 # procedure or an event is NOT read: its semicolons still end a
73 # statement, so a routine has to come dollar-quoted or go to the handle
74 # whole (PostgreSQL's BEGIN ATOMIC and Oracle's PL/SQL units below
75 # excepted). Neither is a trigger's DECLARE section before its BEGIN
76 # (Firebird), nor Transact-SQL, where an END may be followed by a new IF.
77 # One more body is read, under dialect postgresql only: CREATE [OR REPLACE]
78 # FUNCTION | PROCEDURE ... BEGIN ATOMIC <statement>; ... END (PostgreSQL
79 # 14+). It is the same level mechanism: the semicolons from BEGIN ATOMIC
80 # to its END end nothing, and the END closes at the end of the statement.
81 # Without the dialect BEGIN ATOMIC is a word like any other.
82 # * under dialect oracle a PL/SQL unit: a statement that begins CREATE [OR
83 # REPLACE] [EDITIONABLE | NONEDITIONABLE] FUNCTION | PROCEDURE | PACKAGE
84 # [BODY] | TRIGGER | TYPE BODY, or an anonymous block (DECLARE or BEGIN
85 # first). It is one statement up to the semicolon behind the END that
86 # closes its outermost level, and that statement KEEPS the semicolon,
87 # which Oracle needs behind END. A line that holds nothing but / (SQL*Plus)
88 # ends a statement as well and is not delivered. The levels: the IS | AS of
89 # a function, a procedure, a package or a type body opens one (a forward
90 # declaration PROCEDURE p; and a call specification do not), so does a
91 # nested subprogram in a declaration part, a DECLARE or BEGIN of a trigger
92 # or an anonymous block, and a nested BEGIN or DECLARE where a statement
93 # can start; the BEGIN of a level already in its declaration part belongs
94 # to that level. CASE opens a level that END or END CASE closes; END IF and
95 # END LOOP close a construct of their own and END [name] closes a level. An
96 # end or begin behind a dot, or anywhere but where a statement ends, is a
97 # column. A COMPOUND TRIGGER is one unit as well: its declaration part and
98 # each timing-point section (BEFORE STATEMENT IS ... END BEFORE STATEMENT;
99 # and the like) are levels inside it, and the END [name]; of its outermost
100 # level ends it. A block that is not closed at the end of the text, or at a / line,
101 # is refused with an exception and not delivered as a remainder.
102 #
103 # What is delivered is each statement without its semicolon and without the
104 # whitespace around it. Comments in front of a statement are taken off, so no
105 # statement begins with one, and a piece that holds nothing but comments is no
106 # statement and is not delivered; a caller needs no filter of its own, and one
107 # that drops what begins with "--" would drop the statement behind the
108 # comment. /*! ... */ (MySQL runs it) and /*+ ... */ (a hint) are code and
109 # stay. Comments inside and behind a statement are left as they are, like
110 # every other character of it.
111 #
112 # The backslash is where the dialects part, and the text does not say which
113 # one it is written in. Standard SQL reads it as a character -- 'C:\' is a
114 # complete literal -- while MySQL and ClickHouse read it as an escape in
115 # '...', "..." and `...`, so 'it\'s' is one. Without the option both readings
116 # are taken and a semicolon ends a statement if it does under either. A text
117 # that is right under one reading keeps every statement boundary it has; a
118 # wrong cut can only happen where the readings really part, behind a backslash
119 # before a closing quote. One exception keeps that from reaching into a
120 # function body: a semicolon that only ONE reading takes for an end is none
121 # while it stands between dollar-quote delimiters. For that the delimiters are
122 # taken from the bare text, literals and comments unread, because that is
123 # what protected a body before this function read literals at all -- the
124 # reading that has lost its place in the text sees dollar quotes where there
125 # are none, and none where they are. A caller that knows the dialect names the
126 # reading: backslash_escapes => 1 for the MySQL one, 0 for the standard one.
127 # E'...' is the backslash reading under both.
128 #
129 # The seam for that is dialect => NAME (see %DIALECT below): the name of the
130 # engine's SQL, which a storage declares through _ddl_sql_dialect and
131 # DBIO::Deploy::Base::_split_ddl hands over. It stands for the readings that
132 # engine has -- today only the backslash -- so a driver names its engine once
133 # and not each property. An explicit backslash_escapes beats it, an unknown
134 # name throws, and no dialect (or undef) is the behaviour above, unchanged.
135  
136 # What each named SQL dialect reads differently. One row per dialect, one
137 # column per property of the reading; a property is added here when a reader
138 # for it exists, not before.
139 #
140 # backslash_escapes 1: a backslash escapes the next character inside '...'
141 # (MySQL, MariaDB); 0: it is a character (standard SQL)
142 # nested_comments 1: /* ... */ nests (PostgreSQL); 0: the first */ closes
143 # begin_atomic 1: a routine's BEGIN ATOMIC ... END body is one level
144 # (PostgreSQL 14+); 0: BEGIN ATOMIC is an ordinary word
145 # plsql_units 1: a PL/SQL unit (function, procedure, package, type
146 # body, trigger, anonymous block) is one statement
147 # that keeps the semicolon behind its END, and a line
148 # of / ends a statement (Oracle); 0: not read
149 384 my %DIALECT = (
150 postgresql => { backslash_escapes => 0, nested_comments => 1, begin_atomic => 1, plsql_units => 0 },
151 sqlite => { backslash_escapes => 0, nested_comments => 0, begin_atomic => 0, plsql_units => 0 },
152 mysql => { backslash_escapes => 1, nested_comments => 0, begin_atomic => 0, plsql_units => 0 },
153 oracle => { backslash_escapes => 0, nested_comments => 0, begin_atomic => 0, plsql_units => 1 },
154 );
155  
156 352 ▶sub _split_statements {
Subroutine _split_statements: called CC=25 SCAR=32.2
157 352 my ($sql, $opt) = @_;
158  
159 352 ▶ my $backslash = $opt ? $opt->{backslash_escapes} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$opt ? : 77 275 352
160 352 my ($nest, $atomic, $plsql) = (0, 0, 0);
161 352 ▶ if ($opt and defined $opt->{dialect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt and defined $opt->{"dialect"}) 68 284 352
Condition100%3/3
$opt and defined $opt->{"dialect"}
!ll&&!rl&&r
275968
MC/DC100%2/2
$opt and defined $opt->{"dialect"}
$opt defined $opt->{"dialect"}
Truth table100%3/3
A and B
A= $optB= defined $opt->{"dialect"}
ABresult
0X0
100
111
162 my $dialect = $DIALECT{ $opt->{dialect} }
163 or DBIO::Exception->throw(sprintf
164 "_split_statements: unknown SQL dialect '%s' (known: %s)",
165 68 ▶ $opt->{dialect}, join ', ', sort keys %DIALECT);
Branch: 1 branch
BranchTrueFalseTotal
my $dialect = $DIALECT{$opt->{"dialect"}} or "DBIO::Exception"->throw(sprintf(q[_split_statements: unknown SQL dialect '%s' (known: %s)], $opt->{"dialect"}, join(", ", sort(keys %DIALECT)))) 67 1 68
166 67 ▶ $backslash //= $dialect->{backslash_escapes};
Condition66%2/3
$backslash //= $dialect->{"backslash_escapes"}
l!l&&r!l&&!r
2650
MC/DC0%0/2
$backslash //= $dialect->{"backslash_escapes"}
$backslash $dialect->{"backslash_escapes"}
Truth table66%2/3
A //= B
A= $backslashB= $dialect->{"backslash_escapes"}
ABresult
1X1
011
000
167 67 ($nest, $atomic, $plsql) = @$dialect{qw(nested_comments begin_atomic plsql_units)};
168 }
169  
170 # The cuts behind which the statement keeps its semicolon (PL/SQL units).
171 351 ▶ my $keep = $plsql ? {} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$plsql ? : 38 313 351
172  
173 351 my @cuts;
174 351 ▶ if (defined $backslash) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $backslash) { } 72 279 351
elsif (index($sql, '\\') == -1) { } 262 17 279
175 72 ▶ @cuts = _scan_statements($sql, $backslash ? 1 : 0, $nest, $atomic, $keep);
Branch: 1 branch
BranchTrueFalseTotal
$backslash ? : 7 65 72
176 }
177 elsif (index($sql, '\\') < 0) {
178 # No backslash, no second reading.
179 262 @cuts = _scan_statements($sql, 0, $nest, $atomic);
180 }
181 else {
182 17 my %readings;
183 17 $readings{$_}++ for _scan_statements($sql, 0, $nest, $atomic),
184 _scan_statements($sql, 1, $nest, $atomic);
185  
186 17 my @dollar = _dollar_quoted_spans($sql);
187 17 for my $cut (sort { $a <=> $b } keys %readings) {
188 35 ▶ shift @dollar while @dollar and $dollar[0][1] <= $cut;
Branch: 1 branch
BranchTrueFalseTotal
@dollar and $dollar[0][1] <= $cut and do { shift @dollar } 2 35 37
Condition100%3/3
@dollar and $dollar[0][1] <= $cut
!ll&&!rl&&r
3052
MC/DC100%2/2
@dollar and $dollar[0][1] <= $cut
@dollar $dollar[0][1] <= $cut
Truth table100%3/3
A and B
A= @dollarB= $dollar[0][1] <= $cut
ABresult
0X0
100
111
189 push @cuts, $cut
190 35 ▶ if $readings{$cut} == 2 or !@dollar or $dollar[0][0] > $cut;
Branch: 1 branch
BranchTrueFalseTotal
if $readings{$cut} == 2 or not @dollar or $dollar[0][0] > $cut 32 3 35
Condition100%6/6
$readings{$cut} == 2 or not @dollar or $dollar[0][0] > $cut
l!l&&r!l&&!r
3023
$readings{$cut} == 2 or not @dollar
l!l&&r!l&&!r
3275
MC/DC100%3/3
$readings{$cut} == 2 or not @dollar or $dollar[0][0] > $cut
$readings{$cut} == 2 not @dollar $dollar[0][0] > $cut
Truth table100%4/4
A or not(B) or C
A= $readings{$cut} == 2B= not @dollarC= $dollar[0][0] > $cut
ABCresult
1XX1
01X1
0011
0000
191 }
192 }
193  
194 345 my @stmts;
195 345 my $from = 0;
196 345 for my $cut (@cuts, length $sql) {
197 3766 my $stmt = substr $sql, $from, $cut - $from;
198 3766 $from = $cut + 1;
199 3766 ▶ $stmt .= ';' if $keep and $keep->{$cut};
Branch: 1 branch
BranchTrueFalseTotal
if $keep and $keep->{$cut} 34 3732 3766
Condition100%3/3
$keep and $keep->{$cut}
!ll&&!rl&&r
36844834
MC/DC100%2/2
$keep and $keep->{$cut}
$keep $keep->{$cut}
Truth table100%3/3
A and B
A= $keepB= $keep->{$cut}
ABresult
0X0
100
111
200  
201 3766 ▶ if ($nest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nest) { } 42 3724 3766
202 # The same comments, nesting: a block comment ends where the scanner
203 # puts its end, not at the first */.
204 42 while (1) {
205 70 ▶ next if $stmt =~ s/\A(?:\s+|--[^\n]*)//;
Branch: 1 branch
BranchTrueFalseTotal
if $stmt =~ s/\A(?:\s+|--[^\n]*)// 23 47 70
206 47 ▶ last unless $stmt =~ m{\A/\*(?![!+])};
Branch: 1 branch
BranchTrueFalseTotal
unless $stmt =~ m[\A/\*(?![!+])] 42 5 47
207 5 substr($stmt, 0, _comment_end(\$stmt, 0)) = '';
208 }
209 }
210 else {
211 3724 ▶ 1 while $stmt =~ s{\A(?:\s+|--[^\n]*|/\*(?![!+]).*?(?:\*/|\z))}{}s;
Branch: 1 branch
BranchTrueFalseTotal
$stmt =~ s[\A(?:\s+|--[^\n]*|/\*(?![!+]).*?(?:\*/|\z))][]s and do { '???' } 3338 3724 7062
212 }
213 3766 $stmt =~ s/\s+\z//;
214 3766 ▶ push @stmts, $stmt if length $stmt;
Branch: 1 branch
BranchTrueFalseTotal
if length $stmt 3495 271 3766
215 }
216  
217 345 return @stmts;
218 }
219  
220 384 my %Q_QUOTE_CLOSE = ('[' => ']', '{' => '}', '(' => ')', '<' => '>');
221  
222 # The characters a quoted text runs over without looking: everything but its
223 # closing character, and under the backslash reading everything but that and
224 # the backslash.
225 my %QUOTED_RUN = map {
226 384 my $close = quotemeta $_;
227 1536 ($_ => [ qr/\G[^$close]+/, qr/\G[^$close\\]+/ ]);
228 } "'", '"', '`', ']';
229  
230 # A routine whose body may be BEGIN ATOMIC ... END.
231 384 my $ROUTINE_HEAD = qr{
232 \A CREATE \s+
233 (?: OR \s+ REPLACE \s+ )?
234 (?: FUNCTION | PROCEDURE ) \b
235 }xi;
236  
237 384 my $TRIGGER_HEAD = qr{
238 \A CREATE \s+
239 (?: OR \s+ (?: REPLACE | ALTER ) \s+ )?
240 (?: TEMP (?: ORARY )? \s+ )?
241 (?: DEFINER \s* = \s* (?: '[^']*' | "[^"]*" | `[^`]*` | [^\s'"`] )+ \s+ )?
242 TRIGGER \b
243 }xi;
244  
245 # The start of a PL/SQL unit behind its CREATE, under dialect oracle.
246 384 my $PLSQL_WS = qr{(?:\s|--[^\n]*|/\*.*?\*/)+}s;
247 384 my $PLSQL_UNIT = qr{
248 $PLSQL_WS
249 (?: OR $PLSQL_WS REPLACE $PLSQL_WS )?
250 (?: (?: NON )? EDITIONABLE $PLSQL_WS )?
251 (?: FUNCTION | PROCEDURE | PACKAGE | TRIGGER | TYPE $PLSQL_WS BODY ) \b
252 }xi;
253  
254 # The opening delimiter of a dollar-quoted string.
255 384 my $DOLLAR_QUOTE = qr/\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$/;
256  
257 # One pass over the text under one reading of the backslash. Returns the
258 # offsets of the semicolons that end a statement.
259 #
260 # No pattern matched against the text captures: a capturing match keeps a copy
261 # of the string it matched in, and that is the whole text, once per token.
262 368 ▶sub _scan_statements {
Subroutine _scan_statements: called CC=129 SCAR=48.9
263 368 my ($sql, $backslash, $nest, $atomic, $keep) = @_;
264 368 ▶ my $plsql = $keep ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$keep ? : 38 330 368
265  
266 368 my @cuts;
267 368 my $len = length $sql;
268 368 my $start; # offset of the statement's first token, undef before it
269 my $trigger; # is the statement a CREATE TRIGGER? asked at its first BEGIN
270 368 my @levels; # open BEGIN ('B') and CASE ('C') levels of a trigger body
271 368 my $paren = 0; # open parentheses
272 368 my ($unit, $pending, $opened, $compound); # dialect oracle: the PL/SQL unit being read
273 # ('block', 'trigger' or 'routine'), a subprogram head
274 # waiting for its IS | AS, a level opened on the unit,
275 # a COMPOUND TRIGGER
276 368 my $prev = ''; # the token before: ';', '(', a word upper-cased, 'W' (a
277 # long word or a qualified one) or 'Q' (a literal)
278  
279 368 pos($sql) = 0;
280 368 ▶ while ((my $at = pos $sql) < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $at = pos $sql) < $len) 46403 367 46770
281 46403 ▶ next if $sql =~ /\G\s+/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /\G\s+/cg 18537 27866 46403
282  
283 27866 ▶ if ($nest and substr($sql, $at, 2) eq '/*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($nest and substr($sql, $at, 2) eq "/*") 7 27859 27866
Condition100%3/3
$nest and substr($sql, $at, 2) eq "/*"
!ll&&!rl&&r
275602997
MC/DC100%2/2
$nest and substr($sql, $at, 2) eq "/*"
$nest substr($sql, $at, 2) eq "/*"
Truth table100%3/3
A and B
A= $nestB= substr($sql, $at, 2) eq "/*"
ABresult
0X0
100
111
284 7 pos($sql) = _comment_end(\$sql, $at);
285 7 next;
286 }
287  
288 27859 ▶ next if $sql =~ m{\G(?:--[^\n]*|/\*.*?(?:\*/|\z))}gcs;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ m[\G(?:--[^\n]*|/\*.*?(?:\*/|\z))]cgs 119 27740 27859
289  
290 # SQL*Plus: a line of nothing but / ends the statement before it.
291 27740 ▶ if ($plsql and substr($sql, $at, 1) eq '/'
Branch: 1 branch
BranchTrueFalseTotal
if ($plsql and substr($sql, $at, 1) eq "/" and $at == 0 || substr($sql, 0, $at) =~ /(?:\A|\n)[ \t\r]*\z/ and $sql =~ m[\G/[ \t\r]*(?:\n|\z)]) 6 27734 27740
Condition91%11/12
$plsql and substr($sql, $at, 1) eq "/" and $at == 0 || substr($sql, 0, $at) =~ /(?:\A|\n)[ \t\r]*\z/ and $sql =~ m[\G/[ \t\r]*(?:\n|\z)]
!ll&&!rl&&r
2773316
$plsql and substr($sql, $at, 1) eq "/" and $at == 0 || substr($sql, 0, $at) =~ /(?:\A|\n)[ \t\r]*\z/
!ll&&!rl&&r
2773217
$plsql and substr($sql, $at, 1) eq "/"
!ll&&!rl&&r
267879458
$at == 0 or substr($sql, 0, $at) =~ /(?:\A|\n)[ \t\r]*\z/
l!l&&r!l&&!r
071
MC/DC80%4/5
$plsql and substr($sql, $at, 1) eq "/" and $at == 0 || substr($sql, 0, $at) =~ /(?:\A|\n)[ \t\r]*\z/ and $sql =~ m[\G/[ \t\r]*(?:\n|\z)]
$plsql substr($sql, $at, 1) eq "/" $at == 0 substr($sql, 0, $at) =~ /(?:\A|\n)[ \t\r]*\z/ $sql =~ m[\G/[ \t\r]*(?:\n|\z)]
Truth table71%5/7
A and B and C or D and E
A= $plsqlB= substr($sql, $at, 1) eq "/"C= $at == 0D= substr($sql, 0, $at) =~ /(?:\A|\n)[ \t\r]*\z/E= $sql =~ m[\G/[ \t\r]*(?:\n|\z)]
ABCDEresult
0XXXX0
10XXX0
1100X0
111X00
110100
111X11
110111
292 and ($at == 0 or substr($sql, 0, $at) =~ /(?:\A|\n)[ \t\r]*\z/)
293 and $sql =~ m{\G/[ \t\r]*(?:\n|\z)}) {
294 6 ▶ _incomplete_block($sql, $start) if @levels;
Branch: 1 branch
BranchTrueFalseTotal
if @levels 1 5 6
295 5 pos($sql) = $at + 1;
296 5 push @cuts, $at;
297 5 ($start, $unit, $pending, $opened, $compound, $prev) = (undef, undef, 0, 0, 0, ';');
298 5 next;
299 }
300  
301 27734 ▶ $start //= $at;
Condition66%2/3
$start //= $at
l!l&&r!l&&!r
2419135430
MC/DC0%0/2
$start //= $at
$start $at
Truth table66%2/3
A //= B
A= $startB= $at
ABresult
1X1
011
000
302  
303 # A word, with the $ that hangs on it (a$b$, v$session).
304 27734 ▶ if ($sql =~ /\G\w[\w\$]*/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /\G\w[\w\$]*/cg) 18177 9557 27734
305 18177 my $end = pos $sql;
306 18177 my ($was, $word) = ($prev, 'W');
307 18177 $prev = 'W';
308 18177 ▶ next if $end - $at > 5 and !$plsql;
Branch: 1 branch
BranchTrueFalseTotal
if $end - $at > 5 and not $plsql 3839 14338 18177
Condition100%3/3
$end - $at > 5 and not $plsql
!ll&&!rl&&r
141382003839
MC/DC100%2/2
$end - $at > 5 and not $plsql
$end - $at > 5 not $plsql
Truth table100%3/3
A and not(B)
A= $end - $at > 5B= not $plsql
ABresult
0X0
100
111
309 14338 $word = uc substr $sql, $at, $end - $at;
310  
311 14338 ▶ if (substr($sql, $end, 1) eq "'") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($sql, $end, 1) eq "'") 22 14316 14338
312 22 $prev = 'Q';
313 22 ▶ if ($word eq 'E') {
Branch: 2 branches
BranchTrueFalseTotal
if ($word eq 'E') { } 7 15 22
elsif ($word eq 'Q' or $word eq 'NQ') { } 3 12 15
314 7 pos($sql) = $end + 1;
315 7 _skip_quoted(\$sql, "'", 1);
316 }
317 ▶ elsif ($word eq 'Q' or $word eq 'NQ') {
Condition100%3/3
$word eq "Q" or $word eq "NQ"
l!l&&r!l&&!r
2112
MC/DC100%2/2
$word eq "Q" or $word eq "NQ"
$word eq "Q" $word eq "NQ"
Truth table100%3/3
A or B
A= $word eq "Q"B= $word eq "NQ"
ABresult
1X1
011
000
318 # Oracle q'[...]': up to the closing bracket and quote, whatever is
319 # in it. One that is never closed is a word before a literal.
320 3 my $open = substr $sql, $end + 1, 1;
321 3 ▶ my $close = index $sql, ($Q_QUOTE_CLOSE{$open} // $open) . "'", $end + 2;
Condition66%2/3
$Q_QUOTE_CLOSE{$open} // $open
l!l&&r!l&&!r
210
MC/DC0%0/2
$Q_QUOTE_CLOSE{$open} // $open
$Q_QUOTE_CLOSE{$open} $open
Truth table66%2/3
A // B
A= $Q_QUOTE_CLOSE{$open}B= $open
ABresult
1X1
011
000
322 3 ▶ pos($sql) = $close + 2 if $close >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $close >= 0 3 0 3
323 }
324 22 next;
325 }
326  
327 14316 ▶ if ($plsql) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plsql) 686 13630 14316
328 686 $prev = $word;
329 686 ▶ my $dotted = $at && substr($sql, $at - 1, 1) eq '.';
Condition100%3/3
$at && substr($sql, $at - 1, 1) eq '.'
!ll&&!rl&&r
376445
MC/DC100%2/2
$at && substr($sql, $at - 1, 1) eq '.'
$at substr($sql, $at - 1, 1) eq '.'
Truth table100%3/3
A && B
A= $atB= substr($sql, $at - 1, 1) eq '.'
ABresult
0X0
100
111
330  
331 686 ▶ if (!$unit and $at == $start and !$dotted) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $unit and $at == $start and not $dotted) 57 629 686
Condition83%5/6
not $unit and $at == $start and not $dotted
!ll&&!rl&&r
629057
not $unit and $at == $start
!ll&&!rl&&r
5755457
MC/DC66%2/3
not $unit and $at == $start and not $dotted
not $unit $at == $start not $dotted
Truth table75%3/4
not(A) and B and not(C)
A= not $unitB= $at == $startC= not $dotted
ABCresult
0XX0
10X0
1100
1111
332 57 ▶ if ($word eq 'DECLARE' or $word eq 'BEGIN') {
Branch: 2 branches
BranchTrueFalseTotal
if ($word eq 'DECLARE' or $word eq 'BEGIN') { } 13 44 57
elsif ($word eq 'CREATE') { } 29 15 44
Condition100%3/3
$word eq "DECLARE" or $word eq "BEGIN"
l!l&&r!l&&!r
21144
MC/DC100%2/2
$word eq "DECLARE" or $word eq "BEGIN"
$word eq "DECLARE" $word eq "BEGIN"
Truth table100%3/3
A or B
A= $word eq "DECLARE"B= $word eq "BEGIN"
ABresult
1X1
011
000
333 13 ($unit, $opened) = ('block', 1);
334 13 ▶ push @levels, $word eq 'BEGIN' ? 'X' : 'D';
Branch: 1 branch
BranchTrueFalseTotal
$word eq 'BEGIN' ? : 11 2 13
335 }
336 elsif ($word eq 'CREATE') {
337 29 my $here = pos $sql;
338 29 ▶ if ($sql =~ /\G$PLSQL_UNIT/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /\G$PLSQL_UNIT/cg) 27 2 29
339 27 my $head = substr $sql, $here, pos($sql) - $here;
340 27 ▶ $unit = $head =~ /TRIGGER\z/i ? 'trigger' : 'routine';
Branch: 1 branch
BranchTrueFalseTotal
$head =~ /TRIGGER\z/i ? : 8 19 27
341 27 ▶ $pending = $unit eq 'routine' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$unit eq 'routine' ? : 19 8 27
342 27 $opened = 0;
343 }
344 29 pos($sql) = $here;
345 }
346 57 next;
347 }
348  
349 629 ▶ next if !$unit or $dotted;
Branch: 1 branch
BranchTrueFalseTotal
if not $unit or $dotted 59 570 629
Condition100%3/3
not $unit or $dotted
l!l&&r!l&&!r
545570
MC/DC100%2/2
not $unit or $dotted
not $unit $dotted
Truth table100%3/3
not(A) or B
A= not $unitB= $dotted
ABresult
1X1
011
000
350 570 ▶ my $top = @levels ? $levels[-1] : '';
Branch: 1 branch
BranchTrueFalseTotal
@levels ? : 388 182 570
351  
352 570 ▶ if (!@levels and $unit eq 'trigger') {
Branch: 8 branches
BranchTrueFalseTotal
if (not @levels and $unit eq 'trigger') { } 78 492 570
elsif ($compound and @levels == 1 and $top eq 'D' and not $paren and $word eq 'BEFORE' || $word eq 'AFTER' || $word eq 'INSTEAD' and $sql =~ /\G\s+(?:OF\s+)?(?:STATEMENT|EACH\s+ROW)\b/i) { } 7 485 492
elsif ($pending and not $paren and $word eq 'IS' || $word eq 'AS') { } 34 451 485
elsif ($top eq 'D' and $word eq 'FUNCTION' || $word eq 'PROCEDURE') { } 12 439 451
elsif (@levels and $word eq 'CASE') { } 4 435 439
elsif ($word eq 'BEGIN' and $top eq 'D') { } 32 403 435
elsif ($word eq 'BEGIN' || $word eq 'DECLARE' and $top eq 'X' and $was =~ /\A(?:;|>|THEN|ELSE|LOOP|BEGIN)\z/) { } 2 401 403
elsif ($word eq 'END' and @levels) { } 62 678 740
Condition100%3/3
not @levels and $unit eq "trigger"
!ll&&!rl&&r
38810478
MC/DC100%2/2
not @levels and $unit eq "trigger"
not @levels $unit eq "trigger"
Truth table100%3/3
not(A) and B
A= not @levelsB= $unit eq "trigger"
ABresult
0X0
100
111
353 # BEGIN or DECLARE of the trigger: not a column of UPDATE OF ...
354 78 ▶ if (($word eq 'BEGIN' or $word eq 'DECLARE') and !$paren and $was ne 'OF' and $was ne ',') {
Branch: 2 branches
BranchTrueFalseTotal
if ($word eq 'BEGIN' || $word eq 'DECLARE' and not $paren and $was ne 'OF' and $was ne ',') { } 4 74 78
elsif ($word eq 'COMPOUND' and not $paren and $sql =~ /\G\s+TRIGGER\b/cig) { } 4 140 144
Condition75%9/12
$word eq "BEGIN" || $word eq "DECLARE" and not $paren and $was ne "OF" and $was ne ","
!ll&&!rl&&r
7404
$word eq "BEGIN" || $word eq "DECLARE" and not $paren and $was ne "OF"
!ll&&!rl&&r
7404
$word eq "BEGIN" || $word eq "DECLARE" and not $paren
!ll&&!rl&&r
7404
$word eq "BEGIN" or $word eq "DECLARE"
l!l&&r!l&&!r
3174
MC/DC40%2/5
$word eq "BEGIN" || $word eq "DECLARE" and not $paren and $was ne "OF" and $was ne ","
$word eq "BEGIN" $word eq "DECLARE" not $paren $was ne "OF" $was ne ","
Truth table33%3/9
A or B and not(C) and D and E
A= $word eq "BEGIN"B= $word eq "DECLARE"C= not $parenD= $was ne "OF"E= $was ne ","
ABCDEresult
00XXX0
1X0XX0
010XX0
1X10X0
0110X0
1X1100
011100
1X1111
011111
355 4 ▶ push @levels, $word eq 'BEGIN' ? 'X' : 'D';
Branch: 1 branch
BranchTrueFalseTotal
$word eq 'BEGIN' ? : 3 1 4
356 4 $opened = 1;
357 }
358 ▶ elsif ($word eq 'COMPOUND' and !$paren and $sql =~ /\G\s+TRIGGER\b/gci) {
Condition66%4/6
$word eq "COMPOUND" and not $paren and $sql =~ /\G\s+TRIGGER\b/cig
!ll&&!rl&&r
7004
$word eq "COMPOUND" and not $paren
!ll&&!rl&&r
7004
MC/DC33%1/3
$word eq "COMPOUND" and not $paren and $sql =~ /\G\s+TRIGGER\b/cig
$word eq "COMPOUND" not $paren $sql =~ /\G\s+TRIGGER\b/cig
Truth table50%2/4
A and not(B) and C
A= $word eq "COMPOUND"B= not $parenC= $sql =~ /\G\s+TRIGGER\b/cig
ABCresult
0XX0
10X0
1100
1111
359 # COMPOUND TRIGGER: the unit's own level, its timing-point
360 # sections open and close levels of their own inside it.
361 4 push @levels, 'D';
362 4 ($opened, $compound) = (1, 1);
363 4 $prev = 'TRIGGER';
364 }
365 }
366 ▶ elsif ($compound and @levels == 1 and $top eq 'D' and !$paren
Condition85%18/21
$compound and @levels == 1 and $top eq "D" and not $paren and $word eq "BEFORE" || $word eq "AFTER" || $word eq "INSTEAD" and $sql =~ /\G\s+(?:OF\s+)?(?:STATEMENT|EACH\s+ROW)\b/i
!ll&&!rl&&r
48507
$compound and @levels == 1 and $top eq "D" and not $paren and $word eq "BEFORE" || $word eq "AFTER" || $word eq "INSTEAD"
!ll&&!rl&&r
451347
$compound and @levels == 1 and $top eq "D" and not $paren
!ll&&!rl&&r
451041
$compound and @levels == 1 and $top eq "D"
!ll&&!rl&&r
451041
$compound and @levels == 1
!ll&&!rl&&r
4153641
$word eq "BEFORE" or $word eq "AFTER" or $word eq "INSTEAD"
l!l&&r!l&&!r
5234
$word eq "BEFORE" or $word eq "AFTER"
l!l&&r!l&&!r
3236
MC/DC62%5/8
$compound and @levels == 1 and $top eq "D" and not $paren and $word eq "BEFORE" || $word eq "AFTER" || $word eq "INSTEAD" and $sql =~ /\G\s+(?:OF\s+)?(?:STATEMENT|EACH\s+ROW)\b/i
$compound @levels == 1 $top eq "D" not $paren $word eq "BEFORE" $word eq "AFTER" $word eq "INSTEAD" $sql =~ /\G\s+(?:OF\s+)?(?:STATEMENT|EACH\s+ROW)\b/i
Truth table54%6/11
A and B and C and not(D) and E or F or G and H
A= $compoundB= @levels == 1C= $top eq "D"D= not $parenE= $word eq "BEFORE"F= $word eq "AFTER"G= $word eq "INSTEAD"H= $sql =~ /\G\s+(?:OF\s+)?(?:STATEMENT|EACH\s+ROW)\b/i
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
1111000X0
11111XX00
111101X00
111100100
11111XX11
111101X11
111100111
367 and ($word eq 'BEFORE' or $word eq 'AFTER' or $word eq 'INSTEAD')
368 and $sql =~ /\G\s+(?:OF\s+)?(?:STATEMENT|EACH\s+ROW)\b/i) {
369 7 $pending = 1; # BEFORE STATEMENT IS ...: the section's head
370 }
371 ▶ elsif ($pending and !$paren and ($word eq 'IS' or $word eq 'AS')) {
Condition100%9/9
$pending and not $paren and $word eq "IS" || $word eq "AS"
!ll&&!rl&&r
32412734
$pending and not $paren
!ll&&!rl&&r
31212161
$word eq "IS" or $word eq "AS"
l!l&&r!l&&!r
259127
MC/DC100%4/4
$pending and not $paren and $word eq "IS" || $word eq "AS"
$pending not $paren $word eq "IS" $word eq "AS"
Truth table100%5/5
A and not(B) and C or D
A= $pendingB= not $parenC= $word eq "IS"D= $word eq "AS"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
372 34 ▶ next if $word eq 'AS' and $sql =~ /\G\s+RESULT\b/i; # RETURN SELF AS RESULT
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "AS" and $sql =~ /\G\s+RESULT\b/i 1 33 34
Condition100%3/3
$word eq "AS" and $sql =~ /\G\s+RESULT\b/i
!ll&&!rl&&r
2581
MC/DC100%2/2
$word eq "AS" and $sql =~ /\G\s+RESULT\b/i
$word eq "AS" $sql =~ /\G\s+RESULT\b/i
Truth table100%3/3
A and B
A= $word eq "AS"B= $sql =~ /\G\s+RESULT\b/i
ABresult
0X0
100
111
373 33 $pending = 0;
374 # AS LANGUAGE ...: a call specification has no body.
375 33 ▶ unless ($sql =~ /\G\s+(?:LANGUAGE|EXTERNAL)\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sql =~ /\G\s+(?:LANGUAGE|EXTERNAL)\b/i) 33 0 33
376 33 push @levels, 'D';
377 33 $opened = 1;
378 }
379 }
380 ▶ elsif ($top eq 'D' and ($word eq 'FUNCTION' or $word eq 'PROCEDURE')) {
Condition100%6/6
$top eq "D" and $word eq "FUNCTION" || $word eq "PROCEDURE"
!ll&&!rl&&r
30713212
$word eq "FUNCTION" or $word eq "PROCEDURE"
l!l&&r!l&&!r
75132
MC/DC100%3/3
$top eq "D" and $word eq "FUNCTION" || $word eq "PROCEDURE"
$top eq "D" $word eq "FUNCTION" $word eq "PROCEDURE"
Truth table100%4/4
A and B or C
A= $top eq "D"B= $word eq "FUNCTION"C= $word eq "PROCEDURE"
ABCresult
0XX0
1000
11X1
1011
381 12 $pending = 1;
382 }
383 ▶ elsif (@levels and $word eq 'CASE') {
Condition100%3/3
@levels and $word eq "CASE"
!ll&&!rl&&r
853504
MC/DC100%2/2
@levels and $word eq "CASE"
@levels $word eq "CASE"
Truth table100%3/3
A and B
A= @levelsB= $word eq "CASE"
ABresult
0X0
100
111
384 4 push @levels, 'C';
385 }
386 ▶ elsif ($word eq 'BEGIN' and $top eq 'D') {
Condition100%3/3
$word eq "BEGIN" and $top eq "D"
!ll&&!rl&&r
402132
MC/DC100%2/2
$word eq "BEGIN" and $top eq "D"
$word eq "BEGIN" $top eq "D"
Truth table100%3/3
A and B
A= $word eq "BEGIN"B= $top eq "D"
ABresult
0X0
100
111
387 32 $levels[-1] = 'X';
388 }
389 ▶ elsif (($word eq 'BEGIN' or $word eq 'DECLARE') and $top eq 'X'
Condition77%7/9
$word eq "BEGIN" || $word eq "DECLARE" and $top eq "X" and $was =~ /\A(?:;|>|THEN|ELSE|LOOP|BEGIN)\z/
!ll&&!rl&&r
40102
$word eq "BEGIN" || $word eq "DECLARE" and $top eq "X"
!ll&&!rl&&r
40102
$word eq "BEGIN" or $word eq "DECLARE"
l!l&&r!l&&!r
11401
MC/DC50%2/4
$word eq "BEGIN" || $word eq "DECLARE" and $top eq "X" and $was =~ /\A(?:;|>|THEN|ELSE|LOOP|BEGIN)\z/
$word eq "BEGIN" $word eq "DECLARE" $top eq "X" $was =~ /\A(?:;|>|THEN|ELSE|LOOP|BEGIN)\z/
Truth table42%3/7
A or B and C and D
A= $word eq "BEGIN"B= $word eq "DECLARE"C= $top eq "X"D= $was =~ /\A(?:;|>|THEN|ELSE|LOOP|BEGIN)\z/
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
390 and $was =~ /\A(?:;|>|THEN|ELSE|LOOP|BEGIN)\z/) {
391 2 ▶ push @levels, $word eq 'BEGIN' ? 'X' : 'D';
Branch: 1 branch
BranchTrueFalseTotal
$word eq 'BEGIN' ? : 1 1 2
392 }
393 ▶ elsif ($word eq 'END' and @levels) {
Condition66%2/3
$word eq "END" and @levels
!ll&&!rl&&r
339062
MC/DC50%1/2
$word eq "END" and @levels
$word eq "END" @levels
Truth table66%2/3
A and B
A= $word eq "END"B= @levels
ABresult
0X0
100
111
394 62 ▶ if ($sql =~ /\G\s+(?:IF|LOOP)\b/gci) {
Branch: 3 branches
BranchTrueFalseTotal
if ($sql =~ /\G\s+(?:IF|LOOP)\b/cig) { } 6 56 62
elsif ($top eq 'C') { } 4 52 56
elsif ($top eq 'X' and $was ne ';' and $was ne 'BEGIN') { } 2 50 52
395 # END IF, END LOOP: the construct's own end.
396 }
397 elsif ($top eq 'C') {
398 4 pop @levels;
399 4 $sql =~ /\G\s+CASE\b/gci; # END CASE
400 }
401 ▶ elsif ($top eq 'X' and $was ne ';' and $was ne 'BEGIN') {
Condition83%5/6
$top eq "X" and $was ne ";" and $was ne "BEGIN"
!ll&&!rl&&r
5002
$top eq "X" and $was ne ";"
!ll&&!rl&&r
8422
MC/DC66%2/3
$top eq "X" and $was ne ";" and $was ne "BEGIN"
$top eq "X" $was ne ";" $was ne "BEGIN"
Truth table75%3/4
A and B and C
A= $top eq "X"B= $was ne ";"C= $was ne "BEGIN"
ABCresult
0XX0
10X0
1100
1111
402 # SELECT end, ...: a column.
403 }
404 else {
405 50 pop @levels;
406 # END BEFORE STATEMENT: the name of the section it closes.
407 50 ▶ $sql =~ /\G\s+(?:BEFORE|AFTER|INSTEAD)\s+(?:OF\s+)?(?:STATEMENT|EACH\s+ROW)\b/gci
Branch: 1 branch
BranchTrueFalseTotal
if $compound and @levels == 1 8 42 50
Condition100%3/3
$compound and @levels == 1
!ll&&!rl&&r
3938
MC/DC100%2/2
$compound and @levels == 1
$compound @levels == 1
Truth table100%3/3
A and B
A= $compoundB= @levels == 1
ABresult
0X0
100
111
408 if $compound and @levels == 1;
409 }
410 }
411 569 next;
412 }
413  
414 13630 ▶ unless ($word eq 'BEGIN' or @levels and ($word eq 'END' or $word eq 'CASE')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($word eq "BEGIN" or @levels and $word eq "END" || $word eq "CASE") 13487 143 13630
Condition100%9/9
$word eq "BEGIN" or @levels and $word eq "END" || $word eq "CASE"
l!l&&r!l&&!r
707313487
@levels and $word eq "END" || $word eq "CASE"
!ll&&!rl&&r
1320228573
$word eq "END" or $word eq "CASE"
l!l&&r!l&&!r
649285
MC/DC100%4/4
$word eq "BEGIN" or @levels and $word eq "END" || $word eq "CASE"
$word eq "BEGIN" @levels $word eq "END" $word eq "CASE"
Truth table100%5/5
A or B and C or D
A= $word eq "BEGIN"B= @levelsC= $word eq "END"D= $word eq "CASE"
ABCDresult
1XXX1
011X1
01011
00XX0
01000
415 13487 ▶ $prev = $word if $word ne 'W';
Branch: 1 branch
BranchTrueFalseTotal
if $word ne "W" 13487 0 13487
416 13487 next;
417 }
418  
419 # new.end, t.begin: a column.
420 143 ▶ next if $at and substr($sql, $at - 1, 1) eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $at and substr($sql, $at - 1, 1) eq "." 4 139 143
Condition100%3/3
$at and substr($sql, $at - 1, 1) eq "."
!ll&&!rl&&r
21374
MC/DC100%2/2
$at and substr($sql, $at - 1, 1) eq "."
$at substr($sql, $at - 1, 1) eq "."
Truth table100%3/3
A and B
A= $atB= substr($sql, $at - 1, 1) eq "."
ABresult
0X0
100
111
421  
422 139 ▶ if (!@levels) {
Branch: 5 branches
BranchTrueFalseTotal
if (not @levels) { } 54 85 139
elsif ($word eq 'CASE') { } 8 77 85
elsif ($word eq 'BEGIN') { } 15 62 77
elsif ($levels[-1] eq 'B' and $was ne ';' and $was ne 'BEGIN') { } 10 52 62
elsif ($sql =~ /\G\s+(?:IF|LOOP|WHILE|REPEAT)\b/cig) { } 4 48 52
423 54 ▶ $trigger //= substr($sql, $start, $at - $start) =~ $TRIGGER_HEAD ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
substr($sql, $start, $at - $start) =~ /$TRIGGER_HEAD/ ? : 30 24 54
Condition50%1/2
$trigger //= substr($sql, $start, $at - $start) =~ /$TRIGGER_HEAD/ ? 1 : 0
l!l
054
MC/DC0%0/1
$trigger //= substr($sql, $start, $at - $start) =~ /$TRIGGER_HEAD/ ? 1 : 0
$trigger
Truth table50%1/2
A
A= $trigger
Aresult
11
00
424 # BEGIN ATOMIC of a routine (dialect postgresql) is the same level.
425 54 ▶ if ($trigger
Branch: 1 branch
BranchTrueFalseTotal
if ($trigger or $atomic and $word eq "BEGIN" and substr($sql, $start, $at - $start) =~ /$ROUTINE_HEAD/ and $sql =~ /\G\s+ATOMIC\b/cig) 38 16 54
Condition83%10/12
$trigger or $atomic and $word eq "BEGIN" and substr($sql, $start, $at - $start) =~ /$ROUTINE_HEAD/ and $sql =~ /\G\s+ATOMIC\b/cig
l!l&&r!l&&!r
30816
$atomic and $word eq "BEGIN" and substr($sql, $start, $at - $start) =~ /$ROUTINE_HEAD/ and $sql =~ /\G\s+ATOMIC\b/cig
!ll&&!rl&&r
1608
$atomic and $word eq "BEGIN" and substr($sql, $start, $at - $start) =~ /$ROUTINE_HEAD/
!ll&&!rl&&r
1518
$atomic and $word eq "BEGIN"
!ll&&!rl&&r
1509
MC/DC60%3/5
$trigger or $atomic and $word eq "BEGIN" and substr($sql, $start, $at - $start) =~ /$ROUTINE_HEAD/ and $sql =~ /\G\s+ATOMIC\b/cig
$trigger $atomic $word eq "BEGIN" substr($sql, $start, $at - $start) =~ /$ROUTINE_HEAD/ $sql =~ /\G\s+ATOMIC\b/cig
Truth table66%4/6
A or B and C and D and E
A= $triggerB= $atomicC= $word eq "BEGIN"D= substr($sql, $start, $at - $start) =~ /$ROUTINE_HEAD/E= $sql =~ /\G\s+ATOMIC\b/cig
ABCDEresult
1XXXX1
011111
00XXX0
010XX0
0110X0
011100
426 or $atomic and $word eq 'BEGIN'
427 and substr($sql, $start, $at - $start) =~ $ROUTINE_HEAD
428 and $sql =~ /\G\s+ATOMIC\b/gci) {
429 38 push @levels, 'B';
430 38 $prev = 'BEGIN';
431 }
432 }
433 elsif ($word eq 'CASE') {
434 8 push @levels, 'C';
435 }
436 elsif ($word eq 'BEGIN') {
437 # Opens a block where a statement can start: behind a semicolon, a
438 # label or THEN / ELSE / DO / LOOP / REPEAT. Anywhere else (SET begin
439 # = 1, WHERE begin = 1) it is a column.
440 15 ▶ if ($was =~ /\A(?:;|:|BEGIN|THEN|ELSE|DO|LOOP|REPEAT)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($was =~ /\A(?:;|:|BEGIN|THEN|ELSE|DO|LOOP|REPEAT)\z/) 2 13 15
441 2 push @levels, 'B';
442 2 $prev = 'BEGIN';
443 }
444 }
445 ▶ elsif ($levels[-1] eq 'B' and $was ne ';' and $was ne 'BEGIN') {
Condition100%6/6
$levels[-1] eq "B" and $was ne ";" and $was ne "BEGIN"
!ll&&!rl&&r
50210
$levels[-1] eq "B" and $was ne ";"
!ll&&!rl&&r
84212
MC/DC100%3/3
$levels[-1] eq "B" and $was ne ";" and $was ne "BEGIN"
$levels[-1] eq "B" $was ne ";" $was ne "BEGIN"
Truth table100%4/4
A and B and C
A= $levels[-1] eq "B"B= $was ne ";"C= $was ne "BEGIN"
ABCresult
0XX0
10X0
1100
1111
446 # SET end = 1, SELECT end, ...: an END that closes a block stands
447 # where a statement ends or a body is empty. A CASE's END is not
448 # asked this: it closes its CASE wherever it stands.
449 }
450 elsif ($sql =~ /\G\s+(?:IF|LOOP|WHILE|REPEAT)\b/gci) {
451 # Closes a construct whose opening was not counted.
452 }
453 else {
454 48 pop @levels;
455 48 $sql =~ /\G\s+CASE\b/gci; # END CASE: this CASE opens nothing
456 }
457 139 next;
458 }
459  
460 9557 my $char = substr $sql, $at, 1;
461 9557 pos($sql) = $at + 1;
462  
463 9557 $prev = $char;
464  
465 9557 ▶ if ($char eq ';') {
Branch: 6 branches
BranchTrueFalseTotal
if ($char eq ';') { } 3606 5951 9557
elsif ($char eq '(') { } 2585 3366 5951
elsif ($char eq ')') { } 2584 782 3366
elsif ($char eq q['] or $char eq '"' or $char eq '`') { } 499 283 782
elsif ($char eq '[') { } 18 265 283
elsif ($char eq '$') { } 17 248 265
466 3606 ▶ next if @levels or $paren;
Branch: 1 branch
BranchTrueFalseTotal
if @levels or $paren 183 3423 3606
Condition100%3/3
@levels or $paren
l!l&&r!l&&!r
17943423
MC/DC100%2/2
@levels or $paren
@levels $paren
Truth table100%3/3
A or B
A= @levelsB= $paren
ABresult
1X1
011
000
467 3423 push @cuts, $at;
468 3423 ▶ $keep->{$at} = 1 if $plsql and $unit and ($opened or $unit ne 'trigger');
Branch: 1 branch
BranchTrueFalseTotal
if $plsql and $unit and $opened || $unit ne "trigger" 34 3389 3423
Condition66%6/9
$plsql and $unit and $opened || $unit ne "trigger"
!ll&&!rl&&r
3389034
$plsql and $unit
!ll&&!rl&&r
33771234
$opened or $unit ne "trigger"
l!l&&r!l&&!r
3400
MC/DC50%2/4
$plsql and $unit and $opened || $unit ne "trigger"
$plsql $unit $opened $unit ne "trigger"
Truth table60%3/5
A and B and C or D
A= $plsqlB= $unitC= $openedD= $unit ne "trigger"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
469 3423 ($start, $trigger, $unit, $pending, $opened, $compound) = (undef, undef, undef, 0, 0, 0);
470 }
471 elsif ($char eq '(') {
472 2585 $paren++;
473 }
474 elsif ($char eq ')') {
475 2584 ▶ $paren-- if $paren;
Branch: 1 branch
BranchTrueFalseTotal
if $paren 2583 1 2584
476 }
477 ▶ elsif ($char eq "'" or $char eq '"' or $char eq '`') {
Condition100%6/6
$char eq "'" or $char eq "\"" or $char eq "`"
l!l&&r!l&&!r
45049283
$char eq "'" or $char eq "\""
l!l&&r!l&&!r
4428332
MC/DC100%3/3
$char eq "'" or $char eq "\"" or $char eq "`"
$char eq "'" $char eq "\"" $char eq "`"
Truth table100%4/4
A or B or C
A= $char eq "'"B= $char eq "\""C= $char eq "`"
ABCresult
1XX1
01X1
0011
0000
478 499 $prev = 'Q';
479 499 _skip_quoted(\$sql, $char, $backslash);
480 }
481 elsif ($char eq '[') {
482 # An identifier, unless it is a subscript or an array: then the bracket
483 # is a character and what follows it is read on.
484 18 $prev = 'Q';
485 18 ▶ _skip_quoted(\$sql, ']', 0)
Branch: 1 branch
BranchTrueFalseTotal
unless $at and substr($sql, $at - 1, 1) =~ /[\w\]\)]/ or $sql =~ /\G\s*['\[]/ 4 14 18
Condition83%5/6
$at and substr($sql, $at - 1, 1) =~ /[\w\]\)]/ or $sql =~ /\G\s*['\[]/
l!l&&r!l&&!r
774
$at and substr($sql, $at - 1, 1) =~ /[\w\]\)]/
!ll&&!rl&&r
0117
MC/DC66%2/3
$at and substr($sql, $at - 1, 1) =~ /[\w\]\)]/ or $sql =~ /\G\s*['\[]/
$at substr($sql, $at - 1, 1) =~ /[\w\]\)]/ $sql =~ /\G\s*['\[]/
Truth table60%3/5
A and B or C
A= $atB= substr($sql, $at - 1, 1) =~ /[\w\]\)]/C= $sql =~ /\G\s*['\[]/
ABCresult
11X1
0X11
1011
0X00
1000
486 unless ($at and substr($sql, $at - 1, 1) =~ /[\w\]\)]/)
487 or $sql =~ /\G\s*['\[]/;
488 }
489 elsif ($char eq '$') {
490 17 $prev = 'Q';
491 17 pos($sql) = $at;
492 17 ▶ if ($sql =~ /\G$DOLLAR_QUOTE/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /\G$DOLLAR_QUOTE/cg) 11 6 17
493 11 my $tag = substr $sql, $at, pos($sql) - $at;
494 11 my $close = index $sql, $tag, pos $sql;
495 11 ▶ if ($close >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close >= 0) 8 3 11
496 8 pos($sql) = $close + length $tag;
497 8 next;
498 }
499 3 pos($sql) = $at;
500 }
501  
502 # A $ that opens nothing, with what hangs on it: $1, an unclosed $tag$.
503 9 $sql =~ /\G\$[\w\$]*/gc;
504 }
505 }
506  
507 367 ▶ _incomplete_block($sql, $start) if $plsql and @levels;
Branch: 1 branch
BranchTrueFalseTotal
if $plsql and @levels 5 362 367
Condition100%3/3
$plsql and @levels
!ll&&!rl&&r
330325
MC/DC100%2/2
$plsql and @levels
$plsql @levels
Truth table100%3/3
A and B
A= $plsqlB= @levels
ABresult
0X0
100
111
508  
509 362 return @cuts;
510 }
511  
512 6 ▶sub _incomplete_block {
Subroutine _incomplete_block: called CC=2 SCAR=7.0
513 6 my ($sql, $start) = @_;
514  
515 6 ▶ my $head = substr $sql, $start // 0, 60;
Condition50%1/2
$start // 0
l!l
60
MC/DC0%0/1
$start // 0
$start
Truth table50%1/2
A
A= $start
Aresult
11
00
516 6 $head =~ s/\s+/ /g;
517 6 DBIO::Exception->throw(
518 "_split_statements: PL/SQL block is not closed (begins '$head')");
519 }
520  
521 # The offset behind the nesting block comment that opens at $at: its /* and */
522 # are counted, and one never closed runs to the end of the text. Sets pos().
523 12 ▶sub _comment_end {
Subroutine _comment_end: called CC=4 SCAR=13.9
524 12 my ($sql, $at) = @_;
525  
526 12 my $depth = 1;
527 12 pos($$sql) = $at + 2;
528 12 ▶ while ($$sql =~ m{\G.*?(?:/\*|\*/)}gcs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$sql =~ m[\G.*?(?:/\*|\*/)]cgs) 28 4 32
529 28 ▶ $depth += substr($$sql, pos($$sql) - 2, 2) eq '/*' ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
substr($$sql, pos($$sql) - 2, 2) eq '/*' ? : 11 17 28
530 28 ▶ return pos $$sql unless $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless $depth 8 20 28
531 }
532  
533 4 return length $$sql;
534 }
535  
536 # Moves pos() of $$sql from behind an opening quote to behind the closing one,
537 # or to the end of the text when the quote is never closed.
538 510 ▶sub _skip_quoted {
Subroutine _skip_quoted: called CC=7 SCAR=19.5
539 510 my ($sql, $close, $backslash) = @_;
540  
541 510 ▶ my $run = $QUOTED_RUN{$close}[ $backslash ? 1 : 0 ];
Branch: 1 branch
BranchTrueFalseTotal
$backslash ? : 35 475 510
542 510 my $len = length $$sql;
543  
544 510 ▶ while ((my $at = pos $$sql) < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $at = pos $$sql) < $len) 1062 17 1079
545 1062 ▶ next if $$sql =~ /$run/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$sql =~ /$run/cg 537 525 1062
546  
547 525 ▶ if (substr($$sql, $at, 1) ne $close) {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($$sql, $at, 1) ne $close) { } 25 500 525
elsif (substr($$sql, $at + 1, 1) eq $close) { } 7 493 500
548 # A backslash: it and the character behind it.
549 25 ▶ pos($$sql) = $at + 2 > $len ? $len : $at + 2;
Branch: 1 branch
BranchTrueFalseTotal
$at + 2 > $len ? : 0 25 25
550 }
551 elsif (substr($$sql, $at + 1, 1) eq $close) {
552 # Doubled: still inside.
553 7 pos($$sql) = $at + 2;
554 }
555 else {
556 493 pos($$sql) = $at + 1;
557 493 return;
558 }
559 }
560  
561 17 return;
562 }
563  
564 # The [from, to) spans between dollar-quote delimiters in the bare text, in
565 # ascending order: nothing else is read, so a delimiter inside a literal or a
566 # comment counts like any other.
567 17 ▶sub _dollar_quoted_spans {
Subroutine _dollar_quoted_spans: called CC=3 SCAR=11.0
568 17 my ($sql) = @_;
569  
570 17 my @spans;
571 17 ▶ while ($sql =~ /(?<![\w\$])$DOLLAR_QUOTE/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /(?<![\w\$])$DOLLAR_QUOTE/g) 3 17 20
572 3 my $from = $-[0];
573 3 my $tag = substr $sql, $from, pos($sql) - $from;
574 3 my $close = index $sql, $tag, pos $sql;
575 3 ▶ next if $close < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $close < 0 0 3 3
576 3 push @spans, [ $from, $close + length $tag ];
577 3 pos($sql) = $close + length $tag;
578 }
579  
580 17 return @spans;
581 }
582  
583 # The DROP statements of an add_drop_table pre-pass, shared by the synchronous
584 # deploy (DBIO::Deploy::Base::install) and the asynchronous one
585 # (DBIO::Storage::Async::_drop_statements_for).
586 #
587 # my $sql = _drop_statements($schema, sub { quote($_[0]) }, { cascade => 1 });
588 #
589 # First DROP VIEW IF EXISTS ...; for every declared view that is a real view
590 # (virtual views and names that are no plain identifier are left out), in the
591 # reverse of their install order, then DROP TABLE IF EXISTS ... [CASCADE]; for
592 # every regular table source. Every name is written through the $quote code
593 # reference. The statements come back as one string, separated by a blank line.
594 13 ▶sub _drop_statements {
Subroutine _drop_statements: called CC=17 SCAR=28.4
595 13 my ($schema, $quote, $opts) = @_;
596 13 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
130
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
597  
598 13 my (@tables, %view);
599 13 for my $name ($schema->sources) {
600 426 my $source = $schema->source($name);
601 426 my $table = $source->name;
602 426 ▶ next if ref $table;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table 22 404 426
603 404 ▶ next if $table =~ /\s|\(/;
Branch: 1 branch
BranchTrueFalseTotal
if $table =~ /\s|\(/ 3 401 404
604 401 ▶ if ($source->isa('DBIO::ResultSource::View')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->isa('DBIO::ResultSource::View')) { } 25 376 401
605 25 ▶ next if $source->is_virtual;
Branch: 1 branch
BranchTrueFalseTotal
if $source->is_virtual 10 15 25
606 15 $view{$name} = $source;
607 }
608 else {
609 376 push @tables, $name;
610 }
611 }
612 13 my @view_names = sort keys %view;
613  
614 # The install order: source names in order, each behind the views its
615 # deploy_depends_on names (a class that is no view here orders nothing).
616 13 ▶ my %view_of_class = map { ($view{$_}->result_class // '') => $_ } @view_names;
Condition100%2/2
$view{$_}->result_class // ""
l!l
123
MC/DC100%1/1
$view{$_}->result_class // ""
$view{$_}->result_class
Truth table100%2/2
A
A= $view{$_}->result_class
Aresult
11
00
617 13 my (@install, %placed);
618 13 for my $first (@view_names) {
619 15 ▶ next if $placed{$first};
Branch: 1 branch
BranchTrueFalseTotal
if $placed{$first} 1 14 15
620 14 my @path = ($first);
621 14 my %on_path = ($first => 1);
622 14 ▶ while (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 16 14 30
623 next_view: {
624 16 my $on = $view{ $path[-1] }->deploy_depends_on;
625 16 for my $dep (
626 2 ▶ grep { defined $_ && !$placed{$_} && !$on_path{$_} }
Condition66%4/6
defined $_ && !$placed{$_} && !$on_path{$_}
!ll&&!rl&&r
101
defined $_ and not $placed{$_}
!ll&&!rl&&r
011
MC/DC33%1/3
defined $_ && !$placed{$_} && !$on_path{$_}
defined $_ not $placed{$_} !$on_path{$_}
Truth table50%2/4
A and not(B) && not(C)
A= defined $_B= not $placed{$_}C= !$on_path{$_}
ABCresult
0XX0
10X0
1100
1111
627 2 ▶ map { $view_of_class{$_} }
Branch: 2 branches
BranchTrueFalseTotal
ref $on eq 'HASH' ? : 11 5 16
ref $on eq 'ARRAY' ? : 0 5 5
628 ref $on eq 'HASH' ? sort keys %$on : ref $on eq 'ARRAY' ? @$on : ()
629 ) {
630 1 push @path, $dep;
631 1 $on_path{$dep} = 1;
632 1 last next_view;
633 }
634 15 my $done = pop @path;
635 15 delete $on_path{$done};
636 15 push @install, $done;
637 15 $placed{$done} = 1;
638 }
639 }
640 }
641  
642 my @out = (
643 15 (map { sprintf 'DROP VIEW IF EXISTS %s;', $quote->($view{$_}->name) } reverse @install),
644 13 ▶ (map { sprintf 'DROP TABLE IF EXISTS %s' . ($opts->{cascade} ? ' CASCADE' : '') . ';', $quote->($schema->source($_)->name) } @tables),
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'cascade'} ? : 335 41 376
645 );
646 13 return join("\n\n", @out);
647 }
648  
649 384 1;
650  
651 __END__
« blib/lib/DBIO/Test/Schema.pm ↑ Summary blib/lib/DBIO/ResultSet.pm »