Parser.pm

filter: Statementstmt 89.0% 1228 / 1379 Branchbran 73.6% 577 / 784 Conditioncond 71.7% 178 / 248 MC/DCmcdc 43.3% 62 / 143 Subroutinesub 92.9% 79 / 85 Podpod 44.7% 30 / 67 totaltotal 79.6% 2154 / 2706 CC 510 SCAR 31.0
CC
510
Cov
82%
list
132.0
ROW_VALUE
64.8
CREATE
37.1
CRAP 22.3
« Statement.pm ↑ Summary Statement/TermFactory.pm »
1 package SQL::Parser;
2  
3 ######################################################################
4 #
5 # This module is copyright (c), 2001,2005 by Jeff Zucker.
6 # This module is copyright (c), 2007-2020 by Jens Rehsack.
7 # All rights reserved.
8 #
9 # It may be freely distributed under the same terms as Perl itself.
10 # See below for help and copyright information (search for SYNOPSIS).
11 #
12 ######################################################################
13  
14 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use warnings FATAL => "all";
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use constant FUNCTION_NAMES => join( '|', qw(TRIM SUBSTRING) );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 17 ▶use constant BAREWORD_FUNCTIONS =>
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 join( '|', qw(TRIM SUBSTRING CURRENT_DATE CURDATE CURRENT_TIME CURTIME CURRENT_TIMESTAMP NOW UNIX_TIMESTAMP PI DBNAME) );
20 17 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 17 ▶use Params::Util qw(_ARRAY0 _ARRAY _HASH);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 17 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 17 ▶use Text::Balanced qw(extract_bracketed extract_multiple);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 17 $VERSION = '1.414';
26  
27 BEGIN
28 17 ▶{
Subroutine BEGIN: called CC=2 SCAR=9.2
29 0 ▶ if ( $ENV{SQL_USER_DEFS} ) { require SQL::UserDefs; }
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"SQL_USER_DEFS"}) 0 17 17
30 }
31  
32 #############################
33 # PUBLIC METHODS
34 #############################
35  
36 sub new
37 17 ▶{
Subroutine new: called CC=8 SCAR=21.2
Pod documented
38 17 my $class = shift;
39 17 ▶ my $dialect = shift || 'ANSI';
Condition100%2/2
shift() || 'ANSI'
l!l
116
MC/DC100%1/1
shift() || 'ANSI'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
40 17 ▶ $dialect = 'ANSI' if ( uc $dialect eq 'ANSI' );
Branch: 1 branch
BranchTrueFalseTotal
if uc $dialect eq "ANSI" 17 0 17
41 17 ▶ $dialect = 'AnyData' if ( ( uc $dialect eq 'ANYDATA' ) or ( uc $dialect eq 'CSV' ) );
Branch: 1 branch
BranchTrueFalseTotal
if uc $dialect eq "ANYDATA" or uc $dialect eq "CSV" 0 17 17
Condition33%1/3
uc $dialect eq "ANYDATA" or uc $dialect eq "CSV"
l!l&&r!l&&!r
0017
MC/DC0%0/2
uc $dialect eq "ANYDATA" or uc $dialect eq "CSV"
uc $dialect eq "ANYDATA" uc $dialect eq "CSV"
Truth table33%1/3
A or B
A= uc $dialect eq "ANYDATA"B= uc $dialect eq "CSV"
ABresult
1X1
011
000
42 17 ▶ $dialect = 'AnyData' if ( $dialect eq 'SQL::Eval' );
Branch: 1 branch
BranchTrueFalseTotal
if $dialect eq "SQL::Eval" 0 17 17
43  
44 17 ▶ my $flags = shift || {};
Condition100%2/2
shift() || {}
l!l
116
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
45 17 $flags->{dialect} = $dialect;
46 17 ▶ $flags->{PrintError} = 1 unless ( defined( $flags->{PrintError} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags->{"PrintError"} 6 11 17
47  
48 17 my $self = bless( $flags, $class );
49 17 $self->dialect( $self->{dialect} );
50 17 $self->set_feature_flags( $self->{select}, $self->{create} );
51  
52 17 $self->LOAD('LOAD SQL::Statement::Functions');
53  
54 17 return $self;
55 }
56  
57 sub parse
58 872 ▶{
Subroutine parse: called CC=27 SCAR=34.1
Pod documented
59 872 my ( $self, $sql ) = @_;
60 872 ▶ $self->dialect( $self->{dialect} ) unless ( $self->{dialect_set} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dialect_set"} 0 872 872
61 872 $sql =~ s/^\s+//;
62 872 $sql =~ s/\s+$//;
63 872 $sql =~ s/\s*;$//;
64 872 $self->{struct} = { dialect => $self->{dialect} };
65 872 $self->{tmp} = {};
66 872 $self->{original_string} = $sql;
67 872 $self->{struct}->{original_string} = $sql;
68  
69 ################################################################
70 #
71 # COMMENTS
72  
73 # C-STYLE
74 #
75 872 ▶ my $comment_re = $self->{comment_re} || '(\/\*.*?\*\/)';
Condition100%2/2
$self->{'comment_re'} || '(\\/\\*.*?\\*\\/)'
l!l
85616
MC/DC100%1/1
$self->{'comment_re'} || '(\\/\\*.*?\\*\\/)'
$self->{'comment_re'}
Truth table100%2/2
A
A= $self->{'comment_re'}
Aresult
11
00
76 872 $self->{comment_re} = $comment_re;
77 872 my $starts_with_comment;
78 872 ▶ if ( $sql =~ /^\s*$comment_re(.*)$/s )
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /^\s*$comment_re(.*)$/s) 36 836 872
79 {
80 36 $self->{comment} = $1;
81 36 $sql = $2;
82 36 $starts_with_comment = 1;
83 }
84  
85 # SQL STYLE
86 #
87 # SQL-style comment can not begin inside quotes.
88 872 ▶ if ( $sql =~ s/^([^']*?(?:'[^']*'[^'])*?)(--.*)(\n|$)/$1$3/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ s/^([^']*?(?:'[^']*'[^'])*?)(--.*)(\n|$)/$1$3/) 0 872 872
89 {
90 0 $self->{comment} = $2;
91 }
92 ################################################################
93  
94 872 $sql = $self->clean_sql($sql);
95 872 my ($com) = $sql =~ m/^\s*(\S+)\s+/s;
96 872 ▶ if ( !$com )
Branch: 1 branch
BranchTrueFalseTotal
unless ($com) 38 834 872
97 {
98 38 ▶ return 1 if ($starts_with_comment);
Branch: 1 branch
BranchTrueFalseTotal
if $starts_with_comment 36 2 38
99 2 return $self->do_err("Incomplete statement!");
100 }
101 834 $com = uc $com;
102 834 $self->{opts}->{valid_commands}->{CALL} = 1;
103 834 $self->{opts}->{valid_commands}->{LOAD} = 1;
104 834 ▶ if ( $self->{opts}->{valid_commands}->{$com} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'opts'}{'valid_commands'}{$com}) { } 834 0 834
105 {
106 834 my $rv = $self->$com($sql);
107 828 delete $self->{struct}->{literals};
108  
109 828 ▶ return $self->do_err("No command found!") unless ( $self->{struct}->{command} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"struct"}{"command"} 0 828 828
110  
111 828 $self->replace_quoted_ids();
112  
113 620 my @tables = @{ $self->{struct}->{table_names} }
114 828 ▶ if ( defined( _ARRAY0( $self->{struct}->{table_names} ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined &_ARRAY0($self->{"struct"}{"table_names"}) 620 208 828
115 828 push( @{ $self->{struct}->{org_table_names} }, @tables );
116 # REMOVE schema.table info if present
117 828 ▶ @tables = map { s/^.*\.([^\.]+)$/$1/; ( -1 == index( $_, '"' ) ) ? lc $_ : $_ } @tables;
Branch: 1 branch
BranchTrueFalseTotal
(index($_, '"') == -1) ? : 693 13 706
118  
119 828 ▶ if ( exists( $self->{struct}->{join} ) && !defined( _HASH( $self->{struct}->{join} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'struct'}{'join'} and not defined &_HASH($self->{'struct'}{'join'})) { } 464 364 828
Condition100%3/3
exists $self->{"struct"}{"join"} and not defined &_HASH($self->{"struct"}{"join"})
!ll&&!rl&&r
28876464
MC/DC100%2/2
exists $self->{"struct"}{"join"} and not defined &_HASH($self->{"struct"}{"join"})
exists $self->{"struct"}{"join"} not defined &_HASH($self->{"struct"}{"join"})
Truth table100%3/3
A and not(B)
A= exists $self->{"struct"}{"join"}B= not defined &_HASH($self->{"struct"}{"join"})
ABresult
0X0
100
111
120 {
121 464 delete $self->{struct}->{join};
122 }
123 else
124 {
125 $self->{struct}->{join}->{table_order} = $self->{struct}->{table_names}
126 if ( defined( $self->{struct}->{join}->{table_order} )
127 364 ▶ && !defined( _ARRAY0( $self->{struct}->{join}->{table_order} ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"struct"}{"join"}{"table_order"} and not defined &_ARRAY0($self->{"struct"}{"join"}{"table_order"}) 0 364 364
Condition66%2/3
defined $self->{"struct"}{"join"}{"table_order"} and not defined &_ARRAY0($self->{"struct"}{"join"}{"table_order"})
!ll&&!rl&&r
325390
MC/DC0%0/2
defined $self->{"struct"}{"join"}{"table_order"} and not defined &_ARRAY0($self->{"struct"}{"join"}{"table_order"})
defined $self->{"struct"}{"join"}{"table_order"} not defined &_ARRAY0($self->{"struct"}{"join"}{"table_order"})
Truth table66%2/3
A and not(B)
A= defined $self->{"struct"}{"join"}{"table_order"}B= not defined &_ARRAY0($self->{"struct"}{"join"}{"table_order"})
ABresult
0X0
100
111
128 58 @{ $self->{struct}->{join}->{keycols} } =
129 144 map { lc $_ } @{ $self->{struct}->{join}->{keycols} }
130 364 ▶ if ( $self->{struct}->{join}->{keycols} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"struct"}{"join"}{"keycols"} 58 306 364
131 0 @{ $self->{struct}->{join}->{shared_cols} } =
132 0 map { lc $_ } @{ $self->{struct}->{join}->{shared_cols} }
133 364 ▶ if ( $self->{struct}->{join}->{shared_cols} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"struct"}{"join"}{"shared_cols"} 0 364 364
134 }
135  
136 828 ▶ if ( defined( $self->{struct}->{column_defs} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"struct"}{"column_defs"} and defined &_ARRAY($self->{"struct"}{"column_defs"})) 792 36 828
Condition66%2/3
defined $self->{"struct"}{"column_defs"} and defined &_ARRAY($self->{"struct"}{"column_defs"})
!ll&&!rl&&r
360792
MC/DC50%1/2
defined $self->{"struct"}{"column_defs"} and defined &_ARRAY($self->{"struct"}{"column_defs"})
defined $self->{"struct"}{"column_defs"} defined &_ARRAY($self->{"struct"}{"column_defs"})
Truth table66%2/3
A and B
A= defined $self->{"struct"}{"column_defs"}B= defined &_ARRAY($self->{"struct"}{"column_defs"})
ABresult
0X0
100
111
137 && defined( _ARRAY( $self->{struct}->{column_defs} ) ) )
138 {
139 792 my $colname;
140 # FIXME SUBSTR('*')
141 my @fine_defs =
142 792 ▶ grep { defined( $_->{fullorg} ) && ( -1 == index( $_->{fullorg}, '*' ) ) } @{ $self->{struct}->{column_defs} };
Condition100%3/3
defined $_->{'fullorg'} && (index($_->{'fullorg'}, '*') == -1)
!ll&&!rl&&r
4089596
MC/DC100%2/2
defined $_->{'fullorg'} && (index($_->{'fullorg'}, '*') == -1)
defined $_->{'fullorg'} (index($_->{'fullorg'}, '*') == -1)
Truth table100%3/3
A && B
A= defined $_->{'fullorg'}B= (index($_->{'fullorg'}, '*') == -1)
ABresult
0X0
100
111
143 792 foreach my $col (@fine_defs)
144 {
145 596 my $colname = $col->{fullorg};
146 #$cn = lc $cn unless ( $cn =~ m/^(?:\w+\.)?"/ );
147 596 ▶ push( @{ $self->{struct}->{org_col_names} }, $self->{struct}->{ORG_NAME}->{$colname} || $colname );
Condition66%2/3
$self->{'struct'}{'ORG_NAME'}{$colname} || $colname
l!l&&r!l&&!r
2273690
MC/DC0%0/2
$self->{'struct'}{'ORG_NAME'}{$colname} || $colname
$self->{'struct'}{'ORG_NAME'}{$colname} $colname
Truth table66%2/3
A || B
A= $self->{'struct'}{'ORG_NAME'}{$colname}B= $colname
ABresult
1X1
011
000
148 }
149  
150 792 ▶ unless ( $com eq 'CREATE' )
Branch: 1 branch
BranchTrueFalseTotal
unless ($com eq "CREATE") 733 59 792
151 {
152 733 $self->{struct}->{table_names} = \@tables;
153 # For RR aliases, added quoted id protection from upper casing
154 733 foreach my $col (@fine_defs)
155 {
156 # defined( $col->{fullorg} ) && ( -1 == index( $col->{fullorg}, '*' ) ) or next;
157 465 my $orgname = $colname = $col->{fullorg};
158 465 ▶ $colname =~ m/^(?:\p{Word}+\.)?"/ or $colname = lc $colname;
Branch: 1 branch
BranchTrueFalseTotal
$colname =~ /^(?:\p{Word}+\.)?"/ or $colname = lc $colname 0 465 465
159 465 ▶ defined( $self->{struct}->{ORG_NAME}->{$colname} ) and next;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"struct"}{"ORG_NAME"}{$colname} and next 11 454 465
160 $self->{struct}->{ORG_NAME}->{$colname} =
161 454 $self->{struct}->{ORG_NAME}->{$orgname};
162 }
163 #my @uCols = map { ( $_ =~ /^(\w+\.)?"/ ) ? $_ : lc $_ } @{ $self->{struct}->{column_names} };
164 #$self->{struct}->{column_names} = \@uCols;
165 }
166 }
167  
168 828 return $rv;
169 }
170 else
171 {
172 0 $self->{struct} = {};
173 0 ▶ if ( $ENV{SQL_USER_DEFS} )
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"SQL_USER_DEFS"}) 0 0 0
174 {
175 0 return SQL::UserDefs::user_parse( $self, $sql );
176 }
177 0 return $self->do_err("Command '$com' not recognized or not supported!");
178 }
179 }
180  
181 sub replace_quoted_commas
182 9 ▶{
Subroutine replace_quoted_commas: called CC=1 SCAR=0.0
Pod documented
183 9 my ( $self, $id ) = @_;
184 9 $id =~ s/\?COMMA\?/,/gs;
185 9 return $id;
186 }
187  
188 sub replace_quoted_ids
189 1881 ▶{
Subroutine replace_quoted_ids: called CC=10 SCAR=23.2
Pod documented
190 1881 my ( $self, $id ) = @_;
191 1881 ▶ $self->{struct}->{quoted_ids} or $self->{struct}->{literals} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"struct"}{"quoted_ids"} or $self->{"struct"}{"literals"} or return 1081 800 1881
Condition100%3/3
$self->{"struct"}{"quoted_ids"} or $self->{"struct"}{"literals"}
l!l&&r!l&&!r
104977800
MC/DC100%2/2
$self->{"struct"}{"quoted_ids"} or $self->{"struct"}{"literals"}
$self->{"struct"}{"quoted_ids"} $self->{"struct"}{"literals"}
Truth table100%3/3
A or B
A= $self->{"struct"}{"quoted_ids"}B= $self->{"struct"}{"literals"}
ABresult
1X1
011
000
192 1081 ▶ if ($id)
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 1053 28 1081
193 {
194 1053 ▶ if ( $id =~ /^\?QI(\d+)\?$/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($id =~ /^\?QI(\d+)\?$/) { } 44 1009 1053
elsif ($id =~ /^\?(\d+)\?$/) { } 0 1009 1009
195 {
196 44 return '"' . $self->{struct}->{quoted_ids}->[$1] . '"';
197 }
198 elsif ( $id =~ /^\?(\d+)\?$/ )
199 {
200 0 return $self->{struct}->{literals}->[$1];
201 }
202 else
203 {
204 1009 return $id;
205 }
206 }
207 28 ▶ return unless defined $self->{struct}->{table_names};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"struct"}{"table_names"} 2 26 28
208 26 my @tables = @{ $self->{struct}->{table_names} };
209 26 for my $t (@tables)
210 {
211 28 ▶ if ( $t =~ /^\?QI(.+)\?$/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($t =~ /^\?QI(.+)\?$/) { } 6 22 28
elsif ($t =~ /^\?(\d+)\?$/) { } 0 22 22
212 {
213 6 $t = '"' . $self->{struct}->{quoted_ids}->[$1] . '"';
214 }
215 elsif( $t =~ /^\?(\d+)\?$/ )
216 {
217 0 $t = $self->{struct}->{literals}->[$1];
218 }
219 }
220 26 $self->{struct}->{table_names} = \@tables;
221 26 delete $self->{struct}->{quoted_ids};
222 }
223  
224 31 ▶sub structure { $_[0]->{struct} }
Subroutine structure: called CC=1 SCAR=0.0
Pod documented
225 1 ▶sub command { my $x = $_[0]->{struct}->{command} || '' }
Subroutine command: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
$_[0]{'struct'}{'command'} || ''
l!l
10
MC/DC0%0/1
$_[0]{'struct'}{'command'} || ''
$_[0]{'struct'}{'command'}
Truth table50%1/2
A
A= $_[0]{'struct'}{'command'}
Aresult
11
00
226  
227 sub feature
228 9 ▶{
Subroutine feature: called CC=4 SCAR=17.2
Pod documented
229 9 my ( $self, $opt_class, $opt_name, $opt_value ) = @_;
230 9 ▶ if ( defined $opt_value )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt_value) { } 9 0 9
231 {
232 9 ▶ if ( $opt_class eq 'select' )
Branch: 2 branches
BranchTrueFalseTotal
if ($opt_class eq 'select') { } 0 9 9
elsif ($opt_class eq 'create') { } 0 9 9
233 {
234 0 $self->set_feature_flags( { "join" => $opt_value } );
235 }
236 elsif ( $opt_class eq 'create' )
237 {
238 0 $self->set_feature_flags( undef, { $opt_name => $opt_value } );
239 }
240 else
241 {
242  
243 # patch from chromatic
244 9 $self->{opts}->{$opt_class}->{$opt_name} = $opt_value;
245  
246 # $self->{$opt_class}->{$opt_name} = $opt_value;
247 }
248 }
249 else
250 {
251 0 return $self->{opts}->{$opt_class}->{$opt_name};
252 }
253 }
254  
255 10 ▶sub errstr { $_[0]->{struct}->{errstr} }
Subroutine errstr: called CC=1 SCAR=0.0
Pod documented
256  
257 sub list
258 0 ▶{
Subroutine list: not called CC=11 SCAR=48.8
Pod documented
259 0 my $self = shift;
260 0 my $com = uc shift;
261 0 ▶ return () if $com !~ /COMMANDS|RESERVED|TYPES|OPS|OPTIONS|DIALECTS/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $com =~ /COMMANDS|RESERVED|TYPES|OPS|OPTIONS|DIALECTS/i 0 0 0
262 0 ▶ $com = 'valid_commands' if $com eq 'COMMANDS';
Branch: 1 branch
BranchTrueFalseTotal
if $com eq "COMMANDS" 0 0 0
263 0 ▶ $com = 'valid_comparison_operators' if $com eq 'OPS';
Branch: 1 branch
BranchTrueFalseTotal
if $com eq "OPS" 0 0 0
264 0 ▶ $com = 'valid_data_types' if $com eq 'TYPES';
Branch: 1 branch
BranchTrueFalseTotal
if $com eq "TYPES" 0 0 0
265 0 ▶ $com = 'valid_options' if $com eq 'OPTIONS';
Branch: 1 branch
BranchTrueFalseTotal
if $com eq "OPTIONS" 0 0 0
266 0 ▶ $com = 'reserved_words' if $com eq 'RESERVED';
Branch: 1 branch
BranchTrueFalseTotal
if $com eq "RESERVED" 0 0 0
267 0 ▶ $self->dialect( $self->{dialect} ) unless $self->{dialect_set};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dialect_set"} 0 0 0
268  
269 0 ▶ return sort keys %{ $self->{opts}->{$com} } unless $com eq 'DIALECTS';
Branch: 1 branch
BranchTrueFalseTotal
unless $com eq "DIALECTS" 0 0 0
270 0 my $dDir = "SQL/Dialects";
271 0 my @dialects;
272 0 for my $dir (@INC)
273 {
274 0 local *D;
275  
276 0 ▶ if ( opendir( D, "$dir/$dDir" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (opendir D, "$dir/$dDir") 0 0 0
277 {
278 0 @dialects = grep /.*\.pm$/, readdir(D);
279 0 last;
280 }
281 }
282 0 @dialects = map { s/\.pm$//; $_ } @dialects;
283 0 return @dialects;
284 }
285  
286 sub dialect
287 18 ▶{
Subroutine dialect: called CC=5 SCAR=16.1
Pod documented
288 18 my ( $self, $dialect ) = @_;
289 18 ▶ return $self->{dialect} unless ($dialect);
Branch: 1 branch
BranchTrueFalseTotal
unless $dialect 0 18 18
290 18 ▶ return $self->{dialect} if ( $self->{dialect_set} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dialect_set"} 1 17 18
291 17 $self->{opts} = {};
292 17 my $mod_class = "SQL::Dialects::$dialect";
293  
294 17 ▶ $self->_load_class($mod_class) unless $mod_class->can("get_config");
Branch: 1 branch
BranchTrueFalseTotal
unless $mod_class->can("get_config") 16 1 17
295  
296 # This is here for backwards compatibility with existing dialects
297 # before the had the role to add new methods.
298 17 ▶ $self->_inject_role( "SQL::Dialects::Role", $mod_class )
Branch: 1 branch
BranchTrueFalseTotal
unless $mod_class->can("get_config_as_hash") 0 17 17
299 unless ( $mod_class->can("get_config_as_hash") );
300  
301 17 $self->{opts} = $mod_class->get_config_as_hash();
302  
303 17 $self->create_op_regexen();
304 17 $self->{dialect} = $dialect;
305 17 $self->{dialect_set}++;
306  
307 17 return $self->{dialect};
308 }
309  
310 sub _load_class
311 34 ▶{
Subroutine _load_class: called CC=2 SCAR=7.0
312 34 my ( $self, $class ) = @_;
313  
314 34 my $mod = $class;
315 34 $mod =~ s{::}{/}g;
316 34 $mod .= ".pm";
317  
318 34 local ( $!, $@ );
319 34 ▶ eval { require "$mod"; } or return $self->do_err($@);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require "$mod" } } or return $self->do_err($@) 34 0 34
320  
321 34 return 1;
322 }
323  
324 sub _inject_role
325 0 ▶{
Subroutine _inject_role: not called CC=2 SCAR=17.9
326 0 my ( $self, $role, $dest ) = @_;
327  
328 0 ▶ eval qq{
Branch: 1 branch
BranchTrueFalseTotal
eval "\n package $dest;\n use $role;\n 1;\n " or croak("Can't inject $role into ${dest}: $@") 0 0 0
329 package $dest;
330 use $role;
331 1;
332 } or croak "Can't inject $role into $dest: $@";
333 }
334  
335 sub create_op_regexen
336 20 ▶{
Subroutine create_op_regexen: called CC=8 SCAR=21.0
Pod documented
337 20 my ($self) = @_;
338  
339 #
340 # DAA precompute the predicate operator regex's
341 #
342 # JZ moved this into a sub so it can be called from both
343 # dialect() and from CREATE_OPERATOR and DROP_OPERATOR
344 # since those also modify the available operators
345 #
346 20 my @allops = keys %{ $self->{opts}->{valid_comparison_operators} };
347  
348 #
349 # complement operators
350 #
351 20 my @notops;
352 20 for (@allops)
353 {
354 285 ▶ push( @notops, $_ )
Branch: 1 branch
BranchTrueFalseTotal
if /NOT/i 80 205 285
355 if /NOT/i;
356 }
357 20 ▶ $self->{opts}->{valid_comparison_NOT_ops_regex} = '^\s*(.+)\s+(' . join( '|', @notops ) . ')\s+(.*)\s*$'
Branch: 1 branch
BranchTrueFalseTotal
if scalar @notops 20 0 20
358 if scalar @notops;
359  
360 #
361 # <>, <=, >= operators
362 #
363 20 my @compops;
364 20 for (@allops)
365 {
366 285 ▶ push( @compops, $_ )
Branch: 1 branch
BranchTrueFalseTotal
if /<=|>=|<>/ 60 225 285
367 if /<=|>=|<>/;
368 }
369 20 ▶ $self->{opts}->{valid_comparison_twochar_ops_regex} = '^\s*(.+)\s+(' . join( '|', @compops ) . ')\s+(.*)\s*$'
Branch: 1 branch
BranchTrueFalseTotal
if scalar @compops 20 0 20
370 if scalar @compops;
371  
372 #
373 # everything
374 #
375 20 ▶ $self->{opts}->{valid_comparison_ops_regex} = '^\s*(.+)\s+(' . join( '|', @allops ) . ')\s+(.*)\s*$'
Branch: 1 branch
BranchTrueFalseTotal
if scalar @allops 20 0 20
376 if scalar @allops;
377  
378 #
379 # end DAA
380 #
381 }
382  
383 ##################################################################
384 # SQL COMMANDS
385 ##################################################################
386  
387 ####################################################
388 # DROP TABLE <table_name>
389 ####################################################
390 sub DROP
391 19 ▶{
Subroutine DROP: called CC=8 SCAR=21.0
Pod undocumented
392 19 my ( $self, $stmt ) = @_;
393 19 my $features = 'TYPE|KEYWORD|FUNCTION|OPERATOR|PREDICATE';
394 19 ▶ if ( $stmt =~ /^\s*DROP\s+($features)\s+(.+)$/si )
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ /^\s*DROP\s+($features)\s+(.+)$/is) 3 16 19
395 {
396 3 my ( $sub, $arg ) = ( $1, $2 );
397 3 $sub = 'DROP_' . $sub;
398 3 return $self->$sub($arg);
399 }
400 16 my $table_name;
401 16 $self->{struct}->{command} = 'DROP';
402 16 ▶ if ( $stmt =~ /^\s*DROP\s+TABLE\s+IF\s+EXISTS\s+(.*)$/si )
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ /^\s*DROP\s+TABLE\s+IF\s+EXISTS\s+(.*)$/is) 9 7 16
403 {
404 9 $stmt = "DROP TABLE $1";
405 9 $self->{struct}->{ignore_missing_table} = 1;
406 }
407 16 ▶ if ( $stmt =~ /^\s*DROP\s+(\S+)\s+(.+)$/si )
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ /^\s*DROP\s+(\S+)\s+(.+)$/is) { } 16 0 16
408 {
409 16 ▶ my $com2 = $1 || '';
Condition50%1/2
$1 || ''
l!l
160
MC/DC0%0/1
$1 || ''
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
410 16 $table_name = $2;
411 16 ▶ if ( $com2 !~ /^TABLE$/i )
Branch: 1 branch
BranchTrueFalseTotal
unless ($com2 =~ /^TABLE$/i) 0 16 16
412 {
413 0 return $self->do_err("The command 'DROP $com2' is not recognized or not supported!");
414 }
415 16 $table_name =~ s/^\s+//;
416 16 $table_name =~ s/\s+$//;
417 16 ▶ if ( $table_name =~ /(\S+) (RESTRICT|CASCADE)/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($table_name =~ /(\S+) (RESTRICT|CASCADE)/i) 2 14 16
418 {
419 2 $table_name = $1;
420 2 $self->{struct}->{drop_behavior} = uc $2;
421 }
422 }
423 else
424 {
425 0 return $self->do_err("Incomplete DROP statement!");
426  
427 }
428 16 ▶ return undef unless $self->TABLE_NAME($table_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TABLE_NAME($table_name) 0 16 16
429 16 $table_name = $self->replace_quoted_ids($table_name);
430 16 $self->{tmp}->{is_table_name} = { $table_name => 1 };
431 16 $self->{struct}->{table_names} = [$table_name];
432 16 return 1;
433 }
434  
435 ####################################################
436 # DELETE FROM <table_name> WHERE <search_condition>
437 ####################################################
438 sub DELETE
439 9 ▶{
Subroutine DELETE: called CC=5 SCAR=16.2
440 9 my ( $self, $str ) = @_;
441 9 $self->{struct}->{command} = 'DELETE';
442 9 $str =~ s/^DELETE\s+FROM\s+/DELETE /i; # Make FROM optional
443 9 my ( $table_name, $where_clause ) = $str =~ /^DELETE (\S+)(.*)$/i;
444 9 ▶ return $self->do_err('Incomplete DELETE statement!') if !$table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $table_name 0 9 9
445 9 ▶ return undef unless $self->TABLE_NAME($table_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TABLE_NAME($table_name) 0 9 9
446 9 $self->{tmp}->{is_table_name} = { $table_name => 1 };
447 9 $self->{struct}->{table_names} = [$table_name];
448 $self->{struct}->{column_defs} = [
449 {
450 9 type => 'column',
451 value => '*'
452 }
453 ];
454 9 $where_clause =~ s/^\s+//;
455 9 $where_clause =~ s/\s+$//;
456  
457 9 ▶ if ($where_clause)
Branch: 1 branch
BranchTrueFalseTotal
if ($where_clause) 6 3 9
458 {
459 6 $where_clause =~ s/^WHERE\s*(.*)$/$1/i;
460 6 ▶ return undef unless $self->SEARCH_CONDITION($where_clause);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SEARCH_CONDITION($where_clause) 0 6 6
461 }
462 9 return 1;
463 }
464  
465 ##############################################################
466 # SELECT
467 ##############################################################
468 # SELECT [<set_quantifier>] <select_list>
469 # | <set_function_specification>
470 # FROM <from_clause>
471 # [WHERE <search_condition>]
472 # [ORDER BY <order_by_clause>]
473 # [LIMIT <limit_clause>]
474 ##############################################################
475  
476 sub SELECT
477 549 ▶{
Subroutine SELECT: called CC=26 SCAR=33.1
Pod undocumented
478 549 my ( $self, $str ) = @_;
479 549 $self->{struct}->{command} = 'SELECT';
480 549 my ( $from_clause, $where_clause, $order_clause, $groupby_clause, $limit_clause );
481 549 $str =~ s/^SELECT (.+)$/$1/i;
482 549 ▶ if ( $str =~ s/^(.+) LIMIT (.+)$/$1/i ) { $limit_clause = $2; }
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/^(.+) LIMIT (.+)$/$1/i) 9 540 549
483 549 ▶ if ( $str =~ s/^(.+) ORDER BY (.+)$/$1/i ) { $order_clause = $2; }
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/^(.+) ORDER BY (.+)$/$1/i) 20 529 549
484 549 ▶ if ( $str =~ s/^(.+) GROUP BY (.+)$/$1/i ) { $groupby_clause = $2; }
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/^(.+) GROUP BY (.+)$/$1/i) 8 541 549
485 549 ▶ if ( $str =~ s/^(.+?) WHERE (.+)$/$1/i ) { $where_clause = $2; }
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/^(.+?) WHERE (.+)$/$1/i) 219 330 549
486 549 ▶ if ( $str =~ s/^(.+?) FROM (.+)$/$1/i ) { $from_clause = $2; }
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/^(.+?) FROM (.+)$/$1/i) 359 190 549
487  
488 # else {
489 # return $self->do_err("Couldn't find FROM clause in SELECT!");
490 # }
491 # return undef unless $self->FROM_CLAUSE($from_clause);
492 549 ▶ my $has_from_clause = $self->FROM_CLAUSE($from_clause) if ($from_clause);
Branch: 1 branch
BranchTrueFalseTotal
if $from_clause 359 190 549
493  
494 547 ▶ return undef unless ( $self->SELECT_CLAUSE($str) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SELECT_CLAUSE($str) 7 540 547
495  
496 540 ▶ if ($where_clause)
Branch: 1 branch
BranchTrueFalseTotal
if ($where_clause) 219 321 540
497 {
498 219 ▶ return undef unless ( $self->SEARCH_CONDITION($where_clause) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SEARCH_CONDITION($where_clause) 0 217 217
499 }
500 538 ▶ if ($groupby_clause)
Branch: 1 branch
BranchTrueFalseTotal
if ($groupby_clause) 7 531 538
501 {
502 7 ▶ return undef unless ( $self->GROUPBY_LIST($groupby_clause) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->GROUPBY_LIST($groupby_clause) 0 7 7
503 }
504 538 ▶ if ($order_clause)
Branch: 1 branch
BranchTrueFalseTotal
if ($order_clause) 20 518 538
505 {
506 20 ▶ return undef unless ( $self->SORT_SPEC_LIST($order_clause) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SORT_SPEC_LIST($order_clause) 0 20 20
507 }
508 538 ▶ if ($limit_clause)
Branch: 1 branch
BranchTrueFalseTotal
if ($limit_clause) 9 529 538
509 {
510 9 ▶ return undef unless ( $self->LIMIT_CLAUSE($limit_clause) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->LIMIT_CLAUSE($limit_clause) 0 9 9
511 }
512 538 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"struct"}{"join"}{"clause"} and $self->{"struct"}{"join"}{"clause"} eq "ON" or $self->{"struct"}{"multiple_tables"} and not scalar keys %{$$self{"struct"}{"join"};}) 39 499 538
Condition100%9/9
$self->{"struct"}{"join"}{"clause"} and $self->{"struct"}{"join"}{"clause"} eq "ON" or $self->{"struct"}{"multiple_tables"} and not scalar keys %{$$self{"struct"}{"join"};}
l!l&&r!l&&!r
2712499
$self->{"struct"}{"join"}{"clause"} and $self->{"struct"}{"join"}{"clause"} eq "ON"
!ll&&!rl&&r
4763527
$self->{"struct"}{"multiple_tables"} and not scalar keys %{$$self{"struct"}{"join"};}
!ll&&!rl&&r
4643512
MC/DC75%3/4
$self->{"struct"}{"join"}{"clause"} and $self->{"struct"}{"join"}{"clause"} eq "ON" or $self->{"struct"}{"multiple_tables"} and not scalar keys %{$$self{"struct"}{"join"};}
$self->{"struct"}{"join"}{"clause"} $self->{"struct"}{"join"}{"clause"} eq "ON" $self->{"struct"}{"multiple_tables"} not scalar keys %{$$self{"struct"}{"join"};}
Truth table57%4/7
A and B or C and not(D)
A= $self->{"struct"}{"join"}{"clause"}B= $self->{"struct"}{"join"}{"clause"} eq "ON"C= $self->{"struct"}{"multiple_tables"}D= not scalar keys %{$$self{"struct"}{"join"};}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
513 ( $self->{struct}->{join}->{clause} and $self->{struct}->{join}->{clause} eq 'ON' )
514 or ( $self->{struct}->{multiple_tables}
515 47 and !( scalar keys %{ $self->{struct}->{join} } ) )
516 )
517 {
518 39 ▶ return undef unless ( $self->IMPLICIT_JOIN() );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->IMPLICIT_JOIN 0 39 39
519 }
520  
521 538 ▶ if ( $self->{struct}->{set_quantifier}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"struct"}{"set_quantifier"} and "DISTINCT" eq $self->{"struct"}{"set_quantifier"} and $self->{"struct"}{"has_set_functions"} and not defined &_ARRAY($self->{"struct"}{"group_by"})) 0 538 538
Condition88%8/9
$self->{"struct"}{"set_quantifier"} and "DISTINCT" eq $self->{"struct"}{"set_quantifier"} and $self->{"struct"}{"has_set_functions"} and not defined &_ARRAY($self->{"struct"}{"group_by"})
!ll&&!rl&&r
53710
$self->{"struct"}{"set_quantifier"} and "DISTINCT" eq $self->{"struct"}{"set_quantifier"} and $self->{"struct"}{"has_set_functions"}
!ll&&!rl&&r
53161
$self->{"struct"}{"set_quantifier"} and "DISTINCT" eq $self->{"struct"}{"set_quantifier"}
!ll&&!rl&&r
53017
MC/DC0%0/4
$self->{"struct"}{"set_quantifier"} and "DISTINCT" eq $self->{"struct"}{"set_quantifier"} and $self->{"struct"}{"has_set_functions"} and not defined &_ARRAY($self->{"struct"}{"group_by"})
$self->{"struct"}{"set_quantifier"} "DISTINCT" eq $self->{"struct"}{"set_quantifier"} $self->{"struct"}{"has_set_functions"} not defined &_ARRAY($self->{"struct"}{"group_by"})
Truth table80%4/5
A and B and C and not(D)
A= $self->{"struct"}{"set_quantifier"}B= "DISTINCT" eq $self->{"struct"}{"set_quantifier"}C= $self->{"struct"}{"has_set_functions"}D= not defined &_ARRAY($self->{"struct"}{"group_by"})
ABCDresult
0XXX0
10XX0
110X0
11100
11111
522 && ( 'DISTINCT' eq $self->{struct}->{set_quantifier} )
523 && $self->{struct}->{has_set_functions}
524 && !defined( _ARRAY( $self->{struct}->{group_by} ) ) )
525 {
526 0 delete $self->{struct}->{set_quantifier};
527 carp "Specifying DISTINCT when using aggregate functions isn't reasonable - ignored."
528 0 ▶ if ( $self->{PrintError} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"PrintError"} 0 0 0
529 }
530  
531 538 return 1;
532 }
533  
534 sub GROUPBY_LIST
535 7 ▶{
Subroutine GROUPBY_LIST: called CC=3 SCAR=11.1
Pod undocumented
536 7 my ( $self, $gclause ) = @_;
537 7 ▶ return 1 unless ($gclause);
Branch: 1 branch
BranchTrueFalseTotal
unless $gclause 0 7 7
538 7 my $cols = $self->ROW_VALUE_LIST($gclause);
539 7 ▶ return undef if ( $self->{struct}->{errstr} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"struct"}{"errstr"} 0 7 7
540 7 @{ $self->{struct}->{group_by} } = map { $_->{fullorg} } @{$cols};
541 7 return 1;
542 }
543  
544 sub IMPLICIT_JOIN
545 39 ▶{
Subroutine IMPLICIT_JOIN: called CC=4 SCAR=14.0
Pod undocumented
546 39 my $self = $_[0];
547 39 delete $self->{struct}->{multiple_tables};
548 39 ▶ if ( !$self->{struct}->{join}->{clause}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"struct"}{"join"}{"clause"} or $self->{"struct"}{"join"}{"clause"} ne "ON") 12 27 39
Condition66%2/3
not $self->{"struct"}{"join"}{"clause"} or $self->{"struct"}{"join"}{"clause"} ne "ON"
l!l&&r!l&&!r
12027
MC/DC50%1/2
not $self->{"struct"}{"join"}{"clause"} or $self->{"struct"}{"join"}{"clause"} ne "ON"
not $self->{"struct"}{"join"}{"clause"} $self->{"struct"}{"join"}{"clause"} ne "ON"
Truth table66%2/3
not(A) or B
A= not $self->{"struct"}{"join"}{"clause"}B= $self->{"struct"}{"join"}{"clause"} ne "ON"
ABresult
1X1
011
000
549 or $self->{struct}->{join}->{clause} ne 'ON' )
550 {
551 12 $self->{struct}->{join}->{type} = 'INNER';
552 12 $self->{struct}->{join}->{clause} = 'IMPLICIT';
553 }
554 39 ▶ if ( defined $self->{struct}->{keycols} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'struct'}{'keycols'}) { } 39 0 39
555 {
556 39 my @keys;
557 39 my @keys2 = @keys = @{ $self->{struct}->{keycols} };
558 39 $self->{struct}->{join}->{table_order} = $self->order_joins( \@keys2 );
559 39 @{ $self->{struct}->{join}->{keycols} } = @keys;
560 39 delete $self->{struct}->{keycols};
561 }
562 else
563 {
564 0 return $self->do_err("No equijoin condition in WHERE or ON clause");
565 }
566 39 return 1;
567 }
568  
569 sub EXPLICIT_JOIN
570 64 ▶{
Subroutine EXPLICIT_JOIN: called CC=31 SCAR=35.7
Pod undocumented
571 64 my ( $self, $remainder ) = @_;
572 64 ▶ return undef unless ($remainder);
Branch: 1 branch
BranchTrueFalseTotal
unless $remainder 0 64 64
573 64 my ( $tableA, $tableB, $keycols, $jtype, $natural );
574 64 ▶ if ( $remainder =~ m/^(.+?) (NATURAL|INNER|LEFT|RIGHT|FULL|CROSS|UNION|JOIN)(.+)$/is )
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder =~ /^(.+?) (NATURAL|INNER|LEFT|RIGHT|FULL|CROSS|UNION|JOIN)(.+)$/is) { } 64 0 64
575 {
576 64 $tableA = $1;
577 64 $remainder = $2 . $3;
578 }
579 else
580 {
581 0 ( $tableA, $remainder ) = $remainder =~ m/^(\S+) (.*)/i;
582 }
583 64 ▶ if ( $remainder =~ m/^NATURAL (.+)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder =~ /^NATURAL (.+)/) 15 49 64
584 {
585 15 $self->{struct}->{join}->{clause} = 'NATURAL';
586 15 $natural++;
587 15 $remainder = $1;
588 }
589 64 ▶ if ( $remainder =~ m/^(INNER|LEFT|RIGHT|FULL|CROSS|UNION) JOIN (.+)/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder =~ /^(INNER|LEFT|RIGHT|FULL|CROSS|UNION) JOIN (.+)/i) 43 21 64
590 {
591 43 $jtype = $self->{struct}->{join}->{clause} = uc($1);
592 43 $remainder = $2;
593 43 ▶ $jtype = "$jtype OUTER" if $jtype !~ /INNER|UNION/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $jtype =~ /INNER|UNION/i 34 9 43
594 }
595 64 ▶ if ( $remainder =~ m/^(LEFT|RIGHT|FULL|CROSS) OUTER JOIN (.+)/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder =~ /^(LEFT|RIGHT|FULL|CROSS) OUTER JOIN (.+)/i) 0 64 64
596 {
597 0 $jtype = $self->{struct}->{join}->{clause} = uc($1) . " OUTER";
598 0 $remainder = $2;
599 }
600 64 ▶ if ( $remainder =~ m/^JOIN (.+)/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder =~ /^JOIN (.+)/i) 21 43 64
601 {
602 21 $jtype = 'INNER';
603 21 $self->{struct}->{join}->{clause} = 'DEFAULT INNER';
604 21 $remainder = $1;
605 }
606 64 ▶ if ( $self->{struct}->{join} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"struct"}{"join"}) 64 0 64
607 {
608 64 ▶ if ( $remainder && $remainder =~ m/^(.+?) USING \(([^\)]+)\)(.*)/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder and $remainder =~ /^(.+?) USING \(([^\)]+)\)(.*)/i) 20 44 64
Condition66%2/3
$remainder and $remainder =~ /^(.+?) USING \(([^\)]+)\)(.*)/i
!ll&&!rl&&r
04420
MC/DC50%1/2
$remainder and $remainder =~ /^(.+?) USING \(([^\)]+)\)(.*)/i
$remainder $remainder =~ /^(.+?) USING \(([^\)]+)\)(.*)/i
Truth table66%2/3
A and B
A= $remainderB= $remainder =~ /^(.+?) USING \(([^\)]+)\)(.*)/i
ABresult
0X0
100
111
609 {
610 20 $self->{struct}->{join}->{clause} = 'USING';
611 20 $tableB = $1;
612 20 my $keycolstr = $2;
613 20 $remainder = $3;
614 20 @$keycols = split( /,/, $keycolstr );
615 }
616 64 ▶ if ( $remainder && $remainder =~ m/^(.+?) ON (.+)/i )
Branch: 2 branches
BranchTrueFalseTotal
if ($remainder and $remainder =~ /^(.+?) ON (.+)/i) { } 27 37 64
elsif ($remainder =~ /^(.+?)$/i) { } 18 19 37
Condition100%3/3
$remainder and $remainder =~ /^(.+?) ON (.+)/i
!ll&&!rl&&r
191827
MC/DC100%2/2
$remainder and $remainder =~ /^(.+?) ON (.+)/i
$remainder $remainder =~ /^(.+?) ON (.+)/i
Truth table100%3/3
A and B
A= $remainderB= $remainder =~ /^(.+?) ON (.+)/i
ABresult
0X0
100
111
617 {
618 27 $self->{struct}->{join}->{clause} = 'ON';
619 27 $tableB = $1;
620 27 my $keycolstr = $2;
621 27 $remainder = $3;
622 27 @$keycols = split(/ AND|OR /i, $keycolstr);
623  
624 return undef
625 27 ▶ unless $self->TABLE_NAME_LIST( $tableA . ',' . $tableB );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TABLE_NAME_LIST($tableA . ',' . $tableB) 0 27 27
626  
627 # $self->{tmp}->{is_table_name}->{"$tableA"} = 1;
628 # $self->{tmp}->{is_table_name}->{"$tableB"} = 1;
629 27 for my $keycol (@$keycols)
630 {
631 48 my %is_done;
632 48 $keycol =~ s/\)|\(//g;
633 48 my ( $arg1, $arg2 ) = split( m/ [>=<] /, $keycol );
634 48 my ( $c1, $c2 ) = ( $arg1, $arg2 );
635 48 $c1 =~ s/^.*\.([^\.]+)$/$1/;
636 48 $c2 =~ s/^.*\.([^\.]+)$/$1/;
637 48 ▶ if ( $c1 eq $c2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($c1 eq $c2) { } 9 39 48
638 {
639 9 ▶ return undef unless ( $arg1 = $self->ROW_VALUE($c1) );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg1 = $self->ROW_VALUE($c1) 0 9 9
640 9 ▶ if ( $arg1->{type} eq 'column' and !$is_done{$c1} )
Branch: 1 branch
BranchTrueFalseTotal
if ($arg1->{"type"} eq "column" and not $is_done{$c1}) 9 0 9
Condition33%1/3
$arg1->{"type"} eq "column" and not $is_done{$c1}
!ll&&!rl&&r
009
MC/DC0%0/2
$arg1->{"type"} eq "column" and not $is_done{$c1}
$arg1->{"type"} eq "column" not $is_done{$c1}
Truth table33%1/3
A and not(B)
A= $arg1->{"type"} eq "column"B= not $is_done{$c1}
ABresult
0X0
100
111
641 {
642 9 push( @{ $self->{struct}->{keycols} }, $arg1->{value} );
643 9 $is_done{$c1} = 1;
644 }
645 }
646 else
647 {
648 39 ▶ return undef unless ( $arg1 = $self->ROW_VALUE($arg1) );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg1 = $self->ROW_VALUE($arg1) 0 39 39
649 39 ▶ return undef unless ( $arg2 = $self->ROW_VALUE($arg2) );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg2 = $self->ROW_VALUE($arg2) 0 39 39
650 39 ▶ if ( $arg1->{type} eq 'column'
Branch: 1 branch
BranchTrueFalseTotal
if ($arg1->{"type"} eq "column" and $arg2->{"type"} eq "column") 34 5 39
Condition66%2/3
$arg1->{"type"} eq "column" and $arg2->{"type"} eq "column"
!ll&&!rl&&r
0534
MC/DC50%1/2
$arg1->{"type"} eq "column" and $arg2->{"type"} eq "column"
$arg1->{"type"} eq "column" $arg2->{"type"} eq "column"
Truth table66%2/3
A and B
A= $arg1->{"type"} eq "column"B= $arg2->{"type"} eq "column"
ABresult
0X0
100
111
651 and $arg2->{type} eq 'column' )
652 {
653 34 push( @{ $self->{struct}->{keycols} }, $arg1->{value} );
654 34 push( @{ $self->{struct}->{keycols} }, $arg2->{value} );
655  
656 # delete $self->{struct}->{where_clause};
657 }
658 }
659 }
660 }
661 elsif ( $remainder =~ /^(.+?)$/i )
662 {
663 18 $tableB = $1;
664 18 $remainder = $2;
665 }
666 64 ▶ $remainder =~ s/^\s+// if ($remainder);
Branch: 1 branch
BranchTrueFalseTotal
if $remainder 0 64 64
667 }
668  
669 64 ▶ if ($jtype)
Branch: 1 branch
BranchTrueFalseTotal
if ($jtype) 64 0 64
670 {
671 64 ▶ $jtype = "NATURAL $jtype" if ($natural);
Branch: 1 branch
BranchTrueFalseTotal
if $natural 15 49 64
672 64 ▶ if ( $natural and $keycols )
Branch: 1 branch
BranchTrueFalseTotal
if ($natural and $keycols) 0 64 64
Condition66%2/3
$natural and $keycols
!ll&&!rl&&r
49150
MC/DC0%0/2
$natural and $keycols
$natural $keycols
Truth table66%2/3
A and B
A= $naturalB= $keycols
ABresult
0X0
100
111
673 {
674 0 return $self->do_err(qq{Can't use NATURAL with a USING or ON clause!});
675 }
676 64 ▶ return undef unless ( $self->TABLE_NAME_LIST("$tableA,$tableB") );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TABLE_NAME_LIST("$tableA,$tableB") 2 62 64
677 62 $self->{struct}->{join}->{type} = $jtype;
678 62 ▶ $self->{struct}->{join}->{keycols} = $keycols if ($keycols);
Branch: 1 branch
BranchTrueFalseTotal
if $keycols 46 16 62
679 62 return 1;
680 }
681 0 return $self->do_err("Couldn't parse explicit JOIN!");
682 }
683  
684 sub SELECT_CLAUSE
685 547 ▶{
Subroutine SELECT_CLAUSE: called CC=4 SCAR=13.9
Pod undocumented
686 547 my ( $self, $str ) = @_;
687 547 ▶ return undef unless ($str);
Branch: 1 branch
BranchTrueFalseTotal
unless $str 0 547 547
688 547 ▶ if ( $str =~ s/^(DISTINCT|ALL) (.+)$/$2/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/^(DISTINCT|ALL) (.+)$/$2/i) 8 539 547
689 {
690 8 $self->{struct}->{set_quantifier} = uc($1);
691 }
692 547 ▶ return undef unless ( $self->SELECT_LIST($str) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SELECT_LIST($str) 7 540 547
693 }
694  
695 sub FROM_CLAUSE
696 359 ▶{
Subroutine FROM_CLAUSE: called CC=5 SCAR=16.1
Pod undocumented
697 359 my ( $self, $str ) = @_;
698 359 ▶ return undef unless $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $str 0 359 359
699 359 ▶ if ( $str =~ m/ JOIN /i )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ / JOIN /i) { } 64 295 359
700 {
701 64 ▶ return undef unless $self->EXPLICIT_JOIN($str);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->EXPLICIT_JOIN($str) 2 62 64
702 }
703 else
704 {
705 295 ▶ return undef unless $self->TABLE_NAME_LIST($str);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TABLE_NAME_LIST($str) 3 290 293
706 }
707 }
708  
709 sub INSERT
710 174 ▶{
Subroutine INSERT: called CC=12 SCAR=25.9
Pod undocumented
711 174 my ( $self, $str ) = @_;
712 174 my $col_str;
713 174 $str =~ s/^INSERT\s+INTO\s+/INSERT /i; # allow INTO to be optional
714 174 my ( $table_name, $val_str ) = $str =~ m/^INSERT\s+(.+?)\s+VALUES\s+(\(.+\))$/i;
715 174 ▶ if ( $table_name and $table_name =~ m/[()]/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($table_name and $table_name =~ /[()]/) 7 167 174
Condition66%2/3
$table_name and $table_name =~ /[()]/
!ll&&!rl&&r
01677
MC/DC50%1/2
$table_name and $table_name =~ /[()]/
$table_name $table_name =~ /[()]/
Truth table66%2/3
A and B
A= $table_nameB= $table_name =~ /[()]/
ABresult
0X0
100
111
716 {
717 7 ( $table_name, $col_str, $val_str ) = $str =~ m/^INSERT\s+(.+?)\s+\((.+?)\)\s+VALUES\s+(\(.+\))$/i;
718 }
719 174 ▶ return $self->do_err('No table name specified!') unless ($table_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $table_name 0 174 174
720 174 ▶ return $self->do_err('Missing values list!') unless ( defined $val_str );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val_str 0 174 174
721 174 ▶ return undef unless ( $self->TABLE_NAME($table_name) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TABLE_NAME($table_name) 0 174 174
722 174 $self->{struct}->{command} = 'INSERT';
723 174 $self->{struct}->{table_names} = [$table_name];
724 174 ▶ if ($col_str)
Branch: 1 branch
BranchTrueFalseTotal
if ($col_str) { } 7 167 174
725 {
726 7 ▶ return undef unless ( $self->{struct}->{column_defs} = $self->ROW_VALUE_LIST($col_str) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"struct"}{"column_defs"} = $self->ROW_VALUE_LIST($col_str) 0 7 7
727 }
728 else
729 {
730 $self->{struct}->{column_defs} = [
731 {
732 167 type => 'column',
733 value => '*'
734 }
735 ];
736 }
737 174 $self->{struct}->{values} = [];
738 174 ▶ for (my ($v,$line_str) = $val_str;
Branch: 1 branch
BranchTrueFalseTotal
if (($line_str, $v) = &extract_bracketed($v, "('", "") and defined $line_str) 180 0 180
Condition33%1/3
($line_str, $v) = &extract_bracketed($v, "('", "") and defined $line_str
!ll&&!rl&&r
00180
MC/DC0%0/2
($line_str, $v) = &extract_bracketed($v, "('", "") and defined $line_str
($line_str, $v) = &extract_bracketed($v, "('", "") defined $line_str
Truth table33%1/3
A and B
A= ($line_str, $v) = &extract_bracketed($v, "('", "")B= defined $line_str
ABresult
0X0
100
111
739 (($line_str,$v)=extract_bracketed($v,"('",'')) && defined $line_str;
740 ) {
741 180 ▶ return undef unless ( $self->LITERAL_LIST(substr($line_str,1,-1)) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->LITERAL_LIST(substr($line_str, 1, -1)) 0 180 180
742 180 ▶ last unless $v =~ s/\A\s*,\s*//;
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ s/\A\s*,\s*// 174 6 180
743 }
744  
745 174 return 1;
746 }
747  
748 ###################################################################
749 # UPDATE ::=
750 #
751 # UPDATE <table> SET <set_clause_list> [ WHERE <search_condition>]
752 #
753 ###################################################################
754 sub UPDATE
755 12 ▶{
Subroutine UPDATE: called CC=10 SCAR=23.5
Pod undocumented
756 12 my ( $self, $str ) = @_;
757 12 $self->{struct}->{command} = 'UPDATE';
758 12 my ( $table_name, $remainder ) = $str =~ m/^UPDATE (.+?) SET (.+)$/i;
759 12 ▶ return $self->do_err('Incomplete UPDATE clause') unless ( $table_name && $remainder );
Branch: 1 branch
BranchTrueFalseTotal
unless $table_name and $remainder 0 12 12
Condition33%1/3
$table_name and $remainder
!ll&&!rl&&r
0012
MC/DC0%0/2
$table_name and $remainder
$table_name $remainder
Truth table33%1/3
A and B
A= $table_nameB= $remainder
ABresult
0X0
100
111
760 12 ▶ return undef unless ( $self->TABLE_NAME($table_name) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TABLE_NAME($table_name) 0 12 12
761 12 $self->{tmp}->{is_table_name} = { $table_name => 1 };
762 12 $self->{struct}->{table_names} = [$table_name];
763 12 my ( $set_clause, $where_clause ) = $remainder =~ m/(.*?) WHERE (.*)$/i;
764 12 ▶ $set_clause = $remainder if ( !$set_clause );
Branch: 1 branch
BranchTrueFalseTotal
unless $set_clause 6 6 12
765 12 ▶ return undef unless ( $self->SET_CLAUSE_LIST($set_clause) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SET_CLAUSE_LIST($set_clause) 0 12 12
766  
767 12 ▶ if ($where_clause)
Branch: 1 branch
BranchTrueFalseTotal
if ($where_clause) 6 6 12
768 {
769 6 ▶ return undef unless ( $self->SEARCH_CONDITION($where_clause) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SEARCH_CONDITION($where_clause) 0 6 6
770 }
771  
772 12 my @vals = @{ $self->{struct}->{values}->[0] };
773 12 my $num_val_placeholders = 0;
774 12 for my $v (@vals)
775 {
776 22 ▶ ++$num_val_placeholders if ( $v->{type} eq 'placeholder' );
Branch: 1 branch
BranchTrueFalseTotal
if $v->{"type"} eq "placeholder" 4 18 22
777 }
778 12 $self->{struct}->{num_val_placeholders} = $num_val_placeholders;
779  
780 12 return 1;
781 }
782  
783 ############
784 # FUNCTIONS
785 ############
786 sub LOAD
787 18 ▶{
Subroutine LOAD: called CC=3 SCAR=11.0
Pod undocumented
788 18 my ( $self, $str ) = @_;
789 18 $self->{struct}->{command} = 'LOAD';
790 18 $self->{struct}->{no_execute} = 1;
791 18 my ($package) = $str =~ /^LOAD\s+(.+)$/;
792 18 $str = $package;
793 18 $package =~ s/\?(\d+)\?/$self->{struct}->{literals}->[$1]/g;
794  
795 18 $self->_load_class($package);
796  
797 18 my %subs = eval '%' . $package . '::';
798  
799 18 for my $sub ( keys %subs )
800 {
801 2992 ▶ next unless ( $sub =~ m/^SQL_FUNCTION_([A-Z_0-9]+)$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $sub =~ /^SQL_FUNCTION_([A-Z_0-9]+)$/ 1138 1854 2992
802 1854 my $funcName = uc $1;
803 1854 my $subname = $package . '::' . 'SQL_FUNCTION_' . $funcName;
804 1854 $self->{opts}->{function_names}->{$funcName} = $subname;
805 1854 delete $self->{opts}->{_udf_function_names};
806 }
807 18 1;
808 }
809  
810 sub CREATE_RAM_TABLE
811 0 ▶{
Subroutine CREATE_RAM_TABLE: not called CC=5 SCAR=34.0
Pod undocumented
812 0 my ( $self, $stmt ) = @_;
813 0 $self->{struct}->{is_ram_table} = 1;
814 0 $self->{struct}->{command} = 'CREATE_RAM_TABLE';
815 0 my ( $table_name, $table_element_def, %is_col_name );
816 0 ▶ if ( $stmt =~ /^(\S+)\s+LIKE\s*(.+)$/si )
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ /^(\S+)\s+LIKE\s*(.+)$/is) { } 0 0 0
817 {
818 0 $table_name = $1;
819 0 $table_element_def = $2;
820 0 ▶ if ( $table_element_def =~ /^(.*)\s+KEEP CONNECTION\s*$/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($table_element_def =~ /^(.*)\s+KEEP CONNECTION\s*$/i) 0 0 0
821 {
822 0 $table_element_def = $1;
823 0 $self->{struct}->{ram_table_keep_connection} = 1;
824 }
825 }
826 else
827 {
828 0 return $self->CREATE("CREATE TABLE $stmt");
829 }
830 0 ▶ return undef unless $self->TABLE_NAME($table_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TABLE_NAME($table_name) 0 0 0
831 0 for my $col ( split ',', $table_element_def )
832 {
833 0 push( @{ $self->{struct}->{column_defs} }, $self->ROW_VALUE($col) );
834 }
835 0 $self->{struct}->{table_names} = [$table_name];
836 0 return 1;
837 }
838  
839 sub CREATE_FUNCTION
840 3 ▶{
Subroutine CREATE_FUNCTION: called CC=6 SCAR=18.1
Pod undocumented
841 3 my ( $self, $stmt ) = @_;
842 3 $self->{struct}->{command} = 'CREATE_FUNCTION';
843 3 $self->{struct}->{no_execute} = 1;
844 3 my ( $func, $subname );
845 3 $stmt =~ s/\s*EXTERNAL//i;
846 3 ▶ if ( $stmt =~ /^(\S+)\s+NAME\s+(.*)$/smi )
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ /^(\S+)\s+NAME\s+(.*)$/ism) 2 1 3
847 {
848 2 $func = trim($1);
849 2 $subname = trim($2);
850 }
851 3 ▶ $func ||= $stmt;
Condition66%2/3
$func ||= $stmt
l!l&&r!l&&!r
210
MC/DC0%0/2
$func ||= $stmt
$func $stmt
Truth table66%2/3
A ||= B
A= $funcB= $stmt
ABresult
1X1
011
000
852 3 ▶ $subname ||= $func;
Condition66%2/3
$subname ||= $func
l!l&&r!l&&!r
210
MC/DC0%0/2
$subname ||= $func
$subname $func
Truth table66%2/3
A ||= B
A= $subnameB= $func
ABresult
1X1
011
000
853 3 ▶ if ( $func =~ /^\?QI(\d+)\?$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($func =~ /^\?QI(\d+)\?$/) 0 3 3
854 {
855 0 $func = $self->{struct}->{quoted_ids}->[$1];
856 }
857 3 ▶ if ( $subname =~ /^\?QI(\d+)\?$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($subname =~ /^\?QI(\d+)\?$/) 2 1 3
858 {
859 2 $subname = $self->{struct}->{quoted_ids}->[$1];
860 }
861 3 $self->{opts}->{function_names}->{ uc $func } = $subname;
862 3 delete $self->{opts}->{_udf_function_names};
863  
864 3 return 1;
865 }
866  
867 sub CALL
868 0 ▶{
Subroutine CALL: not called CC=1 SCAR=6.9
Pod undocumented
869 0 my ( $self, $stmt ) = @_;
870 0 $stmt =~ s/^CALL\s+(.*)/$1/i;
871 0 $self->{struct}->{command} = 'CALL';
872 0 $self->{struct}->{procedure} = $self->ROW_VALUE($stmt);
873 0 return 1;
874 }
875  
876 sub CREATE_TYPE
877 3 ▶{
Subroutine CREATE_TYPE: called CC=1 SCAR=0.0
Pod undocumented
878 3 my ( $self, $type ) = @_;
879 3 $self->{struct}->{command} = 'CREATE_TYPE';
880 3 $self->{struct}->{no_execute} = 1;
881 3 $self->feature( 'valid_data_types', uc $type, 1 );
882 }
883  
884 sub DROP_TYPE
885 1 ▶{
Subroutine DROP_TYPE: called CC=1 SCAR=0.0
Pod undocumented
886 1 my ( $self, $type ) = @_;
887 1 $self->{struct}->{command} = 'DROP_TYPE';
888 1 $self->{struct}->{no_execute} = 1;
889 1 $self->feature( 'valid_data_types', uc $type, 0 );
890 }
891  
892 sub CREATE_KEYWORD
893 1 ▶{
Subroutine CREATE_KEYWORD: called CC=1 SCAR=0.0
Pod undocumented
894 1 my ( $self, $type ) = @_;
895 1 $self->{struct}->{command} = 'CREATE_KEYWORD';
896 1 $self->{struct}->{no_execute} = 1;
897 1 $self->feature( 'reserved_words', uc $type, 1 );
898 }
899  
900 sub DROP_KEYWORD
901 1 ▶{
Subroutine DROP_KEYWORD: called CC=1 SCAR=0.0
Pod undocumented
902 1 my ( $self, $type ) = @_;
903 1 $self->{struct}->{command} = 'DROP_KEYWORD';
904 1 $self->{struct}->{no_execute} = 1;
905 1 $self->feature( 'reserved_words', uc $type, 0 );
906 }
907  
908 sub CREATE_OPERATOR
909 2 ▶{
Subroutine CREATE_OPERATOR: called CC=6 SCAR=20.5
Pod undocumented
910 2 my ( $self, $stmt ) = @_;
911 2 $self->{struct}->{command} = 'CREATE_OPERATOR';
912 2 $self->{struct}->{no_execute} = 1;
913  
914 2 my ( $func, $subname );
915 2 $stmt =~ s/\s*EXTERNAL//i;
916 2 ▶ if ( $stmt =~ /^(\S+)\s+NAME\s+(.*)$/smi )
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ /^(\S+)\s+NAME\s+(.*)$/ism) 0 2 2
917 {
918 0 $func = trim($1);
919 0 $subname = trim($2);
920 }
921 2 ▶ $func ||= $stmt;
Condition33%1/3
$func ||= $stmt
l!l&&r!l&&!r
020
MC/DC0%0/2
$func ||= $stmt
$func $stmt
Truth table33%1/3
A ||= B
A= $funcB= $stmt
ABresult
1X1
011
000
922 2 ▶ $subname ||= $func;
Condition33%1/3
$subname ||= $func
l!l&&r!l&&!r
020
MC/DC0%0/2
$subname ||= $func
$subname $func
Truth table33%1/3
A ||= B
A= $subnameB= $func
ABresult
1X1
011
000
923 2 ▶ if ( $func =~ /^\?QI(\d+)\?$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($func =~ /^\?QI(\d+)\?$/) 0 2 2
924 {
925 0 $func = $self->{struct}->{quoted_ids}->[$1];
926 }
927 2 ▶ if ( $subname =~ /^\?QI(\d+)\?$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($subname =~ /^\?QI(\d+)\?$/) 0 2 2
928 {
929 0 $subname = $self->{struct}->{quoted_ids}->[$1];
930 }
931 2 $self->{opts}->{function_names}->{ uc $func } = $subname;
932 2 delete $self->{opts}->{_udf_function_names};
933  
934 2 $self->feature( 'valid_comparison_operators', uc $func, 1 );
935 2 return $self->create_op_regexen();
936 }
937  
938 sub DROP_OPERATOR
939 1 ▶{
Subroutine DROP_OPERATOR: called CC=1 SCAR=0.0
Pod undocumented
940 1 my ( $self, $type ) = @_;
941 1 $self->{struct}->{command} = 'DROP_OPERATOR';
942 1 $self->{struct}->{no_execute} = 1;
943 1 $self->feature( 'valid_comparison_operators', uc $type, 0 );
944 1 return $self->create_op_regexen();
945 }
946  
947 sub replace_quoted($)
948 9 ▶{
Subroutine replace_quoted: called CC=1 SCAR=0.0
Pod documented
949 9 my ( $self, $str ) = @_;
950 9 my @l = map { $self->replace_quoted_ids($_) } split( ',', $self->replace_quoted_commas($str) );
951 9 return @l;
952 }
953  
954 #########
955 # CREATE
956 #########
957 sub CREATE
958 70 ▶{
Subroutine CREATE: called CC=30 SCAR=36.1
Pod undocumented
959 70 my ( $self, $stmt ) = @_;
960 70 my $features = 'TYPE|KEYWORD|FUNCTION|OPERATOR|PREDICATE';
961 70 ▶ if ( $stmt =~ m/^\s*CREATE\s+($features)\s+(.+)$/si )
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ /^\s*CREATE\s+($features)\s+(.+)$/is) 9 61 70
962 {
963 9 my ( $sub, $arg ) = ( $1, $2 );
964 9 $sub = 'CREATE_' . uc $sub;
965 9 return $self->$sub($arg);
966 }
967  
968 61 $stmt =~ s/^CREATE (LOCAL|GLOBAL) /CREATE /si;
969 61 ▶ if ( $stmt =~ m/^\s*CREATE\s+(?:TEMP|TEMPORARY)\s+TABLE\s+(.+)$/si )
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ /^\s*CREATE\s+(?:TEMP|TEMPORARY)\s+TABLE\s+(.+)$/is) 43 18 61
970 {
971 43 $stmt = "CREATE TABLE $1";
972 43 $self->{struct}->{is_ram_table} = 1;
973 }
974 61 $self->{struct}->{command} = 'CREATE';
975 61 my ( $table_name, $table_element_def, %is_col_name );
976  
977 61 ▶ if ( $stmt =~ m/^(.*) ON COMMIT (DELETE|PRESERVE) ROWS\s*$/si )
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ /^(.*) ON COMMIT (DELETE|PRESERVE) ROWS\s*$/is) 4 57 61
978 {
979 4 $stmt = $1;
980 4 $self->{struct}->{commit_behaviour} = $2;
981  
982 # return $self->do_err(
983 # "Can't specify commit behaviour for permanent tables."
984 # )
985 # if !defined $self->{struct}->{table_type}
986 # or $self->{struct}->{table_type} !~ /TEMPORARY/;
987 }
988 61 ▶ if ( $stmt =~ m/^CREATE TABLE (\S+) \((.*)\)$/si )
Branch: 2 branches
BranchTrueFalseTotal
if ($stmt =~ /^CREATE TABLE (\S+) \((.*)\)$/is) { } 61 0 61
elsif ($stmt =~ /^CREATE TABLE (\S+) AS (.*)$/is) { } 0 0 0
989 {
990 61 $table_name = $1;
991 61 $table_element_def = $2;
992 }
993 elsif ( $stmt =~ m/^CREATE TABLE (\S+) AS (.*)$/si )
994 {
995 0 $table_name = $1;
996 0 my $subquery = $2;
997 0 ▶ return undef unless $self->TABLE_NAME($table_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TABLE_NAME($table_name) 0 0 0
998 0 $self->{struct}->{table_names} = [$table_name];
999  
1000 # undo subquery replaces
1001 0 $subquery =~ s/\?(\d+)\?/'$self->{struct}{literals}[$1]'/g;
1002 0 $subquery =~ s/\?QI(\d+)\?/"$self->{struct}->{quoted_ids}->[$1]"/g;
1003 0 $subquery =~ s/\?COMMA\?/,/gs;
1004 0 $self->{struct}->{subquery} = $subquery;
1005 0 ▶ if ( -1 != index( $subquery, '?' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (index($subquery, "?") != -1) 0 0 0
1006 {
1007 0 ++$self->{struct}->{num_placeholders};
1008 }
1009 0 return 1;
1010 }
1011 else
1012 {
1013 0 return $self->do_err("Can't find column definitions!");
1014 }
1015 61 ▶ return undef unless ( $self->TABLE_NAME($table_name) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TABLE_NAME($table_name) 0 61 61
1016 61 $table_element_def =~ s/\s+\(/(/g;
1017 61 my $primary_defined;
1018 61 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($table_element_def =~ s/( # start of grouping 1 \( # match a bracket; vi compatible bracket -> \)( [^)]+ # everything up to but not including the comma, no nesting of brackets is required ) # end of grouping 1 , # the comma to be removed to allow splitting on commas ( # start of grouping 2; vi compatible bracket -> \( .*?\) # everything up to and including the end bracket )/$1?COMMA?$2/gsx) 6 61 67
1019 $table_element_def =~ s/( # start of grouping 1
1020 \( # match a bracket; vi compatible bracket -> \)(
1021 [^)]+ # everything up to but not including the comma, no nesting of brackets is required
1022 ) # end of grouping 1
1023 , # the comma to be removed to allow splitting on commas
1024 ( # start of grouping 2; vi compatible bracket -> \(
1025 .*?\) # everything up to and including the end bracket
1026 )/$1?COMMA?$2/sgx
1027 )
1028 {
1029 }
1030  
1031 61 for my $col ( split( ',', $table_element_def ) )
1032 {
1033 139 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($col =~ /^\s*(?:CONSTRAINT\s+(\S+)\s*)? # optional name of foreign key FOREIGN\s+KEY\s*\(\s* # start of list of; vi compatibile bracket -> ( (\s*[^)]+\s*) # field names in this table \s*\)\s* # end of field names in this table REFERENCES # key word \s*(\S+)\s* # table name being referenced in foreign key \(\s* # start of list of; vi compatible bracket -> ( (\s*[^)]+\s*) # field names in foreign table \s*\)\s* # end of field names in foreign table $/x) { } 3 136 139
elsif ($col =~ /^\s*(?:CONSTRAINT\s+(\S+)\s*)? # optional name of foreign key PRIMARY\s+KEY\s*\(\s* # start of list of; vi compatibile bracket -> ( (\s*[^)]+\s*) # field names in this table \s*\)\s* # end of field names in this table $/x) { } 3 133 136
1034 $col =~ m/^\s*(?:CONSTRAINT\s+(\S+)\s*)? # optional name of foreign key
1035 FOREIGN\s+KEY\s*\(\s* # start of list of; vi compatibile bracket -> (
1036 (\s*[^)]+\s*) # field names in this table
1037 \s*\)\s* # end of field names in this table
1038 REFERENCES # key word
1039 \s*(\S+)\s* # table name being referenced in foreign key
1040 \(\s* # start of list of; vi compatible bracket -> (
1041 (\s*[^)]+\s*) # field names in foreign table
1042 \s*\)\s* # end of field names in foreign table
1043 $/x
1044 )
1045 {
1046 3 my ( $name, $local_cols, $referenced_table, $referenced_cols ) = ( $1, $2, $3, $4 );
1047 3 my @local_cols = $self->replace_quoted($local_cols);
1048 3 $referenced_table = $self->replace_quoted_ids($referenced_table);
1049 3 my @referenced_cols = $self->replace_quoted($referenced_cols);
1050  
1051 3 ▶ if ( defined $name )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 2 1 3
1052 {
1053 2 $name = $self->replace_quoted_ids($name);
1054 }
1055 else
1056 {
1057 1 $name = $self->replace_quoted_ids($table_name);
1058 1 my ($quote_char) = '';
1059 1 ▶ if ( $name =~ s/(\W)$// )
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/(\W)$//) 0 1 1
1060 {
1061 0 $quote_char = ($1);
1062 }
1063 1 foreach my $local_col (@local_cols)
1064 {
1065 2 my $col_name = $local_col;
1066 2 $col_name =~ s/^\W//;
1067 2 $col_name =~ s/\W$//;
1068 2 $name .= '_' . $col_name;
1069 }
1070 1 $name .= '_fkey' . $quote_char;
1071 }
1072  
1073 3 $self->{struct}->{table_defs}->{$name}->{type} = 'FOREIGN';
1074 3 $self->{struct}->{table_defs}->{$name}->{local_cols} = \@local_cols;
1075 3 $self->{struct}->{table_defs}->{$name}->{referenced_table} = $referenced_table;
1076 3 $self->{struct}->{table_defs}->{$name}->{referenced_cols} = \@referenced_cols;
1077 3 next;
1078 }
1079 elsif (
1080 $col =~ m/^\s*(?:CONSTRAINT\s+(\S+)\s*)? # optional name of foreign key
1081 PRIMARY\s+KEY\s*\(\s* # start of list of; vi compatibile bracket -> (
1082 (\s*[^)]+\s*) # field names in this table
1083 \s*\)\s* # end of field names in this table
1084 $/x
1085 )
1086 {
1087 3 my ( $name, $local_cols ) = ( $1, $2 );
1088 3 my @local_cols = $self->replace_quoted($local_cols);
1089 3 ▶ if ( defined $name )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 2 1 3
1090 {
1091 2 $name = $self->replace_quoted_ids($name);
1092 }
1093 else
1094 {
1095 1 $name = $table_name;
1096 1 ▶ if ( $name =~ s/(\W)$// )
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/(\W)$//) { } 0 1 1
1097 {
1098 0 $name .= '_pkey' . $1;
1099 }
1100 else
1101 {
1102 1 $name .= '_pkey';
1103 }
1104 }
1105 3 $self->{struct}->{table_defs}->{$name}->{type} = 'PRIMARY';
1106 3 $self->{struct}->{table_defs}->{$name}->{local_cols} = \@local_cols;
1107 3 next;
1108 }
1109  
1110 # it seems, perl 5.6 isn't greedy enough .. let's help a bit
1111 133 my ($data_types_regex) = join( '|', sort { length($b) <=> length($a) } keys %{ $self->{opts}->{valid_data_types} } );
1112 133 $data_types_regex =~ s/ /\\ /g; # backslash spaces to allow the /x modifier below
1113 133 my ( $name, $type, $suffix ) = (
1114 $col =~ m/\s*(\S+)\s+ # capture the column name
1115 ((?:$data_types_regex|\S+) # check for all allowed data types OR anything that looks like a bad data type to give a good error
1116 (?:\s*\(\d+(?:\?COMMA\?\d+)?\))?) # allow the data type to have a precision specifier such as NUMERIC(4,6) on it
1117 \s*(\W.*|$) # capture the suffix of the column definition, e.g. constraints
1118 /ix
1119 );
1120 133 ▶ return $self->do_err("Column definition is missing a data type!") unless ($type);
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 133 133
1121 133 ▶ return undef unless ( $self->IDENTIFIER($name) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->IDENTIFIER($name) 0 133 133
1122  
1123 133 $name = $self->replace_quoted_ids($name);
1124  
1125 133 my @possible_constraints = ('PRIMARY KEY', 'NOT NULL', 'UNIQUE');
1126  
1127 133 for my $constraint (@possible_constraints)
1128 {
1129 399 my $count = $suffix =~ s/$constraint//gi;
1130 399 ▶ next if $count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 389 10 399
1131  
1132 10 ▶ return $self->do_err(qq~Duplicate column constraint: '$constraint'!~)
Branch: 1 branch
BranchTrueFalseTotal
if $count > 1 0 10 10
1133 if $count > 1;
1134  
1135 10 ▶ return $self->do_err(qq{Can't have two PRIMARY KEYs in a table!})
Branch: 1 branch
BranchTrueFalseTotal
if $constraint eq "PRIMARY KEY" and $primary_defined++ 0 10 10
Condition66%2/3
$constraint eq "PRIMARY KEY" and $primary_defined++
!ll&&!rl&&r
820
MC/DC0%0/2
$constraint eq "PRIMARY KEY" and $primary_defined++
$constraint eq "PRIMARY KEY" $primary_defined++
Truth table66%2/3
A and B
A= $constraint eq "PRIMARY KEY"B= $primary_defined++
ABresult
0X0
100
111
1136 if $constraint eq 'PRIMARY KEY' and $primary_defined++;
1137  
1138 10 push @{ $self->{struct}->{table_defs}->{columns}->{$name}->{constraints} }, $constraint;
1139 }
1140  
1141 133 $suffix =~ s/^\s+//;
1142 133 $suffix =~ s/\s+$//;
1143  
1144 133 ▶ return $self->do_err("Unknown column constraint: '$suffix'!") unless ($suffix eq '');
Branch: 1 branch
BranchTrueFalseTotal
unless $suffix eq "" 0 133 133
1145  
1146 133 $type = uc $type;
1147 133 my $length;
1148 133 ▶ if ( $type =~ m/(.+)\((.+)\)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /(.+)\((.+)\)/) 20 113 133
1149 {
1150 20 $type = $1;
1151 20 $length = $2;
1152 }
1153 133 ▶ if ( !$self->{opts}->{valid_data_types}->{$type} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"opts"}{"valid_data_types"}{$type}) 2 131 133
1154 {
1155 2 return $self->do_err("'$type' is not a recognized data type!");
1156 }
1157 131 $self->{struct}->{table_defs}->{columns}->{$name}->{data_type} = $type;
1158 131 $self->{struct}->{table_defs}->{columns}->{$name}->{data_length} = $length;
1159 push(
1160 131 @{ $self->{struct}->{column_defs} },
1161 {
1162 type => 'column',
1163 value => $name,
1164 fullorg => $name,
1165 }
1166 );
1167  
1168 131 my $tmpname = $name;
1169 131 ▶ $tmpname = lc $tmpname unless ( $tmpname =~ m/^(?:\p{Word}+\.)?"/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $tmpname =~ /^(?:\p{Word}+\.)?"/ 125 6 131
1170 131 ▶ return $self->do_err("Duplicate column names!") if $is_col_name{$tmpname}++;
Branch: 1 branch
BranchTrueFalseTotal
if $is_col_name{$tmpname}++ 0 131 131
1171  
1172 }
1173 59 $self->{struct}->{table_names} = [$table_name];
1174 59 return 1;
1175 }
1176  
1177 ###############
1178 # SQL SUBRULES
1179 ###############
1180  
1181 sub SET_CLAUSE_LIST
1182 12 ▶{
Subroutine SET_CLAUSE_LIST: called CC=6 SCAR=18.3
Pod undocumented
1183 12 my ( $self, $set_string ) = @_;
1184 my @sets = extract_multiple($set_string, [
1185 28 ▶ sub { my ($m, $r, $p) = extract_bracketed($_[0], "()", qr/[^,(]*/); (($p||'').($m||''), $r, ''); },
Subroutine __ANON__: called CC=3 SCAR=11.0
Condition100%4/4
$p || ''
l!l
523
$m || ''
l!l
721
MC/DC100%1/1
$p || ''
$p
MC/DC100%1/1
$m || ''
$m
Truth table100%2/2
A
A= $p
Aresult
11
00
Truth table100%2/2
A
A= $m
Aresult
11
00
1186 12 qr/([^,(]+)/,
1187 ], undef, 1);
1188 12 my ( @cols, @vals );
1189 12 for my $set (@sets)
1190 {
1191 20 my ( $col, $val ) = split( m/ = /, $set );
1192 20 ▶ return $self->do_err('Incomplete SET clause!') unless ( defined($col) && defined($val) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col and defined $val 0 20 20
Condition33%1/3
defined $col and defined $val
!ll&&!rl&&r
0020
MC/DC0%0/2
defined $col and defined $val
defined $col defined $val
Truth table33%1/3
A and B
A= defined $colB= defined $val
ABresult
0X0
100
111
1193 20 push( @cols, $col );
1194 20 push( @vals, $val );
1195 }
1196 return undef
1197 12 ▶ unless ( $self->{struct}->{column_defs} = $self->ROW_VALUE_LIST( join ',', @cols ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"struct"}{"column_defs"} = $self->ROW_VALUE_LIST(join(",", @cols)) 0 12 12
1198 12 ▶ return undef unless ( $self->LITERAL_LIST( join ',', @vals ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->LITERAL_LIST(join(",", @vals)) 0 12 12
1199 12 return 1;
1200 }
1201  
1202 sub SET_QUANTIFIER
1203 0 ▶{
Subroutine SET_QUANTIFIER: not called CC=2 SCAR=17.9
Pod undocumented
1204 0 my ( $self, $str ) = @_;
1205 0 ▶ if ( $str =~ /^(DISTINCT|ALL)\s+(.*)$/si )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(DISTINCT|ALL)\s+(.*)$/is) 0 0 0
1206 {
1207 0 $self->{struct}->{set_quantifier} = uc $1;
1208 0 $str = $2;
1209 }
1210 0 return $str;
1211 }
1212  
1213 #
1214 # DAA v1.11
1215 # modify to transform || strings into
1216 # CONCAT(<expr>); note that we
1217 # only xform the topmost expressions;
1218 # if a concat is contained within a subfunction,
1219 # it should get handled by ROW_VALUE()
1220 #
1221 sub transform_concat
1222 1 ▶{
Subroutine transform_concat: called CC=8 SCAR=23.2
Pod documented
1223 1 my ( $obj, $colstr ) = @_;
1224  
1225 1 pos($colstr) = 0;
1226 1 my $parens = 0;
1227 1 my $spos = 0;
1228 1 my @concats = ();
1229 1 ▶ my $alias = ( $colstr =~ s/^(.+)(\s+AS\s+\S+)$/$1/ ) ? $2 : '';
Branch: 1 branch
BranchTrueFalseTotal
$colstr =~ s/^(.+)(\s+AS\s+\S+)$/$1/ ? : 0 1 1
1230  
1231 1 ▶ while ( $colstr =~ /\G.*?([\(\)\|])/gcs )
Branch: 1 branch
BranchTrueFalseTotal
if ($colstr =~ /\G.*?([\(\)\|])/cgs) 10 1 11
1232 {
1233 10 ▶ if ( $1 eq '(' )
Branch: 3 branches
BranchTrueFalseTotal
if ($1 eq '(') { } 3 7 10
elsif ($1 eq ')') { } 3 4 7
elsif (not $parens and substr($colstr, $-[1] + 1, 1) eq '|') { } 0 8 8
1234 {
1235 3 $parens++;
1236 }
1237 elsif ( $1 eq ')' )
1238 {
1239 3 $parens--;
1240 }
1241 ▶ elsif (( !$parens )
Condition33%1/3
not $parens and substr($colstr, $-[1] + 1, 1) eq "|"
!ll&&!rl&&r
400
MC/DC0%0/2
not $parens and substr($colstr, $-[1] + 1, 1) eq "|"
not $parens substr($colstr, $-[1] + 1, 1) eq "|"
Truth table33%1/3
not(A) and B
A= not $parensB= substr($colstr, $-[1] + 1, 1) eq "|"
ABresult
0X0
100
111
1242 && ( substr( $colstr, $-[1] + 1, 1 ) eq '|' ) )
1243 {
1244  
1245 #
1246 # its a concat outside of parens, push prior string on stack
1247 #
1248 0 push @concats, substr( $colstr, $spos, $-[1] - $spos );
1249 0 $spos = $+[1] + 1;
1250 0 pos($colstr) = $spos;
1251 }
1252 }
1253  
1254 #
1255 # no concats, return original
1256 #
1257 1 ▶ return $colstr unless scalar @concats;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @concats 1 0 1
1258  
1259 #
1260 # don't forget the last one!
1261 #
1262 0 push @concats, substr( $colstr, $spos );
1263 0 return 'CONCAT(' . join( ', ', @concats ) . ")$alias";
1264 }
1265  
1266 #
1267 # DAA v1.10
1268 # improved column list extraction
1269 # original doesn't seem to handle
1270 # commas within function argument lists
1271 #
1272 # DAA v1.11
1273 # modify to transform || strings into
1274 # CONCAT(<expr-list>)
1275 #
1276 sub extract_column_list
1277 315 ▶{
Subroutine extract_column_list: called CC=10 SCAR=23.0
Pod documented
1278 315 my ( $self, $colstr ) = @_;
1279  
1280 315 my @collist = ();
1281 315 pos($colstr) = 0;
1282 315 my $parens = 0;
1283 315 my $spos = 0;
1284 315 ▶ while ( $colstr =~ m/\G.*?([\(\),])/gcs )
Branch: 1 branch
BranchTrueFalseTotal
if ($colstr =~ /\G.*?([\(\),])/cgs) 711 315 1026
1285 {
1286 711 ▶ if ( $1 eq '(' )
Branch: 3 branches
BranchTrueFalseTotal
if ($1 eq '(') { } 242 469 711
elsif ($1 eq ')') { } 239 230 469
elsif (not $parens) { } 137 93 230
1287 {
1288 242 $parens++;
1289 }
1290 elsif ( $1 eq ')' )
1291 {
1292 239 $parens--;
1293 }
1294 elsif ( !$parens )
1295 { # its a comma outside of parens
1296 137 push( @collist, substr( $colstr, $spos, $-[1] - $spos ) );
1297 137 $collist[-1] =~ s/^\s+//;
1298 137 $collist[-1] =~ s/\s+$//;
1299 137 ▶ return $self->do_err('Bad column list!') if ( $collist[-1] eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $collist[-1] eq "" 0 137 137
1300 137 $spos = $+[1];
1301 }
1302 }
1303 315 ▶ return $self->do_err('Unbalanced parentheses!') if ($parens);
Branch: 1 branch
BranchTrueFalseTotal
if $parens 3 312 315
1304  
1305 # don't forget the last one!
1306 312 push( @collist, substr( $colstr, $spos ) );
1307 312 $collist[-1] =~ s/^\s+//;
1308 312 $collist[-1] =~ s/\s+$//;
1309 312 ▶ return $self->do_err('Bad column list!') if ( $collist[-1] eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $collist[-1] eq "" 0 312 312
1310  
1311 # scan for and convert string concats to CONCAT()
1312 312 foreach ( 0 .. $#collist )
1313 {
1314 449 ▶ $collist[$_] = $self->transform_concat( $collist[$_] ) if ( $collist[$_] =~ m/\|\|/ );
Branch: 1 branch
BranchTrueFalseTotal
if $collist[$_] =~ /\|\|/ 1 448 449
1315 }
1316  
1317 312 return @collist;
1318 }
1319  
1320 sub SELECT_LIST
1321 547 ▶{
Subroutine SELECT_LIST: called CC=18 SCAR=29.1
Pod undocumented
1322 547 my ( $self, $col_str ) = @_;
1323 547 ▶ if ( $col_str =~ m/^\s*\*\s*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($col_str =~ /^\s*\*\s*$/) 232 315 547
1324 {
1325 $self->{struct}->{column_defs} = [
1326 {
1327 232 type => 'column',
1328 value => '*'
1329 }
1330 ];
1331 232 $self->{struct}->{column_aliases} = {};
1332  
1333 232 return 1;
1334 }
1335 315 my @col_list = $self->extract_column_list($col_str);
1336 315 ▶ return undef unless ( scalar(@col_list) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @col_list 3 312 315
1337  
1338 312 my ( @newcols, %aliases );
1339 312 for my $col (@col_list)
1340 {
1341 # DAA:
1342 # need better alias test here, since AS is a common
1343 # keyword that might be used in a function
1344 445 ▶ my ( $fld, $alias ) =
Branch: 1 branch
BranchTrueFalseTotal
$col =~ /^(.+?)\s+(?:AS\s+)?([A-Z]\p{Word}*|\?QI\d+\?)$/i ? : 16 429 445
1345 ( $col =~ m/^(.+?)\s+(?:AS\s+)?([A-Z]\p{Word}*|\?QI\d+\?)$/i )
1346 ? ( $1, $2 )
1347 : ( $col, undef );
1348 445 $col = $fld;
1349 445 ▶ if ( $col =~ m/^(\S+)\.\*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($col =~ /^(\S+)\.\*$/) { } 2 443 445
1350 {
1351 2 my $table = $1;
1352 2 ▶ if ( defined($alias) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alias) 0 2 2
1353 {
1354 0 return $self->do_err("'$table.*' cannot be aliased");
1355 }
1356 $table = $self->{tmp}->{is_table_alias}->{$table}
1357 2 ▶ if ( $self->{tmp}->{is_table_alias}->{$table} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tmp"}{"is_table_alias"}{$table} 0 2 2
1358 $table = $self->{tmp}->{is_table_alias}->{"\L$table"}
1359 2 ▶ if ( $self->{tmp}->{is_table_alias}->{"\L$table"} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tmp"}{"is_table_alias"}{lc $table} 0 2 2
1360 2 ▶ return undef unless ( $self->TABLE_NAME($table) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TABLE_NAME($table) 0 2 2
1361 2 $table = $self->replace_quoted_ids($table);
1362 2 push(
1363 @newcols,
1364 {
1365 type => 'column',
1366 value => "$table.*",
1367 }
1368 );
1369 }
1370 else
1371 {
1372 443 my $newcol;
1373 443 $newcol = $self->SET_FUNCTION_SPEC($col);
1374 443 ▶ return if ( $self->{struct}->{errstr} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"struct"}{"errstr"} 3 440 443
1375 440 ▶ $newcol ||= $self->ROW_VALUE($col);
Condition66%2/3
$newcol ||= $self->ROW_VALUE($col)
l!l&&r!l&&!r
334070
MC/DC0%0/2
$newcol ||= $self->ROW_VALUE($col)
$newcol $self->ROW_VALUE($col)
Truth table66%2/3
A ||= B
A= $newcolB= $self->ROW_VALUE($col)
ABresult
1X1
011
000
1376 440 ▶ return if ( $self->{struct}->{errstr} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"struct"}{"errstr"} 1 439 440
1377 439 ▶ return $self->do_err("Invalid SELECT entry '$col'")
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_HASH($newcol) 0 439 439
1378 unless ( defined( _HASH($newcol) ) );
1379  
1380 # FIXME this might be better done later and only if not 2 functions with the same name are selected
1381 439 ▶ if ( !defined($alias)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $alias and "function" eq $newcol->{"type"} || "setfunc" eq $newcol->{"type"}) 220 219 439
Condition100%6/6
not defined $alias and "function" eq $newcol->{"type"} || "setfunc" eq $newcol->{"type"}
!ll&&!rl&&r
16203220
"function" eq $newcol->{"type"} or "setfunc" eq $newcol->{"type"}
l!l&&r!l&&!r
18931203
MC/DC100%3/3
not defined $alias and "function" eq $newcol->{"type"} || "setfunc" eq $newcol->{"type"}
not defined $alias "function" eq $newcol->{"type"} "setfunc" eq $newcol->{"type"}
Truth table100%4/4
not(A) and B or C
A= not defined $aliasB= "function" eq $newcol->{"type"}C= "setfunc" eq $newcol->{"type"}
ABCresult
0XX0
1000
11X1
1011
1382 && ( ( 'function' eq $newcol->{type} ) || ( 'setfunc' eq $newcol->{type} ) ) )
1383 {
1384 220 $alias = $newcol->{name};
1385 }
1386  
1387 439 ▶ if ( defined($alias) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alias) 236 203 439
1388 {
1389 236 $alias = $self->replace_quoted_ids($alias);
1390 236 $newcol->{alias} = $alias;
1391 236 $aliases{ $newcol->{fullorg} } = $alias;
1392 236 $self->{struct}->{ORG_NAME}->{ $newcol->{fullorg} } = $alias;
1393 236 $self->{struct}->{ALIASES}->{$alias} = $newcol->{fullorg};
1394 }
1395 439 push( @newcols, $newcol );
1396 }
1397 }
1398 308 $self->{struct}->{column_aliases} = \%aliases;
1399 308 $self->{struct}->{column_defs} = \@newcols;
1400 308 return 1;
1401 }
1402  
1403 sub SET_FUNCTION_SPEC
1404 443 ▶{
Subroutine SET_FUNCTION_SPEC: called CC=8 SCAR=20.8
Pod undocumented
1405 443 my ( $self, $col_str ) = @_;
1406  
1407 443 ▶ if ( $col_str =~ m/^(COUNT|AVG|SUM|MAX|MIN) \((.*)\)\s*$/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($col_str =~ /^(COUNT|AVG|SUM|MAX|MIN) \((.*)\)\s*$/i) { } 34 409 443
1408 {
1409 34 my $set_function_name = uc $1;
1410 34 my $set_function_arg_str = $2;
1411 34 my $distinct = 'ALL';
1412 34 ▶ if ( $set_function_arg_str =~ s/(DISTINCT|ALL) (.+)$/$2/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($set_function_arg_str =~ s/(DISTINCT|ALL) (.+)$/$2/i) 5 29 34
1413 {
1414 5 $distinct = uc $1;
1415 }
1416 34 ▶ my $count_star = ( $set_function_name eq 'COUNT' ) && ( $set_function_arg_str eq '*' );
Condition100%3/3
$set_function_name eq 'COUNT' && $set_function_arg_str eq '*'
!ll&&!rl&&r
2068
MC/DC100%2/2
$set_function_name eq 'COUNT' && $set_function_arg_str eq '*'
$set_function_name eq 'COUNT' $set_function_arg_str eq '*'
Truth table100%3/3
A && B
A= $set_function_name eq 'COUNT'B= $set_function_arg_str eq '*'
ABresult
0X0
100
111
1417  
1418 34 my $set_function_arg;
1419 34 ▶ if ($count_star)
Branch: 1 branch
BranchTrueFalseTotal
if ($count_star) { } 8 26 34
1420 {
1421 8 ▶ return $self->do_err("Keyword DISTINCT is not allowed for COUNT(*)")
Branch: 1 branch
BranchTrueFalseTotal
if "DISTINCT" eq $distinct 1 7 8
1422 if ( 'DISTINCT' eq $distinct );
1423 7 $set_function_arg = {
1424 type => 'column',
1425 value => '*'
1426 };
1427 }
1428 else
1429 {
1430 26 $set_function_arg = $self->ROW_VALUE($set_function_arg_str);
1431 26 ▶ return if ( $self->{struct}->{errstr} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"struct"}{"errstr"} 0 26 26
1432 26 ▶ return unless ( defined( _HASH($set_function_arg) ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_HASH($set_function_arg) 0 26 26
1433 }
1434  
1435 33 $self->{struct}->{has_set_functions} = 1;
1436  
1437 33 my $value = {
1438 name => $set_function_name,
1439 arg => $set_function_arg,
1440 argstr => lc($set_function_arg_str),
1441 distinct => $distinct,
1442 type => 'setfunc',
1443 fullorg => $col_str,
1444 };
1445 33 return $value;
1446 }
1447 else
1448 {
1449 409 return undef;
1450 }
1451 }
1452  
1453 sub LIMIT_CLAUSE
1454 9 ▶{
Subroutine LIMIT_CLAUSE: called CC=10 SCAR=24.5
Pod undocumented
1455 9 my ( $self, $limit_clause ) = @_;
1456  
1457 # $limit_clause = trim($limit_clause);
1458 9 $limit_clause =~ s/^\s+//;
1459 9 $limit_clause =~ s/\s+$//;
1460  
1461 9 ▶ return 1 if !$limit_clause;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit_clause 0 9 9
1462 9 my $offset;
1463 my $limit;
1464 9 my $junk;
1465 9 ($offset, $limit, $junk ) = split /,|OFFSET/i, $limit_clause;
1466 9 ▶ if ($limit_clause =~ m/(\d+)\s+OFFSET\s+(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit_clause =~ /(\d+)\s+OFFSET\s+(\d+)/) { } 1 8 9
1467 1 $limit = $1;
1468 1 $offset = $2;
1469 } else {
1470 8 ( $offset, $limit, $junk ) = split /,/i, $limit_clause;
1471 }
1472 9 ▶ return $self->do_err('Bad limit clause!:'.$limit_clause)
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit and $limit =~ /[^\d]/ or defined $offset and $offset =~ /[^\d]/ or defined $junk 0 9 9
Condition41%5/12
defined $limit and $limit =~ /[^\d]/ or defined $offset and $offset =~ /[^\d]/ or defined $junk
l!l&&r!l&&!r
009
defined $limit and $limit =~ /[^\d]/ or defined $offset and $offset =~ /[^\d]/
l!l&&r!l&&!r
009
defined $limit and $limit =~ /[^\d]/
!ll&&!rl&&r
270
defined $offset and $offset =~ /[^\d]/
!ll&&!rl&&r
090
MC/DC0%0/5
defined $limit and $limit =~ /[^\d]/ or defined $offset and $offset =~ /[^\d]/ or defined $junk
defined $limit $limit =~ /[^\d]/ defined $offset $offset =~ /[^\d]/ defined $junk
Truth table18%2/11
A and B or C and D or E
A= defined $limitB= $limit =~ /[^\d]/C= defined $offsetD= $offset =~ /[^\d]/E= defined $junk
ABCDEresult
11XXX1
0X11X1
1011X1
0X0X11
0X1011
100X11
101011
0X0X00
0X1000
100X00
101000
1473 if ( defined $limit and $limit =~ /[^\d]/ )
1474 or ( defined $offset and $offset =~ /[^\d]/ )
1475 or defined $junk;
1476 9 ▶ if ( defined $offset and !defined $limit )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offset and not defined $limit) 2 7 9
Condition66%2/3
defined $offset and not defined $limit
!ll&&!rl&&r
072
MC/DC50%1/2
defined $offset and not defined $limit
defined $offset not defined $limit
Truth table66%2/3
A and not(B)
A= defined $offsetB= not defined $limit
ABresult
0X0
100
111
1477 {
1478 2 $limit = $offset;
1479 2 undef $offset;
1480 }
1481 $self->{struct}->{limit_clause} = {
1482 9 limit => $limit,
1483 offset => $offset,
1484 };
1485 9 return 1;
1486 }
1487  
1488 sub SORT_SPEC_LIST
1489 20 ▶{
Subroutine SORT_SPEC_LIST: called CC=7 SCAR=19.6
Pod undocumented
1490 20 my ( $self, $order_clause ) = @_;
1491 20 ▶ return 1 if !$order_clause;
Branch: 1 branch
BranchTrueFalseTotal
unless $order_clause 0 20 20
1492 20 my @ocols;
1493 20 my @order_columns = split ',', $order_clause;
1494 20 for my $col (@order_columns)
1495 {
1496 26 my $newcol;
1497 my $newarg;
1498 26 ▶ if ( $col =~ /\s*(\S+)\s+(ASC|DESC)/si )
Branch: 2 branches
BranchTrueFalseTotal
if ($col =~ /\s*(\S+)\s+(ASC|DESC)/is) { } 9 17 26
elsif ($col =~ /^\s*(\S+)\s*$/is) { } 17 0 17
1499 {
1500 9 $newcol = $1;
1501 9 $newarg = uc $2;
1502 }
1503 elsif ( $col =~ /^\s*(\S+)\s*$/si )
1504 {
1505 17 $newcol = $1;
1506 17 $newarg = 'ASC';
1507 }
1508 else
1509 {
1510 0 return $self->do_err('Junk after column name in ORDER BY clause!');
1511 }
1512 26 ▶ $newcol = $self->COLUMN_NAME($newcol) or return;
Branch: 1 branch
BranchTrueFalseTotal
$newcol = $self->COLUMN_NAME($newcol) or return 26 0 26
1513 26 ▶ if ( $newcol =~ /^(.+)\..+$/s )
Branch: 1 branch
BranchTrueFalseTotal
if ($newcol =~ /^(.+)\..+$/s) 3 23 26
1514 {
1515 3 my $table = $1;
1516 3 $self->_verify_tablename( $table, "ORDER BY" );
1517 }
1518 26 push( @ocols, { $newcol => $newarg } );
1519 }
1520 20 $self->{struct}->{sort_spec_list} = \@ocols;
1521 20 return 1;
1522 }
1523  
1524 sub SEARCH_CONDITION
1525 231 ▶{
Subroutine SEARCH_CONDITION: called CC=5 SCAR=16.3
Pod undocumented
1526 231 my ( $self, $str ) = @_;
1527 231 $str =~ s/^\s*WHERE (.+)/$1/;
1528 231 $str =~ s/^\s+//;
1529 231 $str =~ s/\s+$//;
1530 231 ▶ return $self->do_err("Couldn't find WHERE clause!") unless $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $str 0 231 231
1531  
1532 #
1533 # DAA
1534 # make these OO so subclasses can override them
1535 #
1536 231 $str = $self->repl_btwin($str);
1537  
1538 #
1539 # DAA
1540 # add another abstract method so subclasses
1541 # can inject their own syntax transforms
1542 #
1543 231 $str = $self->transform_syntax($str);
1544  
1545 231 my $open_parens = $str =~ tr/\(//;
1546 231 my $close_parens = $str =~ tr/\)//;
1547 231 ▶ if ( $open_parens != $close_parens )
Branch: 1 branch
BranchTrueFalseTotal
if ($open_parens != $close_parens) 0 231 231
1548 {
1549 0 return $self->do_err("Mismatched parentheses in WHERE clause!");
1550 }
1551 231 $str = nongroup_numeric( $self->nongroup_string($str) );
1552 231 ▶ my $pred =
Branch: 1 branch
BranchTrueFalseTotal
$open_parens ? : 135 96 231
1553 $open_parens
1554 ? $self->parens_search( $str, [] )
1555 : $self->non_parens_search( $str, [] );
1556 229 ▶ return $self->do_err("Couldn't find predicate!") unless $pred;
Branch: 1 branch
BranchTrueFalseTotal
unless $pred 0 229 229
1557 229 $self->{struct}->{where_clause} = $pred;
1558 229 return 1;
1559 }
1560  
1561 ############################################################
1562 # UTILITY FUNCTIONS CALLED TO PARSE PARENS IN WHERE CLAUSE
1563 ############################################################
1564  
1565 sub repl_btwin
1566 231 ▶{
Subroutine repl_btwin: called CC=6 SCAR=17.9
Pod documented
1567 231 my ( $self, $str ) = @_; # DAA make OO for subclassing
1568 231 my @lids;
1569  
1570 231 my $i = -1;
1571 231 ▶ while ( $str =~ m/\G.*(?:IN|BETWEEN)\s+\(/g )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G.*(?:IN|BETWEEN)\s+\(/g) 32 231 263
1572 {
1573 32 my $start = pos($str) - 1;
1574 32 my $lparens = 1;
1575 32 my $rparens = 0;
1576 32 ▶ while ( $str =~ m/\G.*?([\(\)])/gcs )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G.*?([\(\)])/cgs) 36 0 36
1577 {
1578 36 ▶ ++$lparens if ( '(' eq $1 );
Branch: 1 branch
BranchTrueFalseTotal
if "(" eq $1 2 34 36
1579 36 ▶ ++$rparens if ( ')' eq $1 );
Branch: 1 branch
BranchTrueFalseTotal
if ")" eq $1 34 2 36
1580 36 ▶ last if ( $lparens == $rparens );
Branch: 1 branch
BranchTrueFalseTotal
if $lparens == $rparens 32 4 36
1581 }
1582 32 my $now = pos($str);
1583 32 ++$i;
1584 32 my $subst = "?LI$i?";
1585 32 my $term = substr( $str, $start, $now - $start, $subst );
1586 32 $term = substr( $term, 1, length($term) - 2 );
1587 32 push( @lids, $term );
1588 32 pos($str) = $start + length($subst);
1589 }
1590  
1591 231 $self->{struct}->{list_ids} = \@lids;
1592 231 return $str;
1593 }
1594  
1595 # groups clauses by nested parens
1596 #
1597 # DAA
1598 # rewrite to correct paren scan
1599 # and optimize code, and remove
1600 # recursion
1601 #
1602 sub parens_search
1603 135 ▶{
Subroutine parens_search: called CC=7 SCAR=19.5
Pod documented
1604 135 my ( $self, $str, $predicates ) = @_;
1605 135 my $index = scalar( @{$predicates} );
1606  
1607 # to handle WHERE (a=b) AND (c=d)
1608 # but needs escape space to not foul up AND/OR
1609  
1610 # locate all open parens
1611 # locate all close parens
1612 # apply non_paren_search to contents of
1613 # inner parens
1614  
1615 135 my $lparens = ( $str =~ tr/\(// );
1616 135 my $rparens = ( $str =~ tr/\)// );
1617 135 ▶ return $self->do_err( 'Unmatched ' . ( ( $lparens > $rparens ) ? 'left' : 'right' ) . " parentheses in '$str'!" )
Branch: 2 branches
BranchTrueFalseTotal
unless $lparens == $rparens 0 135 135
$lparens > $rparens ? : 0 0 0
1618 unless ( $lparens == $rparens );
1619  
1620 135 ▶ return $self->non_parens_search( $str, $predicates )
Branch: 1 branch
BranchTrueFalseTotal
unless $lparens 127 8 135
1621 unless $lparens;
1622  
1623 8 my @lparens = ();
1624 8 ▶ while ( $str =~ m/\G.*?([\(\)])/gcs )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G.*?([\(\)])/cgs) 20 8 28
1625 {
1626 20 ▶ push( @lparens, $-[1] ), next
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "(" 10 10 20
1627 if ( $1 eq '(' );
1628  
1629 #
1630 # got a close paren, so pop the position of matching
1631 # left paren and extract the expression, removing the
1632 # parens
1633 #
1634 10 my $pos = pop @lparens;
1635 10 my $predlen = $+[1] - $pos;
1636 10 my $pred = substr( $str, $pos + 1, $predlen - 2 );
1637  
1638 #
1639 # note that this will pass thru any prior ^$index^ xlation,
1640 # so we don't need to recurse to recover the predicate
1641 #
1642 10 ▶ substr( $str, $pos, $predlen ) = $pred, pos($str) = $pos + length($pred), next
Branch: 1 branch
BranchTrueFalseTotal
unless $pred =~ / (AND|OR) /i 7 3 10
1643 unless ( $pred =~ / (AND|OR) /i );
1644  
1645 #
1646 # handle AND/OR
1647 #
1648 3 push( @$predicates, substr( $str, $pos + 1, $predlen - 2 ) );
1649 3 my $replacement = "^$#$predicates^";
1650 3 substr( $str, $pos, $predlen ) = $replacement;
1651 3 pos($str) = $pos + length($replacement);
1652 }
1653  
1654 8 return $self->non_parens_search( $str, $predicates );
1655 }
1656  
1657 # creates predicates from clauses that either have no parens
1658 # or ANDs or have been previously grouped by parens and ANDs
1659 #
1660 # DAA
1661 # rewrite to fix paren scanning
1662 #
1663 sub non_parens_search
1664 311 ▶{
Subroutine non_parens_search: called CC=12 SCAR=24.9
Pod documented
1665 311 my ( $self, $str, $predicates ) = @_;
1666 311 my $neg = 0;
1667 311 my $nots = {};
1668  
1669 311 ▶ $neg = 1, $nots = { pred => 1 }
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ s/^NOT (\^.+)$/$1/i 0 311 311
1670 if ( $str =~ s/^NOT (\^.+)$/$1/i );
1671  
1672 311 my ( $pred1, $pred2, $op );
1673 311 my $and_preds = [];
1674 311 ( $str, $and_preds ) = group_ands($str);
1675 311 ▶ $str = $and_preds->[$1]
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\s*~(\d+)~\s*$/ 0 311 311
1676 if $str =~ /^\s*~(\d+)~\s*$/;
1677  
1678 311 ▶ return $self->non_parens_search( $$predicates[$1], $predicates )
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\s*\^(\d+)\^\s*$/ 2 309 311
1679 if ( $str =~ /^\s*\^(\d+)\^\s*$/ );
1680  
1681 309 ▶ if ( $str =~ /\G(.*?)\s+(AND|OR)\s+(.*)$/igcs )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G(.*?)\s+(AND|OR)\s+(.*)$/cgis) 39 270 309
1682 {
1683 39 ( $pred1, $op, $pred2 ) = ( $1, $2, $3 );
1684  
1685 39 ▶ if ( $pred1 =~ /^\s*\^(\d+)\^\s*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($pred1 =~ /^\s*\^(\d+)\^\s*$/) { } 1 38 39
1686 {
1687 1 $pred1 = $self->non_parens_search( $$predicates[$1], $predicates );
1688 }
1689 else
1690 {
1691 38 $pred1 =~ s/\~(\d+)\~$/$and_preds->[$1]/g;
1692 38 $pred1 = $self->non_parens_search( $pred1, $predicates );
1693 }
1694  
1695 #
1696 # handle pred2 as a full predicate
1697 #
1698 39 $pred2 =~ s/\~(\d+)\~$/$and_preds->[$1]/g;
1699 39 $pred2 = $self->non_parens_search( $pred2, $predicates );
1700  
1701 return {
1702 39 neg => $neg,
1703 nots => $nots,
1704 arg1 => $pred1,
1705 op => uc $op,
1706 arg2 => $pred2,
1707 };
1708 }
1709  
1710 #
1711 # terminal predicate
1712 # need to check for singleton functions here
1713 #
1714 270 my $xstr = $str;
1715 270 my ( $k, $v );
1716 270 ▶ if ( $str =~ /^\s*([A-Z]\p{Word}*)\s*\[/gcs )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\s*([A-Z]\p{Word}*)\s*\[/cgs) { } 120 150 270
1717 {
1718  
1719 #
1720 # we've got a function, check if its a singleton
1721 #
1722 120 my $parens = 1;
1723 120 my $spos = $-[1];
1724 120 my $epos = 0;
1725 120 ▶ $epos = $-[1], $parens += ( $1 eq '[' ) ? 1 : -1 while ( ( $parens > 0 ) && ( $str =~ /\G.*?([\[\]])/gcs ) );
Branch: 2 branches
BranchTrueFalseTotal
$parens > 0 and $str =~ /\G.*?([\[\]])/cgs and do { $epos = $-[1], $parens += $1 eq "[" ? 1 : -1 } 126 120 246
$1 eq '[' ? : 3 123 126
Condition66%2/3
$parens > 0 and $str =~ /\G.*?([\[\]])/cgs
!ll&&!rl&&r
1200126
MC/DC50%1/2
$parens > 0 and $str =~ /\G.*?([\[\]])/cgs
$parens > 0 $str =~ /\G.*?([\[\]])/cgs
Truth table66%2/3
A and B
A= $parens > 0B= $str =~ /\G.*?([\[\]])/cgs
ABresult
0X0
100
111
1726 120 $k = substr( $str, $spos, $epos - $spos + 1 );
1727 120 $k =~ s/\?(\d+)\?/$self->{struct}{literals}[$1]/g;
1728  
1729 #
1730 # for now we assume our parens are balanced
1731 # now look for a predicate operator and a right operand
1732 #
1733 120 ▶ $v = $1, $v =~ s/\?(\d+)\?/$self->{struct}{literals}[$1]/g
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /\G\s+\S+\s*(.+)\s*$/cgs 66 54 120
1734 if ( $str =~ /\G\s+\S+\s*(.+)\s*$/gcs );
1735 }
1736 else
1737 {
1738 150 $xstr =~ s/\?(\d+)\?/$self->{struct}{literals}[$1]/g;
1739 150 ( $k, $v ) = $xstr =~ /^(\S+?)\s+\S+\s*(.+)\s*$/;
1740 }
1741 270 ▶ push @{ $self->{struct}{where_cols}{$k} }, $v
Branch: 1 branch
BranchTrueFalseTotal
if defined $k 270 0 270
1742 if defined $k;
1743 270 return $self->PREDICATE($str);
1744 }
1745  
1746 # groups AND clauses that aren't already grouped by parens
1747 #
1748 sub group_ands
1749 315 ▶{
Subroutine group_ands: called CC=7 SCAR=19.6
Pod documented
1750 315 my $str = shift;
1751 315 ▶ my $and_preds = shift || [];
Condition100%2/2
shift() || []
l!l
4311
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1752 315 ▶ return ( $str, $and_preds )
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ / AND / and $str =~ / OR / 311 4 315
Condition100%3/3
$str =~ / AND / and $str =~ / OR /
!ll&&!rl&&r
283284
MC/DC100%2/2
$str =~ / AND / and $str =~ / OR /
$str =~ / AND / $str =~ / OR /
Truth table100%3/3
A and B
A= $str =~ / AND /B= $str =~ / OR /
ABresult
0X0
100
111
1753 unless $str =~ / AND / and $str =~ / OR /;
1754  
1755 4 ▶ return $str, $and_preds
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /^(.*?) AND (.*)$/i 0 4 4
1756 unless ( $str =~ /^(.*?) AND (.*)$/i );
1757  
1758 4 my ( $front, $back ) = ( $1, $2 );
1759 4 my $index = scalar @$and_preds;
1760 4 ▶ $front = $1
Branch: 1 branch
BranchTrueFalseTotal
if $front =~ /^.* OR (.*)$/i 0 4 4
1761 if ( $front =~ /^.* OR (.*)$/i );
1762  
1763 4 ▶ $back = $1
Branch: 1 branch
BranchTrueFalseTotal
if $back =~ /^(.*?) (OR|AND) .*$/i 4 0 4
1764 if ( $back =~ /^(.*?) (OR|AND) .*$/i );
1765  
1766 4 my $newpred = "$front AND $back";
1767 4 push @$and_preds, $newpred;
1768 4 $str =~ s/\Q$newpred/~$index~/i;
1769 4 return group_ands( $str, $and_preds );
1770 }
1771  
1772 # replaces string function parens with square brackets
1773 # e.g TRIM (foo) -> TRIM[foo]
1774 #
1775 # DAA update to support UDFs
1776 # and remove recursion
1777 #
1778 sub nongroup_string
1779 231 ▶{
Subroutine nongroup_string: called CC=6 SCAR=17.9
Pod documented
1780 231 my ( $self, $str ) = @_;
1781  
1782 #
1783 # add in any user defined functions
1784 #
1785 231 my $f = join( '|', FUNCTION_NAMES, $self->_udf_function_names );
1786  
1787 #
1788 # we need a scan here to permit arbitrarily nested paren
1789 # arguments to functions
1790 #
1791 231 my $parens = 0;
1792 231 my $pos;
1793 231 my @lparens = ();
1794 231 ▶ while ( $str =~ /\G.*?((\b($f)\s*\()|[\(\)])/igcs )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G.*?((\b($f)\s*\()|[\(\)])/cgis) 298 231 529
1795 {
1796 298 ▶ if ( $1 eq ')' )
Branch: 2 branches
BranchTrueFalseTotal
if ($1 eq ')') { } 149 149 298
elsif ($1 eq '(') { } 15 134 149
1797 {
1798 #
1799 # close paren, see if any pending function open
1800 # paren matches it
1801 #
1802 149 --$parens;
1803 149 ▶ $pos = $+[0], substr( $str, $+[0] - 1, 1 ) = ']', pos($str) = $pos, pop(@lparens)
Branch: 1 branch
BranchTrueFalseTotal
if @lparens and $lparens[-1] == $parens 134 15 149
Condition66%2/3
@lparens and $lparens[-1] == $parens
!ll&&!rl&&r
150134
MC/DC50%1/2
@lparens and $lparens[-1] == $parens
@lparens $lparens[-1] == $parens
Truth table66%2/3
A and B
A= @lparensB= $lparens[-1] == $parens
ABresult
0X0
100
111
1804 if ( @lparens && ( $lparens[-1] == $parens ) );
1805 }
1806 elsif ( $1 eq '(' )
1807 {
1808  
1809 #
1810 # just an open paren, count it and go on
1811 #
1812 15 ++$parens;
1813 }
1814 else
1815 {
1816  
1817 #
1818 # new function definition, capture its open paren
1819 # also uppercase the function name
1820 #
1821 134 $pos = $+[0];
1822 134 substr( $str, $-[3], length($3) ) = uc $3;
1823 134 substr( $str, $+[0] - 1, 1 ) = '[';
1824 134 pos($str) = $pos;
1825 134 push @lparens, $parens;
1826 134 ++$parens;
1827 }
1828 }
1829  
1830 # return $self->do_err('Unmatched ' .
1831 # (($parens > 0) ? 'left' : 'right') . ' parentheses!')
1832 # if $parens;
1833 #
1834 # DAA
1835 # remove scoped recursion
1836 #
1837 # return ( $str =~ /($f)\s*\(/i ) ?
1838 # nongroup_string($str) : $str;
1839 231 return $str;
1840 }
1841  
1842 # replaces math parens with square brackets
1843 # e.g (4-(6+7)*9) -> MATH[4-MATH[6+7]*9]
1844 #
1845 sub nongroup_numeric
1846 233 ▶{
Subroutine nongroup_numeric: called CC=5 SCAR=16.1
Pod documented
1847 233 my $str = $_[0];
1848 233 my $has_op;
1849  
1850 #
1851 # DAA
1852 # optimize regex
1853 #
1854 233 ▶ if ( $str =~ m/\(([\p{Word} \*\/\+\-\[\]\?]+)\)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ m<\(([\p{Word} \*/\+\-\[\]\?]+)\)>) 7 226 233
1855 {
1856 7 my $match = $1;
1857 7 ▶ if ( $match !~ m/(LIKE |IS|BETWEEN|IN)/i )
Branch: 1 branch
BranchTrueFalseTotal
if (not $match =~ /(LIKE |IS|BETWEEN|IN)/i) { } 5 2 7
1858 {
1859 5 my $re = quotemeta($match);
1860 5 $str =~ s/\($re\)/MATH\[$match\]/;
1861 }
1862 else
1863 {
1864 2 $has_op++;
1865 }
1866 }
1867  
1868 #
1869 # DAA
1870 # remove scoped recursion
1871 #
1872 233 ▶ return ( !$has_op and $str =~ /\(([\p{Word} \*\/\+\-\[\]\?]+)\)/ )
Branch: 1 branch
BranchTrueFalseTotal
!$has_op && $str =~ m<\(([\p{Word} \*/\+\-\[\]\?]+)\)> ? : 2 231 233
Condition100%3/3
not $has_op and $str =~ m<\(([\p{Word} \*/\+\-\[\]\?]+)\)>
!ll&&!rl&&r
22292
MC/DC100%2/2
not $has_op and $str =~ m<\(([\p{Word} \*/\+\-\[\]\?]+)\)>
not $has_op $str =~ m<\(([\p{Word} \*/\+\-\[\]\?]+)\)>
Truth table100%3/3
not(A) and B
A= not $has_opB= $str =~ m<\(([\p{Word} \*/\+\-\[\]\?]+)\)>
ABresult
0X0
100
111
1873 ? nongroup_numeric($str)
1874 : $str;
1875 }
1876 ############################################################
1877  
1878 #########################################################
1879 # LITERAL_LIST ::= <literal> [,<literal>]
1880 #########################################################
1881 sub LITERAL_LIST
1882 192 ▶{
Subroutine LITERAL_LIST: called CC=3 SCAR=11.0
Pod undocumented
1883 192 my ( $self, $str ) = @_;
1884 my @tokens = extract_multiple($str, [
1885 1036 ▶ sub { my ($m, $r, $p) = extract_bracketed($_[0], "()", qr/[^,(]*/); (($p||'').($m||''), $r, ''); },
Subroutine __ANON__: called CC=3 SCAR=11.0
Condition100%4/4
$p || ''
l!l
101026
$m || ''
l!l
101026
MC/DC100%1/1
$p || ''
$p
MC/DC100%1/1
$m || ''
$m
Truth table100%2/2
A
A= $p
Aresult
11
00
Truth table100%2/2
A
A= $m
Aresult
11
00
1886 192 qr/([^,(]+)/,
1887 ], undef, 1);
1888 192 my @values;
1889 192 for my $tok (@tokens)
1890 {
1891 614 my $val = $self->ROW_VALUE($tok);
1892 614 ▶ return $self->do_err(qq('$tok' is not a valid value or is not quoted!))
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 614 614
1893 unless $val;
1894 614 push @values, $val;
1895 }
1896 192 push( @{ $self->{struct}->{values} }, \@values );
1897 192 return 1;
1898 }
1899  
1900 #############################################################################
1901 # LITERAL ::= <quoted_string> | <question mark> | <number> | NULL/TRUE/FALSE
1902 #############################################################################
1903 sub LITERAL
1904 1899 ▶{
Subroutine LITERAL: called CC=7 SCAR=19.5
Pod undocumented
1905 1899 my ( $self, $str ) = @_;
1906  
1907 #
1908 # DAA
1909 # strip parens (if any)
1910 #
1911 1899 ▶ $str = $1 while ( $str =~ m/^\s*\(\s*(.+)\s*\)\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /^\s*\(\s*(.+)\s*\)\s*$/ and do { $str = $1 } 0 1899 1899
1912  
1913 1899 ▶ return 'null' if $str =~ m/^NULL$/i; # NULL
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^NULL$/i 27 1872 1899
1914 1872 ▶ return 'boolean' if $str =~ m/^(?:TRUE|FALSE)$/i; # TRUE/FALSE
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^(?:TRUE|FALSE)$/i 13 1859 1872
1915  
1916 # return 'empty_string' if $str =~ /^~E~$/i; # NULL
1917 1859 ▶ if ( $str eq '?' )
Branch: 1 branch
BranchTrueFalseTotal
if ($str eq "?") 36 1823 1859
1918 {
1919 36 $self->{struct}->{num_placeholders}++;
1920 36 return 'placeholder';
1921 }
1922  
1923 # return 'placeholder' if $str eq '?'; # placeholder question mark
1924 1823 ▶ return 'string' if $str =~ m/^'.*'$/s; # quoted string
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^'.*'$/s 1 1822 1823
1925 # return 'number' if $str =~ m/^[+-]?(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/; # number
1926 1822 ▶ return 'number' if ( looks_like_number($str) ); # number
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($str) 696 1126 1822
1927  
1928 1126 return undef;
1929 }
1930 ###################################################################
1931 # PREDICATE
1932 ###################################################################
1933 sub PREDICATE
1934 270 ▶{
Subroutine PREDICATE: called CC=24 SCAR=32.4
Pod undocumented
1935 270 my ( $self, $str ) = @_;
1936  
1937 270 my ( $arg1, $op, $arg2, $opexp );
1938  
1939 $opexp = $self->{opts}{valid_comparison_NOT_ops_regex}, ( $arg1, $op, $arg2 ) = $str =~ /$opexp/i
1940 270 ▶ if $self->{opts}{valid_comparison_NOT_ops_regex};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"valid_comparison_NOT_ops_regex"} 270 0 270
1941  
1942 $opexp = $self->{opts}{valid_comparison_twochar_ops_regex}, ( $arg1, $op, $arg2 ) = $str =~ /$opexp/i
1943 if ( !defined($op)
1944 270 ▶ && $self->{opts}{valid_comparison_twochar_ops_regex} );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $op and $self->{"opts"}{"valid_comparison_twochar_ops_regex"} 260 10 270
Condition66%2/3
not defined $op and $self->{"opts"}{"valid_comparison_twochar_ops_regex"}
!ll&&!rl&&r
100260
MC/DC50%1/2
not defined $op and $self->{"opts"}{"valid_comparison_twochar_ops_regex"}
not defined $op $self->{"opts"}{"valid_comparison_twochar_ops_regex"}
Truth table66%2/3
not(A) and B
A= not defined $opB= $self->{"opts"}{"valid_comparison_twochar_ops_regex"}
ABresult
0X0
100
111
1945  
1946 $opexp = $self->{opts}{valid_comparison_ops_regex}, ( $arg1, $op, $arg2 ) = $str =~ /$opexp/i
1947 270 ▶ if ( !defined($op) && $self->{opts}{valid_comparison_ops_regex} );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $op and $self->{"opts"}{"valid_comparison_ops_regex"} 254 16 270
Condition66%2/3
not defined $op and $self->{"opts"}{"valid_comparison_ops_regex"}
!ll&&!rl&&r
160254
MC/DC50%1/2
not defined $op and $self->{"opts"}{"valid_comparison_ops_regex"}
not defined $op $self->{"opts"}{"valid_comparison_ops_regex"}
Truth table66%2/3
not(A) and B
A= not defined $opB= $self->{"opts"}{"valid_comparison_ops_regex"}
ABresult
0X0
100
111
1948  
1949 #
1950 ### USER-DEFINED PREDICATE
1951 #
1952 269 ▶ unless ( defined $arg1 && defined $op && defined $arg2 )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg1 and defined $op and defined $arg2) 57 212 269
Condition66%4/6
defined $arg1 and defined $op and defined $arg2
!ll&&!rl&&r
570212
defined $arg1 and defined $op
!ll&&!rl&&r
570212
MC/DC33%1/3
defined $arg1 and defined $op and defined $arg2
defined $arg1 defined $op defined $arg2
Truth table50%2/4
A and B and C
A= defined $arg1B= defined $opC= defined $arg2
ABCresult
0XX0
10X0
1100
1111
1953 {
1954 57 $arg1 = $str;
1955 57 $op = 'USER_DEFINED';
1956 57 $arg2 = '';
1957 }
1958  
1959 269 $op = uc $op;
1960  
1961 # my $uname = $self->is_func($arg1);
1962 # if (!$uname) {
1963 # $arg1 =~ s/^(\S+).*$/$1/;
1964 # return $self->do_err("Bad predicate: '$arg1'!");
1965 # }
1966  
1967 269 my $negated = 0; # boolean value showing if predicate is negated
1968 269 my %not; # hash showing elements modified by NOT
1969 #
1970 # e.g. "NOT bar = foo" -> %not = (arg1=>1)
1971 # "bar NOT LIKE foo" -> %not = (op=>1)
1972 # "NOT bar NOT LIKE foo" -> %not = (arg1=>1,op=>1);
1973 # "NOT bar IS NOT NULL" -> %not = (arg1=>1,op=>1);
1974 # "bar = foo" -> %not = undef;
1975 #
1976 269 ▶ $not{arg1}++
Branch: 1 branch
BranchTrueFalseTotal
if $arg1 =~ s/^NOT (.+)$/$1/i 15 254 269
1977 if ( $arg1 =~ s/^NOT (.+)$/$1/i );
1978  
1979 269 ▶ $not{op}++
Branch: 1 branch
BranchTrueFalseTotal
if $op =~ s/^(.+) NOT$/$1/i or $op =~ s/^NOT (.+)$/$1/i 10 259 269
Condition100%3/3
$op =~ s/^(.+) NOT$/$1/i or $op =~ s/^NOT (.+)$/$1/i
l!l&&r!l&&!r
19259
MC/DC100%2/2
$op =~ s/^(.+) NOT$/$1/i or $op =~ s/^NOT (.+)$/$1/i
$op =~ s/^(.+) NOT$/$1/i $op =~ s/^NOT (.+)$/$1/i
Truth table100%3/3
A or B
A= $op =~ s/^(.+) NOT$/$1/iB= $op =~ s/^NOT (.+)$/$1/i
ABresult
1X1
011
000
1980 if ( $op =~ s/^(.+) NOT$/$1/i
1981 || $op =~ s/^NOT (.+)$/$1/i );
1982  
1983 269 ▶ $negated = 1 if %not and scalar keys %not == 1;
Branch: 1 branch
BranchTrueFalseTotal
if %not and scalar keys %not == 1 25 244 269
Condition66%2/3
%not and scalar keys %not == 1
!ll&&!rl&&r
244025
MC/DC50%1/2
%not and scalar keys %not == 1
%not scalar keys %not == 1
Truth table66%2/3
A and B
A= %notB= scalar keys %not == 1
ABresult
0X0
100
111
1984  
1985 269 ▶ return undef unless $arg1 = $self->ROW_VALUE($arg1);
Branch: 1 branch
BranchTrueFalseTotal
unless $arg1 = $self->ROW_VALUE($arg1) 0 268 268
1986  
1987 268 ▶ if ( $op ne 'USER_DEFINED' )
Branch: 1 branch
BranchTrueFalseTotal
if ($op ne 'USER_DEFINED') { } 212 56 268
1988 { # USER-PREDICATE;
1989 212 ▶ return undef unless $arg2 = $self->ROW_VALUE($arg2);
Branch: 1 branch
BranchTrueFalseTotal
unless $arg2 = $self->ROW_VALUE($arg2) 0 212 212
1990 }
1991 else
1992 {
1993  
1994 # $arg2 = $self->ROW_VALUE($arg2);
1995 }
1996  
1997 268 ▶ if ( defined( _HASH($arg1) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined &_HASH($arg1) and defined &_HASH($arg2) and ($arg1->{"type"} || "") eq "column" and ($arg2->{"type"} || "") eq "column" and $op eq "=") 27 241 268
Condition81%13/16
defined &_HASH($arg1) and defined &_HASH($arg2) and ($arg1->{"type"} || "") eq "column" and ($arg2->{"type"} || "") eq "column" and $op eq "="
!ll&&!rl&&r
233827
defined &_HASH($arg1) and defined &_HASH($arg2) and ($arg1->{"type"} || "") eq "column" and ($arg2->{"type"} || "") eq "column"
!ll&&!rl&&r
1656835
defined &_HASH($arg1) and defined &_HASH($arg2) and ($arg1->{"type"} || "") eq "column"
!ll&&!rl&&r
8679103
defined &_HASH($arg1) and defined &_HASH($arg2)
!ll&&!rl&&r
086182
$arg1->{'type'} || ''
l!l
1820
$arg2->{'type'} || ''
l!l
1030
MC/DC40%2/5
defined &_HASH($arg1) and defined &_HASH($arg2) and ($arg1->{"type"} || "") eq "column" and ($arg2->{"type"} || "") eq "column" and $op eq "="
defined &_HASH($arg1) defined &_HASH($arg2) $arg1->{'type'} $arg2->{'type'} $op eq "="
Truth table50%3/6
A and B and C and D and E
A= defined &_HASH($arg1)B= defined &_HASH($arg2)C= $arg1->{'type'}D= $arg2->{'type'}E= $op eq "="
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
1998 and defined( _HASH($arg2) )
1999 and ( ( $arg1->{type} || '' ) eq 'column' )
2000 and ( ( $arg2->{type} || '' ) eq 'column' )
2001 and ( $op eq '=' ) )
2002 {
2003 27 push( @{ $self->{struct}->{keycols} }, $arg1->{value} );
2004 27 push( @{ $self->{struct}->{keycols} }, $arg2->{value} );
2005 }
2006  
2007 return {
2008 268 neg => $negated,
2009 nots => \%not,
2010 arg1 => $arg1,
2011 op => $op,
2012 arg2 => $arg2,
2013 };
2014 }
2015  
2016 sub _udf_function_names
2017 2540 ▶{
Subroutine _udf_function_names: called CC=2 SCAR=6.9
2018 $_[0]->{opts}->{_udf_function_names}
2019 2540 ▶ or return $_[0]->{opts}->{_udf_function_names} = join( "|", map { uc $_ } keys %{ $_[0]->{opts}->{function_names} } );
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"opts"}{"_udf_function_names"} or return $_[0]{"opts"}{"_udf_function_names"} = join("|", map({uc $_;} keys %{$_[0]{"opts"}{"function_names"};})) 2522 18 2540
2020 2522 $_[0]->{opts}->{_udf_function_names};
2021 }
2022  
2023 sub undo_string_funcs
2024 2309 ▶{
Subroutine undo_string_funcs: called CC=6 SCAR=17.9
Pod documented
2025 2309 my ( $self, $str ) = @_;
2026 2309 my $f = join( '|', FUNCTION_NAMES, $self->_udf_function_names );
2027  
2028 # eliminate recursion:
2029 # we have to scan for closing brackets, since we may
2030 # have intervening MATH elements with brackets
2031 2309 my ( $brackets, $pos, @lbrackets ) = (0);
2032 2309 ▶ while ( $str =~ /\G.*?((\b($f)\s*\[)|[\[\]])/igcs )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G.*?((\b($f)\s*\[)|[\[\]])/cgis) 278 2309 2587
2033 {
2034 278 ▶ if ( $1 eq ']' )
Branch: 2 branches
BranchTrueFalseTotal
if ($1 eq ']') { } 139 139 278
elsif ($1 eq '[') { } 5 134 139
2035 {
2036 # close paren, see if any pending function open
2037 # paren matches it
2038 139 $brackets--;
2039 139 ▶ $pos = $+[0], substr( $str, $+[0] - 1, 1 ) = ')', pos($str) = $pos, pop @lbrackets
Branch: 1 branch
BranchTrueFalseTotal
if @lbrackets and $lbrackets[-1] == $brackets 134 5 139
Condition66%2/3
@lbrackets and $lbrackets[-1] == $brackets
!ll&&!rl&&r
50134
MC/DC50%1/2
@lbrackets and $lbrackets[-1] == $brackets
@lbrackets $lbrackets[-1] == $brackets
Truth table66%2/3
A and B
A= @lbracketsB= $lbrackets[-1] == $brackets
ABresult
0X0
100
111
2040 if ( @lbrackets && ( $lbrackets[-1] == $brackets ) );
2041 }
2042 elsif ( $1 eq '[' )
2043 {
2044 # just an open paren, count it and go on
2045 5 $brackets++;
2046 }
2047 else
2048 {
2049 # new function definition, capture its open paren
2050 # also uppercase the function name
2051 134 $pos = $+[0];
2052 134 substr( $str, $-[3], length($3) ) = uc $3;
2053 134 substr( $str, $+[0] - 1, 1 ) = '(';
2054 134 pos($str) = $pos;
2055 134 push @lbrackets, $brackets;
2056 134 $brackets++;
2057 }
2058 }
2059  
2060 2309 return $str;
2061 }
2062  
2063 sub undo_math_funcs
2064 2309 ▶{
Subroutine undo_math_funcs: called CC=2 SCAR=6.9
Pod documented
2065 2309 my $str = $_[0];
2066  
2067 # eliminate recursion
2068 2309 ▶ while ( $str =~ s/MATH\[([^\]\[]+?)\]/($1)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/MATH\[([^\]\[]+?)\]/($1)/) 5 2309 2314
2069 {
2070 }
2071  
2072 2309 return $str;
2073 }
2074  
2075 #
2076 # DAA
2077 # need better nested function/parens handling
2078 #
2079 sub extract_func_args
2080 346 ▶{
Subroutine extract_func_args: called CC=7 SCAR=19.5
Pod documented
2081 346 my ( $self, $value ) = @_;
2082  
2083 346 my @final_args = ();
2084 346 my ( $spos, $parens, $epos, $delim ) = ( 0, 0, 0, 0 );
2085 346 ▶ while ( $value =~ m/\G.*?([\(\),])/gcs )
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\G.*?([\(\),])/cgs) 201 346 547
2086 {
2087 201 $epos = $+[0];
2088 201 $delim = $1;
2089 201 ▶ unless ( $parens or ( $delim ne ',' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($parens or $delim ne ",") 149 52 201
Condition100%3/3
$parens or $delim ne ","
l!l&&r!l&&!r
3319149
MC/DC100%2/2
$parens or $delim ne ","
$parens $delim ne ","
Truth table100%3/3
A or B
A= $parensB= $delim ne ","
ABresult
1X1
011
000
2090 {
2091 149 push( @final_args, $self->ROW_VALUE( substr( $value, $spos, $epos - $spos - 1 ) ) );
2092 149 $spos = $epos;
2093 149 next;
2094 }
2095  
2096 52 ▶ unless ( $delim eq ',' )
Branch: 1 branch
BranchTrueFalseTotal
unless ($delim eq ",") 38 14 52
2097 {
2098 38 ▶ $parens += ( $delim eq '(' ) ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$delim eq '(' ? : 19 19 38
2099 }
2100 }
2101  
2102 # don't forget the last argument
2103 346 ▶ if ( $spos != length($value) )
Branch: 1 branch
BranchTrueFalseTotal
if ($spos != length $value) 312 34 346
2104 {
2105 312 $epos = length($value);
2106 312 push( @final_args, $self->ROW_VALUE( substr( $value, $spos, $epos - $spos ) ) ); # XXX
2107 }
2108  
2109 346 return @final_args;
2110 }
2111  
2112 ###################################################################
2113 # ROW_VALUE ::= <literal> | <column_name>
2114 ###################################################################
2115 sub ROW_VALUE
2116 2309 ▶{
Subroutine ROW_VALUE: called CC=58 SCAR=41.7
Pod undocumented
2117 2309 my ( $self, $str ) = @_;
2118  
2119 2309 $str =~ s/^\s+//;
2120 2309 $str =~ s/\s+$//;
2121 2309 $str = $self->undo_string_funcs($str);
2122 2309 $str = undo_math_funcs($str);
2123 2309 my ( $orgstr, $f, $bf ) = ( $str, FUNCTION_NAMES, BAREWORD_FUNCTIONS );
2124  
2125 # USER-DEFINED FUNCTION
2126 2309 my ( $user_func_name, $user_func_args, $is_func );
2127  
2128 # DAA
2129 # need better paren check here
2130 2309 ▶ if ( $str =~ m/^([^\s\(]+)\s*(.*)\s*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^([^\s\(]+)\s*(.*)\s*$/) { } 2303 6 2309
2131 {
2132 2303 $user_func_name = $1;
2133 2303 $user_func_args = $2;
2134  
2135 # convert operator-like function to parenthetical format
2136 2303 ▶ if ( ( $is_func = $self->is_func($user_func_name) )
Branch: 1 branch
BranchTrueFalseTotal
if ($is_func = $self->is_func($user_func_name) and not $user_func_args =~ /^\(.*\)$/ and $is_func =~ /^(?:$bf)$/i) 8 2295 2303
Condition100%6/6
$is_func = $self->is_func($user_func_name) and not $user_func_args =~ /^\(.*\)$/ and $is_func =~ /^(?:$bf)$/i
!ll&&!rl&&r
229058
$is_func = $self->is_func($user_func_name) and not $user_func_args =~ /^\(.*\)$/
!ll&&!rl&&r
195233813
MC/DC100%3/3
$is_func = $self->is_func($user_func_name) and not $user_func_args =~ /^\(.*\)$/ and $is_func =~ /^(?:$bf)$/i
$is_func = $self->is_func($user_func_name) not $user_func_args =~ /^\(.*\)$/ $is_func =~ /^(?:$bf)$/i
Truth table100%4/4
A and not(B) and C
A= $is_func = $self->is_func($user_func_name)B= not $user_func_args =~ /^\(.*\)$/C= $is_func =~ /^(?:$bf)$/i
ABCresult
0XX0
10X0
1100
1111
2137 && ( $user_func_args !~ m/^\(.*\)$/ )
2138 && ( $is_func =~ /^(?:$bf)$/i ) )
2139 {
2140 8 $orgstr = $str = "$user_func_name ($user_func_args)";
2141 }
2142 }
2143 else
2144 {
2145 6 $user_func_name = $str;
2146 6 $user_func_name =~ s/^(\S+).*$/$1/;
2147 6 $user_func_args = '';
2148 6 $is_func = $self->is_func($user_func_name);
2149 }
2150  
2151 # BLKB
2152 # Limiting the parens convert shortcut, so that "SELECT LOG(1), PI" works as a
2153 # two functions, and "SELECT x FROM log" works as a table
2154 2309 ▶ undef $is_func if ( $is_func && $is_func !~ /^(?:$bf)$/i && $str !~ m/^\S+\s*\(.*\)\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $is_func and not $is_func =~ /^(?:$bf)$/i and not $str =~ /^\S+\s*\(.*\)\s*$/ 5 2304 2309
Condition100%6/6
$is_func and not $is_func =~ /^(?:$bf)$/i and not $str =~ /^\S+\s*\(.*\)\s*$/
!ll&&!rl&&r
19943105
$is_func and not $is_func =~ /^(?:$bf)$/i
!ll&&!rl&&r
195836315
MC/DC100%3/3
$is_func and not $is_func =~ /^(?:$bf)$/i and not $str =~ /^\S+\s*\(.*\)\s*$/
$is_func not $is_func =~ /^(?:$bf)$/i not $str =~ /^\S+\s*\(.*\)\s*$/
Truth table100%4/4
A and not(B) and not(C)
A= $is_funcB= not $is_func =~ /^(?:$bf)$/iC= not $str =~ /^\S+\s*\(.*\)\s*$/
ABCresult
0XX0
10X0
1100
1111
2155  
2156 2309 ▶ if ( $is_func && ( uc($is_func) !~ m/^($f)$/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($is_func and not uc($is_func) =~ /^($f)$/) 346 1963 2309
Condition66%2/3
$is_func and not uc($is_func) =~ /^($f)$/
!ll&&!rl&&r
19630346
MC/DC50%1/2
$is_func and not uc($is_func) =~ /^($f)$/
$is_func not uc($is_func) =~ /^($f)$/
Truth table66%2/3
A and not(B)
A= $is_funcB= not uc($is_func) =~ /^($f)$/
ABresult
0X0
100
111
2157 {
2158 346 my ( $name, $value ) = ( $user_func_name, '' );
2159 346 ▶ if ( $str =~ m/^(\S+)\s*\((.*)\)\s*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(\S+)\s*\((.*)\)\s*$/) 346 0 346
2160 {
2161 346 $name = $1;
2162 346 $value = $2;
2163 346 $is_func = $self->is_func($name);
2164 }
2165  
2166 346 ▶ if ($is_func)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_func) 346 0 346
2167 {
2168 #
2169 # DAA
2170 # need a better argument extractor, since it can
2171 # contain arbitrary (possibly parenthesized)
2172 # expressions/functions
2173 #
2174 # if ($value =~ /\(/ ) {
2175 # $value = $self->ROW_VALUE($value);
2176 # }
2177 # my @args = split ',',$value;
2178  
2179 346 my @final_args = $self->extract_func_args($value);
2180 346 my $usr_sub = $self->{opts}->{function_names}->{$is_func};
2181 346 $self->{struct}->{procedure} = {};
2182 346 ▶ if ($usr_sub)
Branch: 1 branch
BranchTrueFalseTotal
if ($usr_sub) 346 0 346
2183 {
2184 346 $value = {
2185 type => 'function',
2186 name => lc $name,
2187 subname => $usr_sub,
2188 value => \@final_args,
2189 fullorg => $orgstr,
2190 };
2191  
2192 346 return $value;
2193 }
2194 }
2195 }
2196  
2197 1963 my $type;
2198 # MATH
2199 #
2200 1963 ▶ if ( $str =~ m/[\*\+\-\/\%]/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ m[[\*\+\-/\%]]) 45 1918 1963
2201 {
2202 45 my @vals;
2203 45 my $i = -1;
2204 45 my $open_parens = $str =~ tr/\(//;
2205 45 my $close_parens = $str =~ tr/\)//;
2206 45 ▶ if ( $open_parens != $close_parens )
Branch: 1 branch
BranchTrueFalseTotal
if ($open_parens != $close_parens) 0 45 45
2207 {
2208 0 return $self->do_err("Mismatched parentheses in term '$str'!");
2209 }
2210  
2211 # $str =~ s/([^\s\*\+\-\/\%\)\(]+)/push @vals,$1;++$i;"?$i?"/ge;
2212 45 ▶ while ( $str =~ m/\G.*?([^\s\*\+\-\/\%\)\(]+)/g )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ m[\G.*?([^\s\*\+\-/\%\)\(]+)]g) 74 45 119
2213 {
2214 74 my $term = $1;
2215 74 my $start = pos($str) - length($term);
2216 74 ▶ if ( $self->is_func($term) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_func($term)) { } 5 69 74
2217 {
2218 5 my $lparens = 0;
2219 5 my $rparens = 0;
2220 5 ▶ while ( $str =~ m/\G.*?([\(\)])/gcs )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G.*?([\(\)])/cgs) 4 4 8
2221 {
2222 4 ▶ ++$lparens if ( '(' eq $1 );
Branch: 1 branch
BranchTrueFalseTotal
if "(" eq $1 3 1 4
2223 4 ▶ ++$rparens if ( ')' eq $1 );
Branch: 1 branch
BranchTrueFalseTotal
if ")" eq $1 1 3 4
2224 4 ▶ last if ( $lparens == $rparens );
Branch: 1 branch
BranchTrueFalseTotal
if $lparens == $rparens 1 3 4
2225 }
2226 5 my $now = pos($str);
2227 5 ++$i;
2228 5 $term = substr( $str, $start, $now - $start, "?$i?" );
2229 5 push( @vals, $term );
2230 5 pos($str) = $start + length("?$i?");
2231 }
2232 else
2233 {
2234 69 push( @vals, $term );
2235 69 ++$i;
2236 69 substr( $str, $start, length($term), "?$i?" );
2237 69 pos($str) = $start + length("?$i?");
2238 }
2239 }
2240  
2241 45 my @newvalues;
2242 45 foreach my $val (@vals)
2243 {
2244 74 my $newval = $self->ROW_VALUE($val);
2245 74 ▶ if ( $newval && $newval->{type} !~ m/number|column|placeholder|function/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($newval and not $newval->{"type"} =~ /number|column|placeholder|function/) 0 74 74
Condition66%2/3
$newval and not $newval->{"type"} =~ /number|column|placeholder|function/
!ll&&!rl&&r
3710
MC/DC0%0/2
$newval and not $newval->{"type"} =~ /number|column|placeholder|function/
$newval not $newval->{"type"} =~ /number|column|placeholder|function/
Truth table66%2/3
A and not(B)
A= $newvalB= not $newval->{"type"} =~ /number|column|placeholder|function/
ABresult
0X0
100
111
2246 {
2247 0 return $self->do_err(qq[String '$val' not allowed in Numeric expression!]);
2248 }
2249 74 push( @newvalues, $newval );
2250 }
2251  
2252 return {
2253 45 type => 'function',
2254 name => 'numeric_exp',
2255 str => $str,
2256 value => \@newvalues,
2257 fullorg => $orgstr,
2258 };
2259 }
2260  
2261 # SUBSTRING (value FROM start [FOR length])
2262 #
2263 1918 ▶ if ( $str =~ m/^SUBSTRING \((.+?) FROM (.+)\)\s*$/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^SUBSTRING \((.+?) FROM (.+)\)\s*$/i) 5 1913 1918
2264 {
2265 5 my $name = 'SUBSTRING';
2266 5 my $start = $2;
2267 5 my $value = $self->ROW_VALUE($1);
2268 5 my $length;
2269 5 ▶ if ( $start =~ /^(.+?) FOR (.+)$/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($start =~ /^(.+?) FOR (.+)$/i) 4 1 5
2270 {
2271 4 $start = $1;
2272 4 $length = $2;
2273 4 $length = $self->ROW_VALUE($length);
2274 }
2275 5 $start = $self->ROW_VALUE($start);
2276 5 $str =~ s/\?(\d+)\?/$self->{struct}->{literals}->[$1]/g;
2277 5 ▶ if ( ( $start->{type} eq 'string' )
Branch: 1 branch
BranchTrueFalseTotal
if ($start->{"type"} eq "string" or $start->{"length"} and $start->{"length"}{"type"} eq "string") 0 5 5
Condition33%2/6
$start->{"type"} eq "string" or $start->{"length"} and $start->{"length"}{"type"} eq "string"
l!l&&r!l&&!r
005
$start->{"length"} and $start->{"length"}{"type"} eq "string"
!ll&&!rl&&r
500
MC/DC0%0/3
$start->{"type"} eq "string" or $start->{"length"} and $start->{"length"}{"type"} eq "string"
$start->{"type"} eq "string" $start->{"length"} $start->{"length"}{"type"} eq "string"
Truth table25%1/4
A or B and C
A= $start->{"type"} eq "string"B= $start->{"length"}C= $start->{"length"}{"type"} eq "string"
ABCresult
1XX1
0111
00X0
0100
2278 or ( $start->{length} && ( $start->{length}->{type} eq 'string' ) ) )
2279 {
2280 0 return $self->do_err("Can't use a string as a SUBSTRING position: '$str'!");
2281 }
2282 5 ▶ return undef unless ($value);
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 5 5
2283 return $self->do_err("Can't use a number in SUBSTRING: '$str'!")
2284 5 ▶ if $value->{type} eq 'number';
Branch: 1 branch
BranchTrueFalseTotal
if $value->{"type"} eq "number" 0 5 5
2285 return {
2286 5 type => 'function',
2287 name => $name,
2288 value => [$value],
2289 start => $start,
2290 length => $length,
2291 fullorg => $orgstr,
2292 };
2293 }
2294  
2295 # TRIM ( [ [TRAILING|LEADING|BOTH] ['char'] FROM ] value )
2296 #
2297 1913 ▶ if ( $str =~ m/^(TRIM) \((.+)\)\s*$/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(TRIM) \((.+)\)\s*$/i) 11 1902 1913
2298 {
2299 11 my $name = uc $1;
2300 11 my $value = $2;
2301 11 my ( $trim_spec, $trim_char );
2302 11 ▶ if ( $value =~ m/^(.+) FROM ([^\(\)]+)$/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(.+) FROM ([^\(\)]+)$/i) 5 6 11
2303 {
2304 5 my $front = $1;
2305 5 $value = $2;
2306 5 ▶ if ( $front =~ m/^\s*(TRAILING|LEADING|BOTH)(.*)$/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($front =~ /^\s*(TRAILING|LEADING|BOTH)(.*)$/i) { } 5 0 5
2307 {
2308 5 $trim_spec = uc $1;
2309 5 $trim_char = $2;
2310 5 $trim_char =~ s/^\s+//;
2311 5 $trim_char =~ s/\s+$//;
2312 5 ▶ undef $trim_char if ( length($trim_char) == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if length $trim_char == 0 3 2 5
2313 }
2314 else
2315 {
2316 0 $trim_char = $front;
2317 0 $trim_char =~ s/^\s+//;
2318 0 $trim_char =~ s/\s+$//;
2319 }
2320 }
2321  
2322 11 ▶ $trim_char ||= '';
Condition100%2/2
$trim_char ||= ""
l!l
29
MC/DC100%1/1
$trim_char ||= ""
$trim_char
Truth table100%2/2
A
A= $trim_char
Aresult
11
00
2323 11 $trim_char =~ s/\?(\d+)\?/$self->{struct}->{literals}->[$1]/g;
2324 11 $value = $self->ROW_VALUE($value);
2325 11 ▶ return undef unless ($value);
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 11 11
2326 11 $str =~ s/\?(\d+)\?/$self->{struct}->{literals}->[$1]/g;
2327 11 ▶ my $value_type = $value->{type} if ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 11 0 11
2328 11 ▶ $value_type = $value->[0] if ( defined( _ARRAY($value) ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined &_ARRAY($value) 0 11 11
2329 11 ▶ return $self->do_err("Can't use a number in TRIM: '$str'!")
Branch: 1 branch
BranchTrueFalseTotal
if $value_type and $value_type eq "number" 0 11 11
Condition33%1/3
$value_type and $value_type eq "number"
!ll&&!rl&&r
0110
MC/DC0%0/2
$value_type and $value_type eq "number"
$value_type $value_type eq "number"
Truth table33%1/3
A and B
A= $value_typeB= $value_type eq "number"
ABresult
0X0
100
111
2330 if ( $value_type and $value_type eq 'number' );
2331  
2332 return {
2333 11 type => 'function',
2334 name => $name,
2335 value => [$value],
2336 trim_spec => $trim_spec,
2337 trim_char => $trim_char,
2338 fullorg => $orgstr,
2339 };
2340 }
2341  
2342 # UNKNOWN FUNCTION
2343 1902 ▶ if ( $str =~ m/^(\S+) \(/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(\S+) \(/) 2 1900 1902
2344 {
2345 2 return $self->do_err("Unknown function '$1'");
2346 }
2347  
2348 # STRING CONCATENATION
2349 #
2350 1900 ▶ if ( $str =~ m/\|\|/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\|\|/) 1 1899 1900
2351 {
2352 1 my @vals = split( m/ \|\| /, $str );
2353 1 my @newvals;
2354 1 for my $val (@vals)
2355 {
2356 3 my $newval = $self->ROW_VALUE($val);
2357 3 ▶ return undef unless ($newval);
Branch: 1 branch
BranchTrueFalseTotal
unless $newval 0 3 3
2358 return $self->do_err("Can't use a number in string concatenation: '$str'!")
2359 3 ▶ if ( $newval->{type} eq 'number' );
Branch: 1 branch
BranchTrueFalseTotal
if $newval->{"type"} eq "number" 0 3 3
2360 3 push @newvals, $newval;
2361 }
2362 return {
2363 1 type => 'function',
2364 name => 'str_concat',
2365 value => \@newvals,
2366 fullorg => $orgstr,
2367 };
2368 }
2369  
2370 # NULL, BOOLEAN, PLACEHOLDER, NUMBER
2371 #
2372 1899 ▶ if ( $type = $self->LITERAL($str) )
Branch: 1 branch
BranchTrueFalseTotal
if ($type = $self->LITERAL($str)) 773 1126 1899
2373 {
2374 773 ▶ undef $str if ( $type eq 'null' );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "null" 27 746 773
2375 773 ▶ $str = 1 if ( $type eq 'boolean' and $str =~ /^TRUE$/i );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "boolean" and $str =~ /^TRUE$/i 13 760 773
Condition66%2/3
$type eq "boolean" and $str =~ /^TRUE$/i
!ll&&!rl&&r
760013
MC/DC50%1/2
$type eq "boolean" and $str =~ /^TRUE$/i
$type eq "boolean" $str =~ /^TRUE$/i
Truth table66%2/3
A and B
A= $type eq "boolean"B= $str =~ /^TRUE$/i
ABresult
0X0
100
111
2376 773 ▶ $str = 0 if ( $type eq 'boolean' and $str =~ /^FALSE$/i );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "boolean" and $str =~ /^FALSE$/i 0 773 773
Condition66%2/3
$type eq "boolean" and $str =~ /^FALSE$/i
!ll&&!rl&&r
760130
MC/DC0%0/2
$type eq "boolean" and $str =~ /^FALSE$/i
$type eq "boolean" $str =~ /^FALSE$/i
Truth table66%2/3
A and B
A= $type eq "boolean"B= $str =~ /^FALSE$/i
ABresult
0X0
100
111
2377  
2378 # if ($type eq 'empty_string') {
2379 # $str = '';
2380 # $type = 'string';
2381 # }
2382 773 ▶ $str = '' if ( $str and $str eq q('') );
Branch: 1 branch
BranchTrueFalseTotal
if $str and $str eq "''" 1 772 773
Condition100%3/3
$str and $str eq "''"
!ll&&!rl&&r
547181
MC/DC100%2/2
$str and $str eq "''"
$str $str eq "''"
Truth table100%3/3
A and B
A= $strB= $str eq "''"
ABresult
0X0
100
111
2383 return {
2384 773 type => $type,
2385 value => $str,
2386 fullorg => $orgstr,
2387 };
2388 }
2389  
2390 # QUOTED STRING LITERAL
2391 #
2392 1126 ▶ if ( $str =~ m/\?(\d+)\?/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /\?(\d+)\?/) { } 429 697 1126
elsif ($str =~ /^\?LI(\d+)\?$/) { } 30 667 697
2393 {
2394 return {
2395 type => 'string',
2396 value => $self->{struct}->{literals}->[$1],
2397 429 fullorg => $self->{struct}->{literals}->[$1],
2398 };
2399 }
2400 elsif ( $str =~ /^\?LI(\d+)\?$/ )
2401 {
2402 30 return $self->ROW_VALUE_LIST( $self->{struct}->{list_ids}->[$1] );
2403 }
2404  
2405 # COLUMN NAME
2406 #
2407 667 ▶ return undef unless ( $str = $self->COLUMN_NAME($str) );
Branch: 1 branch
BranchTrueFalseTotal
unless $str = $self->COLUMN_NAME($str) 1 665 666
2408  
2409 665 ▶ if ( $str =~ m/^(.*)\./ )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(.*)\./) 119 546 665
2410 {
2411 119 my $table_name = $1;
2412 119 $self->_verify_tablename( $table_name, "WHERE" );
2413 }
2414  
2415 # push @{ $self->{struct}->{where_cols}},$str
2416 # unless $self->{tmp}->{where_cols}->{"$str"};
2417 665 ++$self->{tmp}->{where_cols}->{$str};
2418 return {
2419 665 type => 'column',
2420 value => $str,
2421 fullorg => $orgstr,
2422 };
2423 }
2424  
2425 #########################################################
2426 # ROW_VALUE_LIST ::= <row_value> [,<row_value>...]
2427 #########################################################
2428 sub ROW_VALUE_LIST
2429 56 ▶{
Subroutine ROW_VALUE_LIST: called CC=4 SCAR=14.2
Pod undocumented
2430 56 my ( $self, $row_str ) = @_;
2431 56 my @row_list = split ',', $row_str;
2432 56 ▶ if ( !( scalar @row_list ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @row_list) 0 56 56
2433 {
2434 0 return $self->do_err('Missing row value list!');
2435 }
2436 56 my @newvals;
2437 my $newval;
2438 56 for my $row_val (@row_list)
2439 {
2440 129 $row_val =~ s/^\s+//;
2441 129 $row_val =~ s/\s+$//;
2442  
2443 129 ▶ return undef if !( $newval = $self->ROW_VALUE($row_val) );
Branch: 1 branch
BranchTrueFalseTotal
unless $newval = $self->ROW_VALUE($row_val) 0 129 129
2444 129 push @newvals, $newval;
2445 }
2446 56 return \@newvals;
2447 }
2448  
2449 ###############################################
2450 # COLUMN NAME ::= [<table_name>.] <identifier>
2451 ###############################################
2452  
2453 sub COLUMN_NAME
2454 693 ▶{
Subroutine COLUMN_NAME: called CC=17 SCAR=29.0
Pod undocumented
2455 693 my ( $self, $str ) = @_;
2456 693 my ( $table_name, $col_name );
2457 693 ▶ if ( $str =~ m/^\s*(\S+)\.(\S+)$/s )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\s*(\S+)\.(\S+)$/s) { } 122 571 693
2458 {
2459 122 ( $table_name, $col_name ) = ( $1, $2 );
2460 122 ▶ if ( !$self->{opts}->{valid_options}->{SELECT_MULTIPLE_TABLES} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"opts"}{"valid_options"}{"SELECT_MULTIPLE_TABLES"}) 0 122 122
2461 {
2462 0 return $self->do_err('Dialect does not support multiple tables!');
2463 }
2464 122 ▶ return undef unless ( $table_name = $self->TABLE_NAME($table_name) );
Branch: 1 branch
BranchTrueFalseTotal
unless $table_name = $self->TABLE_NAME($table_name) 0 122 122
2465 122 $table_name = $self->replace_quoted_ids($table_name);
2466 122 $self->_verify_tablename($table_name);
2467 }
2468 else
2469 {
2470 571 $col_name = $str;
2471 }
2472  
2473 693 $col_name =~ s/^\s+//;
2474 693 $col_name =~ s/\s+$//;
2475  
2476 693 my $user_func = $col_name;
2477 693 $user_func =~ s/^(\S+).*$/$1/;
2478 693 ▶ if ( $col_name !~ m/^(TRIM|SUBSTRING)$/i )
Branch: 1 branch
BranchTrueFalseTotal
unless ($col_name =~ /^(TRIM|SUBSTRING)$/i) 693 0 693
2479 {
2480 693 ▶ undef $user_func unless ( $self->{opts}->{function_names}->{ uc $user_func } );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opts"}{"function_names"}{uc $user_func} 690 3 693
2481 }
2482 693 ▶ if ( !$user_func )
Branch: 1 branch
BranchTrueFalseTotal
unless ($user_func) 690 3 693
2483 {
2484 690 ▶ return undef unless ( ( $col_name eq '*' ) || $self->IDENTIFIER($col_name) );
Branch: 1 branch
BranchTrueFalseTotal
unless $col_name eq "*" or $self->IDENTIFIER($col_name) 1 688 689
Condition66%2/3
$col_name eq "*" or $self->IDENTIFIER($col_name)
l!l&&r!l&&!r
06891
MC/DC50%1/2
$col_name eq "*" or $self->IDENTIFIER($col_name)
$col_name eq "*" $self->IDENTIFIER($col_name)
Truth table66%2/3
A or B
A= $col_name eq "*"B= $self->IDENTIFIER($col_name)
ABresult
1X1
011
000
2485 }
2486  
2487 #
2488 # MAKE COL NAMES ALL UPPER CASE UNLESS IS DELIMITED IDENTIFIER
2489 691 my $orgcol = $col_name;
2490  
2491 691 ▶ if ( $col_name =~ m/^\?QI(\d+)\?$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($col_name =~ /^\?QI(\d+)\?$/) { } 10 681 691
2492 {
2493 10 $col_name = '"' . $self->{struct}->{quoted_ids}->[$1] . '"';
2494 }
2495 else
2496 {
2497 $col_name = lc $col_name
2498 unless (
2499 681 ▶ ( $self->{struct}->{command} eq 'CREATE' )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"struct"}{"command"} eq "CREATE" or $col_name =~ /^(?:\p{Word}+\.)?"/ 681 0 681
Condition33%1/3
$self->{"struct"}{"command"} eq "CREATE" or $col_name =~ /^(?:\p{Word}+\.)?"/
l!l&&r!l&&!r
00681
MC/DC0%0/2
$self->{"struct"}{"command"} eq "CREATE" or $col_name =~ /^(?:\p{Word}+\.)?"/
$self->{"struct"}{"command"} eq "CREATE" $col_name =~ /^(?:\p{Word}+\.)?"/
Truth table33%1/3
A or B
A= $self->{"struct"}{"command"} eq "CREATE"B= $col_name =~ /^(?:\p{Word}+\.)?"/
ABresult
1X1
011
000
2500 ##############################################
2501 #
2502 # JZ addition to RR's alias patch
2503 #
2504 or ( $col_name =~ m/^(?:\p{Word}+\.)?"/ )
2505 );
2506  
2507 }
2508  
2509 #
2510 $col_name = $self->{struct}->{column_aliases}->{$col_name}
2511 691 ▶ if ( $self->{struct}->{column_aliases}->{$col_name} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"struct"}{"column_aliases"}{$col_name} 0 691 691
2512  
2513 # $orgcol = $self->replace_quoted_ids($orgcol);
2514 ##############################################
2515  
2516 691 ▶ if ($table_name)
Branch: 1 branch
BranchTrueFalseTotal
if ($table_name) 122 569 691
2517 {
2518 122 my $alias = $self->{tmp}->{is_table_alias}->{"\L$table_name"};
2519 122 ▶ $table_name = $alias if ( defined($alias) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $alias 17 105 122
2520 122 ▶ $table_name = lc $table_name unless ( $table_name =~ m/^"/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $table_name =~ /^"/ 120 2 122
2521 122 ▶ $col_name = "$table_name.$col_name" if ( -1 == index( $col_name, '.' ) );
Branch: 1 branch
BranchTrueFalseTotal
if (index($col_name, ".") == -1) 122 0 122
2522 }
2523 691 return $col_name;
2524 }
2525  
2526 #########################################################
2527 # COLUMN NAME_LIST ::= <column_name> [,<column_name>...]
2528 #########################################################
2529 sub COLUMN_NAME_LIST
2530 0 ▶{
Subroutine COLUMN_NAME_LIST: not called CC=4 SCAR=30.0
Pod undocumented
2531 0 my ( $self, $col_str ) = @_;
2532  
2533 0 my @col_list = split( ',', $col_str );
2534 0 ▶ return $self->do_err('Missing column name list!') unless ( scalar(@col_list) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @col_list 0 0 0
2535  
2536 0 my @newcols;
2537 0 for my $col (@col_list)
2538 {
2539 0 $col =~ s/^\s+//;
2540 0 $col =~ s/\s+$//;
2541  
2542 0 my $newcol;
2543 0 ▶ return undef unless ( $newcol = $self->COLUMN_NAME($col) );
Branch: 1 branch
BranchTrueFalseTotal
unless $newcol = $self->COLUMN_NAME($col) 0 0 0
2544 0 push( @newcols, $newcol );
2545 }
2546  
2547 0 return \@newcols;
2548 }
2549  
2550 #####################################################
2551 # TABLE_NAME_LIST := <table_name> [,<table_name>...]
2552 #####################################################
2553 sub TABLE_NAME_LIST
2554 386 ▶{
Subroutine TABLE_NAME_LIST: called CC=18 SCAR=29.2
Pod undocumented
2555 386 my ( $self, $table_name_str ) = @_;
2556 386 my %aliases = ();
2557 386 my @tables;
2558 386 $table_name_str =~ s/(\?\d+\?),/$1:/g; # fudge commas in functions
2559 386 my @table_names = split ',', $table_name_str;
2560 386 ▶ if ( scalar @table_names > 1
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @table_names > 1 and not $self->{"opts"}{"valid_options"}{"SELECT_MULTIPLE_TABLES"}) 0 386 386
Condition66%2/3
scalar @table_names > 1 and not $self->{"opts"}{"valid_options"}{"SELECT_MULTIPLE_TABLES"}
!ll&&!rl&&r
2831030
MC/DC0%0/2
scalar @table_names > 1 and not $self->{"opts"}{"valid_options"}{"SELECT_MULTIPLE_TABLES"}
scalar @table_names > 1 not $self->{"opts"}{"valid_options"}{"SELECT_MULTIPLE_TABLES"}
Truth table66%2/3
A and not(B)
A= scalar @table_names > 1B= not $self->{"opts"}{"valid_options"}{"SELECT_MULTIPLE_TABLES"}
ABresult
0X0
100
111
2561 and !$self->{opts}->{valid_options}->{SELECT_MULTIPLE_TABLES} )
2562 {
2563 0 return $self->do_err('Dialect does not support multiple tables!');
2564 }
2565  
2566 386 my $bf = BAREWORD_FUNCTIONS;
2567 386 my %is_table_alias;
2568 386 for my $table_str (@table_names)
2569 {
2570 501 $table_str =~ s/(\?\d+\?):/$1,/g; # unfudge commas in functions
2571 501 $table_str =~ s/\s+\(/\(/g; # fudge spaces in functions
2572 501 my ( $table, $alias );
2573 501 my (@tstr) = split( m/\s+/, $table_str );
2574 501 ▶ if ( @tstr == 1 )
Branch: 3 branches
BranchTrueFalseTotal
if (@tstr == 1) { } 475 26 501
elsif (@tstr == 2) { } 11 15 26
elsif (@tstr == 3) { } 13 2 15
2575 {
2576 475 $table = $tstr[0];
2577 }
2578 elsif ( @tstr == 2 )
2579 {
2580 11 $table = $tstr[0];
2581 11 $alias = $tstr[1];
2582 }
2583 elsif ( @tstr == 3 )
2584 {
2585 13 ▶ return $self->do_err("Can't find alias in FROM clause!")
Branch: 1 branch
BranchTrueFalseTotal
unless uc $tstr[1] eq "AS" 0 13 13
2586 unless ( uc( $tstr[1] ) eq 'AS' );
2587 13 $table = $tstr[0];
2588 13 $alias = $tstr[2];
2589 }
2590 else
2591 {
2592 2 return $self->do_err("Can't find table names in FROM clause!");
2593 }
2594  
2595 499 $table =~ s/\(/ \(/g; # unfudge spaces in functions
2596 499 my $u_name = $table;
2597 499 $u_name =~ s/^(\S+)\s*(.*$)/$1/;
2598 499 my $u_args = $2;
2599  
2600 499 ▶ if ( ( $u_name = $self->is_func($u_name) )
Branch: 1 branch
BranchTrueFalseTotal
if ($u_name = $self->is_func($u_name) and $u_name =~ /^(?:$bf)$/i || $table =~ /^$u_name\s*\(/i) { } 2 497 499
Condition83%5/6
$u_name = $self->is_func($u_name) and $u_name =~ /^(?:$bf)$/i || $table =~ /^$u_name\s*\(/i
!ll&&!rl&&r
48982
$u_name =~ /^(?:$bf)$/i or $table =~ /^$u_name\s*\(/i
l!l&&r!l&&!r
028
MC/DC66%2/3
$u_name = $self->is_func($u_name) and $u_name =~ /^(?:$bf)$/i || $table =~ /^$u_name\s*\(/i
$u_name = $self->is_func($u_name) $u_name =~ /^(?:$bf)$/i $table =~ /^$u_name\s*\(/i
Truth table75%3/4
A and B or C
A= $u_name = $self->is_func($u_name)B= $u_name =~ /^(?:$bf)$/iC= $table =~ /^$u_name\s*\(/i
ABCresult
0XX0
1000
11X1
1011
2601 && ( $u_name =~ /^(?:$bf)$/i || $table =~ /^$u_name\s*\(/i ) )
2602 {
2603 2 ▶ $u_args = " $u_args" if ($u_args);
Branch: 1 branch
BranchTrueFalseTotal
if $u_args 2 0 2
2604 2 my $u_func = $self->ROW_VALUE( $u_name . $u_args );
2605 2 $self->{struct}->{table_func}->{$u_name} = $u_func;
2606 2 $self->{struct}->{temp_table} = 1;
2607 2 $table = $u_name;
2608 }
2609 else
2610 {
2611 497 ▶ return undef unless ( $self->TABLE_NAME($table) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TABLE_NAME($table) 3 492 495
2612 }
2613  
2614 494 $table = $self->replace_quoted_ids($table);
2615 494 ▶ push( @tables, $table =~ m/^"/ ? $table : $table );
Branch: 1 branch
BranchTrueFalseTotal
$table =~ /^"/ ? : 7 487 494
2616  
2617 494 ▶ if ($alias)
Branch: 1 branch
BranchTrueFalseTotal
if ($alias) 24 470 494
2618 {
2619 24 ▶ return unless ( $self->TABLE_NAME($alias) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TABLE_NAME($alias) 0 24 24
2620 24 $alias = $self->replace_quoted_ids($alias);
2621 24 ▶ if ( $alias =~ m/^"/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($alias =~ /^"/) { } 0 24 24
2622 {
2623 0 push( @{ $aliases{$table} }, $alias );
2624 0 $is_table_alias{$alias} = $table;
2625 }
2626 else
2627 {
2628 24 push( @{ $aliases{$table} }, "\L$alias" );
2629 24 $is_table_alias{"\L$alias"} = $table;
2630 }
2631 }
2632 }
2633 379 my %is_table_name = map { $_ => 1 } @tables;
2634 379 $self->{tmp}->{is_table_alias} = \%is_table_alias;
2635 379 $self->{tmp}->{is_table_name} = \%is_table_name;
2636 379 $self->{struct}->{table_names} = \@tables;
2637 379 $self->{struct}->{table_alias} = \%aliases;
2638 379 ▶ $self->{struct}->{multiple_tables} = 1 if ( @tables > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @tables > 1 101 278 379
2639 379 return 1;
2640 }
2641  
2642 sub is_func($)
2643 3228 ▶{
Subroutine is_func: called CC=3 SCAR=11.0
Pod documented
2644 3228 my ( $self, $name ) = @_;
2645 3228 $name =~ s/^(\S+).*$/$1/;
2646 3228 ▶ return $name if ( $self->{opts}->{function_names}->{$name} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"function_names"}{$name} 680 2548 3228
2647 2548 ▶ return uc $name if ( $self->{opts}->{function_names}->{ uc $name } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"function_names"}{uc $name} 32 2516 2548
2648 2516 undef;
2649 }
2650  
2651 #############################
2652 # TABLE_NAME := <identifier>
2653 #############################
2654 sub TABLE_NAME
2655 917 ▶{
Subroutine TABLE_NAME: called CC=5 SCAR=18.7
Pod undocumented
2656 917 my ( $self, $table_name ) = @_;
2657 917 ▶ if ( $table_name =~ m/^(.+?)\.([^\.]+)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($table_name =~ /^(.+?)\.([^\.]+)$/) 0 917 917
2658 {
2659 0 my $schema = $1; # ignored
2660 0 $table_name = $2;
2661 }
2662 917 ▶ if ( $table_name =~ m/\s*(\S+)\s+\S+/s )
Branch: 1 branch
BranchTrueFalseTotal
if ($table_name =~ /\s*(\S+)\s+\S+/s) 0 917 917
2663 {
2664 0 return $self->do_err("Junk after table name '$1'!");
2665 }
2666 917 $table_name =~ s/\s+//s;
2667 917 ▶ if ( !$table_name )
Branch: 1 branch
BranchTrueFalseTotal
unless ($table_name) 0 917 917
2668 {
2669 0 return $self->do_err('No table name specified!');
2670 }
2671 917 ▶ return $table_name if ( $self->IDENTIFIER($table_name) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->IDENTIFIER($table_name) 912 3 915
2672  
2673 # return undef if !($self->IDENTIFIER($table_name));
2674 # return 1;
2675 }
2676  
2677 sub _verify_tablename
2678 244 ▶{
Subroutine _verify_tablename: called CC=6 SCAR=18.7
2679 244 my ( $self, $table_name, $location ) = @_;
2680 244 ▶ if ( defined($location) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $location) { } 122 122 244
2681 {
2682 122 $location = " in $location";
2683 }
2684 else
2685 {
2686 122 $location = "";
2687 }
2688  
2689 244 ▶ if ( $table_name =~ m/^"/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($table_name =~ /^"/) { } 4 240 244
2690 {
2691 4 ▶ if ( !$self->{tmp}->{is_table_name}->{$table_name}
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"tmp"}{"is_table_name"}{$table_name} or $self->{"tmp"}{"is_table_alias"}{$table_name}) 0 4 4
Condition33%1/3
$self->{"tmp"}{"is_table_name"}{$table_name} or $self->{"tmp"}{"is_table_alias"}{$table_name}
l!l&&r!l&&!r
400
MC/DC0%0/2
$self->{"tmp"}{"is_table_name"}{$table_name} or $self->{"tmp"}{"is_table_alias"}{$table_name}
$self->{"tmp"}{"is_table_name"}{$table_name} $self->{"tmp"}{"is_table_alias"}{$table_name}
Truth table33%1/3
A or B
A= $self->{"tmp"}{"is_table_name"}{$table_name}B= $self->{"tmp"}{"is_table_alias"}{$table_name}
ABresult
1X1
011
000
2692 and !$self->{tmp}->{is_table_alias}->{$table_name} )
2693 {
2694 0 return $self->do_err("Table '$table_name' referenced$location but not found in FROM list!");
2695 }
2696 }
2697 else
2698 {
2699 240 my @tblnamelist = ( keys( %{ $self->{tmp}->{is_table_name} } ), keys( %{ $self->{tmp}->{is_table_alias} } ) );
2700 240 my $tblnames = join( "|", @tblnamelist );
2701 240 ▶ unless ( $table_name =~ m/^(?:$tblnames)$/i )
Branch: 1 branch
BranchTrueFalseTotal
unless ($table_name =~ /^(?:$tblnames)$/i) 0 240 240
2702 {
2703 0 return $self->do_err(
2704 "Table '$table_name' referenced$location but not found in FROM list (" . join( ",", @tblnamelist ) . ")!" );
2705 }
2706 }
2707  
2708 244 return 1;
2709 }
2710  
2711 ###################################################################
2712 # IDENTIFIER ::= <alphabetic_char> { <alphanumeric_char> | _ }...
2713 #
2714 # and must not be a reserved word or over 128 chars in length
2715 ###################################################################
2716 sub IDENTIFIER
2717 1740 ▶{
Subroutine IDENTIFIER: called CC=12 SCAR=26.9
Pod undocumented
2718 1740 my ( $self, $id ) = @_;
2719 1740 ▶ if ( $id =~ m/^\?QI(.+)\?$/ or $id =~ m/^\?(.+)\?$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^\?QI(.+)\?$/ or $id =~ /^\?(.+)\?$/) 31 1709 1740
Condition66%2/3
$id =~ /^\?QI(.+)\?$/ or $id =~ /^\?(.+)\?$/
l!l&&r!l&&!r
3101709
MC/DC50%1/2
$id =~ /^\?QI(.+)\?$/ or $id =~ /^\?(.+)\?$/
$id =~ /^\?QI(.+)\?$/ $id =~ /^\?(.+)\?$/
Truth table66%2/3
A or B
A= $id =~ /^\?QI(.+)\?$/B= $id =~ /^\?(.+)\?$/
ABresult
1X1
011
000
2720 {
2721 31 return 1;
2722 }
2723 1709 ▶ if ( $id =~ m/^[`](.+)[`]$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^[`](.+)[`]$/) 21 1688 1709
2724 {
2725 21 ▶ $id = $1 and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$id = $1 and return 1 21 0 21
2726 }
2727 1688 ▶ if ( $id =~ m/^(.+)\.([^\.]+)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^(.+)\.([^\.]+)$/) 0 1688 1688
2728 {
2729 0 my $schema = $1; # ignored
2730 0 $id = $2;
2731 }
2732 1688 $id =~ s/\(|\)//g;
2733 1688 ▶ return 1 if $id =~ m/^".+?"$/s; # QUOTED IDENTIFIER
Branch: 1 branch
BranchTrueFalseTotal
if $id =~ /^".+?"$/s 0 1688 1688
2734 1688 my $err = "Bad table or column name: '$id' "; # BAD CHARS
2735 1688 ▶ if ( $id =~ /\W/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /\W/) 4 1684 1688
2736 {
2737 4 $err .= "has chars not alphanumeric or underscore!";
2738 4 return $self->do_err($err);
2739 }
2740 # CSV requires optional start with _
2741 1684 ▶ my $badStartRx = uc( $self->{dialect} ) eq 'ANYDATA' ? qr/^\d/ : qr/^[_\d]/;
Branch: 1 branch
BranchTrueFalseTotal
uc $self->{'dialect'} eq 'ANYDATA' ? : 0 1684 1684
2742 1684 ▶ if ( $id =~ $badStartRx )
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /$badStartRx/) 0 1684 1684
2743 { # BAD START
2744 0 $err .= "starts with non-alphabetic character!";
2745 0 return $self->do_err($err);
2746 }
2747 1684 ▶ if ( length $id > 128 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $id > 128) 0 1684 1684
2748 { # BAD LENGTH
2749 0 $err .= "contains more than 128 characters!";
2750 0 return $self->do_err($err);
2751 }
2752 1684 $id = uc $id;
2753 1684 ▶ if ( $self->{opts}->{reserved_words}->{$id} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opts"}{"reserved_words"}{$id}) 3 1681 1684
2754 { # BAD RESERVED WORDS
2755 3 $err .= "is a SQL reserved word!";
2756 3 return $self->do_err($err);
2757 }
2758 1681 return 1;
2759 }
2760  
2761 ########################################
2762 # PRIVATE METHODS AND UTILITY FUNCTIONS
2763 ########################################
2764 sub order_joins
2765 39 ▶{
Subroutine order_joins: called CC=14 SCAR=26.8
Pod documented
2766 39 my ( $self, $links ) = @_;
2767 39 for my $link (@$links)
2768 {
2769 115 ▶ if ( $link !~ /\./ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($link =~ /\./) 19 96 115
2770 {
2771 19 return [];
2772 }
2773 }
2774 20 @$links = map { s/^(.+)\..*$/$1/; $1; } @$links;
2775 20 my @all_tables;
2776 my %relations;
2777 20 my %is_table;
2778 20 ▶ while (@$links)
Branch: 1 branch
BranchTrueFalseTotal
if (@$links) 48 20 68
2779 {
2780 48 my $t1 = shift @$links;
2781 48 my $t2 = shift @$links;
2782 48 ▶ return undef unless defined $t1 and defined $t2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t1 and defined $t2 0 48 48
Condition33%1/3
defined $t1 and defined $t2
!ll&&!rl&&r
0048
MC/DC0%0/2
defined $t1 and defined $t2
defined $t1 defined $t2
Truth table33%1/3
A and B
A= defined $t1B= defined $t2
ABresult
0X0
100
111
2783 48 ▶ push @all_tables, $t1 unless $is_table{$t1}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_table{$t1}++ 23 25 48
2784 48 ▶ push @all_tables, $t2 unless $is_table{$t2}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_table{$t2}++ 29 19 48
2785 48 $relations{$t1}{$t2}++;
2786 48 $relations{$t2}{$t1}++;
2787 }
2788 20 my @tables = @all_tables;
2789 20 my @order = shift @tables;
2790 20 my %is_ordered = ( $order[0] => 1 );
2791 20 my %visited;
2792 20 ▶ while (@tables)
Branch: 1 branch
BranchTrueFalseTotal
if (@tables) 32 20 52
2793 {
2794 32 my $t = shift @tables;
2795 32 my @rels = keys %{ $relations{$t} };
2796 32 for my $t2 (@rels)
2797 {
2798 36 ▶ next unless $is_ordered{$t2};
Branch: 1 branch
BranchTrueFalseTotal
unless $is_ordered{$t2} 4 32 36
2799 32 push @order, $t;
2800 32 $is_ordered{$t}++;
2801 32 last;
2802 }
2803 32 ▶ if ( !$is_ordered{$t} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_ordered{$t}) 0 32 32
2804 {
2805 0 ▶ push @tables, $t if $visited{$t}++ < @all_tables;
Branch: 1 branch
BranchTrueFalseTotal
if $visited{$t}++ < @all_tables 0 0 0
2806 }
2807 }
2808 20 ▶ return $self->do_err("Unconnected tables in equijoin statement!")
Branch: 1 branch
BranchTrueFalseTotal
if @order < @all_tables 0 20 20
2809 if @order < @all_tables;
2810 20 return \@order;
2811 }
2812  
2813 # PROVIDE BACKWARD COMPATIBILIT FOR JOCHEN'S FEATURE ATTRIBUTES TO NEW
2814 #
2815 #
2816 sub set_feature_flags
2817 17 ▶{
Subroutine set_feature_flags: called CC=4 SCAR=21.1
Pod documented
2818 17 my ( $self, $select, $create ) = @_;
2819 17 ▶ if ( defined $select )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $select) 0 17 17
2820 {
2821 0 delete $self->{select};
2822 $self->{opts}->{valid_options}->{SELECT_MULTIPLE_TABLES} =
2823 0 $self->{opts}->{select}->{join} = $select->{join};
2824 }
2825 17 ▶ if ( defined $create )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $create) 0 17 17
2826 {
2827 0 delete $self->{create};
2828 0 for my $key ( keys %$create )
2829 {
2830 0 my $type = $key;
2831 0 $type =~ s/type_(.*)/\U$1/;
2832 $self->{opts}->{valid_data_types}->{$type} = $self->{opts}->{create}->{$key} =
2833 0 $create->{$key};
2834 }
2835 }
2836 }
2837  
2838 sub clean_sql
2839 872 ▶{
Subroutine clean_sql: called CC=10 SCAR=23.3
Pod documented
2840 872 my ( $self, $sql ) = @_;
2841 872 my $fields;
2842 872 my $i = -1;
2843 872 my $e = '\\';
2844 872 $e = quotemeta($e);
2845  
2846 #
2847 # patch from cpan@goess.org, adds support for col2=''
2848 #
2849 # $sql =~ s~'(([^'$e]|$e.|'')+)'~push(@$fields,$1);$i++;"?$i?"~ge;
2850 872 $sql =~ s~(?<!')'(([^'$e]|$e.|'')+)'~push(@$fields,$1);$i++;"?$i?"~ge;
2851  
2852 #
2853 872 foreach (@$fields) { $_ =~ s/''/\\'/g; }
2854 872 my @a = $sql =~ m/((?<!\\)(?:(?:\\\\)*)')/g;
2855 872 ▶ if ( ( scalar(@a) % 2 ) == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@a) % 2 == 1) 0 872 872
2856 {
2857 0 $sql =~ s/^.*\?(.+)$/$1/;
2858 0 $self->do_err("Mismatched single quote before: <$sql>");
2859 }
2860 872 ▶ if ( $sql =~ m/\?\?(\d)\?/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /\?\?(\d)\?/) 0 872 872
2861 {
2862 0 $sql = $fields->[$1];
2863 0 $self->do_err("Mismatched single quote: <$sql>");
2864 }
2865 872 foreach (@$fields) { $_ =~ s/$e'/'/g; s/^'(.*)'$/$1/; }
2866  
2867 #
2868 # From Steffen G. to correctly return newlines from $dbh->quote;
2869 #
2870 872 foreach (@$fields) { $_ =~ s/([^\\])\\r/$1\r/g; }
2871 872 foreach (@$fields) { $_ =~ s/([^\\])\\n/$1\n/g; }
2872  
2873 872 $self->{struct}->{literals} = $fields;
2874  
2875 872 my $qids;
2876 872 $i = -1;
2877 872 $e = q/""/;
2878  
2879 # $sql =~ s~"(([^"$e]|$e.)+)"~push(@$qids,$1);$i++;"?QI$i?"~ge;
2880 872 $sql =~ s/"(([^"]|"")+)"/push(@$qids,$1);$i++;"?QI$i?"/ge;
2881  
2882 #@$qids = map { s/$e'/'/g; s/^'(.*)'$/$1/; $_} @$qids;
2883 872 ▶ $self->{struct}->{quoted_ids} = $qids if ($qids);
Branch: 1 branch
BranchTrueFalseTotal
if $qids 28 844 872
2884  
2885 # $sql =~ s~'(([^'\\]|\\.)+)'~push(@$fields,$1);$i++;"?$i?"~ge;
2886 # @$fields = map { s/\\'/'/g; s/^'(.*)'$/$1/; $_} @$fields;
2887 #print "$sql [@$fields]\n";# if $sql =~ /SELECT/;
2888  
2889 ## before line 1511
2890 872 my $comment_re = $self->{comment_re};
2891  
2892 # if ( $sql =~ s/($comment_re)//gs) {
2893 # $self->{comment} = $1;
2894 # }
2895 872 ▶ if ( $sql =~ m/(.*)$comment_re$/s )
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /(.*)$comment_re$/s) 0 872 872
2896 {
2897 0 $sql = $1;
2898 0 $self->{comment} = $2;
2899 }
2900 872 ▶ if ( $sql =~ m/^(.*)--(.*)(\n|$)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /^(.*)--(.*)(\n|$)/) 2 870 872
2901 {
2902 2 $sql = $1;
2903 2 $self->{comment} = $2;
2904 }
2905  
2906 872 $sql =~ s/\n/ /g;
2907 872 $sql =~ s/\s+/ /g;
2908 872 $sql =~ s/(\S)\(/$1 (/g; # ensure whitespace before (
2909 872 $sql =~ s/\)(\S)/) $1/g; # ensure whitespace after )
2910 872 $sql =~ s/\(\s*/(/g; # trim whitespace after (
2911 872 $sql =~ s/\s*\)/)/g; # trim whitespace before )
2912 #
2913 # $sql =~ s/\s*\(/(/g; # trim whitespace before (
2914 # $sql =~ s/\)\s*/)/g; # trim whitespace after )
2915 # for my $op (qw(= <> < > <= >= \|\|))
2916 # {
2917 # $sql =~ s/(\S)$op/$1 $op/g;
2918 # $sql =~ s/$op(\S)/$op $1/g;
2919 # }
2920 872 $sql =~ s/(\S)([<>]?=|<>|<|>|\|\|)/$1 $2/g;
2921 872 $sql =~ s/([<>]?=|<>|<|>|\|\|)(\S)/$1 $2/g;
2922 872 $sql =~ s/< >/<>/g;
2923 872 $sql =~ s/< =/<=/g;
2924 872 $sql =~ s/> =/>=/g;
2925 872 $sql =~ s/\s*,/,/g;
2926 872 $sql =~ s/,\s*/,/g;
2927 872 $sql =~ s/^\s+//;
2928 872 $sql =~ s/\s+$//;
2929  
2930 872 return $sql;
2931 }
2932  
2933 sub trim
2934 4 ▶{
Subroutine trim: called CC=2 SCAR=7.0
Pod documented
2935 4 ▶ my $str = $_[0] or return ('');
Branch: 1 branch
BranchTrueFalseTotal
my $str = $_[0] or return "" 4 0 4
2936 4 $str =~ s/^\s+//;
2937 4 $str =~ s/\s+$//;
2938 4 return $str;
2939 }
2940  
2941 sub do_err
2942 19 ▶{
Subroutine do_err: called CC=3 SCAR=11.0
Pod documented
2943 19 my ( $self, $err, $errstr ) = @_;
2944  
2945 # $err = $errtype ? "DIALECT ERROR: $err" : "SQL ERROR: $err";
2946 19 $self->{struct}->{errstr} = $err;
2947  
2948 19 ▶ carp $err if ( $self->{PrintError} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"PrintError"} 0 19 19
2949 19 ▶ croak $err if ( $self->{RaiseError} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"RaiseError"} 6 13 19
2950 13 return;
2951 }
2952  
2953 #
2954 # DAA
2955 # abstract method so subclasses can provide
2956 # their own syntax transformations
2957 #
2958 sub transform_syntax
2959 231 ▶{
Subroutine transform_syntax: called CC=1 SCAR=0.0
Pod documented
2960 231 my ( $self, $str ) = @_;
2961 231 return $str;
2962 }
2963  
2964 sub DESTROY
2965 12 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
2966 12 my $self = $_[0];
2967  
2968 12 undef $self->{opts};
2969 12 undef $self->{struct};
2970 12 undef $self->{tmp};
2971 12 undef $self->{dialect};
2972 12 undef $self->{dialect_set};
2973 }
2974  
2975 17 1;
2976  
2977 __END__
« Statement.pm ↑ Summary Statement/TermFactory.pm »