Statement.pm

filter: Statementstmt 88.0% 1049 / 1191 Branchbran 72.5% 467 / 644 Conditioncond 64.1% 197 / 307 MC/DCmcdc 44.0% 82 / 186 Subroutinesub 90.7% 88 / 97 Podpod 83.7% 36 / 43 totaltotal 77.7% 1919 / 2468 CC 491 SCAR 36.1
CC
491
Cov
80%
CREATE
122.4
order_joins
111.4
UPDATE
85.1
CRAP 37.0
↑ Summary Parser.pm »
1 package SQL::Statement;
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 #
11 # See below for help (search for SYNOPSIS)
12 #########################################################################
13  
14 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 ▶use warnings FATAL => "all";
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 16 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 ▶use vars qw($VERSION $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 16 ▶use SQL::Parser ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 16 ▶use SQL::Eval ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 16 ▶use SQL::Statement::RAM ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 16 ▶use SQL::Statement::TermFactory ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 16 ▶use SQL::Statement::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 16 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 16 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 16 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 16 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 16 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 16 ▶use Params::Util qw(_INSTANCE _STRING _ARRAY _ARRAY0 _HASH0 _HASH);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 #use locale;
34  
35 16 $VERSION = '1.414';
36  
37 sub new
38 872 ▶{
Subroutine new: called CC=13 SCAR=32.9
Pod documented
39 872 my ( $class, $sql, $flags ) = @_;
40  
41 # IF USER DEFINED extend_csv IN SCRIPT
42 # USE THE ANYDATA DIALECT RATHER THAN THE CSV DIALECT
43 # WITH DBD::CSV
44  
45 872 ▶ if ( ( defined($main::extend_csv) && $main::extend_csv )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $main::extend_csv and $main::extend_csv or defined $main::extend_sql and $main::extend_sql) 0 872 872
Condition33%3/9
defined $main::extend_csv and $main::extend_csv or defined $main::extend_sql and $main::extend_sql
l!l&&r!l&&!r
00872
defined $main::extend_csv and $main::extend_csv
!ll&&!rl&&r
87200
defined $main::extend_sql and $main::extend_sql
!ll&&!rl&&r
87200
MC/DC0%0/4
defined $main::extend_csv and $main::extend_csv or defined $main::extend_sql and $main::extend_sql
defined $main::extend_csv $main::extend_csv defined $main::extend_sql $main::extend_sql
Truth table14%1/7
A and B or C and D
A= defined $main::extend_csvB= $main::extend_csvC= defined $main::extend_sqlD= $main::extend_sql
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
46 || ( defined($main::extend_sql) && $main::extend_sql ) )
47 {
48 0 $flags = SQL::Parser->new('AnyData');
49 }
50 872 my $parser = $flags;
51 872 my $self = bless( {}, $class );
52 872 ▶ $flags->{PrintError} = 1 unless defined $flags->{PrintError};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags->{"PrintError"} 0 872 872
53 872 ▶ $flags->{text_numbers} = 1 unless defined $flags->{text_numbers};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags->{"text_numbers"} 16 856 872
54 872 ▶ $flags->{alpha_compare} = 1 unless defined $flags->{alpha_compare};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags->{"alpha_compare"} 16 856 872
55  
56 872 ▶ unless ( blessed($flags) ) # avoid copying stale data from earlier parsing sessions
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($flags)) { } 872 0 872
57 {
58 0 %$self = ( %$self, %{ clone($flags) } );
59 }
60 else
61 {
62 872 $self->{$_} = $flags->{$_} for qw(RaiseError PrintError opts);
63 }
64  
65 872 $self->{dlm} = '~';
66  
67 # Dean Arnold improvement to allow better subclassing
68 # if (!ref($parser) or (ref($parser) and ref($parser) !~ /^SQL::Parser/)) {
69 872 ▶ unless ( _INSTANCE( $parser, 'SQL::Parser' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (&_INSTANCE($parser, "SQL::Parser")) 0 872 872
70 {
71 0 ▶ my $parser_dialect = $flags->{dialect} || 'AnyData';
Condition0%0/2
$flags->{'dialect'} || 'AnyData'
l!l
00
MC/DC0%0/1
$flags->{'dialect'} || 'AnyData'
$flags->{'dialect'}
Truth table0%0/2
A
A= $flags->{'dialect'}
Aresult
11
00
72 0 ▶ $parser_dialect = 'AnyData' if ( $parser_dialect =~ m/^(?:CSV|Excel)$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $parser_dialect =~ /^(?:CSV|Excel)$/ 0 0 0
73  
74 0 $parser = SQL::Parser->new( $parser_dialect, $flags );
75 }
76 872 $self->{termFactory} = SQL::Statement::TermFactory->new($self);
77 872 $self->{capabilities} = {};
78 872 $self->prepare( $sql, $parser );
79 865 return $self;
80 }
81  
82 sub prepare
83 872 ▶{
Subroutine prepare: called CC=9 SCAR=22.0
Pod documented
84 872 my ( $self, $sql, $parser ) = @_;
85  
86 872 ▶ $self->{already_prepared}->{$sql} and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"already_prepared"}{$sql} and return 0 872 872
87  
88 # delete earlier preparations, they're overwritten after this prepare run
89 872 $self->{already_prepared} = {};
90 872 my $rv = $parser->parse($sql);
91 865 ▶ if ($rv)
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 855 10 865
92 {
93 855 undef $self->{errstr};
94 855 my $parser_struct = clone( $parser->{struct} );
95 855 ▶ while ( my ( $k, $v ) = each( %{$parser_struct} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$parser_struct;}) 8709 855 9564
96 {
97 8709 $self->{$k} = $v;
98 }
99 855 undef $self->{where_terms}; # force rebuild when needed
100 855 undef $self->{columns};
101 855 undef $self->{splitted_all_cols};
102 855 $self->{argnum} = 0;
103  
104 855 my $values = $self->{values};
105 855 my $param_num = -1;
106 855 ▶ if ( $self->{limit_clause} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"limit_clause"}) 9 846 855
107 {
108 9 $self->{limit_clause} = SQL::Statement::Limit->new( $self->{limit_clause} );
109 }
110  
111 855 ▶ if ( defined( $self->{num_placeholders} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"num_placeholders"}) 17 838 855
112 {
113 17 for my $i ( 0 .. $self->{num_placeholders} - 1 )
114 {
115 36 $self->{params}->[$i] = SQL::Statement::Param->new($i);
116 }
117 }
118  
119 855 $self->{tables} = [ map { SQL::Statement::Table->new($_) } @{ $self->{table_names} } ];
120  
121 855 ▶ if ( $self->{where_clause} && !defined( $self->{where_terms} ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"where_clause"} and not defined $self->{"where_terms"}) 229 626 855
Condition66%2/3
$self->{"where_clause"} and not defined $self->{"where_terms"}
!ll&&!rl&&r
6260229
MC/DC50%1/2
$self->{"where_clause"} and not defined $self->{"where_terms"}
$self->{"where_clause"} not defined $self->{"where_terms"}
Truth table66%2/3
A and not(B)
A= $self->{"where_clause"}B= not defined $self->{"where_terms"}
ABresult
0X0
100
111
122 {
123 229 $self->{where_terms} = $self->{termFactory}->buildCondition( $self->{where_clause} );
124 #if ( $self->{where_clause}->{combiners} )
125 #{
126 # $self->{has_OR} = 1
127 # if ( first { -1 != index( $_, 'OR' ) } @{ $self->{where_clause}->{combiners} } );
128 #}
129 }
130  
131 855 ++$self->{already_prepared}->{$sql};
132 855 return $self;
133 }
134 else
135 {
136 10 $self->{errstr} = $parser->errstr;
137 10 ++$self->{already_prepared}->{$sql};
138 10 return;
139 }
140 }
141  
142 sub execute
143 4654 ▶{
Subroutine execute: called CC=10 SCAR=23.1
Pod documented
144 4654 my ( $self, $data, $params ) = @_;
145 ( $self->{NUM_OF_ROWS}, $self->{NUM_OF_FIELDS}, $self->{data} ) = ( 0, 0, [] ) and return 'OEO'
146 4654 ▶ if ( $self->{no_execute} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"no_execute"} 5 4649 4654
Condition60%3/5
$self->{'no_execute'} && ((($self->{'NUM_OF_ROWS'}, $self->{'NUM_OF_FIELDS'}, $self->{'data'}) = (0, 0, [])) && (return 'OEO'))
!ll
464950
($self->{"NUM_OF_ROWS"}, $self->{"NUM_OF_FIELDS"}, $self->{"data"}) = (0, 0, []) and return "OEO"
!ll
05
MC/DC0%0/2
$self->{'no_execute'} && ((($self->{'NUM_OF_ROWS'}, $self->{'NUM_OF_FIELDS'}, $self->{'data'}) = (0, 0, [])) && (return 'OEO'))
$self->{'no_execute'} ($self->{"NUM_OF_ROWS"}, $self->{"NUM_OF_FIELDS"}, $self->{"data"}) = (0, 0, [])
Truth table33%1/3
A && B
A= $self->{'no_execute'}B= ($self->{"NUM_OF_ROWS"}, $self->{"NUM_OF_FIELDS"}, $self->{"data"}) = (0, 0, [])
ABresult
0X0
100
111
147 4649 ▶ $self->{procedure}->{data} = $data if ( $self->{procedure} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"procedure"} 186 4463 4649
148 4649 $self->{params} = $params;
149  
150 4649 my ($command) = $self->command();
151 4649 ▶ return $self->do_err('No command found!') unless ($command);
Branch: 1 branch
BranchTrueFalseTotal
unless $command 0 4649 4649
152  
153 $self->{where_clause}
154 and !defined( $self->{where_terms} )
155 4649 ▶ and $self->{where_terms} = $self->{termFactory}->buildCondition( $self->{where_clause} );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"where_clause"} and not defined $self->{"where_terms"} and $self->{"where_terms"} = $self->{"termFactory"}->buildCondition($self->{"where_clause"}) 22 4627 4649
Condition100%3/3
$self->{"where_clause"} and not defined $self->{"where_terms"}
!ll&&!rl&&r
45666122
MC/DC100%2/2
$self->{"where_clause"} and not defined $self->{"where_terms"}
$self->{"where_clause"} not defined $self->{"where_terms"}
Truth table100%3/3
A and not(B)
A= $self->{"where_clause"}B= not defined $self->{"where_terms"}
ABresult
0X0
100
111
156  
157 4649 ( $self->{NUM_OF_ROWS}, $self->{NUM_OF_FIELDS}, $self->{data} ) = $self->$command( $data, $params );
158  
159 $self->{NAME} =
160 4648 ▶ _ARRAY0( $self->{columns} ) ? [ map { delete $_->{term}->{fastpath}; $_->display_name() } @{ $self->{columns} } ] : [];
Branch: 1 branch
BranchTrueFalseTotal
&_ARRAY0($self->{'columns'}) ? : 4642 6 4648
161  
162 # Force closing the tables
163 4648 $self->{tables} = [ map { SQL::Statement::Table->new($_->{name}) } @{ delete $self->{tables} } ]; # create keen defs
164  
165 4648 undef $self->{where_terms}; # force rebuild when needed
166  
167 4648 ▶ return unless ( defined( $self->{NUM_OF_ROWS} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"NUM_OF_ROWS"} 4 4644 4648
168 4644 ▶ return $self->{NUM_OF_ROWS} || '0E0';
Condition100%2/2
$self->{'NUM_OF_ROWS'} || '0E0'
l!l
459747
MC/DC100%1/1
$self->{'NUM_OF_ROWS'} || '0E0'
$self->{'NUM_OF_ROWS'}
Truth table100%2/2
A
A= $self->{'NUM_OF_ROWS'}
Aresult
11
00
169 }
170  
171 sub CREATE ($$$)
172 35 ▶{
Subroutine CREATE: called CC=16 SCAR=48.1
Pod undocumented
173 35 my ( $self, $data, $params ) = @_;
174 35 my $names;
175  
176 # CREATE TABLE AS ...
177 35 my $subquery = $self->{subquery};
178 35 ▶ if ($subquery)
Branch: 1 branch
BranchTrueFalseTotal
if ($subquery) 0 35 35
179 {
180 0 my $sth;
181  
182 # AS IMPORT
183 0 ▶ if ( $subquery =~ m/^IMPORT/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($subquery =~ /^IMPORT/i) { } 0 0 0
184 {
185 $sth = $data->{Database}->prepare("SELECT * FROM $subquery")
186 0 ▶ or return $self->do_err( $data->{Database}->errstr() );
Branch: 1 branch
BranchTrueFalseTotal
$sth = $data->{"Database"}->prepare("SELECT * FROM $subquery") or return $self->do_err($data->{"Database"}->errstr) 0 0 0
187 0 ▶ $sth->execute(@$params) or return $self->do_err( $sth->errstr() );
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@$params) or return $self->do_err($sth->errstr) 0 0 0
188 0 $names = $sth->{NAME};
189 }
190  
191 # AS SELECT
192 else
193 {
194 $sth = $data->{Database}->prepare($subquery)
195 0 ▶ or return $self->do_err( $data->{Database}->errstr() );
Branch: 1 branch
BranchTrueFalseTotal
$sth = $data->{"Database"}->prepare($subquery) or return $self->do_err($data->{"Database"}->errstr) 0 0 0
196 0 ▶ $sth->execute() or return $self->do_err( $sth->errstr() );
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->do_err($sth->errstr) 0 0 0
197 0 $names = $sth->{NAME};
198 }
199 0 ▶ $names = $sth->{NAME} unless defined $names;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $names 0 0 0
200 0 my $tbl_data = $sth->{sql_stmt}->{data};
201 0 ▶ my $tbl_name = $self->{org_table_names}->[0] || $self->tables(0)->name;
Condition0%0/3
$self->{'org_table_names'}[0] || $self->tables(0)->name
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'org_table_names'}[0] || $self->tables(0)->name
$self->{'org_table_names'}[0] $self->tables(0)->name
Truth table0%0/3
A || B
A= $self->{'org_table_names'}[0]B= $self->tables(0)->name
ABresult
1X1
011
000
202  
203 # my @tbl_cols = map {$_->name} $sth->{sql_stmt}->columns;
204 #my @tbl_cols=map{$_->name} $sth->{sql_stmt}->columns if $sth->{sql_stmt};
205 0 my @tbl_cols;
206  
207 # @tbl_cols=@{ $sth->{NAME} } unless @tbl_cols;
208 0 ▶ @tbl_cols = @{$names} unless (@tbl_cols);
Branch: 1 branch
BranchTrueFalseTotal
unless @tbl_cols 0 0 0
209 0 my $create_sql = "CREATE TABLE $tbl_name";
210 0 ▶ $create_sql = "CREATE TEMP TABLE $tbl_name" if ( $self->{is_ram_table} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_ram_table"} 0 0 0
211 0 my @coldefs = map { "'$_' TEXT" } @tbl_cols;
212 0 $create_sql .= '(' . join( ',', @coldefs ) . ')';
213 0 ▶ $data->{Database}->do($create_sql) or die "Can't do <$create_sql>: " . $data->{Database}->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$data->{"Database"}->do($create_sql) or die "Can't do <$create_sql>: " . $data->{"Database"}->errstr 0 0 0
214 0 my $colstr = ('?,') x @tbl_cols;
215 0 my $insert_sql = "INSERT INTO $tbl_name VALUES($colstr)";
216 0 my $local_sth = $data->{Database}->prepare($insert_sql);
217 0 $local_sth->execute(@$_) for @$tbl_data;
218 0 return ( 0, 0 );
219 }
220 35 my ( $eval, $foo ) = $self->open_tables( $data, 1, 1 );
221 35 ▶ return unless ($eval);
Branch: 1 branch
BranchTrueFalseTotal
unless $eval 0 35 35
222 35 $eval->params($params);
223 35 my ( $row, $table, $col ) = ( [], $eval->table( $self->tables(0)->name() ) );
224 35 ▶ if ( _ARRAY( $table->col_names() ) )
Branch: 1 branch
BranchTrueFalseTotal
if (&_ARRAY(scalar $table->col_names)) 0 35 35
225 {
226 0 return $self->do_err( "Table '" . $self->tables(0)->name() . "' already exists." );
227 }
228 35 foreach $col ( $self->columns() )
229 {
230 97 push( @{$row}, $col->name() );
231 }
232 35 $table->push_names( $data, $row );
233  
234 35 return ( 0, 0 );
235 }
236  
237 sub CALL
238 0 ▶{
Subroutine CALL: not called CC=1 SCAR=6.9
Pod undocumented
239 0 my ( $self, $data, $params ) = @_;
240  
241 # my $dbh = $data->{Database};
242 # $self->{procedure}->{data} = $data;
243  
244 0 my $procTerm = $self->{termFactory}->buildCondition( $self->{procedure} );
245  
246 0 ( $self->{NUM_OF_ROWS}, $self->{NUM_OF_FIELDS}, $self->{data} ) = $procTerm->value($data);
247 }
248  
249 16 my $enoentstr = "Cannot open .*\(" . Errno::ENOENT . "\)";
250 16 my $eabstrstr = "Abstract method .*::open_table called";
251 16 my $notblrx = qr/(?:$enoentstr|$eabstrstr)/;
252  
253 sub DROP ($$$)
254 12 ▶{
Subroutine DROP: called CC=11 SCAR=27.0
Pod undocumented
255 12 my ( $self, $data, $params ) = @_;
256 12 my $eval;
257 my @err;
258 12 eval {
259 0 ▶ local $SIG{__WARN__} = sub { push @err, @_ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
260 12 ($eval) = $self->open_tables( $data, 0, 1 );
261 };
262 12 ▶ if ( $self->{ignore_missing_table}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ignore_missing_table"} and $@ || @err || $self->{"errstr"} and grep {$_ =~ /$notblrx/;} @err, $@, $self->{"errstr"}) 5 7 12
Condition66%8/12
$self->{"ignore_missing_table"} and $@ || @err || $self->{"errstr"} and grep {$_ =~ /$notblrx/;} @err, $@, $self->{"errstr"}
!ll&&!rl&&r
705
$self->{"ignore_missing_table"} and $@ || @err || $self->{"errstr"}
!ll&&!rl&&r
345
$@ or @err or $self->{"errstr"}
l!l&&r!l&&!r
054
$@ or @err
l!l&&r!l&&!r
009
MC/DC40%2/5
$self->{"ignore_missing_table"} and $@ || @err || $self->{"errstr"} and grep {$_ =~ /$notblrx/;} @err, $@, $self->{"errstr"}
$self->{"ignore_missing_table"} $@ @err $self->{"errstr"} grep {$_ =~ /$notblrx/;} @err, $@, $self->{"errstr"}
Truth table37%3/8
A and B or C or D and E
A= $self->{"ignore_missing_table"}B= $@C= @errD= $self->{"errstr"}E= grep {$_ =~ /$notblrx/;} @err, $@, $self->{"errstr"}
ABCDEresult
0XXXX0
1000X0
11XX00
101X00
100100
11XX11
101X11
100111
263 and ( $@ or @err or $self->{errstr} )
264 10 and grep { $_ =~ $notblrx } ( @err, $@, $self->{errstr} ) )
265 {
266 5 return ( -1, 0 );
267 }
268  
269 7 ▶ return if $self->{errstr};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"errstr"} 0 7 7
270 7 ▶ return $self->do_err( $@ || $err[0] ) if ( $@ || @err );
Branch: 1 branch
BranchTrueFalseTotal
if $@ or @err 0 7 7
Condition16%1/6
$@ or @err
l!l&&r!l&&!r
007
$@ || $err[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or @err
$@ @err
MC/DC0%0/2
$@ || $err[0]
$@ $err[0]
Truth table33%1/3
A or B
A= $@B= @err
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $@B= $err[0]
ABresult
1X1
011
000
271  
272 # return undef unless $eval;
273 7 ▶ return ( -1, 0 ) unless $eval;
Branch: 1 branch
BranchTrueFalseTotal
unless $eval 0 7 7
274  
275 # $eval->params($params);
276 7 my ($table) = $eval->table( $self->tables(0)->name() );
277 7 $table->drop($data);
278  
279 #use mylibs; zwarn $self->{sql_stmt};
280 7 return ( -1, 0 );
281 }
282  
283 sub INSERT ($$$)
284 4275 ▶{
Subroutine INSERT: called CC=16 SCAR=29.5
Pod undocumented
285 4275 my ( $self, $data, $params ) = @_;
286  
287 4275 my ( $eval, $all_cols ) = $self->open_tables( $data, 0, 1 );
288 4275 ▶ return unless ($eval);
Branch: 1 branch
BranchTrueFalseTotal
unless $eval 0 4275 4275
289  
290 4275 ▶ $params and $eval->params($params);
Branch: 1 branch
BranchTrueFalseTotal
$params and $eval->params($params) 4257 18 4275
291 4275 ▶ $self->verify_columns( $data, $eval, $all_cols ) if ( scalar( $self->columns() ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar $self->columns 4275 0 4275
292 4275 ▶ return if ( $self->{errstr} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"errstr"} 0 4275 4275
293  
294 4275 my ($table) = $eval->table( $self->tables(0)->name() );
295 4275 ▶ $table->seek( $data, 0, 2 ) unless ( $table->capability('insert_new_row') );
Branch: 1 branch
BranchTrueFalseTotal
unless $table->capability("insert_new_row") 0 4275 4275
296  
297 4275 my ( $val, $col, $i, $k );
298 4275 my ($cNum) = scalar( $self->columns() );
299 4275 my $param_num = 0;
300  
301 4275 ▶ $cNum
Branch: 1 branch
BranchTrueFalseTotal
$cNum or return $self->do_err("Bad col names in INSERT") 4275 0 4275
302 or return $self->do_err("Bad col names in INSERT");
303  
304 4275 my $maxCol = $#$all_cols;
305  
306 # INSERT INTO $table (row, ...) VALUES (value, ...), (...)
307 4275 ▶ for ( $k = 0; $k < scalar( @{ $self->{values} } ); ++$k )
Branch: 1 branch
BranchTrueFalseTotal
if ($k < scalar @{$self->{"values"};}) 4280 4275 8555
308 {
309 4280 my ($array) = [];
310 4280 ▶ for ( $i = 0; $i < $cNum; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $cNum) 12791 4280 17071
311 {
312 12791 $col = $self->columns($i);
313 12791 $val = $self->row_values( $k, $i );
314 12791 ▶ if ( defined( _INSTANCE( $val, 'SQL::Statement::Param' ) ) )
Branch: 4 branches
BranchTrueFalseTotal
if (defined &_INSTANCE($val, 'SQL::Statement::Param')) { } 0 12791 12791
elsif (defined &_INSTANCE($val, 'SQL::Statement::Term')) { } 0 12791 12791
elsif ($val and $val->{'type'} eq 'placeholder') { } 12242 549 12791
elsif (defined &_HASH($val)) { } 549 0 549
315 {
316 0 $val = $eval->param( $val->idx() );
317 }
318 elsif ( defined( _INSTANCE( $val, 'SQL::Statement::Term' ) ) )
319 {
320 0 $val = $val->value($eval);
321 }
322 ▶ elsif ( $val and $val->{type} eq 'placeholder' )
Condition66%2/3
$val and $val->{"type"} eq "placeholder"
!ll&&!rl&&r
054912242
MC/DC50%1/2
$val and $val->{"type"} eq "placeholder"
$val $val->{"type"} eq "placeholder"
Truth table66%2/3
A and B
A= $valB= $val->{"type"} eq "placeholder"
ABresult
0X0
100
111
323 {
324 12242 $val = $eval->param( $param_num++ );
325 }
326 elsif ( defined( _HASH($val) ) )
327 {
328 549 $val = $self->{termFactory}->buildCondition($val);
329 549 $val = $val->value($eval);
330 }
331 else
332 {
333 0 return $self->do_err('Internal error: Unexpected column type');
334 }
335 12791 $array->[ $table->column_num( $col->name() ) ] = $val;
336 }
337  
338 # Extend row to put values in ALL fields
339 4280 ▶ $#$array < $maxCol and $array->[$maxCol] = undef;
Branch: 1 branch
BranchTrueFalseTotal
$array->$#* < $maxCol and $array->[$maxCol] = undef 0 4280 4280
340  
341 4280 ▶ $table->capability('insert_new_row')
Branch: 1 branch
BranchTrueFalseTotal
$table->capability('insert_new_row') ? : 4280 0 4280
342 ? $table->insert_new_row( $data, $array )
343 : $table->push_row( $data, $array );
344 }
345  
346 4275 return ( $k, 0 );
347 }
348  
349 sub DELETE ($$$)
350 6 ▶{
Subroutine DELETE: called CC=15 SCAR=32.1
351 6 my ( $self, $data, $params ) = @_;
352 6 my ( $eval, $all_cols ) = $self->open_tables( $data, 0, 1 );
353 6 ▶ return unless $eval;
Branch: 1 branch
BranchTrueFalseTotal
unless $eval 0 6 6
354 6 $eval->params($params);
355 6 $self->verify_columns( $data, $eval, $all_cols );
356 6 ▶ return if ( $self->{errstr} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"errstr"} 0 6 6
357 6 my $tname = $self->tables(0)->name();
358 6 my ($table) = $eval->table($tname);
359 6 my $affected = 0;
360 6 my ( @rows, $array );
361  
362 6 ▶ while ( $array = $table->fetch_row($data) )
Branch: 1 branch
BranchTrueFalseTotal
if ($array = $table->fetch_row($data)) 29 6 35
363 {
364 29 ▶ if ( $self->eval_where( $eval, $tname, $array ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->eval_where($eval, $tname, $array)) 10 19 29
365 {
366 10 ++$affected;
367 10 ▶ if ( $table->capability('rowwise_delete') and $table->capability('inplace_delete') )
Branch: 2 branches
BranchTrueFalseTotal
if ($table->capability('rowwise_delete') and $table->capability('inplace_delete')) { } 10 0 10
elsif ($table->capability('rowwise_delete')) { } 0 0 0
Condition33%1/3
$table->capability("rowwise_delete") and $table->capability("inplace_delete")
!ll&&!rl&&r
0010
MC/DC0%0/2
$table->capability("rowwise_delete") and $table->capability("inplace_delete")
$table->capability("rowwise_delete") $table->capability("inplace_delete")
Truth table33%1/3
A and B
A= $table->capability("rowwise_delete")B= $table->capability("inplace_delete")
ABresult
0X0
100
111
368 {
369 10 ▶ if ( $table->capability('delete_one_row') )
Branch: 2 branches
BranchTrueFalseTotal
if ($table->capability('delete_one_row')) { } 0 10 10
elsif ($table->capability('delete_current_row')) { } 10 0 10
370 {
371 0 $table->delete_one_row( $data, $array );
372 }
373 elsif ( $table->capability('delete_current_row') )
374 {
375 10 $table->delete_current_row( $data, $array );
376 }
377 }
378 elsif ( $table->capability('rowwise_delete') )
379 {
380 0 push( @rows, $array );
381 }
382  
383 10 next;
384 }
385  
386 19 ▶ push( @rows, $array ) unless ( $table->capability('rowwise_delete') );
Branch: 1 branch
BranchTrueFalseTotal
unless $table->capability("rowwise_delete") 0 19 19
387 }
388  
389 6 ▶ if ($affected)
Branch: 1 branch
BranchTrueFalseTotal
if ($affected) 6 0 6
390 {
391 6 ▶ if ( $table->capability('rowwise_delete') )
Branch: 1 branch
BranchTrueFalseTotal
if ($table->capability('rowwise_delete')) { } 6 0 6
392 { # @rows is empty in case of inplace_delete capability
393 6 foreach my $array (@rows)
394 {
395 0 $table->delete_one_row( $data, $array );
396 }
397 }
398 else
399 {
400 # rewrite table without deleted elements
401 0 $table->seek( $data, 0, 0 );
402 0 foreach my $array (@rows)
403 {
404 0 $table->push_row( $data, $array );
405 }
406 0 $table->truncate($data);
407 }
408 }
409  
410 6 return ( $affected, 0 );
411 }
412  
413 sub UPDATE ($$$)
414 5 ▶{
Subroutine UPDATE: called CC=29 SCAR=44.4
Pod undocumented
415 5 my ( $self, $data, $params ) = @_;
416  
417 5 my ( $eval, $all_cols ) = $self->open_tables( $data, 0, 1 );
418 5 ▶ return unless $eval;
Branch: 1 branch
BranchTrueFalseTotal
unless $eval 0 5 5
419  
420 5 my $valnum = $self->{num_val_placeholders};
421 5 ▶ my @val_params = splice( @{$params}, 0, $valnum ) if ($valnum);
Branch: 1 branch
BranchTrueFalseTotal
if $valnum 3 2 5
422 5 ▶ $self->{params} ||= $params;
Condition33%1/3
$self->{"params"} ||= $params
l!l&&r!l&&!r
500
MC/DC0%0/2
$self->{"params"} ||= $params
$self->{"params"} $params
Truth table33%1/3
A ||= B
A= $self->{"params"}B= $params
ABresult
1X1
011
000
423 5 $eval->params($params);
424 5 $self->verify_columns( $data, $eval, $all_cols );
425 5 ▶ return if ( $self->{errstr} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"errstr"} 0 5 5
426  
427 5 my $tname = $self->tables(0)->name();
428 5 my ($table) = $eval->table($tname);
429 5 my $affected = 0;
430 5 my @rows;
431  
432 5 ▶ while ( my $array = $table->fetch_row($data) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $array = $table->fetch_row($data)) 21 5 26
433 {
434 21 my $originalValues;
435 21 ▶ if ( $self->eval_where( $eval, $tname, $array ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->eval_where($eval, $tname, $array)) 7 14 21
436 {
437 7 my $valpos = 0;
438 7 ▶ if ( $table->capability('update_specific_row') )
Branch: 1 branch
BranchTrueFalseTotal
if ($table->capability("update_specific_row")) 0 7 7
439 {
440 0 $originalValues = clone($array);
441 }
442  
443 7 ▶ for ( my $i = 0; $i < $self->columns(); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->columns) 9 7 16
444 {
445 9 my $val = $self->row_values( 0, $i );
446 9 ▶ if ( defined( _INSTANCE( $val, 'SQL::Statement::Param' ) ) )
Branch: 4 branches
BranchTrueFalseTotal
if (defined &_INSTANCE($val, 'SQL::Statement::Param')) { } 0 9 9
elsif (defined &_INSTANCE($val, 'SQL::Statement::Term')) { } 0 9 9
elsif ($val and $val->{'type'} eq 'placeholder') { } 6 3 9
elsif (defined &_HASH($val)) { } 3 0 3
447 {
448 0 $val = $val_params[ $valpos++ ];
449 }
450 elsif ( defined( _INSTANCE( $val, 'SQL::Statement::Term' ) ) )
451 {
452 0 $val = $val->value($eval);
453 }
454 ▶ elsif ( $val and $val->{type} eq 'placeholder' )
Condition66%2/3
$val and $val->{"type"} eq "placeholder"
!ll&&!rl&&r
036
MC/DC50%1/2
$val and $val->{"type"} eq "placeholder"
$val $val->{"type"} eq "placeholder"
Truth table66%2/3
A and B
A= $valB= $val->{"type"} eq "placeholder"
ABresult
0X0
100
111
455 {
456 6 $val = $val_params[ $valpos++ ];
457 }
458 elsif ( defined( _HASH($val) ) )
459 {
460 3 $val = $self->{termFactory}->buildCondition($val);
461 3 $val = $val->value($eval);
462 }
463 else
464 {
465 0 return $self->do_err('Internal error: Unexpected column type');
466 }
467  
468 9 my $col = $self->columns($i);
469 9 $array->[ $table->column_num( $col->name() ) ] = $val;
470 }
471  
472 7 ++$affected;
473 7 ▶ if ( $table->capability('rowwise_update') and $table->capability('inplace_update') )
Branch: 2 branches
BranchTrueFalseTotal
if ($table->capability('rowwise_update') and $table->capability('inplace_update')) { } 7 0 7
elsif ($table->capability('rowwise_update')) { } 0 0 0
Condition33%1/3
$table->capability("rowwise_update") and $table->capability("inplace_update")
!ll&&!rl&&r
007
MC/DC0%0/2
$table->capability("rowwise_update") and $table->capability("inplace_update")
$table->capability("rowwise_update") $table->capability("inplace_update")
Truth table33%1/3
A and B
A= $table->capability("rowwise_update")B= $table->capability("inplace_update")
ABresult
0X0
100
111
474 {
475 # Martin Fabiani <martin@fabiani.net>:
476 # the following block is the most important enhancement to SQL::Statement::UPDATE
477 7 ▶ if ( $table->capability('update_specific_row') )
Branch: 3 branches
BranchTrueFalseTotal
if ($table->capability('update_specific_row')) { } 0 7 7
elsif ($table->capability('update_one_row')) { } 0 7 7
elsif ($table->capability('update_current_row')) { } 7 0 7
478 {
479 0 $table->update_specific_row( $data, $array, $originalValues );
480 }
481 elsif ( $table->capability('update_one_row') )
482 {
483 # NOTE: this prevents from updating index keys
484 0 $table->update_one_row( $data, $array );
485 }
486 elsif ( $table->capability('update_current_row') )
487 {
488 7 $table->update_current_row( $data, $array );
489 }
490 }
491 elsif ( $table->capability('rowwise_update') )
492 {
493 0 ▶ push( @rows, $array ) unless ( $table->capability('update_specific_row') );
Branch: 1 branch
BranchTrueFalseTotal
unless $table->capability("update_specific_row") 0 0 0
494 0 ▶ push( @rows, [ $array, $originalValues ] )
Branch: 1 branch
BranchTrueFalseTotal
if $table->capability("update_specific_row") 0 0 0
495 if ( $table->capability('update_specific_row') );
496 }
497 }
498  
499 21 ▶ push( @rows, $array ) unless ( $table->capability('rowwise_update') );
Branch: 1 branch
BranchTrueFalseTotal
unless $table->capability("rowwise_update") 0 21 21
500 }
501  
502 5 ▶ if ($affected)
Branch: 1 branch
BranchTrueFalseTotal
if ($affected) 5 0 5
503 {
504 5 ▶ if ( $table->capability('rowwise_update') )
Branch: 1 branch
BranchTrueFalseTotal
if ($table->capability('rowwise_update')) { } 5 0 5
505 { # @rows is empty in case of inplace_update capability
506 5 foreach my $array (@rows)
507 {
508 0 ▶ if ( $table->capability('update_specific_row') )
Branch: 2 branches
BranchTrueFalseTotal
if ($table->capability('update_specific_row')) { } 0 0 0
elsif ($table->capability('update_one_row')) { } 0 0 0
509 {
510 0 $table->update_specific_row( $data, $array->[0], $array->[1] );
511 }
512 elsif ( $table->capability('update_one_row') )
513 {
514 0 $table->update_one_row( $data, $array );
515 }
516 }
517 }
518 else
519 {
520 # rewrite table with updated elements
521 0 $table->seek( $data, 0, 0 );
522 0 foreach my $array (@rows)
523 {
524 0 $table->push_row( $data, $array );
525 }
526 0 $table->truncate($data);
527 }
528 }
529  
530 5 return ( $affected, 0 );
531 }
532  
533 sub find_join_columns
534 32 ▶{
Subroutine find_join_columns: called CC=19 SCAR=29.5
Pod documented
535 32 my ( $self, @all_cols ) = @_;
536 32 my $display_combine = 'NAMED';
537 32 ▶ $display_combine = 'NATURAL' if ( -1 != index( $self->{join}->{type}, 'NATURAL' ) );
Branch: 1 branch
BranchTrueFalseTotal
if (index($self->{"join"}{"type"}, "NATURAL") != -1) 6 26 32
538 32 ▶ $display_combine = 'USING' if ( -1 != index( $self->{join}->{clause}, 'USING' ) );
Branch: 1 branch
BranchTrueFalseTotal
if (index($self->{"join"}{"clause"}, "USING") != -1) 10 22 32
539 32 my @display_cols;
540 32 my @keycols = ();
541 25 @keycols = @{ $self->{join}->{keycols} }
542 32 ▶ if $self->{join}->{keycols};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"join"}{"keycols"} 25 7 32
543 32 foreach (@keycols) { $_ =~ s/\./$self->{dlm}/ }
544 32 my %is_key_col;
545 32 %is_key_col = map { $_ => 1 } @keycols;
546  
547 # IF NAMED COLUMNS, USE NAMED COLUMNS
548 #
549 32 ▶ if ( $display_combine eq 'NAMED' )
Branch: 2 branches
BranchTrueFalseTotal
if ($display_combine eq 'NAMED') { } 16 16 32
elsif ($display_combine eq 'NONE') { } 0 16 16
550 {
551 16 @display_cols = $self->columns();
552  
553 #
554 # DAA
555 # need to get to $self's table objects to get the name
556 #
557 # @display_cols = map {$_->table . $self->{dlm} . $_->name} @display_cols;
558 # @display_cols = map {$_->table->{NAME} . $self->{dlm} . $_->name} @display_cols;
559  
560 16 my @tbls = $self->tables();
561 16 my %tables = ();
562  
563 16 $tables{ $_->name() } = $_ foreach (@tbls);
564  
565 16 foreach ( 0 .. $#display_cols )
566 {
567 $display_cols[$_] = (
568 $display_cols[$_]->table()
569 ? $tables{ $display_cols[$_]->table() }->name()
570 : ''
571 )
572 . $self->{dlm}
573 72 ▶ . $display_cols[$_]->name();
Branch: 1 branch
BranchTrueFalseTotal
$display_cols[$_]->table ? : 71 1 72
574 }
575 }
576  
577 # IF ASTERISKED COLUMNS AND NOT NATURAL OR USING
578 # USE ALL COLUMNS, IN ORDER OF NAMING OF TABLES
579 #
580 elsif ( $display_combine eq 'NONE' )
581 {
582 0 @display_cols = @all_cols;
583 }
584  
585 # IF NATURAL, COMBINE ALL SHARED COLUMNS
586 # IF USING, COMBINE ALL KEY COLUMNS
587 #
588 else
589 {
590 16 my %is_natural;
591 16 for my $full_col (@all_cols)
592 {
593 64 my ( $table, $col ) = $full_col =~ m/^([^$self->{dlm}]+)$self->{dlm}(.+)$/;
594 64 ▶ next if ( ( $display_combine eq 'NATURAL' ) and $is_natural{$col} );
Branch: 1 branch
BranchTrueFalseTotal
if $display_combine eq "NATURAL" and $is_natural{$col} 6 58 64
Condition100%3/3
$display_combine eq "NATURAL" and $is_natural{$col}
!ll&&!rl&&r
40186
MC/DC100%2/2
$display_combine eq "NATURAL" and $is_natural{$col}
$display_combine eq "NATURAL" $is_natural{$col}
Truth table100%3/3
A and B
A= $display_combine eq "NATURAL"B= $is_natural{$col}
ABresult
0X0
100
111
595 58 ▶ next if ( ( $display_combine eq 'USING' ) && $is_natural{$col} && $is_key_col{$col} );
Branch: 1 branch
BranchTrueFalseTotal
if $display_combine eq "USING" and $is_natural{$col} and $is_key_col{$col} 10 48 58
Condition83%5/6
$display_combine eq "USING" and $is_natural{$col} and $is_key_col{$col}
!ll&&!rl&&r
48010
$display_combine eq "USING" and $is_natural{$col}
!ll&&!rl&&r
183010
MC/DC66%2/3
$display_combine eq "USING" and $is_natural{$col} and $is_key_col{$col}
$display_combine eq "USING" $is_natural{$col} $is_key_col{$col}
Truth table75%3/4
A and B and C
A= $display_combine eq "USING"B= $is_natural{$col}C= $is_key_col{$col}
ABCresult
0XX0
10X0
1100
1111
596 48 push( @display_cols, $full_col );
597 48 $is_natural{$col}++;
598 }
599 }
600 32 my @shared = ();
601 32 my %is_shared;
602 32 ▶ if ( $self->{join}->{type} =~ m/NATURAL/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'join'}{'type'} =~ /NATURAL/) { } 6 26 32
603 {
604 6 for my $full_col (@all_cols)
605 {
606 24 my ( $table, $col ) = $full_col =~ m/^([^$self->{dlm}]+)$self->{dlm}(.+)$/;
607 24 ▶ push( @shared, $col ) if ( $is_shared{$col}++ ); # using side-effect of post-inc
Branch: 1 branch
BranchTrueFalseTotal
if $is_shared{$col}++ 6 18 24
608 }
609 }
610 else
611 {
612 26 @shared = @keycols;
613 }
614 32 $self->{join}->{shared_cols} = \@shared;
615 32 $self->{join}->{display_cols} = \@display_cols;
616 }
617  
618 sub JOIN
619 34 ▶{
Subroutine JOIN: called CC=16 SCAR=28.0
Pod undocumented
620 34 my ( $self, $data, $params ) = @_;
621  
622 34 my ( $eval, $all_cols ) = $self->open_tables( $data, 0, 0 );
623 34 ▶ return undef unless $eval;
Branch: 1 branch
BranchTrueFalseTotal
unless $eval 0 34 34
624 34 $eval->params($params);
625 34 $self->verify_columns( $data, $eval, $all_cols );
626 34 ▶ return if ( $self->{errstr} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"errstr"} 2 32 34
627 32 ▶ if ( $self->{join}->{keycols}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"join"}{"keycols"} and $self->{"join"}{"table_order"} and scalar @{$self->{"join"}{"table_order"};} == 0) 7 25 32
Condition100%6/6
$self->{"join"}{"keycols"} and $self->{"join"}{"table_order"} and scalar @{$self->{"join"}{"table_order"};} == 0
!ll&&!rl&&r
1787
$self->{"join"}{"keycols"} and $self->{"join"}{"table_order"}
!ll&&!rl&&r
71015
MC/DC100%3/3
$self->{"join"}{"keycols"} and $self->{"join"}{"table_order"} and scalar @{$self->{"join"}{"table_order"};} == 0
$self->{"join"}{"keycols"} $self->{"join"}{"table_order"} scalar @{$self->{"join"}{"table_order"};} == 0
Truth table100%4/4
A and B and C
A= $self->{"join"}{"keycols"}B= $self->{"join"}{"table_order"}C= scalar @{$self->{"join"}{"table_order"};} == 0
ABCresult
0XX0
10X0
1100
1111
628 and $self->{join}->{table_order}
629 15 and ( scalar( @{ $self->{join}->{table_order} } ) == 0 ) )
630 {
631 7 $self->{join}->{table_order} = $self->order_joins( $self->{join}->{keycols} );
632 $self->{join}->{table_order} = $self->{table_names}
633 7 ▶ unless ( defined( $self->{join}->{table_order} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"join"}{"table_order"} 7 0 7
634 }
635 32 my @tables = $self->tables;
636  
637 # GET THE LIST OF QUALIFIED COLUMN NAMES FOR DISPLAY
638 # *IN ORDER BY NAMING OF TABLES*
639 #
640 32 my @all_cols;
641 32 for my $table (@tables)
642 {
643 76 my @cols = @{ $eval->table( $table->{name} )->col_names };
644 76 for my $col (@cols)
645 {
646 204 push( @all_cols, $table->{name} . $self->{dlm} . $col );
647 }
648 }
649 32 $self->find_join_columns(@all_cols);
650  
651 # JOIN THE TABLES
652 # *IN ORDER *BY JOINS*
653 #
654 32 ▶ @tables = @{ $self->{join}->{table_order} } if ( $self->{join}->{table_order} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"join"}{"table_order"} 15 17 32
655 32 my ( $tableA, $tableB ) = splice( @tables, 0, 2 );
656 32 ▶ $tableA = $tableA->{name} if ( ref($tableA) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $tableA 17 15 32
657 32 ▶ $tableB = $tableB->{name} if ( ref($tableB) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $tableB 17 15 32
658 32 my ( $tableAobj, $tableBobj ) = ( $eval->table($tableA), $eval->table($tableB) );
659 32 ▶ $tableAobj->{NAME} ||= $tableA;
Condition33%1/3
$tableAobj->{"NAME"} ||= $tableA
l!l&&r!l&&!r
3200
MC/DC0%0/2
$tableAobj->{"NAME"} ||= $tableA
$tableAobj->{"NAME"} $tableA
Truth table33%1/3
A ||= B
A= $tableAobj->{"NAME"}B= $tableA
ABresult
1X1
011
000
660 32 ▶ $tableBobj->{NAME} ||= $tableB;
Condition33%1/3
$tableBobj->{"NAME"} ||= $tableB
l!l&&r!l&&!r
3200
MC/DC0%0/2
$tableBobj->{"NAME"} ||= $tableB
$tableBobj->{"NAME"} $tableB
Truth table33%1/3
A ||= B
A= $tableBobj->{"NAME"}B= $tableB
ABresult
1X1
011
000
661 32 $self->join_2_tables( $data, $params, $tableAobj, $tableBobj );
662  
663 32 for my $next_table (@tables)
664 {
665 12 $tableAobj = $self->{join}->{table};
666 12 $tableBobj = $eval->table($next_table);
667 12 ▶ $tableBobj->{NAME} ||= $next_table;
Condition33%1/3
$tableBobj->{"NAME"} ||= $next_table
l!l&&r!l&&!r
1200
MC/DC0%0/2
$tableBobj->{"NAME"} ||= $next_table
$tableBobj->{"NAME"} $next_table
Truth table33%1/3
A ||= B
A= $tableBobj->{"NAME"}B= $next_table
ABresult
1X1
011
000
668 12 $self->join_2_tables( $data, $params, $tableAobj, $tableBobj );
669 12 $self->{cur_table} = $next_table;
670 }
671 32 return $self->SELECT( $data, $params );
672 }
673  
674 sub join_2_tables
675 44 ▶{
Subroutine join_2_tables: called CC=75 SCAR=43.9
Pod documented
676 44 my ( $self, $data, $params, $tableAobj, $tableBobj ) = @_;
677 44 my $share_type = 'IMPLICIT';
678 44 ▶ $share_type = 'NATURAL' if ( -1 != index( $self->{join}->{type}, 'NATURAL' ) );
Branch: 1 branch
BranchTrueFalseTotal
if (index($self->{"join"}{"type"}, "NATURAL") != -1) 6 38 44
679 44 ▶ $share_type = 'USING' if ( -1 != index( $self->{join}->{clause}, 'USING' ) );
Branch: 1 branch
BranchTrueFalseTotal
if (index($self->{"join"}{"clause"}, "USING") != -1) 10 34 44
680 44 ▶ $share_type = 'ON' if ( -1 != index( $self->{join}->{clause}, 'ON' ) );
Branch: 1 branch
BranchTrueFalseTotal
if (index($self->{"join"}{"clause"}, "ON") != -1) 7 37 44
681 $share_type = 'USING'
682 44 ▶ if ( ( $share_type eq 'ON' ) && ( scalar( @{ $self->{join}->{keycols} } ) == 1 ) );
Branch: 1 branch
BranchTrueFalseTotal
if $share_type eq "ON" and scalar @{$self->{"join"}{"keycols"};} == 1 7 37 44
Condition66%2/3
$share_type eq "ON" and scalar @{$self->{"join"}{"keycols"};} == 1
!ll&&!rl&&r
3707
MC/DC50%1/2
$share_type eq "ON" and scalar @{$self->{"join"}{"keycols"};} == 1
$share_type eq "ON" scalar @{$self->{"join"}{"keycols"};} == 1
Truth table66%2/3
A and B
A= $share_type eq "ON"B= scalar @{$self->{"join"}{"keycols"};} == 1
ABresult
0X0
100
111
683 44 my $join_type = 'INNER';
684 44 ▶ $join_type = 'LEFT' if ( -1 != index( $self->{join}->{type}, 'LEFT' ) );
Branch: 1 branch
BranchTrueFalseTotal
if (index($self->{"join"}{"type"}, "LEFT") != -1) 9 35 44
685 44 ▶ $join_type = 'RIGHT' if ( -1 != index( $self->{join}->{type}, 'RIGHT' ) );
Branch: 1 branch
BranchTrueFalseTotal
if (index($self->{"join"}{"type"}, "RIGHT") != -1) 3 41 44
686 44 ▶ $join_type = 'FULL' if ( -1 != index( $self->{join}->{type}, 'FULL' ) );
Branch: 1 branch
BranchTrueFalseTotal
if (index($self->{"join"}{"type"}, "FULL") != -1) 2 42 44
687  
688 44 my $right_join = $join_type eq 'RIGHT';
689 44 ▶ if ($right_join)
Branch: 1 branch
BranchTrueFalseTotal
if ($right_join) 3 41 44
690 {
691 3 my $tmpTbl = $tableAobj;
692 3 $tableAobj = $tableBobj;
693 3 $tableBobj = $tmpTbl;
694 }
695  
696 44 my $tableA = $tableAobj->{NAME};
697 44 ▶ ( 0 != index( $tableA, '"' ) ) and $tableA = lc $tableA;
Branch: 1 branch
BranchTrueFalseTotal
0 != index($tableA, "\"") and $tableA = lc $tableA 44 0 44
698 44 my $tableB = $tableBobj->{NAME};
699 44 ▶ ( 0 != index( $tableB, '"' ) ) and $tableB = lc $tableB;
Branch: 1 branch
BranchTrueFalseTotal
0 != index($tableB, "\"") and $tableB = lc $tableB 44 0 44
700 44 my @colsA = @{ $tableAobj->col_names() };
701 44 my @colsB = @{ $tableBobj->col_names() };
702 44 my ( %isunqualA, %isunqualB, @shared_cols );
703 44 $isunqualB{ $colsB[$_] } = 1 for ( 0 .. $#colsB );
704 44 my @tmpshared = @{ $self->{join}->{shared_cols} };
705  
706 44 ▶ if ( $share_type eq 'ON' )
Branch: 3 branches
BranchTrueFalseTotal
if ($share_type eq 'ON') { } 0 44 44
elsif ($share_type eq 'USING') { } 17 27 44
elsif ($share_type eq 'NATURAL') { } 6 21 27
707 {
708 0 ▶ $right_join and @tmpshared = reverse @tmpshared;
Branch: 1 branch
BranchTrueFalseTotal
$right_join and @tmpshared = reverse @tmpshared 0 0 0
709 }
710 elsif ( $share_type eq 'USING' )
711 {
712 17 foreach my $c (@tmpshared)
713 {
714 17 substr( $c, 0, index( $c, $self->{dlm} ) + 1 ) = '';
715 17 push( @shared_cols, $tableA . $self->{dlm} . $c );
716 17 push( @shared_cols, $tableB . $self->{dlm} . $c );
717 }
718 }
719 elsif ( $share_type eq 'NATURAL' )
720 {
721 6 for my $c (@colsA)
722 {
723 12 ▶ if ( $tableA eq $self->{dlm} . 'tmp' )
Branch: 1 branch
BranchTrueFalseTotal
if ($tableA eq $self->{"dlm"} . 'tmp') 0 12 12
724 {
725 0 substr( $c, 0, index( $c, $self->{dlm} ) + 1 ) = '';
726 }
727 12 ▶ if ( $isunqualB{$c} )
Branch: 1 branch
BranchTrueFalseTotal
if ($isunqualB{$c}) 6 6 12
728 {
729 6 push( @shared_cols, $tableA . $self->{dlm} . $c );
730 6 push( @shared_cols, $tableB . $self->{dlm} . $c );
731 }
732 }
733 }
734  
735 44 my %whichqual;
736 44 ▶ if ( $share_type eq 'ON' || $share_type eq 'IMPLICIT' )
Branch: 1 branch
BranchTrueFalseTotal
if ($share_type eq 'ON' or $share_type eq 'IMPLICIT') { } 21 23 44
Condition66%2/3
$share_type eq "ON" or $share_type eq "IMPLICIT"
l!l&&r!l&&!r
02123
MC/DC50%1/2
$share_type eq "ON" or $share_type eq "IMPLICIT"
$share_type eq "ON" $share_type eq "IMPLICIT"
Truth table66%2/3
A or B
A= $share_type eq "ON"B= $share_type eq "IMPLICIT"
ABresult
1X1
011
000
737 {
738 21 foreach my $colb (@colsB)
739 {
740 77 $colb = $whichqual{$colb} = $tableB . $self->{dlm} . $colb;
741 }
742 }
743 else
744 {
745 23 @colsB = map { $tableB . $self->{dlm} . $_ } @colsB;
746 }
747  
748 44 my @all_cols = map { $tableA . $self->{dlm} . $_ } @colsA;
749 44 ▶ @all_cols = $right_join ? ( @colsB, @all_cols ) : ( @all_cols, @colsB );
Branch: 1 branch
BranchTrueFalseTotal
$right_join ? : 3 41 44
750 {
751 44 my $str = $self->{dlm} . "tmp" . $self->{dlm};
752 44 foreach (@all_cols)
753 {
754 343 my $pos = index( $_, $str );
755 343 ▶ $pos >= 0 and substr( $_, $pos, length($str) ) = '';
Branch: 1 branch
BranchTrueFalseTotal
$pos >= 0 and substr($_, $pos, length $str) = "" 139 204 343
756 }
757 }
758 44 ▶ if ( $tableA eq $self->{dlm} . 'tmp' )
Branch: 1 branch
BranchTrueFalseTotal
if ($tableA eq $self->{'dlm'} . 'tmp') { } 12 32 44
759 {
760 12 foreach my $colA (@colsA)
761 {
762 139 my $c = substr( $colA, index( $colA, $self->{dlm} ) + 1 );
763 139 $isunqualA{$c} = $colA;
764 }
765 #%isunqualA =
766 # map { my ($c) = $_ =~ m/^(?:[^$self->{dlm}]+)$self->{dlm}(.+)$/; $c => $_ } @colsA;
767 }
768 else
769 {
770 32 foreach my $cola (@colsA)
771 {
772 81 $cola = $isunqualA{$cola} = $tableA . $self->{dlm} . $cola;
773 }
774 }
775  
776 44 my ( %col_numsA, %col_numsB );
777 44 $col_numsA{ $colsA[$_] } = $_ for ( 0 .. $#colsA );
778 44 $col_numsB{ $colsB[$_] } = $_ for ( 0 .. $#colsB );
779  
780 44 ▶ if ( $share_type eq 'ON' || $share_type eq 'IMPLICIT' )
Branch: 1 branch
BranchTrueFalseTotal
if ($share_type eq "ON" or $share_type eq "IMPLICIT") 21 23 44
Condition66%2/3
$share_type eq "ON" or $share_type eq "IMPLICIT"
l!l&&r!l&&!r
02123
MC/DC50%1/2
$share_type eq "ON" or $share_type eq "IMPLICIT"
$share_type eq "ON" $share_type eq "IMPLICIT"
Truth table66%2/3
A or B
A= $share_type eq "ON"B= $share_type eq "IMPLICIT"
ABresult
1X1
011
000
781 {
782 21 %whichqual = ( %whichqual, %isunqualA );
783  
784 21 ▶ while (@tmpshared)
Branch: 1 branch
BranchTrueFalseTotal
if (@tmpshared) 62 21 83
785 {
786 62 my ( $k1, $k2 ) = splice( @tmpshared, 0, 2 );
787  
788 # if both keys are in one table, bail out - FIXME: errmsg?
789 62 ▶ next if ( $isunqualA{$k1} && $isunqualA{$k2} );
Branch: 1 branch
BranchTrueFalseTotal
if $isunqualA{$k1} and $isunqualA{$k2} 0 62 62
Condition33%1/3
$isunqualA{$k1} and $isunqualA{$k2}
!ll&&!rl&&r
6200
MC/DC0%0/2
$isunqualA{$k1} and $isunqualA{$k2}
$isunqualA{$k1} $isunqualA{$k2}
Truth table33%1/3
A and B
A= $isunqualA{$k1}B= $isunqualA{$k2}
ABresult
0X0
100
111
790 62 ▶ next if ( $isunqualB{$k1} && $isunqualB{$k2} );
Branch: 1 branch
BranchTrueFalseTotal
if $isunqualB{$k1} and $isunqualB{$k2} 0 62 62
Condition33%1/3
$isunqualB{$k1} and $isunqualB{$k2}
!ll&&!rl&&r
6200
MC/DC0%0/2
$isunqualB{$k1} and $isunqualB{$k2}
$isunqualB{$k1} $isunqualB{$k2}
Truth table33%1/3
A and B
A= $isunqualB{$k1}B= $isunqualB{$k2}
ABresult
0X0
100
111
791  
792 62 ▶ defined( $whichqual{$k1} ) and $k1 = $whichqual{$k1};
Branch: 1 branch
BranchTrueFalseTotal
defined $whichqual{$k1} and $k1 = $whichqual{$k1} 0 62 62
793 62 ▶ defined( $whichqual{$k2} ) and $k2 = $whichqual{$k2};
Branch: 1 branch
BranchTrueFalseTotal
defined $whichqual{$k2} and $k2 = $whichqual{$k2} 0 62 62
794  
795 62 ▶ if ( defined( $col_numsA{$k1} ) && defined( $col_numsB{$k2} ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $col_numsA{$k1} and defined $col_numsB{$k2}) { } 17 45 62
elsif (defined $col_numsA{$k2} and defined $col_numsB{$k1}) { } 3 57 60
Condition100%3/3
defined $col_numsA{$k1} and defined $col_numsB{$k2}
!ll&&!rl&&r
232217
MC/DC100%2/2
defined $col_numsA{$k1} and defined $col_numsB{$k2}
defined $col_numsA{$k1} defined $col_numsB{$k2}
Truth table100%3/3
A and B
A= defined $col_numsA{$k1}B= defined $col_numsB{$k2}
ABresult
0X0
100
111
796 {
797 17 push( @shared_cols, $k1, $k2 );
798 }
799 ▶ elsif ( defined( $col_numsA{$k2} ) && defined( $col_numsB{$k1} ) )
Condition100%3/3
defined $col_numsA{$k2} and defined $col_numsB{$k1}
!ll&&!rl&&r
15273
MC/DC100%2/2
defined $col_numsA{$k2} and defined $col_numsB{$k1}
defined $col_numsA{$k2} defined $col_numsB{$k1}
Truth table100%3/3
A and B
A= defined $col_numsA{$k2}B= defined $col_numsB{$k1}
ABresult
0X0
100
111
800 {
801 3 push( @shared_cols, $k2, $k1 );
802 }
803 }
804 }
805  
806 44 my %is_shared;
807 44 for my $c (@shared_cols)
808 {
809 86 $is_shared{$c} = 1;
810 defined( $col_numsA{$c} )
811 86 ▶ or defined( $col_numsB{$c} )
Branch: 1 branch
BranchTrueFalseTotal
defined $col_numsA{$c} or defined $col_numsB{$c} or return $self->do_err("Can't find shared columns!") 86 0 86
Condition66%2/3
defined $col_numsA{$c} or defined $col_numsB{$c}
l!l&&r!l&&!r
43430
MC/DC0%0/2
defined $col_numsA{$c} or defined $col_numsB{$c}
defined $col_numsA{$c} defined $col_numsB{$c}
Truth table66%2/3
A or B
A= defined $col_numsA{$c}B= defined $col_numsB{$c}
ABresult
1X1
011
000
812 or return $self->do_err("Can't find shared columns!");
813 }
814 44 my ( $posA, $posB ) = ( [], [] );
815 44 for my $f (@shared_cols)
816 {
817 86 ▶ defined( $col_numsA{$f} ) and push( @{$posA}, $col_numsA{$f} );
Branch: 1 branch
BranchTrueFalseTotal
defined $col_numsA{$f} and push @{$posA;}, $col_numsA{$f} 43 43 86
818 86 ▶ defined( $col_numsB{$f} ) and push( @{$posB}, $col_numsB{$f} );
Branch: 1 branch
BranchTrueFalseTotal
defined $col_numsB{$f} and push @{$posB;}, $col_numsB{$f} 43 43 86
819 }
820  
821 44 my $is_inner_join = $join_type eq "INNER";
822 #use mylibs; zwarn $self->{join};
823 # CYCLE THROUGH TABLE B, CREATING A HASH OF ITS VALUES
824 #
825 44 my $hashB = {};
826 44 ▶ TBLBFETCH: while ( my $array = $tableBobj->fetch_row($data) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $array = $tableBobj->fetch_row($data)) 294 44 338
827 {
828 294 my @key_vals = @$array[@$posB];
829 294 ▶ if ($is_inner_join)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_inner_join) 247 47 294
830 {
831 247 ▶ defined($_) or next TBLBFETCH for (@key_vals);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ or next TBLBFETCH 244 0 244
832 }
833 294 my $hashkey = join( ' ', @key_vals );
834 294 push( @{ $hashB->{$hashkey} }, $array );
835 }
836  
837 # CYCLE THROUGH TABLE A
838 #
839 44 my $blankRow;
840 44 my $joined_table = [];
841 44 my %visited;
842 44 ▶ TBLAFETCH: while ( my $arrayA = $tableAobj->fetch_row($data) ) # use tbl1st & tbl2nd
Branch: 1 branch
BranchTrueFalseTotal
if (my $arrayA = $tableAobj->fetch_row($data)) 355 44 399
843 {
844 355 my @key_vals = @$arrayA[@$posA];
845 355 ▶ if ($is_inner_join)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_inner_join) 311 44 355
846 {
847 311 ▶ defined($_) or next TBLAFETCH for (@key_vals);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ or next TBLAFETCH 308 0 308
848 }
849 355 my $hashkey = join( ' ', @key_vals );
850 355 my $rowsB = $hashB->{$hashkey};
851 355 ▶ if ( !defined($rowsB) && ( $join_type ne 'INNER' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rowsB and $join_type ne "INNER") 14 341 355
Condition100%3/3
not defined $rowsB and $join_type ne "INNER"
!ll&&!rl&&r
3043714
MC/DC100%2/2
not defined $rowsB and $join_type ne "INNER"
not defined $rowsB $join_type ne "INNER"
Truth table100%3/3
not(A) and B
A= not defined $rowsBB= $join_type ne "INNER"
ABresult
0X0
100
111
852 {
853 14 ▶ defined($blankRow) or $blankRow = [ (undef) x scalar(@colsB) ];
Branch: 1 branch
BranchTrueFalseTotal
defined $blankRow or $blankRow = [(undef) x scalar(@colsB)] 0 14 14
854 14 $rowsB = [$blankRow];
855 }
856  
857 355 ▶ if ( $join_type ne 'UNION' )
Branch: 1 branch
BranchTrueFalseTotal
if ($join_type ne "UNION") 355 0 355
858 {
859 355 for my $arrayB ( @{$rowsB} )
860 {
861 404 ▶ my $newRow = $right_join ? [ @{$arrayB}, @{$arrayA} ] : [ @{$arrayA}, @{$arrayB} ];
Branch: 1 branch
BranchTrueFalseTotal
$right_join ? : 11 393 404
862  
863 404 push( @$joined_table, $newRow );
864 }
865 }
866  
867 355 ++$visited{$hashkey};
868 }
869  
870 # ADD THE LEFTOVER B ROWS IF NEEDED
871 #
872 44 ▶ if ( $join_type eq 'FULL' || $join_type eq 'UNION' )
Branch: 1 branch
BranchTrueFalseTotal
if ($join_type eq "FULL" or $join_type eq "UNION") 2 42 44
Condition66%2/3
$join_type eq "FULL" or $join_type eq "UNION"
l!l&&r!l&&!r
2042
MC/DC50%1/2
$join_type eq "FULL" or $join_type eq "UNION"
$join_type eq "FULL" $join_type eq "UNION"
Truth table66%2/3
A or B
A= $join_type eq "FULL"B= $join_type eq "UNION"
ABresult
1X1
011
000
873 {
874 my $st_is_NaturalOrUsing = ( -1 != index( $self->{join}->{type}, 'NATURAL' ) )
875 2 ▶ || ( -1 != index( $self->{join}->{clause}, 'USING' ) );
Condition66%2/3
(index($self->{'join'}{'type'}, 'NATURAL') != -1) || (index($self->{'join'}{'clause'}, 'USING') != -1)
l!l&&r!l&&!r
011
MC/DC50%1/2
(index($self->{'join'}{'type'}, 'NATURAL') != -1) || (index($self->{'join'}{'clause'}, 'USING') != -1)
(index($self->{'join'}{'type'}, 'NATURAL') != -1) (index($self->{'join'}{'clause'}, 'USING') != -1)
Truth table66%2/3
A || B
A= (index($self->{'join'}{'type'}, 'NATURAL') != -1)B= (index($self->{'join'}{'clause'}, 'USING') != -1)
ABresult
1X1
011
000
876 2 ▶ while ( my ( $k, $v ) = each %{$hashB} )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$hashB;}) 6 2 8
877 {
878 6 ▶ next if ( $visited{$k} );
Branch: 1 branch
BranchTrueFalseTotal
if $visited{$k} 4 2 6
879 2 for my $rowB (@$v)
880 {
881 2 my ( @arrayA, @tmpB, $rowhash );
882 2 @{$rowhash}{@colsB} = @{$rowB};
883 2 for my $c (@all_cols)
884 {
885 8 my ( $table, $col ) = split( $self->{dlm}, $c, 2 );
886 8 ▶ push( @arrayA, undef ) if ( $table eq $tableA );
Branch: 1 branch
BranchTrueFalseTotal
if $table eq $tableA 4 4 8
887 8 ▶ push( @tmpB, $rowhash->{$c} ) if ( $table eq $tableB );
Branch: 1 branch
BranchTrueFalseTotal
if $table eq $tableB 4 4 8
888 }
889 2 ▶ @arrayA[@$posA] = @tmpB[@$posB] if ($st_is_NaturalOrUsing);
Branch: 1 branch
BranchTrueFalseTotal
if $st_is_NaturalOrUsing 1 1 2
890 2 my $newRow = [ @arrayA, @tmpB ];
891 2 push( @{$joined_table}, $newRow );
892 }
893 }
894 }
895  
896 44 undef $hashB;
897 44 undef $tableAobj;
898 44 undef $tableBobj;
899  
900 $self->{join}->{table} =
901 44 SQL::Statement::TempTable->new( $self->{dlm} . 'tmp', \@all_cols, $self->{join}->{display_cols}, $joined_table );
902  
903 44 return;
904 }
905  
906 sub run_functions
907 176 ▶{
Subroutine run_functions: called CC=2 SCAR=6.9
Pod documented
908 176 my ( $self, $data, $params ) = @_;
909 176 my ( $eval, $all_cols ) = $self->open_tables( $data, 0, 0 );
910 176 my @row = ();
911 176 for my $col ( $self->columns() )
912 {
913 176 my $val = $col->value($eval); # FIXME approve
914 176 push( @row, $val );
915 }
916 176 return ( 1, scalar @row, [ \@row ] );
917 }
918  
919 sub SELECT($$)
920 348 ▶{
Subroutine SELECT: called CC=54 SCAR=40.5
Pod undocumented
921 348 my ( $self, $data, $params ) = @_;
922  
923 348 ▶ $self->{params} ||= $params;
Condition66%2/3
$self->{"params"} ||= $params
l!l&&r!l&&!r
329019
MC/DC50%1/2
$self->{"params"} ||= $params
$self->{"params"} $params
Truth table66%2/3
A ||= B
A= $self->{"params"}B= $params
ABresult
1X1
011
000
924 348 ▶ defined( _ARRAY( $self->{table_names} ) ) or return $self->run_functions( $data, $params );
Branch: 1 branch
BranchTrueFalseTotal
defined &_ARRAY($self->{"table_names"}) or return $self->run_functions($data, $params) 172 176 348
925  
926 172 my ( $eval, $all_cols, $tableName, $table );
927 172 ▶ if ( defined( $self->{join} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'join'}) { } 66 106 172
928 {
929 66 ▶ defined $self->{join}->{table} or return $self->JOIN( $data, $params );
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"join"}{"table"} or return $self->JOIN($data, $params) 32 34 66
930 32 $tableName = $self->{dlm} . 'tmp';
931 32 $table = $self->{join}->{table};
932 }
933 else
934 {
935 106 ( $eval, $all_cols ) = $self->open_tables( $data, 0, 0 );
936 105 ▶ return unless $eval;
Branch: 1 branch
BranchTrueFalseTotal
unless $eval 1 104 105
937 104 $eval->params($params);
938 104 $self->verify_columns( $data, $eval, $all_cols );
939 104 ▶ return if ( $self->{errstr} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"errstr"} 1 103 104
940 103 $tableName = $self->tables(0)->name();
941 103 $table = $eval->table($tableName);
942 }
943  
944 135 my $rows = [];
945  
946 # In a loop, build the list of columns to retrieve; this will be
947 # used both for fetching data and ordering.
948 135 my ( $cList, $col, $tbl, $ar, $i, $c );
949 135 my $numFields = 0;
950 135 my %columns;
951 my @names;
952 135 my %funcs = ();
953  
954 #
955 # DAA
956 #
957 # lets just disable this and see where it leads...
958 #
959 # if ($self->{join}) {
960 # @names = @{ $table->col_names };
961 # for my $col(@names) {
962 # $columns{$tableName}->{"$col"} = $numFields++;
963 # push(@$cList, $table->column_num($col));
964 # }
965 # }
966 # else {
967 135 foreach my $column ( $self->columns() )
968 {
969 313 ▶ if ( _INSTANCE( $column, 'SQL::Statement::Param' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (&_INSTANCE($column, 'SQL::Statement::Param')) { } 0 313 313
970 {
971 0 my $val = $eval->param( $column->idx() );
972 0 ▶ if ( -1 != ( my $idx = index( $val, '.' ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (-1 != (my $idx = index($val, '.'))) { } 0 0 0
973 {
974 0 $col = substr( $val, 0, $idx );
975 0 $tbl = substr( $val, $idx + 1 );
976 }
977 else
978 {
979 0 $col = $val;
980 0 $tbl = $tableName;
981 }
982 0 ▶ $tbl ||= '';
Condition0%0/2
$tbl ||= ""
l!l
00
MC/DC0%0/1
$tbl ||= ""
$tbl
Truth table0%0/2
A
A= $tbl
Aresult
11
00
983 0 $columns{$tbl}->{$col} = $numFields++;
984 }
985 else
986 {
987 313 ( $col, $tbl ) = ( $column->name(), $column->table() );
988 313 ▶ $tbl ||= '';
Condition100%2/2
$tbl ||= ""
l!l
28033
MC/DC100%1/1
$tbl ||= ""
$tbl
Truth table100%2/2
A
A= $tbl
Aresult
11
00
989 313 $columns{$tbl}->{ $column->display_name() } = $columns{$tbl}->{$col} = $numFields++;
990 }
991  
992 #
993 # handle functions in select list
994 #
995 # DAA
996 #
997 # check for a join temp table; if so, check if we can locate
998 # the column in its delimited set
999 #
1000 my $cnum =
1001 ( ( $tableName eq ( $self->{dlm} . 'tmp' ) ) && ( $tbl ne '' ) )
1002 313 ▶ ? $table->column_num( $tbl . $self->{dlm} . $col )
Branch: 1 branch
BranchTrueFalseTotal
$tableName eq $self->{'dlm'} . 'tmp' && $tbl ne '' ? : 118 195 313
Condition100%3/3
$tableName eq $self->{"dlm"} . 'tmp' and $tbl ne ""
!ll&&!rl&&r
1914118
MC/DC100%2/2
$tableName eq $self->{"dlm"} . 'tmp' and $tbl ne ""
$tableName eq $self->{"dlm"} . 'tmp' $tbl ne ""
Truth table100%3/3
A and B
A= $tableName eq $self->{"dlm"} . 'tmp'B= $tbl ne ""
ABresult
0X0
100
111
1003 : $table->column_num($col);
1004  
1005 313 ▶ if ( !defined $cnum || $column->{function} )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cnum or $column->{"function"}) 33 280 313
Condition66%2/3
not defined $cnum or $column->{"function"}
l!l&&r!l&&!r
330280
MC/DC50%1/2
not defined $cnum or $column->{"function"}
not defined $cnum $column->{"function"}
Truth table66%2/3
not(A) or B
A= not defined $cnumB= $column->{"function"}
ABresult
1X1
011
000
1006 {
1007 33 $funcs{$col} = $column->{function};
1008 33 $cnum = $col;
1009 }
1010 313 push( @$cList, $cnum );
1011  
1012 # push(@$cList, $table->column_num($col));
1013 313 push( @names, $col );
1014 }
1015  
1016 # }
1017 135 ▶ $cList = [] unless ( defined($cList) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cList 0 135 135
1018 135 ▶ if ( $self->{join} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"join"}) 32 103 135
1019 {
1020 32 foreach (@names) { $_ =~ s/^[^$self->{dlm}]+$self->{dlm}//; }
1021 }
1022 135 $self->{NAME} = \@names;
1023 # $self->verify_order_cols($table);
1024 135 my @order_by = $self->order();
1025 135 my @extraSortCols = ();
1026  
1027 135 ▶ if (@order_by)
Branch: 1 branch
BranchTrueFalseTotal
if (@order_by) 17 118 135
1028 {
1029 17 my $nFields = $numFields;
1030  
1031 # It is possible that the user gave an ORDER BY clause with columns
1032 # that are not part of $cList yet. These columns will need to be
1033 # present in the array of arrays for sorting, but will be stripped
1034 # off later.
1035 17 my $i = -1;
1036 17 foreach my $column (@order_by)
1037 {
1038 20 ++$i;
1039 20 ( $col, $tbl ) = ( $column->column(), $column->table() );
1040 20 my $pos;
1041 20 ▶ $tbl ||= $self->colname2table($col);
Condition66%2/3
$tbl ||= $self->colname2table($col)
l!l&&r!l&&!r
1901
MC/DC50%1/2
$tbl ||= $self->colname2table($col)
$tbl $self->colname2table($col)
Truth table66%2/3
A ||= B
A= $tblB= $self->colname2table($col)
ABresult
1X1
011
000
1042 20 ▶ $tbl ||= '';
Condition100%2/2
$tbl ||= ""
l!l
191
MC/DC100%1/1
$tbl ||= ""
$tbl
Truth table100%2/2
A
A= $tbl
Aresult
11
00
1043 20 ▶ if ( $self->{join} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"join"}) 6 14 20
1044 {
1045 6 $pos = $table->column_num( $tbl . $self->{dlm} . $col );
1046 6 ▶ defined($pos)
Branch: 1 branch
BranchTrueFalseTotal
defined $pos or $pos = $table->column_num($tbl . '_' . $col) 5 1 6
1047 or $pos = $table->column_num( $tbl . '_' . $col );
1048 }
1049 20 ▶ next if ( exists( $columns{$tbl}->{$col} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $columns{$tbl}{$col} 19 1 20
1050 1 ▶ $pos = $table->column_num($col) unless ( defined($pos) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 1 0 1
1051 1 push( @extraSortCols, $pos );
1052 1 $columns{$tbl}->{$col} = $nFields++;
1053 }
1054 }
1055  
1056 135 ▶ my $e = $self->{join} ? $table : $eval;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'join'} ? : 32 103 135
1057  
1058 # begin count for limiting if there's a limit clause and no order clause
1059 #
1060 135 ▶ my $limit_count = 0 if ( $self->limit() and !$self->order() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->limit and not $self->order 1 134 135
Condition100%3/3
$self->limit and not $self->order
!ll&&!rl&&r
13311
MC/DC100%2/2
$self->limit and not $self->order
$self->limit not $self->order
Truth table100%3/3
A and not(B)
A= $self->limitB= not $self->order
ABresult
0X0
100
111
1061 135 my $limit = $self->limit();
1062 135 my $row_count = 0;
1063 135 ▶ my $offset = $self->offset() || 0;
Condition100%2/2
$self->offset || 0
l!l
2133
MC/DC100%1/1
$self->offset || 0
$self->offset
Truth table100%2/2
A
A= $self->offset
Aresult
11
00
1064 135 ▶ while ( my $array = $table->fetch_row($data) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $array = $table->fetch_row($data)) 8673 134 8807
1065 {
1066 8673 ▶ if ( $self->eval_where( $e, $tableName, $array, \%funcs ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->eval_where($e, $tableName, $array, \%funcs)) 8432 241 8673
1067 {
1068 8432 ▶ next if ( defined($limit_count) and ( $row_count++ < $offset ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit_count and $row_count++ < $offset 5 8427 8432
Condition100%3/3
defined $limit_count and $row_count++ < $offset
!ll&&!rl&&r
842435
MC/DC100%2/2
defined $limit_count and $row_count++ < $offset
defined $limit_count $row_count++ < $offset
Truth table100%3/3
A and B
A= defined $limit_countB= $row_count++ < $offset
ABresult
0X0
100
111
1069  
1070 8427 my @row = map { $_->value($e) } $self->columns();
1071 8427 push( @{$rows}, \@row );
1072  
1073 # We quit here if there's a limit clause without order clause
1074 # and the limit has been reached
1075 8427 ▶ defined($limit_count)
Branch: 1 branch
BranchTrueFalseTotal
defined $limit_count and ++$limit_count >= $limit and return $limit, $numFields, $rows 1 8426 8427
Condition100%3/3
defined $limit_count and ++$limit_count >= $limit
!ll&&!rl&&r
842421
MC/DC100%2/2
defined $limit_count and ++$limit_count >= $limit
defined $limit_count ++$limit_count >= $limit
Truth table100%3/3
A and B
A= defined $limit_countB= ++$limit_count >= $limit
ABresult
0X0
100
111
1076 and ( ++$limit_count >= $limit )
1077 and return ( $limit, $numFields, $rows );
1078 }
1079 }
1080  
1081 134 ▶ if ( $self->distinct() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->distinct) 5 129 134
1082 {
1083 5 my %seen;
1084 5 @{$rows} = map {
1085 33 ▶ $seen{ join( "\0", ( map { defined($_) ? $_ : '' } @{$_} ) ) }++
Branch: 2 branches
BranchTrueFalseTotal
$seen{join "\000", map({defined $_ ? $_ : '';} @{$_;})}++ ? : 13 20 33
defined $_ ? : 89 0 89
1086 ? ()
1087 : $_
1088 5 } @{$rows};
1089 }
1090  
1091 134 ▶ if ( $self->{has_set_functions} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"has_set_functions"}) 19 115 134
1092 {
1093 19 my $aggreg;
1094 19 ▶ if ( $self->{group_by} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'group_by'}) { } 7 12 19
1095 {
1096 7 my @keycols = @{ $self->{colpos} }{ @{ $self->{group_by} } };
1097 7 $aggreg = SQL::Statement::Group->new( $self, $rows, \@keycols );
1098 }
1099 else
1100 {
1101 12 $aggreg = SQL::Statement::Aggregate->new( $self, $rows );
1102 }
1103 19 $rows = $aggreg->calc();
1104 # FIXME re-order if order_by
1105 }
1106  
1107 134 ▶ if (@order_by)
Branch: 1 branch
BranchTrueFalseTotal
if (@order_by) 17 117 134
1108 {
1109 16 ▶ use sort 'stable';
Subroutine BEGIN: called CC=1 SCAR=0.0
1110 my @sortCols = map {
1111 17 my ( $col, $tbl ) = ( $_->column(), $_->table() );
1112 20 ▶ $self->{join} and $table->is_shared($col) and $tbl = 'shared';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"join"} and $table->is_shared($col) and $tbl = "shared" 0 20 20
Condition66%2/3
$self->{"join"} and $table->is_shared($col)
!ll&&!rl&&r
1460
MC/DC0%0/2
$self->{"join"} and $table->is_shared($col)
$self->{"join"} $table->is_shared($col)
Truth table66%2/3
A and B
A= $self->{"join"}B= $table->is_shared($col)
ABresult
0X0
100
111
1113 20 ▶ $tbl ||= $self->colname2table($col) || '';
Condition60%3/5
$tbl ||= $self->colname2table($col) || ""
l!l&&r!l&&!r
1901
$self->colname2table($col) || ''
l!l
01
MC/DC50%1/2
$tbl ||= $self->colname2table($col) || ""
$tbl $self->colname2table($col)
Truth table66%2/3
A ||= B
A= $tblB= $self->colname2table($col)
ABresult
1X1
011
000
1114 20 ( $columns{$tbl}->{$col}, $_->desc() )
1115 } @order_by;
1116  
1117 17 $i = scalar(@sortCols);
1118 do
1119 17 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$i > 0 and do { do { my $desc = $sortCols[--$i]; my $colNum = $sortCols[--$i]; @{$rows;} = (sort {my $result; $result = &_anycmp($SQL::Statement::a->[$colNum], $SQL::Statement::b->[$colNum]); $desc and $result = -$result; $result;} @{$rows;}) } } 3 17 20
1120 20 my $desc = $sortCols[ --$i ];
1121 20 my $colNum = $sortCols[ --$i ];
1122 20 @{$rows} = sort {
1123 132 my $result;
1124 132 $result = _anycmp( $a->[$colNum], $b->[$colNum] );
1125 132 ▶ $desc and $result = -$result;
Branch: 1 branch
BranchTrueFalseTotal
$desc and $result = -$result 56 76 132
1126 $result;
1127 20 } @{$rows};
1128 } while ( $i > 0 );
1129 16 ▶ use sort 'defaults'; # for perl < 5.10.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1130 }
1131  
1132 134 ▶ if ( defined( $self->limit() ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->limit) 1 133 134
1133 {
1134 1 ▶ my $offset = $self->offset() || 0;
Condition50%1/2
$self->offset || 0
l!l
10
MC/DC0%0/1
$self->offset || 0
$self->offset
Truth table50%1/2
A
A= $self->offset
Aresult
11
00
1135 1 ▶ my $limit = $self->limit() || 0;
Condition50%1/2
$self->limit || 0
l!l
10
MC/DC0%0/1
$self->limit || 0
$self->limit
Truth table50%1/2
A
A= $self->limit
Aresult
11
00
1136 1 @{$rows} = splice( @{$rows}, $offset, $limit );
1137 }
1138  
1139 # Rip off columns that have been added for @extraSortCols only
1140 134 ▶ if (@extraSortCols)
Branch: 1 branch
BranchTrueFalseTotal
if (@extraSortCols) 1 133 134
1141 {
1142 1 foreach my $row ( @{$rows} )
1143 {
1144 4 splice( @{$row}, $numFields, scalar(@extraSortCols) );
1145 }
1146 }
1147  
1148 134 ( scalar( @{$rows} ), $numFields, $rows );
1149 }
1150  
1151 sub _anycmp($$;$)
1152 32226 ▶{
Subroutine _anycmp: called CC=7 SCAR=21.2
1153 32226 my ( $a, $b, $case_fold ) = @_;
1154  
1155 32226 ▶ if ( !defined($a) || !defined($b) )
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $a && defined $b) { } 4 32222 32226
elsif (&looks_like_number($a) and &looks_like_number($b)) { } 24164 8058 32222
Condition66%2/3
defined $a && defined $b
!ll&&!rl&&r
4032222
MC/DC50%1/2
defined $a && defined $b
defined $a defined $b
Truth table66%2/3
A && B
A= defined $aB= defined $b
ABresult
0X0
100
111
1156 {
1157 4 return defined($a) - defined($b);
1158 }
1159 ▶ elsif ( looks_like_number($a) && looks_like_number($b) )
Condition66%2/3
&looks_like_number($a) and &looks_like_number($b)
!ll&&!rl&&r
8058024164
MC/DC50%1/2
&looks_like_number($a) and &looks_like_number($b)
&looks_like_number($a) &looks_like_number($b)
Truth table66%2/3
A and B
A= &looks_like_number($a)B= &looks_like_number($b)
ABresult
0X0
100
111
1160 {
1161 24164 return $a <=> $b;
1162 }
1163 else
1164 {
1165 8058 ▶ return $case_fold ? lc($a) cmp lc($b) || $a cmp $b : $a cmp $b;
Branch: 1 branch
BranchTrueFalseTotal
$case_fold ? : 0 8058 8058
Condition0%0/3
lc $a cmp lc $b or $a cmp $b
l!l&&r!l&&!r
000
MC/DC0%0/2
lc $a cmp lc $b or $a cmp $b
lc $a cmp lc $b $a cmp $b
Truth table0%0/3
A or B
A= lc $a cmp lc $bB= $a cmp $b
ABresult
1X1
011
000
1166 }
1167 }
1168  
1169 sub eval_where
1170 8723 ▶{
Subroutine eval_where: called CC=2 SCAR=6.9
Pod documented
1171 8723 my ( $self, $eval, $tname, $rowary ) = @_;
1172 8723 ▶ return 1 unless ( defined( $self->{where_terms} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"where_terms"} 8265 458 8723
1173 458 $self->{argnum} = 0;
1174  
1175 458 return $self->{where_terms}->value($eval);
1176 }
1177  
1178 sub fetch_row
1179 123 ▶{
Subroutine fetch_row: called CC=4 SCAR=13.9
Pod documented
1180 123 my ($self) = @_;
1181 123 ▶ $self->{data} ||= [];
Condition50%1/2
$self->{"data"} ||= []
l!l
1230
MC/DC0%0/1
$self->{"data"} ||= []
$self->{"data"}
Truth table50%1/2
A
A= $self->{"data"}
Aresult
11
00
1182 123 my $row = shift @{ $self->{data} };
1183 123 ▶ return unless $row and scalar @$row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row and scalar @$row 30 93 123
Condition100%2/2
$row and scalar @$row
!ll
3093
MC/DC100%1/1
$row and scalar @$row
$row
Truth table100%2/2
A
A= $row
Aresult
00
11
1184 93 return $row;
1185 }
1186  
1187 16 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
1188 16 *fetch = \&fetch_row;
1189  
1190 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1191  
1192 sub fetch_rows
1193 250 ▶{
Subroutine fetch_rows: called CC=2 SCAR=7.0
Pod documented
1194 250 my $self = $_[0];
1195 250 ▶ my $rows = $self->{data} || [];
Condition50%1/2
$self->{'data'} || []
l!l
2500
MC/DC0%0/1
$self->{'data'} || []
$self->{'data'}
Truth table50%1/2
A
A= $self->{'data'}
Aresult
11
00
1196 250 $self->{data} = [];
1197 250 return $rows;
1198 }
1199  
1200 7 ▶sub open_table ($$$$$) { croak "Abstract method " . ref( $_[0] ) . "::open_table called" }
Subroutine open_table: called CC=1 SCAR=0.0
Pod documented
1201  
1202 sub open_tables
1203 4649 ▶{
Subroutine open_tables: called CC=22 SCAR=31.4
Pod documented
1204 4649 my ( $self, $data, $createMode, $lockMode ) = @_;
1205 4649 my @c;
1206 4649 my $t = {};
1207 4649 my @tables = $self->tables();
1208 4649 my $count = -1;
1209 4649 for my $tbl (@tables)
1210 {
1211 4519 ++$count;
1212 4519 my $name = $tbl->name();
1213 4519 ▶ if ( $name =~ m/^(.+)\.([^\.]+)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^(.+)\.([^\.]+)$/) 0 4519 4519
1214 {
1215 0 my $schema = $1; # ignored
1216 0 $name = $tbl->{name} = $2;
1217 }
1218  
1219 4519 ▶ if ( defined( $self->{table_func} ) && defined( $self->{table_func}->{ uc $name } ) )
Branch: 3 branches
BranchTrueFalseTotal
if (defined $self->{'table_func'} and defined $self->{'table_func'}{uc $name}) { } 2 4517 4519
elsif (defined $data->{'Database'}{'sql_ram_tables'} and defined $data->{'Database'}{'sql_ram_tables'}{$name} and $data->{'Database'}{'sql_ram_tables'}{$name}) { } 4475 42 4517
elsif ($self->{'is_ram_table'}) { } 35 7 42
Condition66%2/3
defined $self->{"table_func"} and defined $self->{"table_func"}{uc $name}
!ll&&!rl&&r
451702
MC/DC50%1/2
defined $self->{"table_func"} and defined $self->{"table_func"}{uc $name}
defined $self->{"table_func"} defined $self->{"table_func"}{uc $name}
Truth table66%2/3
A and B
A= defined $self->{"table_func"}B= defined $self->{"table_func"}{uc $name}
ABresult
0X0
100
111
1220 {
1221 2 my $u_func = $self->{table_func}->{ uc $name };
1222 2 $t->{$name} = $self->get_user_func_table( $name, $u_func );
1223 }
1224 ▶ elsif (defined( $data->{Database}->{sql_ram_tables} )
Condition83%5/6
defined $data->{"Database"}{"sql_ram_tables"} and defined $data->{"Database"}{"sql_ram_tables"}{$name} and $data->{"Database"}{"sql_ram_tables"}{$name}
!ll&&!rl&&r
4204475
defined $data->{"Database"}{"sql_ram_tables"} and defined $data->{"Database"}{"sql_ram_tables"}{$name}
!ll&&!rl&&r
13294475
MC/DC66%2/3
defined $data->{"Database"}{"sql_ram_tables"} and defined $data->{"Database"}{"sql_ram_tables"}{$name} and $data->{"Database"}{"sql_ram_tables"}{$name}
defined $data->{"Database"}{"sql_ram_tables"} defined $data->{"Database"}{"sql_ram_tables"}{$name} $data->{"Database"}{"sql_ram_tables"}{$name}
Truth table75%3/4
A and B and C
A= defined $data->{"Database"}{"sql_ram_tables"}B= defined $data->{"Database"}{"sql_ram_tables"}{$name}C= $data->{"Database"}{"sql_ram_tables"}{$name}
ABCresult
0XX0
10X0
1100
1111
1225 && defined( $data->{Database}->{sql_ram_tables}->{$name} )
1226 && $data->{Database}->{sql_ram_tables}->{$name} )
1227 {
1228 4475 $t->{$name} = $data->{Database}->{sql_ram_tables}->{$name};
1229 4475 $t->{$name}->seek( $data, 0, 0 );
1230 $t->{$name}->init_table( $data, $name, $createMode, $lockMode )
1231 4475 ▶ if ( $t->{$name}->can('init_table') );
Branch: 1 branch
BranchTrueFalseTotal
if $t->{$name}->can("init_table") 0 4475 4475
1232 }
1233 elsif ( $self->{is_ram_table} )
1234 {
1235 35 $t->{$name} = $data->{Database}->{sql_ram_tables}->{$name} =
1236 SQL::Statement::RAM::Table->new( $name, [], [] );
1237 }
1238 else
1239 {
1240 7 undef $@;
1241 7 eval {
1242 7 my $open_name = $self->{org_table_names}->[$count];
1243 7 $t->{$name} = $self->open_table( $data, $open_name, $createMode, $lockMode );
1244 };
1245 7 my $err = $t->{$name}->{errstr};
1246 7 ▶ return $self->do_err($err) if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 7 7
1247 7 ▶ return $self->do_err($@) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 7 0 7
1248 }
1249  
1250 4512 my @cnames;
1251 4512 my $table_cols = $t->{$name}->{org_col_names};
1252 4512 ▶ $table_cols = $t->{$name}->{col_names} unless $table_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $table_cols 37 4475 4512
1253 4512 for my $c (@$table_cols)
1254 {
1255 13311 ▶ my $newc = $c =~ m/^"/ ? $c : lc($c);
Branch: 1 branch
BranchTrueFalseTotal
$c =~ /^"/ ? : 40 13271 13311
1256 13311 push( @cnames, $newc );
1257 13311 $self->{ORG_NAME}->{$newc} = $c;
1258 }
1259  
1260 #
1261 # set the col_num => col_obj hash for the table
1262 #
1263 4512 my $col_nums;
1264 4512 my $i = 0;
1265 4512 for (@cnames)
1266 {
1267 13311 $col_nums->{$_} = $i++;
1268 }
1269 4512 $t->{$name}->{col_nums} = $col_nums;
1270 4512 $t->{$name}->{col_names} = \@cnames;
1271  
1272 4512 my $tcols = $t->{$name}->col_names();
1273 4512 my @newcols;
1274 4512 for (@$tcols)
1275 {
1276 13311 ▶ next unless ( defined($_) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 13311 13311
1277 13311 my $ncol = $_;
1278 13311 ▶ $ncol = $name . '.' . $ncol unless ( $ncol =~ m/\./ );
Branch: 1 branch
BranchTrueFalseTotal
unless $ncol =~ /\./ 13311 0 13311
1279 13311 push( @newcols, $ncol );
1280 }
1281 4512 @c = ( @c, @newcols );
1282 }
1283  
1284 4642 $self->buildColumnObjects( $t, \@tables );
1285 4642 ▶ return $self->do_err( $self->{errstr} ) if ( $self->{errstr} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"errstr"} 0 4642 4642
1286  
1287 ##################################################
1288 # Patch from Cosimo Streppone <cosimoATcpan.org>
1289  
1290 # my $all_cols = $self->{all_cols}
1291 # || [ map {$_->{name} }@{$self->{columns}} ]
1292 # || [];
1293 # @$all_cols = (@$all_cols,@c);
1294 # $self->{all_cols} = $all_cols;
1295 4642 ▶ if ( !$self->{all_cols} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"all_cols"}) 502 4140 4642
1296 {
1297 502 my $all_cols = [];
1298 502 $all_cols = [ map { $_->{name} } @{ $self->{columns} } ];
1299 502 ▶ $all_cols ||= []; # ?
Condition50%1/2
$all_cols ||= []
l!l
5020
MC/DC0%0/1
$all_cols ||= []
$all_cols
Truth table50%1/2
A
A= $all_cols
Aresult
11
00
1300 502 @$all_cols = ( @$all_cols, @c );
1301 502 $self->{all_cols} = $all_cols;
1302 }
1303 ##################################################
1304  
1305 4642 return SQL::Eval->new( { 'tables' => $t } ), \@c;
1306 }
1307  
1308 sub getColumnObject($)
1309 686 ▶{
Subroutine getColumnObject: called CC=20 SCAR=30.8
Pod documented
1310 686 my ( $self, $newcol, $t, $tables ) = @_;
1311 686 my @columns;
1312  
1313 686 ▶ if ( ( $newcol->{type} eq 'column' ) && ( -1 != index( $newcol->{value}, '*' ) ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($newcol->{'type'} eq 'column' and (index($newcol->{'value'}, '*') != -1)) { } 199 487 686
elsif ('CREATE' eq $self->command or 'DROP' eq $self->command) { } 97 390 487
Condition100%3/3
$newcol->{"type"} eq "column" and (index($newcol->{"value"}, "*") != -1)
!ll&&!rl&&r
211276199
MC/DC100%2/2
$newcol->{"type"} eq "column" and (index($newcol->{"value"}, "*") != -1)
$newcol->{"type"} eq "column" (index($newcol->{"value"}, "*") != -1)
Truth table100%3/3
A and B
A= $newcol->{"type"} eq "column"B= (index($newcol->{"value"}, "*") != -1)
ABresult
0X0
100
111
1314 {
1315 199 my $tbl;
1316 my @tables;
1317 199 ▶ if ( $newcol->{value} =~ m/^(.+)\.\*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($newcol->{'value'} =~ /^(.+)\.\*$/) { } 0 199 199
1318 {
1319 0 $tbl = $1;
1320 0 ▶ return $self->do_err("No table name given in '$newcol->{value}'")
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_STRING($tbl) 0 0 0
1321 unless ( defined( _STRING($tbl) ) );
1322 0 @tables = ($tbl);
1323 }
1324 else
1325 {
1326 199 @tables = map { $_->name() } @{$tables};
1327 }
1328  
1329 my $join = defined( _HASH( $self->{join} ) )
1330 && ( ( -1 != index( $self->{join}->{type}, 'NATURAL' ) )
1331 199 ▶ || ( -1 != index( $self->{join}->{clause}, 'USING' ) ) );
Condition100%6/6
defined &_HASH($self->{'join'}) && ((index($self->{'join'}{'type'}, 'NATURAL') != -1) || (index($self->{'join'}{'clause'}, 'USING') != -1))
!ll&&!rl&&r
181108
(index($self->{"join"}{"type"}, "NATURAL") != -1) or (index($self->{"join"}{"clause"}, "USING") != -1)
l!l&&r!l&&!r
3510
MC/DC100%3/3
defined &_HASH($self->{'join'}) && ((index($self->{'join'}{'type'}, 'NATURAL') != -1) || (index($self->{'join'}{'clause'}, 'USING') != -1))
defined &_HASH($self->{'join'}) (index($self->{"join"}{"type"}, "NATURAL") != -1) (index($self->{"join"}{"clause"}, "USING") != -1)
Truth table100%4/4
A && B or C
A= defined &_HASH($self->{'join'})B= (index($self->{"join"}{"type"}, "NATURAL") != -1)C= (index($self->{"join"}{"clause"}, "USING") != -1)
ABCresult
0XX0
1000
11X1
1011
1332 199 my %shared_cols;
1333  
1334 199 foreach my $table (@tables)
1335 {
1336 212 ▶ return $self->do_err("Can't find table '$table'") unless ( defined( $t->{$table} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t->{$table} 0 212 212
1337 212 my $tcols = $t->{$table}->{col_names};
1338 212 ▶ return $self->do_err("Couldn't find column names for table '$table'!")
Branch: 1 branch
BranchTrueFalseTotal
unless &_ARRAY($tcols) 0 212 212
1339 unless ( _ARRAY($tcols) );
1340 212 foreach my $colName ( @{$tcols} )
1341 {
1342 652 ▶ next if ( $join && $shared_cols{$colName}++ );
Branch: 1 branch
BranchTrueFalseTotal
if $join and $shared_cols{$colName}++ 8 644 652
Condition100%3/3
$join and $shared_cols{$colName}++
!ll&&!rl&&r
620248
MC/DC100%2/2
$join and $shared_cols{$colName}++
$join $shared_cols{$colName}++
Truth table100%3/3
A and B
A= $joinB= $shared_cols{$colName}++
ABresult
0X0
100
111
1343 644 my $expcol = [
1344 $colName, # column name
1345 $table, # table name
1346 SQL::Statement::ColumnValue->new( $self, $table . '.' . $colName ), # term
1347 $colName, # display name
1348 $colName,
1349 $newcol,
1350 ];
1351 644 push( @columns, $expcol );
1352 }
1353 }
1354 }
1355 ▶ elsif ( ( 'CREATE' eq $self->command() ) || ( 'DROP' eq $self->command() ) )
Condition66%2/3
"CREATE" eq $self->command or "DROP" eq $self->command
l!l&&r!l&&!r
970390
MC/DC50%1/2
"CREATE" eq $self->command or "DROP" eq $self->command
"CREATE" eq $self->command "DROP" eq $self->command
Truth table66%2/3
A or B
A= "CREATE" eq $self->commandB= "DROP" eq $self->command
ABresult
1X1
011
000
1356 {
1357 return $self->do_err("Invalid column type '$newcol->{type}'")
1358 97 ▶ unless ( 'column' eq $newcol->{type} );
Branch: 1 branch
BranchTrueFalseTotal
unless "column" eq $newcol->{"type"} 0 97 97
1359 my $expcol = [
1360 $newcol->{value}, # column name
1361 undef, # table name
1362 undef, # term
1363 $newcol->{value}, # display name
1364 $newcol->{value}, # original name
1365 97 $newcol, # coldef
1366 ];
1367 97 push( @columns, $expcol );
1368 }
1369 else
1370 {
1371 390 my $col;
1372 390 ▶ if ( $newcol->{type} eq 'setfunc' )
Branch: 1 branch
BranchTrueFalseTotal
if ($newcol->{'type'} eq 'setfunc') { } 24 366 390
1373 {
1374 24 my @cols = $self->getColumnObject( $newcol->{arg}, $t );
1375 24 ▶ if ( 1 == scalar(@cols) )
Branch: 1 branch
BranchTrueFalseTotal
if (1 == scalar @cols) { } 18 6 24
1376 {
1377 18 $col = $cols[0]->[2];
1378 }
1379 else
1380 {
1381 # FIXME add '\0' constants between items?
1382 my $colSep = $self->{termFactory}->buildCondition(
1383 {
1384 6 type => 'string',
1385 value => "\0",
1386 }
1387 );
1388 0 @cols = map { $_->[2], $colSep } @cols;
1389 6 pop(@cols);
1390 $col = $self->{termFactory}->buildCondition(
1391 {
1392 6 type => 'function',
1393 name => 'str_concat',
1394 value => \@cols,
1395 }
1396 );
1397 }
1398 }
1399 else
1400 {
1401 366 $col = $self->{termFactory}->buildCondition($newcol);
1402 }
1403  
1404 my $expcol = [
1405 $newcol->{name} || $newcol->{value}, # column name
1406 undef, # table name
1407 $col, # term
1408 $newcol->{alias} || $newcol->{fullorg}, # display name
1409 $newcol->{fullorg}, # original name
1410 390 ▶ $newcol, # coldef
Condition66%4/6
$newcol->{'name'} || $newcol->{'value'}
l!l&&r!l&&!r
2101800
$newcol->{'alias'} || $newcol->{'fullorg'}
l!l&&r!l&&!r
2151750
MC/DC0%0/2
$newcol->{'name'} || $newcol->{'value'}
$newcol->{'name'} $newcol->{'value'}
MC/DC0%0/2
$newcol->{'alias'} || $newcol->{'fullorg'}
$newcol->{'alias'} $newcol->{'fullorg'}
Truth table66%2/3
A || B
A= $newcol->{'name'}B= $newcol->{'value'}
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $newcol->{'alias'}B= $newcol->{'fullorg'}
ABresult
1X1
011
000
1411 ];
1412 390 push( @columns, $expcol );
1413 }
1414  
1415 686 return @columns;
1416 }
1417  
1418 sub buildColumnObjects($)
1419 4642 ▶{
Subroutine buildColumnObjects: called CC=7 SCAR=19.5
Pod documented
1420 4642 my ( $self, $t, $tables ) = @_;
1421  
1422 4642 ▶ defined( _ARRAY0( $self->{column_defs} ) ) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined &_ARRAY0($self->{"column_defs"}) or return 4635 7 4642
1423 4635 ▶ defined( _ARRAY0( $self->{columns} ) ) and return;
Branch: 1 branch
BranchTrueFalseTotal
defined &_ARRAY0($self->{"columns"}) and return 4140 495 4635
1424  
1425 495 $self->{columns} = [];
1426  
1427 495 my $coldefs = $self->{column_defs};
1428  
1429 495 ▶ for ( my $i = 0; $i < scalar( @{$coldefs} ); ++$i )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$coldefs;}) 662 495 1157
1430 {
1431 662 my $colentry = $coldefs->[$i];
1432  
1433 662 my @columns = $self->getColumnObject( $colentry, $t, $tables );
1434 662 ▶ return if ( $self->{errstr} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"errstr"} 0 662 662
1435  
1436 662 foreach my $col (@columns)
1437 {
1438 1113 my $expcol = SQL::Statement::Util::Column->new( @{$col} );
1439 1113 push( @{ $self->{columns} }, $expcol );
1440 1113 ▶ $self->{column_aliases}->{ $col->[4] } ||= $col->[3];
Condition66%2/3
$self->{"column_aliases"}{$col->[4]} ||= $col->[3]
l!l&&r!l&&!r
2268870
MC/DC0%0/2
$self->{"column_aliases"}{$col->[4]} ||= $col->[3]
$self->{"column_aliases"}{$col->[4]} $col->[3]
Truth table66%2/3
A ||= B
A= $self->{"column_aliases"}{$col->[4]}B= $col->[3]
ABresult
1X1
011
000
1441 1113 $self->{colpos}->{ $col->[3] } = scalar( @{ $self->{columns} } ) - 1;
1442 }
1443 }
1444  
1445 495 return;
1446 }
1447  
1448 sub verify_expand_column
1449 13143 ▶{
Subroutine verify_expand_column: called CC=20 SCAR=30.7
Pod documented
1450 13143 my ( $self, $c, $i, $usr_cols, $is_duplicate, $col_exists ) = @_;
1451  
1452 # XXX
1453 13143 ▶ defined $self->{ALIASES}->{$c} and $c = $self->{ALIASES}->{$c};
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"ALIASES"}{$c} and $c = $self->{"ALIASES"}{$c} 28 13115 13143
1454  
1455 13143 my ( $table, $col, $col_obj );
1456 13143 ▶ if ( $c =~ m/(\S+)\.(\S+)/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($c =~ /(\S+)\.(\S+)/) { } 1 13142 13143
elsif (++${$i;} >= 0) { } 13122 20 13142
1457 {
1458 1 $table = $1;
1459 1 $col = $2;
1460 }
1461 13142 elsif ( ++${$i} >= 0 )
1462 {
1463 13122 $col_obj = $usr_cols->[ ${$i} ];
1464 13122 ( $table, $col ) = ( $col_obj->{table}, $col_obj->{name} );
1465 }
1466 else
1467 {
1468 20 ( $table, $col ) = $self->full_qualified_column_name($c);
1469 }
1470 13143 ▶ return unless ($col);
Branch: 1 branch
BranchTrueFalseTotal
unless $col 0 13143 13143
1471  
1472 my $is_column =
1473 13143 ▶ ( defined( _INSTANCE( $col_obj, 'SQL::Statement::Util::Column' ) ) and ( $col_obj->{coldef}->{type} eq 'column' ) )
Branch: 1 branch
BranchTrueFalseTotal
defined &_INSTANCE($col_obj, 'SQL::Statement::Util::Column') && $col_obj->{'coldef'}{'type'} eq 'column' ? : 13088 55 13143
Condition100%3/3
defined &_INSTANCE($col_obj, "SQL::Statement::Util::Column") and $col_obj->{"coldef"}{"type"} eq "column"
!ll&&!rl&&r
213413088
MC/DC100%2/2
defined &_INSTANCE($col_obj, "SQL::Statement::Util::Column") and $col_obj->{"coldef"}{"type"} eq "column"
defined &_INSTANCE($col_obj, "SQL::Statement::Util::Column") $col_obj->{"coldef"}{"type"} eq "column"
Truth table100%3/3
A and B
A= defined &_INSTANCE($col_obj, "SQL::Statement::Util::Column")B= $col_obj->{"coldef"}{"type"} eq "column"
ABresult
0X0
100
111
1474 ? 1
1475 : 0;
1476  
1477 13143 ▶ unless ( $is_column and defined($table) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_column and defined $table) 210 12933 13143
Condition100%3/3
$is_column and defined $table
!ll&&!rl&&r
5515512933
MC/DC100%2/2
$is_column and defined $table
$is_column defined $table
Truth table100%3/3
A and B
A= $is_columnB= defined $table
ABresult
0X0
100
111
1478 {
1479 210 ( $table, undef ) = $self->full_qualified_column_name($col);
1480 }
1481  
1482 13143 ▶ if ( defined( _INSTANCE( $table, 'SQL::Statement::Table' ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined &_INSTANCE($table, "SQL::Statement::Table")) 0 13143 13143
1483 {
1484 0 $table = $table->name();
1485 }
1486  
1487 13143 ▶ if ( $is_column and !$table )
Branch: 2 branches
BranchTrueFalseTotal
if ($is_column and not $table) { } 2 13141 13143
elsif ($is_column) { } 13086 55 13141
Condition100%3/3
$is_column and not $table
!ll&&!rl&&r
55130862
MC/DC100%2/2
$is_column and not $table
$is_column not $table
Truth table100%3/3
A and not(B)
A= $is_columnB= not $table
ABresult
0X0
100
111
1488 {
1489 2 ▶ return $self->do_err("Ambiguous column name '$c'") if ( $is_duplicate->{$c} );
Branch: 1 branch
BranchTrueFalseTotal
if $is_duplicate->{$c} 0 2 2
1490 2 return $self->do_err("No such column '$col'");
1491 0 $col = $c;
1492 }
1493 elsif ($is_column)
1494 {
1495 13086 ▶ my $is_user_def = 1 if ( $self->{opts}->{function_defs}->{$col} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opts"}{"function_defs"}{$col} 0 13086 13086
1496 return $self->do_err("No such column '$table.$col'")
1497 unless ( $col_exists->{"$table.$col"}
1498 13086 ▶ or $col_exists->{ "\L$table." . $col }
Branch: 1 branch
BranchTrueFalseTotal
unless $$col_exists{"$table.$col"} or $$col_exists{lc($table . '.') . $col} or $is_user_def 0 13086 13086
Condition33%2/6
$$col_exists{"$table.$col"} or $$col_exists{lc($table . '.') . $col} or $is_user_def
l!l&&r!l&&!r
1308600
$$col_exists{"$table.$col"} or $$col_exists{lc($table . '.') . $col}
l!l&&r!l&&!r
1308600
MC/DC0%0/3
$$col_exists{"$table.$col"} or $$col_exists{lc($table . '.') . $col} or $is_user_def
$$col_exists{"$table.$col"} $$col_exists{lc($table . '.') . $col} $is_user_def
Truth table25%1/4
A or B or C
A= $$col_exists{"$table.$col"}B= $$col_exists{lc($table . '.') . $col}C= $is_user_def
ABCresult
1XX1
01X1
0011
0000
1499 or $is_user_def );
1500 }
1501  
1502 13141 ▶ return ( $table, $col ) if ( $is_column or ${$i} < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $is_column or ${$i;} < 0 13107 34 13141
Condition100%3/3
$is_column or ${$i;} < 0
l!l&&r!l&&!r
130862134
MC/DC100%2/2
$is_column or ${$i;} < 0
$is_column ${$i;} < 0
Truth table100%3/3
A or B
A= $is_columnB= ${$i;} < 0
ABresult
1X1
011
000
1503 34 return;
1504 }
1505  
1506 sub verify_columns
1507 4424 ▶{
Subroutine verify_columns: called CC=39 SCAR=38.2
Pod documented
1508 4424 my ( $self, $data, $eval, $all_cols ) = @_;
1509  
1510 #
1511 # NOTE FOR LATER:
1512 # perhaps cache column names and skip this after first table open
1513 #
1514 4424 ▶ $all_cols ||= [];
Condition50%1/2
$all_cols ||= []
l!l
44240
MC/DC0%0/1
$all_cols ||= []
$all_cols
Truth table50%1/2
A
A= $all_cols
Aresult
11
00
1515 4424 my @tmp_cols = @{$all_cols};
1516 4424 my @usr_cols = $self->columns();
1517 4424 ▶ return $self->do_err('No fetchable columns') if ( 0 == scalar(@usr_cols) );
Branch: 1 branch
BranchTrueFalseTotal
if 0 == scalar @usr_cols 0 4424 4424
1518  
1519 4424 my ( $cnum, $fully_qualified_cols ) = ( 0, [] );
1520 4424 my @tmpcols = map { $_->{name} } @usr_cols;
1521 4424 my %col_exists = map { $_ => 1 } @tmp_cols;
1522  
1523 4424 my ( %is_member, @duplicates, %is_duplicate );
1524 # $_ =~ s/[^.]*\.(.*)/$1/;
1525 4424 foreach (@$all_cols)
1526 {
1527 13296 substr( $_, 0, index( $_, '.' ) + 1 ) = '';
1528 } # XXX we're modifying $all_cols from caller!
1529 4424 @duplicates = grep( $is_member{$_}++, @$all_cols );
1530 4424 %is_duplicate = map { $_ => 1 } @duplicates;
1531 4424 ▶ if ( exists( $self->{join} ) && defined( _HASH( $self->{join} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"join"} and defined &_HASH($self->{"join"})) 34 4390 4424
Condition100%3/3
exists $self->{"join"} and defined &_HASH($self->{"join"})
!ll&&!rl&&r
104428634
MC/DC100%2/2
exists $self->{"join"} and defined &_HASH($self->{"join"})
exists $self->{"join"} defined &_HASH($self->{"join"})
Truth table100%3/3
A and B
A= exists $self->{"join"}B= defined &_HASH($self->{"join"})
ABresult
0X0
100
111
1532 {
1533 34 my $join = $self->{join};
1534 34 ▶ if ( -1 != index( uc $join->{type}, 'NATURAL' ) )
Branch: 2 branches
BranchTrueFalseTotal
if (index(uc $join->{'type'}, 'NATURAL') != -1) { } 6 28 34
elsif ('USING' eq $join->{'clause'}) { } 10 18 28
1535 {
1536 6 %is_duplicate = ();
1537 }
1538  
1539 # the following should be probably conditioned on an option,
1540 # but I do not know which --BW
1541 elsif ( 'USING' eq $join->{clause} )
1542 {
1543 10 my @keys = @{ $join->{keycols} };
1544 10 delete @is_duplicate{@keys};
1545 }
1546 }
1547  
1548 4424 my %set_func_nofunc;
1549 4424 ▶ if ( defined( $self->{has_set_functions} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"has_set_functions"}) 20 4404 4424
1550 {
1551 20 my @set_func_nofunc = grep { ( $_->{type} ne 'setfunc' ) } @{ $self->{column_defs} };
1552 20 ▶ %set_func_nofunc = map { ( $_->{alias} || $_->{fullorg} ) => 1 } @set_func_nofunc;
Condition66%2/3
$_->{'alias'} || $_->{'fullorg'}
l!l&&r!l&&!r
180
MC/DC0%0/2
$_->{'alias'} || $_->{'fullorg'}
$_->{'alias'} $_->{'fullorg'}
Truth table66%2/3
A || B
A= $_->{'alias'}B= $_->{'fullorg'}
ABresult
1X1
011
000
1553 }
1554 4424 my ( $is_fully, $set_fully ) = ( {}, {} );
1555 4424 my $i = -1;
1556 4424 my $num_tables = $self->tables();
1557 4424 for my $c (@tmpcols)
1558 {
1559 13122 my ( $table, $col ) = $self->verify_expand_column( $c, \$i, \@usr_cols, \%is_duplicate, \%col_exists );
1560 13122 ▶ return if ( $self->{errstr} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"errstr"} 2 13120 13122
1561 13120 ▶ next unless ( $table && $col );
Branch: 1 branch
BranchTrueFalseTotal
unless $table and $col 34 13086 13120
Condition66%2/3
$table and $col
!ll&&!rl&&r
34013086
MC/DC50%1/2
$table and $col
$table $col
Truth table66%2/3
A and B
A= $tableB= $col
ABresult
0X0
100
111
1562  
1563 13086 my $ftc = "$table.$col";
1564 13086 ▶ next if ( $table and $col and $is_fully->{$ftc} );
Branch: 1 branch
BranchTrueFalseTotal
if $table and $col and $is_fully->{$ftc} 0 13086 13086
Condition33%2/6
$table and $col and $is_fully->{$ftc}
!ll&&!rl&&r
0130860
$table and $col
!ll&&!rl&&r
0013086
MC/DC0%0/3
$table and $col and $is_fully->{$ftc}
$table $col $is_fully->{$ftc}
Truth table25%1/4
A and B and C
A= $tableB= $colC= $is_fully->{$ftc}
ABCresult
0XX0
10X0
1100
1111
1565  
1566 13086 $self->{columns}->[$i]->{name} = $col;
1567 13086 $self->{columns}->[$i]->{table} = $table;
1568  
1569 13086 ▶ if ( $table and $col )
Branch: 1 branch
BranchTrueFalseTotal
if ($table and $col) 13086 0 13086
Condition33%1/3
$table and $col
!ll&&!rl&&r
0013086
MC/DC0%0/2
$table and $col
$table $col
Truth table33%1/3
A and B
A= $tableB= $col
ABresult
0X0
100
111
1570 {
1571 13086 push( @$fully_qualified_cols, $ftc );
1572 13086 ++$is_fully->{$ftc};
1573 13086 ▶ ++$set_fully->{$ftc} if ( $set_func_nofunc{$c} );
Branch: 1 branch
BranchTrueFalseTotal
if $set_func_nofunc{$c} 8 13078 13086
1574 }
1575 }
1576  
1577 4422 ▶ if ( defined( $self->{has_set_functions} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"has_set_functions"}) 20 4402 4422
1578 {
1579 20 ▶ if ( defined( _ARRAY( $self->{group_by} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined &_ARRAY($self->{"group_by"})) 7 13 20
1580 {
1581 7 foreach my $grpby ( @{ $self->{group_by} } )
1582 {
1583 8 $i = -2;
1584 8 my ( $table, $col ) = $self->verify_expand_column( $grpby, \$i, \@usr_cols, \%is_duplicate, \%col_exists );
1585 8 ▶ return if ( $self->{errstr} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"errstr"} 0 8 8
1586 8 ▶ $col ||= $grpby;
Condition33%1/3
$col ||= $grpby
l!l&&r!l&&!r
800
MC/DC0%0/2
$col ||= $grpby
$col $grpby
Truth table33%1/3
A ||= B
A= $colB= $grpby
ABresult
1X1
011
000
1587 8 ▶ ( $table, $col ) = $self->full_qualified_column_name($col)
Branch: 1 branch
BranchTrueFalseTotal
if defined $col and not defined $table 0 8 8
Condition33%1/3
defined $col and not defined $table
!ll&&!rl&&r
080
MC/DC0%0/2
defined $col and not defined $table
defined $col not defined $table
Truth table33%1/3
A and not(B)
A= defined $colB= not defined $table
ABresult
0X0
100
111
1588 if ( defined($col) && !defined($table) );
1589 8 ▶ next unless ( defined($table) && defined($col) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table and defined $col 0 8 8
Condition33%1/3
defined $table and defined $col
!ll&&!rl&&r
008
MC/DC0%0/2
defined $table and defined $col
defined $table defined $col
Truth table33%1/3
A and B
A= defined $tableB= defined $col
ABresult
0X0
100
111
1590 8 delete $set_fully->{"$table.$col"};
1591 }
1592 }
1593  
1594 20 ▶ if ( defined( _HASH($set_fully) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined &_HASH($set_fully)) 1 19 20
1595 {
1596 return $self->do_err(
1597 sprintf(
1598 "Column%s '%s' must appear in the GROUP BY clause or be used in an aggregate function",
1599 1 scalar( keys( %{$set_fully} ) ) > 1 ? 's' : '',
1600 1 ▶ join( "', '", keys( %{$set_fully} ) )
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %{$set_fully;} > 1 ? : 0 1 1
1601 )
1602 );
1603 }
1604 }
1605  
1606 4421 ▶ if ( $self->{sort_spec_list} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sort_spec_list"}) 17 4404 4421
1607 {
1608 17 for my $n ( 0 .. scalar @{ $self->{sort_spec_list} } - 1 )
1609 {
1610 20 ▶ defined( _INSTANCE( $self->{sort_spec_list}->[$n], 'SQL::Statement::Order' ) ) and next;
Branch: 1 branch
BranchTrueFalseTotal
defined &_INSTANCE($self->{"sort_spec_list"}[$n], "SQL::Statement::Order") and next 7 13 20
1611 13 my ( $newcol, $direction ) = each %{ $self->{sort_spec_list}->[$n] };
1612 13 ▶ my $desc = $direction && ( $direction eq "DESC" ); # ($direction || "ASC") eq "DESC";
Condition66%2/3
$direction && $direction eq 'DESC'
!ll&&!rl&&r
094
MC/DC50%1/2
$direction && $direction eq 'DESC'
$direction $direction eq 'DESC'
Truth table66%2/3
A && B
A= $directionB= $direction eq 'DESC'
ABresult
0X0
100
111
1613  
1614 # XXX parse order by like group by and select list
1615 13 $i = -2;
1616 13 my ( $table, $col ) = $self->verify_expand_column( $newcol, \$i, \@usr_cols, \%is_duplicate, \%col_exists );
1617 13 ▶ $self->{errstr} and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"errstr"} and return 0 13 13
1618 13 ▶ ( $table, $col ) = $self->full_qualified_column_name($newcol)
Branch: 1 branch
BranchTrueFalseTotal
if defined $col and not defined $table 1 12 13
Condition66%2/3
defined $col and not defined $table
!ll&&!rl&&r
0121
MC/DC50%1/2
defined $col and not defined $table
defined $col not defined $table
Truth table66%2/3
A and not(B)
A= defined $colB= not defined $table
ABresult
0X0
100
111
1619 if ( defined($col) && !defined($table) );
1620 13 ▶ defined($table) and $col = $table . "." . $col;
Branch: 1 branch
BranchTrueFalseTotal
defined $table and $col = $table . '.' . $col 12 1 13
1621 13 $self->{sort_spec_list}->[$n] = SQL::Statement::Order->new(
1622 col => SQL::Statement::Util::Column->new(
1623 $col, # column name
1624 $table, # table name
1625 SQL::Statement::ColumnValue->new( $self, $col ), # term
1626 $newcol # display name
1627 ),
1628 direction => $direction,
1629 desc => $desc,
1630 );
1631 }
1632 }
1633  
1634 4421 return $fully_qualified_cols;
1635 }
1636  
1637 sub distinct()
1638 134 ▶{
Subroutine distinct: called CC=2 SCAR=7.1
Pod documented
1639 134 my $q = _STRING( $_[0]->{set_quantifier} );
1640 134 ▶ return defined($q) && ( 'DISTINCT' eq $q );
Condition66%2/3
defined $q && 'DISTINCT' eq $q
!ll&&!rl&&r
12905
MC/DC50%1/2
defined $q && 'DISTINCT' eq $q
defined $q 'DISTINCT' eq $q
Truth table66%2/3
A && B
A= defined $qB= 'DISTINCT' eq $q
ABresult
0X0
100
111
1641 }
1642  
1643 sub column_names()
1644 0 ▶{
Subroutine column_names: not called CC=1 SCAR=6.9
Pod documented
1645 0 my @cols = map { $_->name() } $_[0]->columns();
1646 0 return @cols;
1647 }
1648  
1649 5823 ▶sub command() { return $_[0]->{command} }
Subroutine command: called CC=1 SCAR=0.0
Pod documented
1650  
1651 sub params(;$)
1652 2 ▶{
Subroutine params: called CC=5 SCAR=20.9
Pod documented
1653 2 ▶ if ( !$_[0]->{params} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0]{"params"}) 0 2 2
1654 {
1655 0 ▶ return wantarray ? () : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1656 }
1657 2 ▶ return $_[0]->{params}->[ $_[1] ] if ( defined $_[1] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 2 2
1658  
1659 0 ▶ return wantarray ? @{ $_[0]->{params} } : scalar @{ $_[0]->{params} };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
1660 }
1661  
1662 sub row_values(;$$)
1663 12803 ▶{
Subroutine row_values: called CC=8 SCAR=26.2
Pod documented
1664 12803 ▶ unless ( defined( _ARRAY( $_[0]->{values} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &_ARRAY($_[0]{"values"})) 0 12803 12803
1665 {
1666 0 ▶ return wantarray ? () : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1667 }
1668 12803 ▶ if ( defined( $_[1] ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) { } 12802 1 12803
1669 {
1670 12802 ▶ return 0 unless ( defined( $_[0]->{values}->[ $_[1] ] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0]{"values"}[$_[1]] 0 12802 12802
1671 12802 ▶ return $_[0]->{values}->[ $_[1] ]->[ $_[2] ] if ( defined $_[2] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[2] 12801 1 12802
1672  
1673 return wantarray
1674 0 ? map { $_->{value} } @{ $_[0]->{values}->[ $_[1] ] }
1675 1 ▶ : scalar @{ $_[0]->{values}->[ $_[1] ] };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
1676 }
1677 else
1678 {
1679 return wantarray
1680 ? map {
1681 0 [ map { $_->{value} } @{$_} ]
1682 0 } @{ $_[0]->{values} }
1683 1 ▶ : scalar( @{ $_[0]->{values} } );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
1684 }
1685 }
1686  
1687 #
1688 # $num_of_cols = $stmt->columns() # number of columns
1689 # @cols = $stmt->columns() # array of S::S::Column objects
1690 # $col = $stmt->columns($cnum) # S::S::Column obj for col number $cnum
1691 # $col = $stmt->columns($cname) # S::S::Column obj for col named $cname
1692 #
1693 sub columns
1694 34581 ▶{
Subroutine columns: called CC=9 SCAR=24.3
Pod documented
1695 34581 my ( $self, $col ) = @_;
1696 34581 ▶ if ( !$self->{columns} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"columns"}) 1 34580 34581
1697 {
1698 1 ▶ return wantarray ? () : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
1699 }
1700  
1701 34580 ▶ if ( defined $col and $col =~ m/^\d+$/ )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $col and $col =~ /^\d+$/) { } 12801 21779 34580
elsif (defined $col) { } 0 21779 21779
Condition66%2/3
defined $col and $col =~ /^\d+$/
!ll&&!rl&&r
21779012801
MC/DC50%1/2
defined $col and $col =~ /^\d+$/
defined $col $col =~ /^\d+$/
Truth table66%2/3
A and B
A= defined $colB= $col =~ /^\d+$/
ABresult
0X0
100
111
1702 { # arg1 = a number
1703 12801 return $self->{columns}->[$col];
1704 }
1705 elsif ( defined $col )
1706 { # arg1 = string
1707 0 for my $c ( @{ $self->{columns} } )
1708 {
1709 0 ▶ return $c if ( $c->name() eq $col );
Branch: 1 branch
BranchTrueFalseTotal
if $c->name eq $col 0 0 0
1710 }
1711 }
1712  
1713 21779 ▶ return wantarray ? @{ $self->{columns} } : scalar @{ $self->{columns} };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 13213 8566 21779
1714 }
1715  
1716 sub colname2colnum
1717 0 ▶{
Subroutine colname2colnum: not called CC=4 SCAR=30.0
Pod documented
1718 0 ▶ if ( !$_[0]->{columns} ) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0]{"columns"}) 0 0 0
1719 0 for my $i ( 0 .. $#{ $_[0]->{columns} } )
1720 {
1721 0 ▶ return $i if ( $_[0]->{columns}->[$i]->name() eq $_[1] );
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"columns"}[$i]->name eq $_[1] 0 0 0
1722 }
1723 0 return undef;
1724 }
1725  
1726 sub colname2table($)
1727 2 ▶{
Subroutine colname2table: called CC=8 SCAR=25.7
Pod documented
1728 2 my ( $self, $col_name ) = @_;
1729 2 ▶ return undef unless defined $col_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col_name 0 2 2
1730  
1731 2 my ( $tbl, $col );
1732 2 ▶ if ( $col_name =~ /^(.+)\.(.+)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($col_name =~ /^(.+)\.(.+)$/) { } 0 2 2
1733 {
1734 0 ( $tbl, $col ) = ( $1, $2 );
1735 }
1736 else
1737 {
1738 2 $col = $col_name;
1739 }
1740  
1741 2 my $found_table;
1742 2 for my $full_col ( @{ $self->{all_cols} } )
1743 {
1744 10 my ( $stbl, $scol ) = $full_col =~ /^(.+)\.(.+)$/;
1745 10 ▶ next unless ( $scol || '' ) eq $col;
Branch: 1 branch
BranchTrueFalseTotal
unless ($scol || "") eq $col 10 0 10
Condition100%2/2
$scol || ''
l!l
82
MC/DC100%1/1
$scol || ''
$scol
Truth table100%2/2
A
A= $scol
Aresult
11
00
1746 0 ▶ next if ( defined($tbl) && ( $tbl ne $stbl ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $tbl and $tbl ne $stbl 0 0 0
Condition0%0/3
defined $tbl and $tbl ne $stbl
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tbl and $tbl ne $stbl
defined $tbl $tbl ne $stbl
Truth table0%0/3
A and B
A= defined $tblB= $tbl ne $stbl
ABresult
0X0
100
111
1747 0 $found_table = $stbl;
1748 0 last;
1749 }
1750 2 return $found_table;
1751 }
1752  
1753 sub full_qualified_column_name($)
1754 574 ▶{
Subroutine full_qualified_column_name: called CC=15 SCAR=27.1
Pod documented
1755 574 my ( $self, $col_name ) = @_;
1756 574 ▶ return unless ( defined($col_name) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col_name 0 574 574
1757  
1758 # XXX
1759 574 ▶ defined $self->{ALIASES}->{$col_name} and $col_name = $self->{ALIASES}->{$col_name};
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"ALIASES"}{$col_name} and $col_name = $self->{"ALIASES"}{$col_name} 26 548 574
1760  
1761 574 my ( $tbl, $col );
1762 574 ▶ unless ( ( $tbl, $col ) = $col_name =~ m/^((?:"[^"]+")|(?:[^.]+))\.(.*)$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless (() = $col_name =~ /^((?:"[^"]+")|(?:[^.]+))\.(.*)$/) 449 125 574
1763 {
1764 449 $col = $col_name;
1765 }
1766  
1767 574 ▶ unless ( defined( $self->{splitted_all_cols} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'splitted_all_cols'}) { } 456 118 574
1768 {
1769 118 my @rc;
1770 118 for my $full_col ( @{ $self->{all_cols} } )
1771 {
1772 722 ▶ if ( my ( $stbl, $scol ) = $full_col =~ m/^((?:"[^"]+")|(?:[^.]+))\.(.*)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (() = $full_col =~ /^((?:"[^"]+")|(?:[^.]+))\.(.*)$/) 440 282 722
1773 {
1774 440 push( @{ $self->{splitted_all_cols} }, [ $stbl, $scol ] );
1775 440 ▶ defined($tbl) and ( $tbl ne $stbl ) and next;
Branch: 1 branch
BranchTrueFalseTotal
defined $tbl and $tbl ne $stbl and next 24 416 440
Condition100%3/3
defined $tbl and $tbl ne $stbl
!ll&&!rl&&r
3912524
MC/DC100%2/2
defined $tbl and $tbl ne $stbl
defined $tbl $tbl ne $stbl
Truth table100%3/3
A and B
A= defined $tblB= $tbl ne $stbl
ABresult
0X0
100
111
1776 416 ▶ ( $scol eq $col ) and @rc = ( $stbl, $scol );
Branch: 1 branch
BranchTrueFalseTotal
$scol eq $col and @rc = 109 307 416
1777 }
1778 }
1779 118 ▶ @rc and return @rc;
Branch: 1 branch
BranchTrueFalseTotal
@rc and return @rc 99 19 118
1780 }
1781 else
1782 {
1783 456 for my $splitted_col ( @{ $self->{splitted_all_cols} } )
1784 {
1785 1576 ▶ defined($tbl) and ( $tbl ne $splitted_col->[0] ) and next;
Branch: 1 branch
BranchTrueFalseTotal
defined $tbl and $tbl ne $splitted_col->[0] and next 348 1228 1576
Condition100%3/3
defined $tbl and $tbl ne $splitted_col->[0]
!ll&&!rl&&r
1033195348
MC/DC100%2/2
defined $tbl and $tbl ne $splitted_col->[0]
defined $tbl $tbl ne $splitted_col->[0]
Truth table100%3/3
A and B
A= defined $tblB= $tbl ne $splitted_col->[0]
ABresult
0X0
100
111
1786 1228 ▶ ( $splitted_col->[1] eq $col ) and return @$splitted_col;
Branch: 1 branch
BranchTrueFalseTotal
$splitted_col->[1] eq $col and return @$splitted_col 436 792 1228
1787 }
1788 }
1789  
1790 39 return ( $tbl, $col );
1791 }
1792  
1793 #sub verify_order_cols
1794 #{
1795 # my ( $self, $table ) = @_;
1796 # return unless $self->{sort_spec_list};
1797 # my @ocols = $self->order();
1798 # my @tcols = @{ $table->col_names() };
1799 # my @n_ocols;
1800 #
1801 # for my $colnum ( 0 .. $#ocols )
1802 # {
1803 # my $col = $self->order($colnum);
1804 #
1805 # if ( !defined( $col->table() ) )
1806 # {
1807 # my $cname = $ocols[$colnum]->{col}->name();
1808 # my $tname = $self->colname2table($cname);
1809 # return $self->do_err("No such column '$cname'.") unless ($tname);
1810 # $self->{sort_spec_list}->[$colnum]->{col}->{table} = $tname;
1811 # push( @n_ocols, $tname );
1812 # }
1813 # }
1814 #
1815 # return 1;
1816 #}
1817  
1818 407 ▶sub limit ($) { $_[0]->{limit_clause}->{limit}; }
Subroutine limit: called CC=1 SCAR=0.0
Pod documented
1819 138 ▶sub offset ($) { $_[0]->{limit_clause}->{offset}; }
Subroutine offset: called CC=1 SCAR=0.0
Pod documented
1820  
1821 sub order
1822 139 ▶{
Subroutine order: called CC=5 SCAR=16.1
Pod documented
1823 139 ▶ return unless ( defined $_[0]->{sort_spec_list} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0]{"sort_spec_list"} 119 20 139
1824  
1825 return
1826 defined( $_[1] ) && looks_like_number( $_[1] ) ? $_[0]->{sort_spec_list}->[ $_[1] ]
1827 18 : wantarray ? @{ $_[0]->{sort_spec_list} }
1828 20 ▶ : scalar @{ $_[0]->{sort_spec_list} };
Branch: 2 branches
BranchTrueFalseTotal
defined $_[1] && &looks_like_number($_[1]) ? : 1 19 20
wantarray ? : 18 1 19
Condition66%2/3
defined $_[1] and &looks_like_number($_[1])
!ll&&!rl&&r
1901
MC/DC50%1/2
defined $_[1] and &looks_like_number($_[1])
defined $_[1] &looks_like_number($_[1])
Truth table66%2/3
A and B
A= defined $_[1]B= &looks_like_number($_[1])
ABresult
0X0
100
111
1829 }
1830  
1831 sub tables
1832 13557 ▶{
Subroutine tables: called CC=4 SCAR=13.9
Pod documented
1833 return
1834 defined( $_[1] ) && looks_like_number( $_[1] ) ? $_[0]->{tables}->[ $_[1] ]
1835 4701 : wantarray ? @{ $_[0]->{tables} }
1836 13557 ▶ : scalar @{ $_[0]->{tables} };
Branch: 2 branches
BranchTrueFalseTotal
defined $_[1] && &looks_like_number($_[1]) ? : 4432 9125 13557
wantarray ? : 4701 4424 9125
Condition66%2/3
defined $_[1] and &looks_like_number($_[1])
!ll&&!rl&&r
912504432
MC/DC50%1/2
defined $_[1] and &looks_like_number($_[1])
defined $_[1] &looks_like_number($_[1])
Truth table66%2/3
A and B
A= defined $_[1]B= &looks_like_number($_[1])
ABresult
0X0
100
111
1837 }
1838  
1839 sub order_joins
1840 7 ▶{
Subroutine order_joins: called CC=15 SCAR=47.1
Pod documented
1841 7 my ( $self, $links ) = @_;
1842 7 my ( @new_keycols, @new_links );
1843 7 for (@$links)
1844 {
1845 7 my ( $tbl, $col ) = $self->full_qualified_column_name($_);
1846 7 push( @new_keycols, $tbl . $self->{dlm} . $col );
1847 7 push( @new_links, $tbl );
1848 }
1849 7 $self->{join}->{keycols} = $links = \@new_keycols;
1850 # my @tmp = @new_keycols;
1851 # foreach (@tmp) { $_ =~ s/\./$self->{dlm}/g; }
1852 # $self->{join}->{keycols} = \@tmp;
1853 # @$links = \@new_keycols;
1854 7 my @all_tables;
1855 my %relations;
1856 7 my %is_table;
1857  
1858 7 ▶ while (@new_links)
Branch: 1 branch
BranchTrueFalseTotal
if (@new_links) 7 0 7
1859 {
1860 7 my $t1 = shift(@new_links);
1861 7 my $t2 = shift(@new_links);
1862 7 ▶ return undef unless ( defined($t1) and defined($t2) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t1 and defined $t2 7 0 7
Condition33%1/3
defined $t1 and defined $t2
!ll&&!rl&&r
070
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
1863 0 ▶ push @all_tables, $t1 unless ( $is_table{$t1}++ );
Branch: 1 branch
BranchTrueFalseTotal
unless $is_table{$t1}++ 0 0 0
1864 0 ▶ push @all_tables, $t2 unless ( $is_table{$t2}++ );
Branch: 1 branch
BranchTrueFalseTotal
unless $is_table{$t2}++ 0 0 0
1865 0 $relations{$t1}{$t2}++;
1866 0 $relations{$t2}{$t1}++;
1867 }
1868 0 my @tables = @all_tables;
1869 0 my @order = shift @tables;
1870 0 my %is_ordered = ( $order[0] => 1 );
1871 0 my %visited;
1872 0 ▶ while (@tables)
Branch: 1 branch
BranchTrueFalseTotal
if (@tables) 0 0 0
1873 {
1874 0 my $t = shift @tables;
1875 0 my @rels = keys %{ $relations{$t} };
1876 0 for my $t2 (@rels)
1877 {
1878 0 ▶ next unless $is_ordered{$t2};
Branch: 1 branch
BranchTrueFalseTotal
unless $is_ordered{$t2} 0 0 0
1879 0 push @order, $t;
1880 0 $is_ordered{$t}++;
1881 0 last;
1882 }
1883 0 ▶ if ( !$is_ordered{$t} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_ordered{$t}) 0 0 0
1884 {
1885 0 ▶ push( @tables, $t ) if ( $visited{$t}++ < @all_tables );
Branch: 1 branch
BranchTrueFalseTotal
if $visited{$t}++ < @all_tables 0 0 0
1886 }
1887 }
1888 0 ▶ if ( @order < @all_tables )
Branch: 1 branch
BranchTrueFalseTotal
if (@order < @all_tables) 0 0 0
1889 {
1890 0 my @missing;
1891 0 my %in_order = map { $_ => 1 } @order;
1892 0 foreach my $tbl (@all_tables)
1893 {
1894 0 ▶ next if ( $in_order{$tbl} );
Branch: 1 branch
BranchTrueFalseTotal
if $in_order{$tbl} 0 0 0
1895 0 push( @missing, $tbl );
1896 }
1897 0 return $self->do_err( sprintf( 'Unconnected tables (%s) in equijoin statement!', join( ', ', @missing ) ) );
1898 }
1899 0 $self->{join}->{table_order} = \@order;
1900 0 return \@order;
1901 }
1902  
1903 sub do_err
1904 12 ▶{
Subroutine do_err: called CC=5 SCAR=16.1
Pod documented
1905 12 my $self = shift;
1906 12 my $err = shift;
1907 12 my $errtype = shift;
1908 12 my @c = caller 6;
1909  
1910 #$err = "[" . $self->{original_string} . "]\n$err\n\n";
1911 # $err = "$err\n\n";
1912 12 my $prog = $c[1];
1913 12 my $line = $c[2];
1914 12 ▶ $prog = defined($prog) ? " called from $prog" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $prog ? : 9 3 12
1915 12 ▶ $prog .= defined($line) ? " at $line" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $line ? : 9 3 12
1916 12 $err = "\nExecution ERROR: $err$prog.\n\n";
1917  
1918 12 $self->{errstr} = $err;
1919 12 ▶ carp $err if $self->{PrintError};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"PrintError"} 0 12 12
1920 12 ▶ croak "$err" if $self->{RaiseError};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"RaiseError"} 1 11 12
1921 11 return;
1922 }
1923  
1924 14 ▶sub errstr() { return $_[0]->{errstr}; }
Subroutine errstr: called CC=1 SCAR=0.0
Pod documented
1925  
1926 1 ▶sub where_hash() { return $_[0]->{where_clause}; }
Subroutine where_hash: called CC=1 SCAR=0.0
Pod documented
1927  
1928 0 ▶sub column_defs() { return $_[0]->{column_defs}; }
Subroutine column_defs: not called CC=1 SCAR=6.9
Pod documented
1929  
1930 sub where()
1931 5 ▶{
Subroutine where: called CC=2 SCAR=7.2
Pod documented
1932 5 ▶ return undef unless $_[0]->{where_terms};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"where_terms"} 0 5 5
1933 5 return $_[0]->{where_terms};
1934 }
1935  
1936 sub get_user_func_table
1937 2 ▶{
Subroutine get_user_func_table: called CC=2 SCAR=7.1
Pod documented
1938 2 my ( $self, $name, $u_func ) = @_;
1939 2 my $term = $self->{termFactory}->buildCondition($u_func);
1940  
1941 2 my @data_aryref = @{ $term->value(undef) };
1942 2 my $col_names = shift @data_aryref;
1943  
1944 # my $tempTable = SQL::Statement::TempTable->new(
1945 # $name, $col_names, $col_names, $data_aryref
1946 # );
1947 2 my $tempTable = SQL::Statement::RAM::Table->new( $name, $col_names, \@data_aryref );
1948 2 ▶ $tempTable->{all_cols} ||= $col_names;
Condition33%1/3
$tempTable->{"all_cols"} ||= $col_names
l!l&&r!l&&!r
020
MC/DC0%0/2
$tempTable->{"all_cols"} ||= $col_names
$tempTable->{"all_cols"} $col_names
Truth table33%1/3
A ||= B
A= $tempTable->{"all_cols"}B= $col_names
ABresult
1X1
011
000
1949 2 return $tempTable;
1950 }
1951  
1952 sub capability($)
1953 0 ▶{
Subroutine capability: not called CC=2 SCAR=17.9
Pod documented
1954 0 my ( $self, $capname ) = @_;
1955 0 ▶ return $self->{capabilities}->{$capname} if ( defined( $self->{capabilities}->{$capname} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"capabilities"}{$capname} 0 0 0
1956  
1957 0 return;
1958 }
1959  
1960 sub DESTROY
1961 870 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
1962 870 my $self = $_[0];
1963  
1964 870 undef $self->{NAME};
1965 870 undef $self->{ORG_NAME};
1966 870 undef $self->{all_cols};
1967 870 undef $self->{already_prepared};
1968 870 undef $self->{argnum};
1969 870 undef $self->{col_obj};
1970 870 undef $self->{column_names};
1971 870 undef $self->{columns};
1972 870 undef $self->{cur_table};
1973 870 undef $self->{data};
1974 870 undef $self->{group_by};
1975 #undef $self->{has_OR};
1976 870 undef $self->{join};
1977 870 undef $self->{limit_clause};
1978 870 undef $self->{num_placeholders};
1979 870 undef $self->{num_val_placeholders};
1980 870 undef $self->{org_table_names};
1981 870 undef $self->{params};
1982 870 undef $self->{opts};
1983 870 undef $self->{procedure};
1984 870 undef $self->{set_function};
1985 870 undef $self->{sort_spec_list};
1986 870 undef $self->{subquery};
1987 870 undef $self->{tables};
1988 870 undef $self->{table_names};
1989 870 undef $self->{table_func};
1990 870 undef $self->{where_clause};
1991 870 undef $self->{where_terms};
1992 870 undef $self->{values};
1993 }
1994  
1995 package SQL::Statement::Aggregate;
1996  
1997 16 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
1998 16 ▶use Params::Util qw(_HASH);
Subroutine BEGIN: called CC=1 SCAR=0.0
1999 16 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
2000  
2001 sub new
2002 19 ▶{
Subroutine new: called CC=1 SCAR=0.0
2003 19 my ( $class, $owner, $rows ) = @_;
2004 19 my $self = {
2005 owner => $owner,
2006 records => $rows,
2007 };
2008 19 return bless( $self, $class );
2009 }
2010  
2011 16 my $empty_agg = {
2012 uniq => [],
2013 count => 0,
2014 sum => undef,
2015 min => undef,
2016 max => undef,
2017 };
2018  
2019 sub do_calc()
2020 19 ▶{
Subroutine do_calc: called CC=13 SCAR=25.6
2021 19 my $self = $_[0];
2022  
2023 19 foreach my $line ( 0 .. ( scalar( @{ $self->{records} } ) - 1 ) )
2024 {
2025 8073 my $row = $self->{records}->[$line];
2026 8073 my $result = $self->getAffectedResult($row);
2027  
2028 8073 foreach my $colidx ( 0 .. ( scalar( @{ $self->{owner}->{columns} } ) - 1 ) )
2029 {
2030 20115 my $coldef = $self->{owner}->{columns}->[$colidx]->{coldef};
2031 20115 my $colval = $row->[$colidx];
2032  
2033 20115 ▶ if ( $coldef->{type} eq 'setfunc' )
Branch: 1 branch
BranchTrueFalseTotal
if ($coldef->{'type'} eq 'setfunc') { } 16086 4029 20115
2034 {
2035 16086 ▶ if ( $coldef->{distinct} eq 'DISTINCT' )
Branch: 1 branch
BranchTrueFalseTotal
if ($coldef->{"distinct"} eq "DISTINCT") 9 16077 16086
2036 {
2037 9 ▶ next if defined( $result->{uniq}->[$colidx]->{$colval} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $result->{"uniq"}[$colidx]{$colval} 3 6 9
2038 6 $result->{uniq}->[$colidx]->{$colval} = 1;
2039 }
2040  
2041 $result->{agg}->[$colidx] = clone($empty_agg)
2042 16083 ▶ unless ( defined( _HASH( $result->{agg}->[$colidx] ) ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_HASH($result->{"agg"}[$colidx]) 36 16047 16083
2043 16083 my $agg = $result->{agg}->[$colidx];
2044  
2045 16083 ++$agg->{count};
2046 16083 ▶ unless ( defined( $agg->{max} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $agg->{"max"} and &SQL::Statement::_anycmp($colval, $agg->{"max"}) < 0) 16038 45 16083
Condition100%3/3
defined $agg->{"max"} and &SQL::Statement::_anycmp($colval, $agg->{"max"}) < 0
!ll&&!rl&&r
361600245
MC/DC100%2/2
defined $agg->{"max"} and &SQL::Statement::_anycmp($colval, $agg->{"max"}) < 0
defined $agg->{"max"} &SQL::Statement::_anycmp($colval, $agg->{"max"}) < 0
Truth table100%3/3
A and B
A= defined $agg->{"max"}B= &SQL::Statement::_anycmp($colval, $agg->{"max"}) < 0
ABresult
0X0
100
111
2047 && ( SQL::Statement::_anycmp( $colval, $agg->{max} ) < 0 ) )
2048 {
2049 16038 $agg->{max} = $colval;
2050 }
2051 16083 ▶ unless ( defined( $agg->{min} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $agg->{"min"} and &SQL::Statement::_anycmp($colval, $agg->{"min"}) > 0) 4069 12014 16083
Condition100%3/3
defined $agg->{"min"} and &SQL::Statement::_anycmp($colval, $agg->{"min"}) > 0
!ll&&!rl&&r
36403312014
MC/DC100%2/2
defined $agg->{"min"} and &SQL::Statement::_anycmp($colval, $agg->{"min"}) > 0
defined $agg->{"min"} &SQL::Statement::_anycmp($colval, $agg->{"min"}) > 0
Truth table100%3/3
A and B
A= defined $agg->{"min"}B= &SQL::Statement::_anycmp($colval, $agg->{"min"}) > 0
ABresult
0X0
100
111
2052 && ( SQL::Statement::_anycmp( $colval, $agg->{min} ) > 0 ) )
2053 {
2054 4069 $agg->{min} = $colval;
2055 }
2056 16083 ▶ $agg->{sum} += $colval if ( looks_like_number($colval) );
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($colval) 12063 4020 16083
2057 }
2058 else
2059 {
2060 $result->{pure}->[$colidx] = $colval
2061 4029 ▶ unless ( defined( $result->{pure}->[$colidx] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result->{"pure"}[$colidx] 23 4006 4029
2062 }
2063 }
2064 }
2065 }
2066  
2067 sub build_row # (\%)
2068 32 ▶{
Subroutine build_row: called CC=11 SCAR=24.2
2069 32 my ( $self, $result ) = @_;
2070 32 my @row;
2071  
2072 32 foreach my $colidx ( 0 .. ( scalar( @{ $self->{owner}->{columns} } ) - 1 ) )
2073 {
2074 65 my $coldef = $self->{owner}->{columns}->[$colidx]->{coldef};
2075  
2076 65 ▶ if ( $coldef->{type} eq 'setfunc' )
Branch: 1 branch
BranchTrueFalseTotal
if ($coldef->{'type'} eq 'setfunc') { } 41 24 65
2077 {
2078 41 ▶ if ( $coldef->{name} eq 'COUNT' )
Branch: 5 branches
BranchTrueFalseTotal
if ($coldef->{'name'} eq 'COUNT') { } 20 21 41
elsif ($coldef->{'name'} eq 'MAX') { } 11 10 21
elsif ($coldef->{'name'} eq 'MIN') { } 1 9 10
elsif ($coldef->{'name'} eq 'SUM') { } 8 1 9
elsif ($coldef->{'name'} eq 'AVG') { } 1 0 1
2079 {
2080 20 ▶ push( @row, $result->{agg}->[$colidx]->{count} || 0 );
Condition100%2/2
$result->{'agg'}[$colidx]{'count'} || 0
l!l
164
MC/DC100%1/1
$result->{'agg'}[$colidx]{'count'} || 0
$result->{'agg'}[$colidx]{'count'}
Truth table100%2/2
A
A= $result->{'agg'}[$colidx]{'count'}
Aresult
11
00
2081 }
2082 elsif ( $coldef->{name} eq 'MAX' )
2083 {
2084 11 push( @row, $result->{agg}->[$colidx]->{max} );
2085 }
2086 elsif ( $coldef->{name} eq 'MIN' )
2087 {
2088 1 push( @row, $result->{agg}->[$colidx]->{min} );
2089 }
2090 elsif ( $coldef->{name} eq 'SUM' )
2091 {
2092 8 push( @row, $result->{agg}->[$colidx]->{sum} );
2093 }
2094 elsif ( $coldef->{name} eq 'AVG' )
2095 {
2096 1 my $count = $result->{agg}->[$colidx]->{count};
2097 1 my $sum = $result->{agg}->[$colidx]->{sum};
2098 1 ▶ my $avg = $sum / $count if ( $count && $sum );
Branch: 1 branch
BranchTrueFalseTotal
if $count and $sum 1 0 1
Condition33%1/3
$count and $sum
!ll&&!rl&&r
001
MC/DC0%0/2
$count and $sum
$count $sum
Truth table33%1/3
A and B
A= $countB= $sum
ABresult
0X0
100
111
2099 1 push( @row, $avg );
2100 }
2101 else
2102 {
2103 0 return $self->{owner}->do_err("Invalid SET FUNCTION '$coldef->{name}'");
2104 }
2105 }
2106 else
2107 {
2108 24 push( @row, $result->{pure}->[$colidx] );
2109 }
2110 }
2111  
2112 32 return \@row;
2113 }
2114  
2115 sub calc()
2116 12 ▶{
Subroutine calc: called CC=1 SCAR=0.0
2117 12 my $self = $_[0];
2118  
2119 12 $self->{final_row} = {};
2120  
2121 12 $self->do_calc();
2122  
2123 12 my $final_row = $self->build_row( $self->{final_row} );
2124  
2125 12 return [$final_row];
2126 }
2127  
2128 sub getAffectedResult # (\@)
2129 4049 ▶{
Subroutine getAffectedResult: called CC=1 SCAR=0.0
2130 4049 return $_[0]->{final_row};
2131 }
2132  
2133 package SQL::Statement::Group;
2134  
2135 16 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
2136  
2137 16 ▶use Params::Util qw(_HASH);
Subroutine BEGIN: called CC=1 SCAR=0.0
2138  
2139 16 @ISA = qw(SQL::Statement::Aggregate);
2140  
2141 sub new
2142 7 ▶{
Subroutine new: called CC=1 SCAR=0.0
2143 7 my ( $class, $owner, $rows, $keycols ) = @_;
2144  
2145 7 my $self = $class->SUPER::new( $owner, $rows );
2146 7 $self->{keycols} = $keycols;
2147  
2148 7 return $self;
2149 }
2150  
2151 sub calc()
2152 7 ▶{
Subroutine calc: called CC=3 SCAR=11.0
2153 7 my $self = $_[0];
2154 7 my @final_table;
2155  
2156 7 $self->do_calc();
2157  
2158 7 ▶ if ( scalar( keys( %{ $self->{final_rows} } ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$self{'final_rows'};}) { } 6 1 7
2159 {
2160 6 foreach my $key ( keys( %{ $self->{final_rows} } ) )
2161 {
2162 19 my $final_row = $self->build_row( $self->{final_rows}->{$key} );
2163 19 push( @final_table, $final_row );
2164 }
2165 }
2166 else
2167 {
2168 1 my $final_row = $self->build_row( {} );
2169 1 push( @final_table, $final_row );
2170 }
2171  
2172 7 return \@final_table;
2173 }
2174  
2175 sub getAffectedResult # (\@)
2176 4024 ▶{
Subroutine getAffectedResult: called CC=2 SCAR=6.9
2177 4024 my ( $self, $row ) = @_;
2178  
2179 4024 my $rowkey = join( "\0", @$row[ @{ $self->{keycols} } ] );
2180  
2181 $self->{final_rows}->{$rowkey} = {}
2182 4024 ▶ unless ( defined( _HASH( $self->{final_rows}->{$rowkey} ) ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_HASH($self->{"final_rows"}{$rowkey}) 19 4005 4024
2183  
2184 4024 return $self->{final_rows}->{$rowkey};
2185 }
2186  
2187 package SQL::Statement::TempTable;
2188  
2189 16 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
2190  
2191 BEGIN
2192 16 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
2193 16 require SQL::Eval;
2194  
2195 16 @SQL::Statement::TempTable::ISA = qw(SQL::Eval::Table);
2196 }
2197  
2198 sub new
2199 44 ▶{
Subroutine new: called CC=2 SCAR=6.9
2200 44 my ( $class, $name, $col_names, $table_cols, $table ) = @_;
2201 44 my %col_nums;
2202 44 $col_nums{ $col_names->[$_] } = $_ for ( 0 .. scalar @$col_names - 1 );
2203 44 my @display_order = @col_nums{@$table_cols};
2204 44 my $self = {
2205 col_names => $col_names,
2206 table_cols => \@display_order,
2207 col_nums => \%col_nums,
2208 table => $table,
2209 NAME => $name,
2210 rowpos => 0,
2211 maxrow => scalar @$table
2212 };
2213 44 return $class->SUPER::new($self);
2214 }
2215  
2216 6 ▶sub is_shared($) { $_[0]->{is_shared}->{ $_[1] }; }
Subroutine is_shared: called CC=1 SCAR=0.0
2217 0 ▶sub get_pos() { $_[0]->{rowpos} }
Subroutine get_pos: not called CC=1 SCAR=6.9
2218  
2219 sub column_num($)
2220 129 ▶{
Subroutine column_num: called CC=3 SCAR=11.0
2221 129 my ( $s, $col ) = @_;
2222 129 my $new_col = $s->{col_nums}->{$col};
2223 129 ▶ unless ( defined($new_col) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new_col) 6 123 129
2224 {
2225 6 my @tmp = split( '~', $col );
2226 6 ▶ return unless ( 2 == scalar(@tmp) );
Branch: 1 branch
BranchTrueFalseTotal
unless 2 == scalar @tmp 5 1 6
2227 1 $new_col = lc( $tmp[0] ) . '~' . $tmp[1];
2228 1 $new_col = $s->{col_nums}->{$new_col};
2229 }
2230 124 return $new_col;
2231 }
2232  
2233 sub fetch_row()
2234 450 ▶{
Subroutine fetch_row: called CC=2 SCAR=6.9
2235 return $_[0]->{row} =
2236 ( $_[0]->{rowpos} >= $_[0]->{maxrow} )
2237 ? undef
2238 450 ▶ : $_[0]->{table}->[ $_[0]->{rowpos}++ ];
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'rowpos'} >= $_[0]{'maxrow'} ? : 44 406 450
2239 }
2240  
2241 1189 ▶sub column($) { return $_[0]->{row}->[ $_[0]->{col_nums}->{ $_[1] } ]; }
Subroutine column: called CC=1 SCAR=0.0
2242  
2243 package SQL::Statement::Order;
2244  
2245 sub new ($$)
2246 13 ▶{
Subroutine new: called CC=2 SCAR=7.6
2247 13 my $proto = shift;
2248 13 my $self = {@_};
2249 13 ▶ bless( $self, ( ref($proto) || $proto ) );
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0130
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
2250 }
2251 40 ▶sub table ($) { $_[0]->{col}->table(); }
Subroutine table: called CC=1 SCAR=0.0
2252 40 ▶sub column ($) { $_[0]->{col}->display_name(); }
Subroutine column: called CC=1 SCAR=0.0
2253 20 ▶sub desc ($) { $_[0]->{desc}; }
Subroutine desc: called CC=1 SCAR=0.0
2254 0 ▶sub direction ($) { $_[0]->{direction}; }
Subroutine direction: not called CC=1 SCAR=6.9
2255  
2256 package SQL::Statement::Limit;
2257  
2258 sub new ($$)
2259 9 ▶{
Subroutine new: called CC=2 SCAR=8.1
2260 9 my ( $proto, $self ) = @_;
2261 9 ▶ bless( $self, ( ref($proto) || $proto ) );
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
090
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
2262 }
2263  
2264 #sub limit ($) { shift->{limit}; }
2265 #sub offset ($) { shift->{offset}; }
2266  
2267 package SQL::Statement::Param;
2268  
2269 sub new
2270 36 ▶{
Subroutine new: called CC=1 SCAR=0.0
2271 36 my ( $class, $idx ) = @_;
2272 36 my $self = { 'idx' => $idx };
2273 36 return bless( $self, $class );
2274 }
2275  
2276 0 ▶sub idx ($) { $_[0]->{idx}; }
Subroutine idx: not called CC=1 SCAR=6.9
2277  
2278 package SQL::Statement::Table;
2279  
2280 sub new
2281 5222 ▶{
Subroutine new: called CC=2 SCAR=6.9
2282 5222 my ( $class, $table_name ) = @_;
2283  
2284 5222 ▶ if ( $table_name !~ m/"/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($table_name =~ /"/) 5194 28 5222
2285 {
2286 5194 $table_name = lc $table_name;
2287 }
2288  
2289 5222 my $self = {
2290 name => $table_name,
2291 };
2292  
2293 5222 return bless( $self, $class );
2294 }
2295  
2296 9281 ▶sub name { $_[0]->{name} }
Subroutine name: called CC=1 SCAR=0.0
2297  
2298 16 1;
2299 __END__
↑ Summary Parser.pm »