lib/DBIx/OptimalQuery.pm

filter: Statementstmt 4.1% 37 / 901 Branchbran 0.0% 0 / 432 Conditioncond 0.0% 0 / 170 MC/DCmcdc 0.0% 0 / 98 Subroutinesub 19.3% 11 / 57 Podpod 0.0% 0 / 8 totaltotal 2.8% 48 / 1666 CC 323 SCAR 64.8
CC
323
Cov
2%
__ANON__
1892.0
_normalize
1482.0
execute
1190.0
CRAP 649.0
« blib/lib/DBIx/OptimalQuery.pm ↑ Summary blib/lib/CGI/OptimalQuery/SaveSearchTool.pm »
1 package DBIx::OptimalQuery::sth;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no warnings qw( uninitialized once );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use DBI();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Parse::RecDescent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub Dumper {
13 0 ▶ local $Data::Dumper::Indent = 1;
Subroutine Dumper: not called CC=1 SCAR=6.9
14 0 local $Data::Dumper::SortKeys = 1;
15 0 Data::Dumper::Dumper(@_);
16 }
17  
18  
19 =comment
20 prepare a DBI sth from user defined selects, filters, sorts
21  
22 this constructor 'new' is called when a DBIx::OptimalQuery->prepare method
23 call is issued.
24  
25 my %opts = (
26 show => []
27 filter => ""
28 sort => ""
29 );
30  
31 $sth = $oq->prepare(%opts);
32 - same as -
33 $sth = DBIx::OptimalQuery::sth->new($oq,%opts);
34  
35 $sth->execute( limit => [0, 10]);
36 =cut
37 sub new {
38 0 ▶ my $class = shift;
Subroutine new: not called CC=2 SCAR=17.9
39 0 my $oq = shift;
40 0 my %args = @_;
41  
42 0 ▶ $$oq{error_handler}->("DEBUG: \$sth = $class->new(\$oq,\n".Dumper(\%args).")\n") if $$oq{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $oq->{"debug"} 0 0 0
43  
44 0 my $sth = bless \%args, $class;
45 0 $sth->{oq} = $oq;
46 0 $sth->_normalize();
47 0 $sth->create_select();
48 0 $sth->create_where();
49 0 $sth->create_order_by();
50  
51 0 return $sth;
52 }
53  
54 0 ▶sub get_lo_rec { $_[0]{limit}[0] }
Subroutine get_lo_rec: not called CC=1 SCAR=6.9
55 0 ▶sub get_hi_rec { $_[0]{limit}[1] }
Subroutine get_hi_rec: not called CC=1 SCAR=6.9
56  
57 sub set_limit {
58 0 ▶ my ($sth, $limit) = @_;
Subroutine set_limit: not called CC=1 SCAR=6.9
59 0 $$sth{limit} = $limit;
60 0 return undef;
61 }
62  
63 # execute statement
64 # notice that we can't execute other child cursors
65 # because their bind params are dependant on
66 # their parent cursor value
67 sub execute {
68 0 ▶ my ($sth) = @_;
Subroutine execute: not called CC=34 SCAR=70.8
69 0 ▶ return undef if $$sth{_already_executed};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"_already_executed"} 0 0 0
70 0 $$sth{_already_executed}=1;
71  
72 0 ▶ $$sth{oq}{error_handler}->("DEBUG: \$sth->execute()\n") if $$sth{oq}{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"oq"}{"debug"} 0 0 0
73 0 ▶ return undef if $sth->count()==0;
Branch: 1 branch
BranchTrueFalseTotal
if $sth->count == 0 0 0 0
74  
75 0 local $$sth{oq}{dbh}{LongReadLen};
76  
77 # build SQL for main cursor
78 0 { my $c = $sth->{cursors}->[0];
79 0 my @all_deps = (@{$c->{select_deps}}, @{$c->{where_deps}}, @{$c->{order_by_deps}});
80 0 my ($order) = @{ $sth->{oq}->_order_deps(\@all_deps) };
81 0 my @from_deps; push @from_deps, @$_ for @$order;
82  
83 # create from_sql, from_binds
84 # vars prefixed with old_ is used for supported non sql-92 joins
85 0 my ($from_sql, @from_binds, $old_join_sql, @old_join_binds );
86  
87 0 foreach my $from_dep (@from_deps) {
88 0 my ($sql, @binds) = @{ $sth->{oq}->{joins}->{$from_dep}->[1] };
89 0 ▶ push @from_binds, @binds if @binds;
Branch: 1 branch
BranchTrueFalseTotal
if @binds 0 0 0
90  
91 # if this is the driving table join
92 0 ▶ if (! $sth->{oq}->{joins}->{$from_dep}->[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $sth->{'oq'}{'joins'}{$from_dep}[0]) { } 0 0 0
elsif (not defined $sth->{'oq'}{'joins'}{$from_dep}[2]) { } 0 0 0
93  
94 # alias it if not already aliased in sql
95 0 $from_sql .= $sql.' ';
96 0 ▶ $from_sql .= "$from_dep" unless $sql =~ /\b$from_dep\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sql =~ /\b$from_dep\s*$/ 0 0 0
97 0 $from_sql .= "\n";
98 }
99
100
101 # if SQL-92 type join?
102 elsif (! defined $sth->{oq}->{joins}->{$from_dep}->[2]) {
103 0 $from_sql .= $sql."\n";
104 }
105
106 # old style join
107 else {
108 0 $from_sql .= ", ".$sql.' '.$from_dep."\n";
109 0 my ($sql, @binds) = @{ $sth->{oq}->{joins}->{$from_dep}->[2] };
110 0 ▶ $old_join_sql .= " AND " if $old_join_sql ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $old_join_sql ne "" 0 0 0
111 0 $old_join_sql .= $sql;
112 0 push @old_join_binds, @binds;
113 }
114 }
115
116
117 # construct where clause
118 0 my $where;
119 0 { my @where;
120 0 ▶ push @where, '('.$old_join_sql.') ' if $old_join_sql;
Branch: 1 branch
BranchTrueFalseTotal
if $old_join_sql 0 0 0
121 0 ▶ push @where, '('.$c->{where_sql}.') ' if $c->{where_sql};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"where_sql"} 0 0 0
122 0 ▶ $where = ' WHERE '.join(' AND ', @where) if @where;
Branch: 1 branch
BranchTrueFalseTotal
if @where 0 0 0
123 }
124
125 # generate sql and bind params
126 0 ▶ $$c{sql} = "SELECT ".join(',', @{ $c->{select_sql} })." FROM $from_sql $where ".
Branch: 1 branch
BranchTrueFalseTotal
$c->{'order_by_sql'} ? : 0 0 0
127 0 (($c->{order_by_sql}) ? "ORDER BY ".$c->{order_by_sql} : '');
128  
129 0 my @binds = (@{ $c->{select_binds} }, @from_binds, @old_join_binds,
130 0 @{$c->{where_binds}}, @{$c->{order_by_binds}} );
131 0 $$c{binds} = \@binds;
132  
133 # if clobs have been selected, find & set LongReadLen
134 0 ▶ if ($$sth{oq}{dbtype} eq 'Oracle' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}{"AutoSetLongReadLen"} and scalar @{$c->{"selected_lobs"};}) 0 0 0
Condition0%0/5
$sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}{"AutoSetLongReadLen"} and scalar @{$c->{"selected_lobs"};}
!ll
00
$sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}{"AutoSetLongReadLen"}
!ll&&!rl&&r
000
MC/DC0%0/2
$sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}{"AutoSetLongReadLen"} and scalar @{$c->{"selected_lobs"};}
$sth->{"oq"}{"dbtype"} eq "Oracle" $sth->{"oq"}{"AutoSetLongReadLen"}
Truth table0%0/3
A and B
A= $sth->{"oq"}{"dbtype"} eq "Oracle"B= $sth->{"oq"}{"AutoSetLongReadLen"}
ABresult
0X0
100
111
135 $$sth{'oq'}{'AutoSetLongReadLen'} &&
136 0 scalar(@{$$c{'selected_lobs'}})) {
137  
138 my $maxlenlobsql = "SELECT greatest(".join(',',
139 0 map { "nvl(max(DBMS_LOB.GETLENGTH($_)),0)" } @{$$c{'selected_lobs'}}
140 0 ).") FROM (".$$c{'sql'}.")";
141  
142 0 my ($SetLongReadLen) = $$sth{oq}{dbh}->selectrow_array($maxlenlobsql, undef, @{$$c{'binds'}});
143  
144 0 ▶ if (! $$sth{oq}{dbh}{LongReadLen} || $SetLongReadLen > $$sth{oq}{dbh}{LongReadLen}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sth->{"oq"}{"dbh"}{"LongReadLen"} or $SetLongReadLen > $sth->{"oq"}{"dbh"}{"LongReadLen"}) 0 0 0
Condition0%0/3
not $sth->{"oq"}{"dbh"}{"LongReadLen"} or $SetLongReadLen > $sth->{"oq"}{"dbh"}{"LongReadLen"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $sth->{"oq"}{"dbh"}{"LongReadLen"} or $SetLongReadLen > $sth->{"oq"}{"dbh"}{"LongReadLen"}
not $sth->{"oq"}{"dbh"}{"LongReadLen"} $SetLongReadLen > $sth->{"oq"}{"dbh"}{"LongReadLen"}
Truth table0%0/3
not(A) or B
A= not $sth->{"oq"}{"dbh"}{"LongReadLen"}B= $SetLongReadLen > $sth->{"oq"}{"dbh"}{"LongReadLen"}
ABresult
1X1
011
000
145 0 $$sth{oq}{dbh}{LongReadLen} = $SetLongReadLen;
146 }
147 }
148  
149 0 $sth->add_limit_sql();
150 }
151  
152  
153 # build children cursors
154 0 my $cursors = $sth->{cursors};
155 0 foreach my $i (1 .. $#$cursors) {
156 0 my $c = $sth->{cursors}->[$i];
157 0 my $sd = $c->{select_deps};
158  
159 # define sql and binds for joins for this child cursor
160 # in the following vars
161 0 my ($from_sql, @from_binds, $where_sql, @where_binds );
162  
163 # define vars for child cursor driving table
164 # these are handled differently since we aren't joining in parent deps
165 # they were precomputed in _normalize method when constructing $oq
166  
167 ($from_sql, @from_binds) =
168 0 @{ $sth->{oq}->{joins}->{$sd->[0]}->[3]->{new_cursor}->{sql} };
169 0 $where_sql = $sth->{oq}->{joins}->{$sd->[0]}->[3]->{new_cursor}->{'join'};
170 0 my $order_by_sql = '';
171 0 ▶ if ($sth->{oq}->{joins}->{$sd->[0]}->[3]->{new_cursor_order_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"oq"}{"joins"}{$sd->[0]}[3]{"new_cursor_order_by"}) 0 0 0
172 0 $order_by_sql = " ORDER BY ".$sth->{oq}->{joins}->{$sd->[0]}->[3]->{new_cursor_order_by};
173 }
174  
175 0 $from_sql .= "\n";
176  
177 # now join in all other deps normally for this cursor
178 0 foreach my $i (1 .. $#$sd) {
179 0 my $joinAlias = $sd->[$i];
180  
181 0 my ($sql, @binds) = @{ $sth->{oq}->{joins}->{$joinAlias}->[1] };
182  
183 # these will NOT be defined for sql-92 type joins
184 my ($joinWhereSql, @joinWhereBinds) =
185 0 @{ $sth->{oq}->{joins}->{$joinAlias}->[2] }
186 0 ▶ if defined $sth->{oq}->{joins}->{$joinAlias}->[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $sth->{"oq"}{"joins"}{$joinAlias}[2] 0 0 0
187  
188 # if SQL-92 type join?
189 0 ▶ if (! defined $joinWhereSql) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $joinWhereSql) { } 0 0 0
190 0 $from_sql .= $sql."\n";
191 0 push @from_binds, @binds;
192 }
193  
194 # old style join
195 else {
196 0 $from_sql .= ",\n$sql $joinAlias";
197 0 push @from_binds, @binds;
198 0 ▶ if ($joinWhereSql) {
Branch: 1 branch
BranchTrueFalseTotal
if ($joinWhereSql) 0 0 0
199 0 ▶ $where_sql .= " AND " if $where_sql;
Branch: 1 branch
BranchTrueFalseTotal
if $where_sql 0 0 0
200 0 $where_sql .= $joinWhereSql;
201 }
202 0 push @where_binds, @joinWhereBinds;
203 }
204 }
205  
206 # build child cursor sql
207 $c->{sql} = "
208 0 SELECT ".join(',', @{ $c->{select_sql} })."
209 FROM $from_sql
210 WHERE $where_sql
211 $order_by_sql ";
212 0 $c->{binds} = [ @{ $c->{select_binds} }, @from_binds, @where_binds ];
213  
214 # if clobs have been selected, find & set LongReadLen
215 0 ▶ if ($$sth{oq}{dbtype} eq 'Oracle' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}{"AutoSetLongReadLen"} and scalar @{$c->{"selected_lobs"};}) 0 0 0
Condition0%0/5
$sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}{"AutoSetLongReadLen"} and scalar @{$c->{"selected_lobs"};}
!ll
00
$sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}{"AutoSetLongReadLen"}
!ll&&!rl&&r
000
MC/DC0%0/2
$sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}{"AutoSetLongReadLen"} and scalar @{$c->{"selected_lobs"};}
$sth->{"oq"}{"dbtype"} eq "Oracle" $sth->{"oq"}{"AutoSetLongReadLen"}
Truth table0%0/3
A and B
A= $sth->{"oq"}{"dbtype"} eq "Oracle"B= $sth->{"oq"}{"AutoSetLongReadLen"}
ABresult
0X0
100
111
216 $$sth{'oq'}{'AutoSetLongReadLen'} &&
217 0 scalar(@{$$c{'selected_lobs'}})) {
218 my ($SetLongReadLen) = $$sth{oq}{dbh}->selectrow_array("
219 SELECT greatest(".join(',',
220 0 map { "nvl(max(DBMS_LOB.GETLENGTH($_)),0)" } @{$$c{'selected_lobs'}}
221 ).")
222 0 FROM (".$$c{'sql'}.")", undef, @{$$c{'binds'}});
223 0 ▶ if (! $$sth{oq}{dbh}{LongReadLen} || $SetLongReadLen > $$sth{oq}{dbh}{LongReadLen}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sth->{"oq"}{"dbh"}{"LongReadLen"} or $SetLongReadLen > $sth->{"oq"}{"dbh"}{"LongReadLen"}) 0 0 0
Condition0%0/3
not $sth->{"oq"}{"dbh"}{"LongReadLen"} or $SetLongReadLen > $sth->{"oq"}{"dbh"}{"LongReadLen"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $sth->{"oq"}{"dbh"}{"LongReadLen"} or $SetLongReadLen > $sth->{"oq"}{"dbh"}{"LongReadLen"}
not $sth->{"oq"}{"dbh"}{"LongReadLen"} $SetLongReadLen > $sth->{"oq"}{"dbh"}{"LongReadLen"}
Truth table0%0/3
not(A) or B
A= not $sth->{"oq"}{"dbh"}{"LongReadLen"}B= $SetLongReadLen > $sth->{"oq"}{"dbh"}{"LongReadLen"}
ABresult
1X1
011
000
224 0 $$sth{oq}{dbh}{LongReadLen} = $SetLongReadLen;
225 }
226 }
227 }
228  
229 0 eval {
230 0 my $c;
231  
232 # prepare all cursors
233 0 foreach $c (@$cursors) {
234 0 $c->{sth} = $sth->{oq}->{dbh}->prepare($c->{sql});
235 }
236 0 $c = $$cursors[0];
237 0 $c->{sth}->execute( @{ $c->{binds} } );
238 0 my @fieldnames = @{ $$c{select_field_order} };
239 0 my %rec;
240 0 my @bindcols = \( @rec{ @fieldnames } );
241 0 $c->{sth}->bind_columns(@bindcols);
242 0 $c->{bind_hash} = \%rec;
243 };
244 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
245 0 die "Problem with SQL; $@\n";
246 }
247 0 return undef;
248 }
249  
250 # function to add limit sql
251 # $sth->add_limit_sql()
252 sub add_limit_sql {
253 0 ▶ my ($sth) = @_;
Subroutine add_limit_sql: not called CC=13 SCAR=52.0
254  
255 0 ▶ $$sth{oq}{error_handler}->("DEBUG: \$sth->add_limit_sql()\n") if $$sth{oq}{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"oq"}{"debug"} 0 0 0
256 0 ▶ my $lo_limit = $$sth{limit}[0] || 0;
Condition0%0/2
$sth->{'limit'}[0] || 0
l!l
00
MC/DC0%0/1
$sth->{'limit'}[0] || 0
$sth->{'limit'}[0]
Truth table0%0/2
A
A= $sth->{'limit'}[0]
Aresult
11
00
257 0 ▶ my $hi_limit = $$sth{limit}[1] || $sth->count();
Condition0%0/3
$sth->{'limit'}[1] || $sth->count
l!l&&r!l&&!r
000
MC/DC0%0/2
$sth->{'limit'}[1] || $sth->count
$sth->{'limit'}[1] $sth->count
Truth table0%0/3
A || B
A= $sth->{'limit'}[1]B= $sth->count
ABresult
1X1
011
000
258 0 my $c = $sth->{cursors}->[0];
259  
260 0 ▶ if ($$sth{oq}{dbtype} eq 'Oracle') {
Branch: 3 branches
BranchTrueFalseTotal
if ($sth->{'oq'}{'dbtype'} eq 'Oracle') { } 0 0 0
elsif ($sth->{'oq'}{'dbtype'} eq 'Microsoft SQL Server') { } 0 0 0
elsif ($sth->{'oq'}{'dbtype'} eq 'Pg') { } 0 0 0
261 $c->{sql} = "
262 SELECT *
263 FROM (
264 SELECT tablernk1.*, rownum RANK
265 FROM (
266 0 ".$c->{sql}."
267 ) tablernk1
268 WHERE rownum <= ?
269 ) tablernk2
270 WHERE tablernk2.RANK >= ? ";
271 0 push @{$$c{binds}}, ($hi_limit, $lo_limit);
272 0 push @{$$c{select_field_order}}, "DBIXOQRANK";
273 }
274  
275 # sqlserver doesn't support limit/offset until Sql Server 2012 (which I don't have to test)
276 # the workaround is this ugly hack...
277 elsif ($$sth{oq}{dbtype} eq 'Microsoft SQL Server') {
278 0 ▶ die "missing required U_ID in select" unless exists $$sth{oq}{select}{U_ID};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $sth->{"oq"}{"select"}{"U_ID"} 0 0 0
279  
280 0 my $sql = $c->{sql};
281  
282 # extract order by sql, and binds in order by from sql
283 0 my $orderbysql;
284 0 ▶ if ($sql =~ s/\ (ORDER BY\ .*?)$//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sql =~ s/\ (ORDER BY\ .*?)$//) { } 0 0 0
elsif (exists $sth->{'oq'}{'select'}{'U_ID'}) { } 0 0 0
285 0 $orderbysql = $1;
286 0 my $copy = $orderbysql;
287 0 my $bindCount = $copy =~ tr/,//;
288 0 ▶ if ($bindCount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bindCount > 0) 0 0 0
289 0 my @newBinds;
290 0 push @newBinds, pop @{$$c{binds}} for 1 .. $bindCount;
291 0 @{$$c{binds}} = (reverse @newBinds, @{$$c{binds}});
292 }
293 0 $orderbysql .= ", ".$$sth{oq}{select}{U_ID}[1][0];
294 } elsif (exists $$sth{oq}{select}{U_ID}) {
295 0 $orderbysql = " ORDER BY ".$$sth{oq}{select}{U_ID}[1][0];
296 }
297  
298 # remove first select keyword, and add new one with windowing
299 0 ▶ if ($sql =~ s/^(\s*SELECT\s*)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ s/^(\s*SELECT\s*)//) 0 0 0
300 0 my $limit = int($hi_limit - $lo_limit + 1);
301 0 my $lo_limit = int($lo_limit);
302  
303 # sqlserver doesn't allow placeholders for limit and offset here
304 0 $c->{sql} = "SELECT TOP $limit * FROM (SELECT ROW_NUMBER() OVER ($orderbysql) AS RANK, $sql) tablerank1 WHERE tablerank1.RANK >= $lo_limit";
305 0 unshift @{$$c{select_field_order}}, "DBIXOQRANK";
306 }
307 }
308  
309 elsif ($$sth{oq}{dbtype} eq 'Pg') {
310 0 my $a = $lo_limit - 1;
311 0 my $b = $hi_limit - $lo_limit + 1;
312 0 $c->{sql} .= "\nLIMIT ? OFFSET ?";
313 0 push @{$$c{binds}}, ($b, $a);
314 }
315  
316 else {
317 0 my $a = $lo_limit - 1;
318 0 my $b = $hi_limit - $lo_limit + 1;
319 0 $c->{sql} .= "\nLIMIT ?,?";
320 0 push @{$$c{binds}}, ($a, $b);
321 }
322  
323 0 return undef;
324 }
325  
326  
327 # normalize member variables
328 sub _normalize {
329 0 ▶ my $sth = shift;
Subroutine _normalize: not called CC=6 SCAR=37.4
330 0 ▶ $$sth{oq}{error_handler}->("DEBUG: \$sth->_normalize()\n") if $$sth{oq}{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"oq"}{"debug"} 0 0 0
331  
332 # if show is not defined - then define it
333 0 ▶ if (! exists $sth->{show}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $sth->{"show"}) 0 0 0
334 0 my @select;
335 0 foreach my $select (@{ $sth->{oq}->{'select'} } ) {
336 0 push @select, $select;
337 }
338 0 $sth->{show} = \@select;
339 }
340  
341 # define filter & sort if not defined
342 0 ▶ $sth->{'filter'} = "" if ! exists $sth->{'filter'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $sth->{"filter"} 0 0 0
343 0 ▶ $sth->{'sort'} = "" if ! exists $sth->{'sort'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $sth->{"sort"} 0 0 0
344 0 $sth->{'fetch_index'} = 0;
345 0 $sth->{'count'} = undef;
346 0 $sth->{'cursors'} = undef;
347  
348 0 return undef;
349 }
350  
351  
352  
353 # define @select & @select_binds, and add deps
354 sub create_select {
355 0 ▶ my $sth = shift;
Subroutine create_select: not called CC=27 SCAR=66.3
356 0 ▶ $$sth{oq}{error_handler}->("DEBUG: \$sth->create_select()\n") if $$sth{oq}{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"oq"}{"debug"} 0 0 0
357  
358
359 # find all of the columns that need to be shown
360 0 my %show;
361  
362 # find all deps to be used in select including cols marked always_select
363 0 my (@deps, @select_sql, @select_binds);
364 0 { my %deps;
365  
366 # add deps, @select, @select_binds for items in show
367 0 foreach my $show (@{ $sth->{show} }) {
368 0 ▶ $show{$show} = 1 if exists $sth->{'oq'}->{'select'}->{$show};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sth->{"oq"}{"select"}{$show} 0 0 0
369 0 foreach my $dep (@{ $sth->{'oq'}->{'select'}->{$show}->[0] }) {
370 0 $deps{$dep} = 1;
371 }
372 }
373  
374 # add deps used in always_select
375 0 foreach my $colAlias (keys %{ $sth->{'oq'}->{'select'} }) {
376 0 ▶ if ($sth->{'oq'}->{'select'}->{$colAlias}->[3]->{always_select} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"oq"}{"select"}{$colAlias}[3]{"always_select"}) 0 0 0
377 0 $show{$colAlias} = 1;
378 0 $deps{$_} = 1 for @{ $sth->{'oq'}->{'select'}->{$colAlias}->[0] };
379 }
380 }
381 0 @deps = keys %deps;
382 }
383  
384 # order and index deps into appropriate cursors
385 0 my ($dep_order, $dep_idx) = @{ $sth->{oq}->_order_deps(\@deps) };
386  
387 # look though select again and add all cols with is_hidden option
388 # if all their deps have been fulfilled
389 0 foreach my $colAlias (keys %{ $sth->{'oq'}->{'select'} }) {
390 0 ▶ if ($sth->{'oq'}->{'select'}->{$colAlias}->[3]->{is_hidden}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"oq"}{"select"}{$colAlias}[3]{"is_hidden"}) 0 0 0
391 0 my $deps = $sth->{'oq'}->{'select'}->{$colAlias}->[0];
392 0 my $all_deps_met = 1;
393 0 for (@$deps) {
394 0 ▶ if (! exists $dep_idx->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $dep_idx->{$_}) 0 0 0
395 0 $all_deps_met = 0;
396 0 last;
397 }
398 }
399 0 ▶ $show{$colAlias} = 1 if $all_deps_met;
Branch: 1 branch
BranchTrueFalseTotal
if $all_deps_met 0 0 0
400 }
401 }
402  
403 # create main cursor structure & attach deps for main cursor
404 0 $sth->{'cursors'} = [ $sth->_get_main_cursor_template() ];
405 0 $sth->{'cursors'}->[0]->{'select_deps'} = $dep_order->[0];
406  
407 # unique counter that is used to uniquely identify cols in parent cursors
408 # to their children cursors
409 0 my $parent_bind_tag_idx = 0;
410  
411 # create other cursors (if they exist)
412 # and define how they join to their parent cursors
413 # by defining parent_join, parent_keys
414 0 foreach my $i (1 .. $#$dep_order) {
415 0 push @{ $sth->{'cursors'} }, $sth->_get_sub_cursor_template();
416 0 $sth->{'cursors'}->[$i]->{'select_deps'} = $dep_order->[$i];
417  
418 # add parent_join, parent_keys for this child cursor
419 0 my $driving_child_join_alias = $dep_order->[$i]->[0];
420 0 my $cursor_opts = $sth->{'oq'}->{'joins'}->{$driving_child_join_alias}->[3]->{new_cursor};
421 0 foreach my $part (@{ $cursor_opts->{'keys'} } ) {
422 0 my ($dep,$sql) = @$part;
423 0 my $key = 'DBIXOQMJK'.$parent_bind_tag_idx; $parent_bind_tag_idx++;
424 0 my $parent_cursor_idx = $dep_idx->{$dep};
425 0 ▶ die "could not find dep: $dep for new cursor" if $parent_cursor_idx eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $parent_cursor_idx eq "" 0 0 0
426 0 push @{ $sth->{'cursors'}->[$parent_cursor_idx]->{select_field_order} }, $key;
427 0 push @{ $sth->{'cursors'}->[$parent_cursor_idx]->{select_sql} }, "$dep.$sql AS $key";
428 0 push @{ $sth->{'cursors'}->[$i]->{'parent_keys'} }, $key;
429 }
430 0 $sth->{'cursors'}->[$i]->{'parent_join'} = $cursor_opts->{'join'};
431 }
432
433 # plug in select_sql, select_binds for cursors
434 0 foreach my $show (keys %show) {
435 0 my $select = $sth->{'oq'}->{'select'}->{$show};
436 0 ▶ next if ! $select;
Branch: 1 branch
BranchTrueFalseTotal
unless $select 0 0 0
437  
438 0 my $cursor = $sth->{'cursors'}->[$dep_idx->{$select->[0]->[0]}];
439  
440 0 my $select_sql;
441  
442 # if type is date then use specified date format
443 0 ▶ if (! $$select[3]{select_sql} && $$select[3]{date_format}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $select->[3]{'select_sql'} and $select->[3]{'date_format'}) { } 0 0 0
Condition0%0/3
not $select->[3]{"select_sql"} and $select->[3]{"date_format"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $select->[3]{"select_sql"} and $select->[3]{"date_format"}
not $select->[3]{"select_sql"} $select->[3]{"date_format"}
Truth table0%0/3
not(A) and B
A= not $select->[3]{"select_sql"}B= $select->[3]{"date_format"}
ABresult
0X0
100
111
444 0 my @tmp = @{ $select->[1] }; $select_sql = \ @tmp; # need a real copy cause we are going to mutate it
445 0 ▶ if ($$sth{oq}{dbtype} eq 'Oracle' ||
Branch: 2 branches
BranchTrueFalseTotal
if ($sth->{'oq'}{'dbtype'} eq 'Oracle' or $sth->{'oq'}{'dbtype'} eq 'Pg') { } 0 0 0
elsif ($sth->{'oq'}{'dbtype'} eq 'mysql') { } 0 0 0
Condition0%0/3
$sth->{"oq"}{"dbtype"} eq "Oracle" or $sth->{"oq"}{"dbtype"} eq "Pg"
l!l&&r!l&&!r
000
MC/DC0%0/2
$sth->{"oq"}{"dbtype"} eq "Oracle" or $sth->{"oq"}{"dbtype"} eq "Pg"
$sth->{"oq"}{"dbtype"} eq "Oracle" $sth->{"oq"}{"dbtype"} eq "Pg"
Truth table0%0/3
A or B
A= $sth->{"oq"}{"dbtype"} eq "Oracle"B= $sth->{"oq"}{"dbtype"} eq "Pg"
ABresult
1X1
011
000
446 $$sth{oq}{dbtype} eq 'Pg') {
447 0 $$select_sql[0] = "to_char(".$$select_sql[0].",'".$$select[3]{date_format}."')";
448 } elsif ($$sth{oq}{dbtype} eq 'mysql') {
449 0 $$select_sql[0] = "date_format(".$$select_sql[0].",'".$$select[3]{date_format}."')";
450 } else {
451 0 die "unsupported DB";
452 }
453 }
454  
455 # else just copy the select
456 else {
457 0 ▶ $select_sql = $select->[3]->{select_sql} || $select->[1];
Condition0%0/3
$select->[3]{'select_sql'} || $select->[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$select->[3]{'select_sql'} || $select->[1]
$select->[3]{'select_sql'} $select->[1]
Truth table0%0/3
A || B
A= $select->[3]{'select_sql'}B= $select->[1]
ABresult
1X1
011
000
458 }
459  
460 # remember if a lob is selected
461 0 ▶ if ($$sth{oq}{dbtype} eq 'Oracle' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}->get_col_types("select")->{$show} eq "clob") 0 0 0
Condition0%0/3
$sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}->get_col_types("select")->{$show} eq "clob"
!ll&&!rl&&r
000
MC/DC0%0/2
$sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}->get_col_types("select")->{$show} eq "clob"
$sth->{"oq"}{"dbtype"} eq "Oracle" $sth->{"oq"}->get_col_types("select")->{$show} eq "clob"
Truth table0%0/3
A and B
A= $sth->{"oq"}{"dbtype"} eq "Oracle"B= $sth->{"oq"}->get_col_types("select")->{$show} eq "clob"
ABresult
0X0
100
111
462 $sth->{oq}->get_col_types('select')->{$show} eq 'clob') {
463 0 push @{ $cursor->{selected_lobs} }, $show;
464 #$select_sql->[0] = 'to_char('.$select_sql->[0].')';
465 }
466  
467 0 ▶ if ($select_sql->[0] ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($select_sql->[0] ne "") 0 0 0
468 0 push @{ $cursor->{select_field_order} }, $show;
469 0 push @{ $cursor->{select_sql} }, $select_sql->[0].' AS '.$show;
470 0 push @{ $cursor->{select_binds} }, @$select_sql[1 .. $#$select_sql];
471 }
472 }
473  
474 0 return undef;
475 }
476
477  
478  
479  
480 # template for the main cursor
481 sub _get_main_cursor_template {
482 0 ▶ { sth => undef,
Subroutine _get_main_cursor_template: not called CC=1 SCAR=6.9
483 sql => "",
484 binds => [],
485 selected_lobs => [],
486 select_field_order => [],
487 select_sql => [],
488 select_binds => [],
489 select_deps => [],
490 where_sql => "",
491 where_binds => [],
492 where_deps => [],
493 where_name => "",
494 order_by_sql => "",
495 order_by_binds => [],
496 order_by_deps => [],
497 order_by_name => []
498 };
499 }
500  
501 # template for explicitly defined additional cursors
502 sub _get_sub_cursor_template {
503 0 ▶ { sth => undef,
Subroutine _get_sub_cursor_template: not called CC=1 SCAR=6.9
504 sql => "",
505 binds => [],
506 selected_lobs => [],
507 select_field_order => [],
508 select_sql => [],
509 select_deps => [],
510 select_binds => [],
511 parent_join => "",
512 parent_keys => [],
513 };
514 }
515  
516  
517  
518  
519
520
521  
522  
523 # modify cursor and add where clause data
524 sub create_where {
525 0 ▶ my $sth = shift;
Subroutine create_where: not called CC=13 SCAR=52.0
526  
527 0 ▶ $$sth{oq}{error_handler}->("DEBUG: \$sth->create_where()\n") if $$sth{oq}{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"oq"}{"debug"} 0 0 0
528 0 ▶ return undef if $sth->{'filter'} eq '' && $sth->{'hiddenFilter'} eq '' && $sth->{'forceFilter'} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"filter"} eq "" and $sth->{"hiddenFilter"} eq "" and $sth->{"forceFilter"} eq "" 0 0 0
Condition0%0/6
$sth->{"filter"} eq "" and $sth->{"hiddenFilter"} eq "" and $sth->{"forceFilter"} eq ""
!ll&&!rl&&r
000
$sth->{"filter"} eq "" and $sth->{"hiddenFilter"} eq ""
!ll&&!rl&&r
000
MC/DC0%0/3
$sth->{"filter"} eq "" and $sth->{"hiddenFilter"} eq "" and $sth->{"forceFilter"} eq ""
$sth->{"filter"} eq "" $sth->{"hiddenFilter"} eq "" $sth->{"forceFilter"} eq ""
Truth table0%0/4
A and B and C
A= $sth->{"filter"} eq ""B= $sth->{"hiddenFilter"} eq ""C= $sth->{"forceFilter"} eq ""
ABCresult
0XX0
10X0
1100
1111
529  
530 # this sub glues together a parsed expression
531 # basically is glues statements that look like:
532 # '(' { sql => '', binds => [], deps => [], name => '' } 'LIKE'
533 # { sql => '', binds => [], deps => [], name => '' } ')'
534 # and then returns a single hash
535 my $glue_exp = sub {
536 0 ▶ my @deps;
Subroutine __ANON__: not called CC=7 SCAR=40.3
537 0 my $sql = '';
538 0 my @binds;
539 my $name;
540 0 foreach my $i (@_) {
541 0 ▶ if (! ref($i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $i) { } 0 0 0
542 0 $sql .= $i.' ';
543 0 $name .= $i.' ';
544 } else {
545 0 ▶ push @deps, @{ $$i{deps} } if ref($$i{deps}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $i->{"deps"} eq "ARRAY" 0 0 0
546 0 ▶ push @binds, @{ $$i{binds} } if ref($$i{binds}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $i->{"binds"} eq "ARRAY" 0 0 0
547 0 ▶ $sql .= $$i{sql}.' ' if exists $$i{sql};
Branch: 1 branch
BranchTrueFalseTotal
if exists $i->{"sql"} 0 0 0
548 0 ▶ $name .= $$i{name}.' ' if exists $$i{name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $i->{"name"} 0 0 0
549 }
550 }
551 0 my $rv = { deps=> \@deps, sql => $sql,
552 binds => \@binds, name => $name};
553 0 return $rv;
554 0 };
555  
556 my %translations = (
557 0 ▶ '*default*' => sub { $_[2] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
558  
559 'compOp' => sub {
560 0 ▶ my $rv = { name => lc($_[2]), sql => uc($_[2]) };
Subroutine __ANON__: not called CC=3 SCAR=24.8
561 0 ▶ if (uc($_[2]) eq 'CONTAINS') { $$rv{sql} = 'LIKE'; }
Branch: 2 branches
BranchTrueFalseTotal
if (uc $_[2] eq 'CONTAINS') { } 0 0 0
elsif (uc $_[2] eq 'NOT CONTAINS') { } 0 0 0
562 0 elsif (uc($_[2]) eq 'NOT CONTAINS') { $$rv{sql} = 'NOT LIKE'; }
563 0 return $rv;
564 },
565  
566 'colAlias' => sub {
567 0 ▶ my $oq = $_[0];
Subroutine __ANON__: not called CC=3 SCAR=24.8
568 0 my $colAlias = $_[3];
569 0 ▶ die "could not find colAlias $colAlias" unless exists $$oq{select}{$colAlias};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $oq->{"select"}{$colAlias} 0 0 0
570 0 my $deps = $$oq{select}{$colAlias}[0];
571 0 ▶ my @tmp = @{ $$oq{select}{$colAlias}[3]{filter_sql} || $$oq{select}{$colAlias}[1] };
Branch: 1 branch
BranchTrueFalseTotal
$oq->{"select"}{$colAlias}[3]{"filter_sql"} or $oq->{"select"}{$colAlias}[1] 0 0 0
572 0 my $sql = shift @tmp;
573 0 my $binds = \ @tmp;
574 0 my $name = $$oq{select}{$colAlias}[2];
575 0 my $rv = { colAlias => $colAlias, deps => $deps, sql => $sql, binds => $binds, name => '['.$name.']'};
576 0 return $rv;
577 },
578  
579 'bindVal' => sub {
580 0 ▶ my $val = $_[2];
Subroutine __ANON__: not called CC=2 SCAR=17.9
581 0 my $nice = $val;
582 0 ▶ $nice = "'".$nice."'" if $nice !~ /^[\d\.\-]+/;
Branch: 1 branch
BranchTrueFalseTotal
unless $nice =~ /^[\d\.\-]+/ 0 0 0
583 0 return { sql => '?', binds => [$val], name => $nice };
584 },
585  
586 'quotedString' => sub {
587 0 ▶ my ($v) = $_[2];
Subroutine __ANON__: not called CC=4 SCAR=30.0
588 0 ▶ ($v=~s/^\'// && $v=~s/\'$//) || ($v=~s/^\"// && $v=~s/\"$//);
Branch: 1 branch
BranchTrueFalseTotal
$v =~ s/^\'// and $v =~ s/\'$// or $v =~ s/^\"// and $v =~ s/\"$// 0 0 0
Condition0%0/9
$v =~ s/^\'// && $v =~ s/\'$// || $v =~ s/^\"// && $v =~ s/\"$//
l!l&&r!l&&!r
000
$v =~ s/^\'// and $v =~ s/\'$//
!ll&&!rl&&r
000
$v =~ s/^\"// and $v =~ s/\"$//
!ll&&!rl&&r
000
MC/DC0%0/4
$v =~ s/^\'// && $v =~ s/\'$// || $v =~ s/^\"// && $v =~ s/\"$//
$v =~ s/^\'// $v =~ s/\'$// $v =~ s/^\"// $v =~ s/\"$//
Truth table0%0/7
A and B || C and D
A= $v =~ s/^\'//B= $v =~ s/\'$//C= $v =~ s/^\"//D= $v =~ s/\"$//
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
589 0 return $v;
590 },
591  
592 'exp' => sub {
593 0 ▶ my $oq = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
594 0 my $rule = shift;
595 0 return $glue_exp->(@_);
596 },
597  
598  
599 'comparisonExp' => sub {
600 0 ▶ my $oq = shift;
Subroutine __ANON__: not called CC=43 SCAR=75.5
601 0 my $rule = shift;
602 0 my @token = @_;
603  
604 # if this is a comparison to null / not null
605 0 ▶ if ($token[2]{sql} eq '?' && $token[2]{binds}[0] eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($token[2]{'sql'} eq '?' and $token[2]{'binds'}[0] eq '') { } 0 0 0
elsif ($token[0]{'colAlias'} and $token[2]{'colAlias'}) { } 0 0 0
Condition0%0/3
$token[2]{"sql"} eq "?" and $token[2]{"binds"}[0] eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
$token[2]{"sql"} eq "?" and $token[2]{"binds"}[0] eq ""
$token[2]{"sql"} eq "?" $token[2]{"binds"}[0] eq ""
Truth table0%0/3
A and B
A= $token[2]{"sql"} eq "?"B= $token[2]{"binds"}[0] eq ""
ABresult
0X0
100
111
606 0 pop @token; # remove the last token
607 0 my $op = $token[1]{sql};
608 0 ▶ if ($op =~ /NOT\ /i || $op =~ /\!/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ /NOT\ /i or $op =~ /\!/) { } 0 0 0
Condition0%0/3
$op =~ /NOT\ /i or $op =~ /\!/
l!l&&r!l&&!r
000
MC/DC0%0/2
$op =~ /NOT\ /i or $op =~ /\!/
$op =~ /NOT\ /i $op =~ /\!/
Truth table0%0/3
A or B
A= $op =~ /NOT\ /iB= $op =~ /\!/
ABresult
1X1
011
000
609 0 $token[1]{sql} = 'is not null';
610 0 $token[1]{name} = 'is defined';
611 } else {
612 0 $token[1]{sql} = 'is null';
613 0 $token[1]{name} = 'is undefined';
614 }
615 }
616  
617 # if we are comparing 2 cols
618 ▶ elsif ($token[0]{colAlias} && $token[2]{colAlias}) {
Condition0%0/3
$token[0]{"colAlias"} and $token[2]{"colAlias"}
!ll&&!rl&&r
000
MC/DC0%0/2
$token[0]{"colAlias"} and $token[2]{"colAlias"}
$token[0]{"colAlias"} $token[2]{"colAlias"}
Truth table0%0/3
A and B
A= $token[0]{"colAlias"}B= $token[2]{"colAlias"}
ABresult
0X0
100
111
619 0 my $t0 = $oq->get_col_type($token[0]{colAlias},'filter');
620 0 my $t1 = $oq->get_col_type($token[2]{colAlias},'filter');
621  
622 # if types are equal
623 0 ▶ if ($t0 ne $t1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t0 ne $t1) 0 0 0
624 0 ▶ if ($$oq{dbtype} eq 'Oracle') {
Branch: 1 branch
BranchTrueFalseTotal
if ($oq->{"dbtype"} eq "Oracle") 0 0 0
625 0 ▶ $token[0]{sql} = "TO_CHAR(".$token[0]{sql}.")" unless $t0 eq 'char';
Branch: 1 branch
BranchTrueFalseTotal
unless $t0 eq "char" 0 0 0
626 0 ▶ $token[2]{sql} = "TO_CHAR(".$token[2]{sql}.")" unless $t1 eq 'char';
Branch: 1 branch
BranchTrueFalseTotal
unless $t1 eq "char" 0 0 0
627 }
628 }
629 0 ▶ if ($token[1]{name} =~ /contains/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token[1]{"name"} =~ /contains/) 0 0 0
630 0 $token[0]{sql} = "UPPER(".$token[0]{sql}.")";
631 0 $token[2]{sql} = "UPPER(".$token[2]{sql}.")";
632  
633 0 ▶ if ($$oq{dbtype} eq 'Oracle' || $$oq{dbtype} eq 'SQLite') {
Branch: 1 branch
BranchTrueFalseTotal
if ($oq->{'dbtype'} eq 'Oracle' or $oq->{'dbtype'} eq 'SQLite') { } 0 0 0
Condition0%0/3
$oq->{"dbtype"} eq "Oracle" or $oq->{"dbtype"} eq "SQLite"
l!l&&r!l&&!r
000
MC/DC0%0/2
$oq->{"dbtype"} eq "Oracle" or $oq->{"dbtype"} eq "SQLite"
$oq->{"dbtype"} eq "Oracle" $oq->{"dbtype"} eq "SQLite"
Truth table0%0/3
A or B
A= $oq->{"dbtype"} eq "Oracle"B= $oq->{"dbtype"} eq "SQLite"
ABresult
1X1
011
000
634 0 $token[2]{sql} = "'%'||".$token[2]{sql}."||'%'";
635 } else {
636 0 $token[2]{sql} = "CONCAT('%',".$token[2]{sql}.",'%')";
637 }
638 }
639 }
640  
641 # else we are comparing a column to a value
642 else {
643  
644 # add some code to support contains operator
645 # basically rewritten as a fuzzy search
646 0 ▶ if ($token[1]{name} =~ /contains/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token[1]{'name'} =~ /contains/) { } 0 0 0
elsif ($token[1]{'sql'} =~ /like/i and $token[2]{'sql'} eq '?') { } 0 0 0
647 0 ▶ if (! exists $$oq{select}{$token[0]{colAlias}}[3]{date_format}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $oq->{"select"}{$token[0]{"colAlias"}}[3]{"date_format"}) 0 0 0
648 0 $token[0]{sql} = 'UPPER('.$token[0]{sql}.')';
649 }
650 0 ▶ if ($token[2]{sql} eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token[2]{'sql'} eq '?') { } 0 0 0
651 0 $token[2]{binds}[0] =~ s/\*/\%/g;
652 0 $token[2]{binds}[0] = '%'.uc($token[2]{binds}[0]).'%';
653 0 $token[2]{binds}[0] =~ s/\%\%/\%/g;
654 } else {
655 0 ▶ if (! exists $$oq{select}{$token[0]{colAlias}}[3]{date_format}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $oq->{"select"}{$token[0]{"colAlias"}}[3]{"date_format"}) 0 0 0
656 0 $token[2]{sql} = 'UPPER('.$token[2]{sql}.')';
657 }
658 }
659 }
660  
661 # if like search convert all * to wildcard %
662 ▶ elsif ($token[1]{sql} =~ /like/i && $token[2]{sql} eq '?') {
Condition0%0/3
$token[1]{"sql"} =~ /like/i and $token[2]{"sql"} eq "?"
!ll&&!rl&&r
000
MC/DC0%0/2
$token[1]{"sql"} =~ /like/i and $token[2]{"sql"} eq "?"
$token[1]{"sql"} =~ /like/i $token[2]{"sql"} eq "?"
Truth table0%0/3
A and B
A= $token[1]{"sql"} =~ /like/iB= $token[2]{"sql"} eq "?"
ABresult
0X0
100
111
663 0 $token[2]{binds}[0] =~ s/\*/\%/g;
664 }
665  
666 # if lval is a date and we are doing a like comparison and rval is a value
667 # convert rval to a string using date_format
668 0 ▶ if (exists $$oq{select}{$token[0]{colAlias}}[3]{date_format} &&
Branch: 3 branches
BranchTrueFalseTotal
if (exists $oq->{'select'}{$token[0]{'colAlias'}}[3]{'date_format'} and $token[1]{'sql'} =~ /like/i and $token[2]{'sql'} eq '?') { } 0 0 0
elsif (exists $oq->{'select'}{$token[0]{'colAlias'}}[3]{'date_format'} and not $token[1]{'sql'} =~ /like/i and $token[2]{'sql'} eq '?') { } 0 0 0
elsif ($token[1]{'sql'} =~ /\=|\<|\>/ and $oq->get_col_type($token[0]{'colAlias'}, 'filter') eq 'num') { } 0 0 0
Condition0%0/6
exists $oq->{"select"}{$token[0]{"colAlias"}}[3]{"date_format"} and $token[1]{"sql"} =~ /like/i and $token[2]{"sql"} eq "?"
!ll&&!rl&&r
000
exists $oq->{"select"}{$token[0]{"colAlias"}}[3]{"date_format"} and $token[1]{"sql"} =~ /like/i
!ll&&!rl&&r
000
MC/DC0%0/3
exists $oq->{"select"}{$token[0]{"colAlias"}}[3]{"date_format"} and $token[1]{"sql"} =~ /like/i and $token[2]{"sql"} eq "?"
exists $oq->{"select"}{$token[0]{"colAlias"}}[3]{"date_format"} $token[1]{"sql"} =~ /like/i $token[2]{"sql"} eq "?"
Truth table0%0/4
A and B and C
A= exists $oq->{"select"}{$token[0]{"colAlias"}}[3]{"date_format"}B= $token[1]{"sql"} =~ /like/iC= $token[2]{"sql"} eq "?"
ABCresult
0XX0
10X0
1100
1111
669 $token[1]{sql} =~ /like/i && $token[2]{sql} eq '?') {
670 0 ▶ if ($$oq{dbtype} eq 'Oracle') {
Branch: 2 branches
BranchTrueFalseTotal
if ($oq->{'dbtype'} eq 'Oracle') { } 0 0 0
elsif ($oq->{'dbtype'} eq 'mysql') { } 0 0 0
671 0 $token[0]{sql} = "to_char(".$token[0]{sql}.",'".$$oq{select}{$token[0]{colAlias}}[3]{date_format}."')";
672 } elsif ($$oq{dbtype} eq 'mysql') {
673 0 $token[0]{sql} = "date_format(".$token[0]{sql}.",'".$$oq{select}{$token[0]{colAlias}}[3]{date_format}."')";
674 }
675 }
676
677  
678 # if lval is a date and we are doing a numerical comparison and rval is a value
679 # convert rval to a date using date_format
680 ▶ elsif (exists $$oq{select}{$token[0]{colAlias}}[3]{date_format} &&
Condition0%0/6
exists $oq->{"select"}{$token[0]{"colAlias"}}[3]{"date_format"} and not $token[1]{"sql"} =~ /like/i and $token[2]{"sql"} eq "?"
!ll&&!rl&&r
000
exists $oq->{"select"}{$token[0]{"colAlias"}}[3]{"date_format"} and not $token[1]{"sql"} =~ /like/i
!ll&&!rl&&r
000
MC/DC0%0/3
exists $oq->{"select"}{$token[0]{"colAlias"}}[3]{"date_format"} and not $token[1]{"sql"} =~ /like/i and $token[2]{"sql"} eq "?"
exists $oq->{"select"}{$token[0]{"colAlias"}}[3]{"date_format"} not $token[1]{"sql"} =~ /like/i $token[2]{"sql"} eq "?"
Truth table0%0/4
A and not(B) and C
A= exists $oq->{"select"}{$token[0]{"colAlias"}}[3]{"date_format"}B= not $token[1]{"sql"} =~ /like/iC= $token[2]{"sql"} eq "?"
ABCresult
0XX0
10X0
1100
1111
681 $token[1]{sql} !~ /like/i && $token[2]{sql} eq '?') {
682 0 ▶ if ($$oq{dbtype} eq 'Oracle') {
Branch: 2 branches
BranchTrueFalseTotal
if ($oq->{'dbtype'} eq 'Oracle') { } 0 0 0
elsif ($oq->{'dbtype'} eq 'mysql') { } 0 0 0
683 0 $token[2]{sql} = "to_date(?,'".$$oq{select}{$token[0]{colAlias}}[3]{date_format}."')";
684 } elsif ($$oq{dbtype} eq 'mysql') {
685 0 $token[2]{sql} = "str_to_date(?,'".$$oq{select}{$token[0]{colAlias}}[3]{date_format}."')";
686 }
687 }
688  
689 # if this is a numerical compare expression and the left side
690 # is a number force the right side to also be a number
691 ▶ elsif ($token[1]{sql} =~ /\=|\<|\>/ &&
Condition0%0/3
$token[1]{"sql"} =~ /\=|\<|\>/ and $oq->get_col_type($token[0]{"colAlias"}, "filter") eq "num"
!ll&&!rl&&r
000
MC/DC0%0/2
$token[1]{"sql"} =~ /\=|\<|\>/ and $oq->get_col_type($token[0]{"colAlias"}, "filter") eq "num"
$token[1]{"sql"} =~ /\=|\<|\>/ $oq->get_col_type($token[0]{"colAlias"}, "filter") eq "num"
Truth table0%0/3
A and B
A= $token[1]{"sql"} =~ /\=|\<|\>/B= $oq->get_col_type($token[0]{"colAlias"}, "filter") eq "num"
ABresult
0X0
100
111
692 $oq->get_col_type($token[0]{colAlias},'filter') eq 'num') {
693 0 my $v = $token[2]{binds}[0];
694 0 $v =~ s/[^\d\.\-]//g;
695 0 ▶ $v = 0 unless $v =~ /^\-?(\d*\.\d+|\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /^\-?(\d*\.\d+|\d+)$/ 0 0 0
696 0 $token[2]{binds}[0] = $v;
697 0 $token[2]{name} = $v;
698 }
699 }
700  
701 # if this field comes from a dep with new_cursor => 1
702 # token 0 is the left side of the expression realized as a hashref:
703 # { sql => '', binds => [], deps => [], name => '' }
704 # we need to add additional tokens if a filter is done on a field
705 # with an ancestor dependancy with option new_cursor => 1
706  
707 # get ancestor path from newest to oldest new_cursor dep
708 0 my @path = ( $$oq{select}{$token[0]{colAlias}}[0][0] );
709 0 { my $joinDep = $path[0];
710 0 while (1) {
711 0 my $parentDep = $$oq{joins}{$joinDep}[0][0];
712 0 ▶ if ($parentDep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parentDep) { } 0 0 0
713 0 push @path, $parentDep;
714 0 $joinDep = $parentDep;
715 } else {
716 0 last;
717 }
718 }
719 }
720  
721 # remove all oldest parents until we find a new_cursor (keep that one)
722 0 ▶ while (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 0 0 0
723 0 ▶ if ($$oq{joins}{$path[-1]}[3]{new_cursor}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oq->{'joins'}{$path[-1]}[3]{'new_cursor'}) { } 0 0 0
724 0 last;
725 } else {
726 0 pop @path;
727 }
728 }
729  
730 # if ancestors with new_cursor option exists
731 0 ▶ if (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 0 0 0
732 0 @path = reverse @path;
733 0 my ($preSql, $postSql, @preBinds);
734 0 foreach my $joinDep (@path) {
735 0 my ($fromSql, @fromBinds) = @{ $$oq{joins}{$joinDep}[1] };
736  
737 # unwrap SQL-92 join and add join to where
738 0 $fromSql =~ s/^\s+//;
739 0 $fromSql =~ s/^LEFT\s*//i;
740 0 $fromSql =~ s/^OUTER\s*//i;
741 0 $fromSql =~ s/^JOIN\s*//i;
742  
743 0 my $corelatedJoin;
744 0 ▶ if ($fromSql =~ /^(.*)\bON\s*\((.*)\)\s*$/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fromSql =~ /^(.*)\bON\s*\((.*)\)\s*$/is) { } 0 0 0
745 0 $fromSql = $1;
746 0 $corelatedJoin = $2;
747 } else {
748 0 die "could not parse for corelated join";
749 }
750  
751 0 ▶ if ($token[1]{sql} =~ s/\bNOT\b//) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token[1]{'sql'} =~ s/\bNOT\b//) { } 0 0 0
elsif ($token[1]{'sql'} eq '!=') { } 0 0 0
elsif ($token[1]{'sql'} eq 'is null') { } 0 0 0
752 0 $preSql .= 'NOT ';
753 } elsif ($token[1]{sql} eq '!=') {
754 0 $token[1]{sql} = '=';
755 0 $preSql .= 'NOT ';
756 } elsif ($token[1]{sql} eq 'is null' ) {
757 0 $token[1]{sql} = 'is not null';
758 0 $preSql .= 'NOT ';
759 }
760 0 $preSql .= 'EXISTS ( SELECT 1 FROM '.$fromSql.' WHERE ('.$corelatedJoin.') AND ';
761 0 $postSql .= ')';
762 0 push @preBinds, @fromBinds;
763 }
764  
765 # update left expression deps and binds
766 0 $token[0]{deps} = $$oq{joins}{$path[0]}[0];
767 0 ▶ unshift @{ $token[0]{binds} }, @preBinds if @preBinds;
Branch: 1 branch
BranchTrueFalseTotal
if @preBinds 0 0 0
768  
769 # add new pre/post sql tokens
770 0 unshift @token, { sql => $preSql, name => '' };
771 0 push @token, { sql => $postSql, name => '' };
772  
773 }
774  
775 0 return $glue_exp->(@token);
776 },
777  
778 'namedFilter' => sub {
779 0 ▶ my $oq = $_[0];
Subroutine __ANON__: not called CC=8 SCAR=42.8
780 0 my $namedFilterAlias = $_[2];
781 0 my $args = $_[4];
782 0 ▶ die "was expecting that namedFilter args would be an array ref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "ARRAY" 0 0 0
783 unless ref($args) eq 'ARRAY';
784 0 my $r = $$oq{named_filters}{$namedFilterAlias};
785 0 my ($deps, $sql, $binds, $name);
786 0 ▶ if (ref($r) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $r eq 'ARRAY') { } 0 0 0
elsif (ref $r eq 'HASH') { } 0 0 0
787 0 $deps= $$r[0];
788 0 my @tmp = @{ $$r[1] };
789 0 $sql = shift @tmp;
790 0 $binds = \@tmp;
791 0 $name = $$r[2];
792 } elsif (ref($r) eq 'HASH') {
793 die "could not find sql_generator for named_filter $namedFilterAlias"
794 0 ▶ unless ref($$r{sql_generator}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $r->{"sql_generator"} eq "CODE" 0 0 0
795 0 ($deps, $binds, $name) = @{ $$r{sql_generator}->(@$args) };
796 0 ▶ $deps = [$deps] if ! ref $deps;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $deps 0 0 0
797 0 ▶ if (ref($binds) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $binds eq 'ARRAY') { } 0 0 0
798 0 $sql = shift @$binds;
799 } else {
800 0 $sql = $binds;
801 0 $binds = [];
802 }
803 } else {
804 0 ▶ die "could not find named_filter $namedFilterAlias" unless ref $r;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $r 0 0 0
805 }
806 0 return { deps => $deps, sql => '('.$sql.')', binds => $binds, name => $name };
807 }
808 0 );
809  
810  
811 0 my $c = $sth->{cursors}->[0];
812  
813 # add filter parts to cursor's where parts
814 0 ▶ if ($sth->{'filter'} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"filter"} ne "") 0 0 0
815 my $filter = $$sth{oq}->parse($DBIx::OptimalQuery::filterGrammar, $sth->{'filter'}, \%translations)
816 0 ▶ or die "could not parse filter: ".$sth->{'filter'};
Branch: 1 branch
BranchTrueFalseTotal
my $filter = $sth->{"oq"}->parse($DBIx::OptimalQuery::filterGrammar, $sth->{"filter"}, \%translations) or die 'could not parse filter: ' . $sth->{"filter"} 0 0 0
817  
818 0 push @{ $c->{where_deps} }, @{ $$filter{deps} };
819 0 $c->{where_sql} = $$filter{sql};
820 0 push @{ $c->{where_binds} }, @{ $$filter{binds} };
821 0 $c->{where_name} = $$filter{name};
822 }
823  
824 # add hidden filter parts to cursor's where parts
825 0 ▶ if ($sth->{'hiddenFilter'} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"hiddenFilter"} ne "") 0 0 0
826 my $hiddenFilter = $$sth{oq}->parse($DBIx::OptimalQuery::filterGrammar, $sth->{'hiddenFilter'}, \%translations)
827 0 ▶ or die "could not parse hiddenFilter: ".$sth->{'hiddenFilter'};
Branch: 1 branch
BranchTrueFalseTotal
my $hiddenFilter = $sth->{"oq"}->parse($DBIx::OptimalQuery::filterGrammar, $sth->{"hiddenFilter"}, \%translations) or die 'could not parse hiddenFilter: ' . $sth->{"hiddenFilter"} 0 0 0
828 0 push @{ $c->{where_deps} }, @{ $$hiddenFilter{deps} };
829 0 ▶ $c->{where_sql} = '('.$c->{where_sql}.') AND ' if $c->{where_sql} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"where_sql"} ne "" 0 0 0
830 0 $c->{where_sql} .= '('.$$hiddenFilter{sql}.')';
831 0 push @{ $c->{where_binds} }, @{ $$hiddenFilter{binds} };
832 }
833  
834 # add system filter parts to cursor's where parts
835 0 ▶ if ($sth->{'forceFilter'} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"forceFilter"} ne "") 0 0 0
836 my $forceFilter = $$sth{oq}->parse($DBIx::OptimalQuery::filterGrammar, $sth->{'forceFilter'}, \%translations)
837 0 ▶ or die "could not parse forceFilter: ".$sth->{'forceFilter'};
Branch: 1 branch
BranchTrueFalseTotal
my $forceFilter = $sth->{"oq"}->parse($DBIx::OptimalQuery::filterGrammar, $sth->{"forceFilter"}, \%translations) or die 'could not parse forceFilter: ' . $sth->{"forceFilter"} 0 0 0
838 0 push @{ $c->{where_deps} }, @{ $$forceFilter{deps} };
839 0 ▶ $c->{where_sql} = '('.$c->{where_sql}.') AND ' if $c->{where_sql} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"where_sql"} ne "" 0 0 0
840 0 $c->{where_sql} .= '('.$$forceFilter{sql}.')';
841 0 push @{ $c->{where_binds} }, @{ $$forceFilter{binds} };
842 }
843  
844 0 return undef;
845 }
846  
847  
848  
849  
850 # modify cursor and add order by data
851 sub create_order_by {
852 0 ▶ my $sth = shift;
Subroutine create_order_by: not called CC=4 SCAR=30.0
853  
854 0 ▶ if ($sth->{'sort'} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"sort"} ne "") 0 0 0
855 0 ▶ $$sth{oq}{error_handler}->("DEBUG: \$sth->create_order_by()\n") if $$sth{oq}{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"oq"}{"debug"} 0 0 0
856 my %translations = (
857 0 ▶ '*default*' => sub { $_[2] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
858  
859 'expList' => sub {
860 0 ▶ my ($oq) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
861 0 my (%deps, @sql, @binds, @nice);
862 0 ▶ die "was expecting an array ref!" unless ref($_[2]) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[2] eq "ARRAY" 0 0 0
863 0 foreach my $sort (@{ $_[2] }) {
864 0 ▶ die "was expecting a hash ref!" unless ref($sort) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sort eq "HASH" 0 0 0
865 0 $deps{$_} = 1 for @{ $$sort{deps} };
866 0 push @sql, $$sort{sql};
867 0 push @binds, @{ $$sort{binds} };
868 0 push @nice, $$sort{nice};
869 }
870 0 my @deps = keys %deps;
871 0 return [ \@deps, join(', ', @sql), \@binds, \@nice ];
872 },
873  
874 'expression' => sub {
875 0 ▶ my $oq = $_[0];
Subroutine __ANON__: not called CC=3 SCAR=24.8
876 0 my $def = $_[2];
877 0 my $sql_sort_opts_to_append = lc(join(' ', @{$_[3]}));
878  
879 0 ▶ if ($sql_sort_opts_to_append) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql_sort_opts_to_append) 0 0 0
880 0 $$def{sql} .= ' '.$sql_sort_opts_to_append;
881 0 ▶ $$def{nice} .= ($sql_sort_opts_to_append =~ /desc/) ?
Branch: 1 branch
BranchTrueFalseTotal
$sql_sort_opts_to_append =~ /desc/ ? : 0 0 0
882 ' (reverse)' : $sql_sort_opts_to_append;
883 }
884 0 return $def;
885 },
886  
887 'quotedString' => sub {
888 0 ▶ $_ = $_[2]; (s/^\'// && s/\'$//) || (s/^\"// && s/\"$//); $_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
s/^\'// and s/\'$// or s/^\"// and s/\"$// 0 0 0
Condition0%0/9
s/^\'// && s/\'$// || s/^\"// && s/\"$//
l!l&&r!l&&!r
000
s/^\'// and s/\'$//
!ll&&!rl&&r
000
s/^\"// and s/\"$//
!ll&&!rl&&r
000
MC/DC0%0/4
s/^\'// && s/\'$// || s/^\"// && s/\"$//
s/^\'// s/\'$// s/^\"// s/\"$//
Truth table0%0/7
A and B || C and D
A= s/^\'//B= s/\'$//C= s/^\"//D= s/\"$//
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
889 },
890  
891 'namedSort' => sub {
892 0 ▶ my $oq = $_[0];
Subroutine __ANON__: not called CC=7 SCAR=40.3
893 0 my $namedSortAlias = $_[2];
894 0 my $args = $_[4];
895 0 ▶ die "was expecting that namedSort args would be an array ref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "ARRAY" 0 0 0
896 unless ref($args) eq 'ARRAY';
897 0 my $r = $$oq{named_sorts}{$namedSortAlias};
898 0 my ($deps, $sql, $binds, $nice);
899 0 ▶ if (ref($r) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $r eq 'ARRAY') { } 0 0 0
elsif (ref $r eq 'HASH') { } 0 0 0
900 0 $deps = $$r[0];
901 0 my @tmp = @{ $$r[1] };
902 0 $sql = shift @tmp;
903 0 $binds = \ @tmp;
904 0 $nice = $$r[3];
905 } elsif (ref($r) eq 'HASH') {
906 die "could not find sql_generator for named_sort $namedSortAlias"
907 0 ▶ unless ref($$r{sql_generator}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $r->{"sql_generator"} eq "CODE" 0 0 0
908 0 ($deps, $binds, $nice) = @{ $$r{sql_generator}->(@$args) };
909 0 ▶ $deps = [$deps] if ! ref $deps;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $deps 0 0 0
910 0 $sql = shift @$binds;
911 } else {
912 0 ▶ die "could not find named_sort $namedSortAlias" unless ref $r;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $r 0 0 0
913 }
914 0 return { deps => $deps, sql => $sql, binds => $binds, nice => $nice };
915 },
916  
917 'colAlias' => sub {
918 0 ▶ my $oq = $_[0];
Subroutine __ANON__: not called CC=7 SCAR=40.3
919 0 my $colAlias = $_[3];
920 die "could not find colAlias $colAlias"
921 0 ▶ unless exists $$oq{select}{$colAlias};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $oq->{"select"}{$colAlias} 0 0 0
922 0 my $deps = $$oq{select}{$colAlias}[0];
923 my @tmp = @{
924 0 ▶ $$oq{select}{$colAlias}[3]{sort_sql} || $$oq{select}{$colAlias}[1] };
Branch: 1 branch
BranchTrueFalseTotal
$oq->{"select"}{$colAlias}[3]{"sort_sql"} or $oq->{"select"}{$colAlias}[1] 0 0 0
925 0 my $sql = shift @tmp;
926 0 my $binds = \@tmp;
927 0 ▶ die "could not find nice name" if $$oq{select}{$colAlias}[2] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $oq->{"select"}{$colAlias}[2] eq "" 0 0 0
928 0 my $nice = '['.$$oq{select}{$colAlias}[2].']';
929  
930 0 ▶ if ($$sth{oq}{dbtype} eq 'Oracle' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}->get_col_types("select")->{$colAlias} eq "clob" and not $sql =~ /^cast\(/i) 0 0 0
Condition0%0/6
$sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}->get_col_types("select")->{$colAlias} eq "clob" and not $sql =~ /^cast\(/i
!ll&&!rl&&r
000
$sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}->get_col_types("select")->{$colAlias} eq "clob"
!ll&&!rl&&r
000
MC/DC0%0/3
$sth->{"oq"}{"dbtype"} eq "Oracle" and $sth->{"oq"}->get_col_types("select")->{$colAlias} eq "clob" and not $sql =~ /^cast\(/i
$sth->{"oq"}{"dbtype"} eq "Oracle" $sth->{"oq"}->get_col_types("select")->{$colAlias} eq "clob" not $sql =~ /^cast\(/i
Truth table0%0/4
A and B and not(C)
A= $sth->{"oq"}{"dbtype"} eq "Oracle"B= $sth->{"oq"}->get_col_types("select")->{$colAlias} eq "clob"C= not $sql =~ /^cast\(/i
ABCresult
0XX0
10X0
1100
1111
931 $sth->{oq}->get_col_types('select')->{$colAlias} eq 'clob' &&
932 $sql !~ /^cast\(/i) {
933 0 $sql = "cast($sql as varchar2(100))";
934 }
935 0 return { deps => $deps, sql => $sql, binds => $binds, nice => $nice };
936 }
937 0 );
938  
939 my $result = $$sth{oq}->parse($DBIx::OptimalQuery::sortGrammar, $sth->{'sort'}, \%translations)
940 0 ▶ or die "could not parse sort: ".$sth->{'sort'};
Branch: 1 branch
BranchTrueFalseTotal
my $result = $sth->{"oq"}->parse($DBIx::OptimalQuery::sortGrammar, $sth->{"sort"}, \%translations) or die 'could not parse sort: ' . $sth->{"sort"} 0 0 0
941  
942 0 my $c = $sth->{cursors}->[0];
943 ($c->{order_by_deps}, $c->{order_by_sql},
944 0 $c->{order_by_binds}, $c->{order_by_name}) = @$result;
945 }
946 0 return undef;
947 }
948  
949  
950  
951  
952  
953  
954  
955  
956  
957
958  
959  
960 # fetch next row or return undef when done
961 sub fetchrow_hashref {
962 0 ▶ my ($sth) = @_;
Subroutine fetchrow_hashref: not called CC=10 SCAR=47.0
963 0 ▶ return undef unless $sth->count() > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $sth->count > 0 0 0 0
964 0 $sth->execute(); # execute if not already existed
965  
966 0 ▶ $$sth{oq}{error_handler}->("DEBUG: \$sth->fetchrow_hashref()\n") if $$sth{oq}{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"oq"}{"debug"} 0 0 0
967  
968 0 my $cursors = $sth->{cursors};
969 0 my $c = $cursors->[0];
970  
971 # bind hash value to column data
972 0 my $rec = $$c{bind_hash};
973  
974 # fetch record
975 0 ▶ if (my $v = $c->{sth}->fetch()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $v = $c->{'sth'}->fetch) { } 0 0 0
976  
977 0 foreach my $i (0 .. $#$v) {
978  
979 # if col type is decimal auto trim 0s after decimal
980 0 ▶ if ($c->{sth}->{TYPE}->[$i] eq '3' && $$v[$i] =~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"sth"}{"TYPE"}[$i] eq 3 and $v->[$i] =~ /\./) 0 0 0
Condition0%0/3
$c->{"sth"}{"TYPE"}[$i] eq 3 and $v->[$i] =~ /\./
!ll&&!rl&&r
000
MC/DC0%0/2
$c->{"sth"}{"TYPE"}[$i] eq 3 and $v->[$i] =~ /\./
$c->{"sth"}{"TYPE"}[$i] eq 3 $v->[$i] =~ /\./
Truth table0%0/3
A and B
A= $c->{"sth"}{"TYPE"}[$i] eq 3B= $v->[$i] =~ /\./
ABresult
0X0
100
111
981 0 $$v[$i] =~ s/0+$//;
982 0 $$v[$i] =~ s/\.$//;
983 }
984 }
985
986 0 $sth->{'fetch_index'}++;
987  
988 # execute other cursors
989 0 foreach my $i (1 .. $#$cursors) {
990 0 $c = $cursors->[$i];
991  
992 0 $c->{sth}->execute( @{ $c->{binds} },
993 0 map { $$rec{$_} } @{ $c->{parent_keys} } );
994  
995 0 my $cols = $$c{select_field_order};
996 0 @$rec{ @$cols } = [];
997  
998 0 ▶ while (my @vals = $c->{sth}->fetchrow_array()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@vals) = $c->{"sth"}->fetchrow_array) 0 0 0
999 0 ▶ for (my $i=0; $i <= $#$cols; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $cols->$#*) 0 0 0
1000 0 push @{ $$rec{$$cols[$i]} }, $vals[$i];
1001 }
1002 }
1003 0 $c->{sth}->finish();
1004 }
1005 0 return $rec;
1006 } else {
1007 0 return undef;
1008 }
1009 }
1010  
1011 # finish sth
1012 sub finish {
1013 0 ▶ my ($sth) = @_;
Subroutine finish: not called CC=4 SCAR=30.0
1014 0 ▶ $$sth{oq}{error_handler}->("DEBUG: \$sth->finish()\n") if $$sth{oq}{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"oq"}{"debug"} 0 0 0
1015 0 foreach my $c (@{$$sth{cursors}}) {
1016 0 ▶ $$c{sth}->finish() if $$c{sth};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"sth"} 0 0 0
1017 0 undef $$c{sth};
1018 }
1019 0 return undef;
1020 }
1021  
1022 # get count for sth
1023 sub count {
1024 0 ▶ my $sth = shift;
Subroutine count: not called CC=15 SCAR=54.8
1025  
1026 # if count is not already defined, define it
1027 0 ▶ if (! defined $sth->{count}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sth->{"count"}) 0 0 0
1028 0 ▶ $$sth{oq}{error_handler}->("DEBUG: \$sth->count()\n") if $$sth{oq}{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"oq"}{"debug"} 0 0 0
1029  
1030 0 my $c = $sth->{cursors}->[0];
1031  
1032 0 my $drivingTable = $c->{select_deps}->[0];
1033  
1034 # only need to join in driving table with
1035 # deps used in where clause
1036 0 my $deps = [ $drivingTable, @{$c->{where_deps}} ];
1037 0 ($deps) = @{ $sth->{oq}->_order_deps($deps) };
1038 0 my @from_deps; push @from_deps, @$_ for @$deps;
1039  
1040 # create from_sql, from_binds
1041 # vars prefixed with old_ is used for supported non sql-92 joins
1042 0 my ($from_sql, @from_binds, $old_join_sql, @old_join_binds );
1043 0 foreach my $from_dep (@from_deps) {
1044 0 my ($sql, @binds) = @{ $sth->{oq}->{joins}->{$from_dep}->[1] };
1045 0 ▶ push @from_binds, @binds if @binds;
Branch: 1 branch
BranchTrueFalseTotal
if @binds 0 0 0
1046  
1047 # if this is the driving table join
1048 0 ▶ if (! $sth->{oq}->{joins}->{$from_dep}->[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $sth->{'oq'}{'joins'}{$from_dep}[0]) { } 0 0 0
elsif (not $sth->{'oq'}{'joins'}{$from_dep}[2]) { } 0 0 0
1049  
1050 # alias it if not already aliased in sql
1051 0 ▶ $sql .= " $from_dep" unless $sql =~ /\b$from_dep\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sql =~ /\b$from_dep\s*$/ 0 0 0
1052 0 $from_sql .= $sql;
1053 }
1054  
1055 # if SQL-92 type join?
1056 elsif (! $sth->{oq}->{joins}->{$from_dep}->[2]) {
1057 0 $from_sql .= "\n".$sql;
1058 }
1059  
1060 # old style join
1061 else {
1062 0 $from_sql .= ",\n".$sql.' '.$from_dep;
1063 0 my ($sql, @binds) = @{ $sth->{oq}->{joins}->{$from_dep}->[2] };
1064 0 ▶ if ($sql) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql) 0 0 0
1065 0 ▶ $old_join_sql .= " AND " if $old_join_sql ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $old_join_sql ne "" 0 0 0
1066 0 $old_join_sql .= $sql;
1067 }
1068 0 push @old_join_binds, @binds;
1069 }
1070 }
1071  
1072  
1073 # construct where clause
1074 0 my $where;
1075 0 { my @where;
1076 0 ▶ push @where, '('.$old_join_sql.') ' if $old_join_sql;
Branch: 1 branch
BranchTrueFalseTotal
if $old_join_sql 0 0 0
1077 0 ▶ push @where, '('.$c->{where_sql}.') ' if $c->{where_sql};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"where_sql"} 0 0 0
1078 0 ▶ $where = 'WHERE '.join(' AND ', @where) if @where;
Branch: 1 branch
BranchTrueFalseTotal
if @where 0 0 0
1079 }
1080  
1081 # generate sql and bind params
1082 0 my $sql = "
1083 SELECT count(*)
1084 FROM (
1085 SELECT $drivingTable.*
1086 FROM $from_sql
1087 $where
1088 ) cnt_query";
1089 0 my @binds = (@from_binds, @old_join_binds, @{$c->{where_binds}});
1090  
1091 0 eval {
1092 0 ($sth->{count}) = $sth->{oq}->{dbh}->selectrow_array($sql, undef, @binds);
1093 0 ▶ }; if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1094 0 die "Problem finding count for SQL:\n$sql\nBINDS:\n".join(',',@binds)."\n\n$@\n";
1095 }
1096 }
1097  
1098 0 return $sth->{count};
1099 }
1100  
1101 0 ▶sub fetch_index { $_->{'fetch_index'} }
Subroutine fetch_index: not called CC=1 SCAR=6.9
1102  
1103 sub filter_descr {
1104 0 ▶ my $sth = shift;
Subroutine filter_descr: not called CC=1 SCAR=6.9
1105 0 return $sth->{cursors}->[0]->{'where_name'};
1106 }
1107  
1108 sub sort_descr {
1109 0 ▶ my $sth = shift;
Subroutine sort_descr: not called CC=2 SCAR=17.9
1110 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
1111 0 return @{ $sth->{cursors}->[0]->{'order_by_name'} };
1112 } else {
1113 0 return join(', ', @{ $sth->{cursors}->[0]->{'order_by_name'} });
1114 }
1115 }
1116  
1117  
1118  
1119  
1120  
1121  
1122  
1123  
1124  
1125  
1126  
1127  
1128  
1129  
1130  
1131  
1132  
1133  
1134  
1135  
1136  
1137  
1138  
1139  
1140  
1141  
1142  
1143  
1144  
1145  
1146  
1147  
1148  
1149  
1150  
1151  
1152  
1153  
1154  
1155 package DBIx::OptimalQuery;
1156  
1157 =comment
1158  
1159 use DBIx::OptimalQuery;
1160 my $oq = DBIx::OptimalQuery->new(
1161 select => {
1162 'alias' => [dep, sql, nice_name, { OPTIONS } ]
1163 }
1164  
1165 joins => {
1166 'alias' => [dep, join_sql, where_sql, { OPTIONS } ]
1167 }
1168  
1169 named_filters => {
1170 'name' => [dep, sql, nice]
1171 'name' => {
1172 sql_generator => sub {
1173 my %args = @_;
1174 return [dep, sql, name]
1175 }
1176 title => "text displayed on interactive filter"
1177 }
1178 },
1179  
1180 named_sorts => {
1181 'name' => [dep, sql, nice]
1182 'name' => { sql_generator => sub { return [dep, sql, name] } }
1183 },
1184  
1185
1186  
1187 debug => 0 | 1
1188 );
1189 =cut
1190  
1191 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1192 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
1193 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
1194 1 ▶use DBI();
Subroutine BEGIN: called CC=1 SCAR=0.0
1195  
1196 sub new {
1197 0 ▶ my $class = shift;
Subroutine new: not called CC=7 SCAR=40.3
Pod undocumented
1198 0 my %args = @_;
1199 0 my $oq = bless \%args, $class;
1200  
1201 0 ▶ $$oq{debug} || 0;
Branch: 1 branch
BranchTrueFalseTotal
$oq->{"debug"} or '???' 0 0 0
1202 0 ▶ $$oq{error_handler}->("DEBUG: $class->new(".Dumper(\%args).")\n") if $$oq{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $oq->{"debug"} 0 0 0
1203  
1204 die "BAD_PARAMS - must provide a dbh!"
1205 0 ▶ unless $oq->{'dbh'};
Branch: 1 branch
BranchTrueFalseTotal
unless $oq->{"dbh"} 0 0 0
1206 die "BAD_PARAMS - must define a select key in call to constructor"
1207 0 ▶ unless ref($oq->{'select'}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $oq->{"select"} eq "HASH" 0 0 0
1208 die "BAD_PARAMS - must define a joins key in call to constructor"
1209 0 ▶ unless ref($oq->{'joins'}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $oq->{"joins"} eq "HASH" 0 0 0
1210  
1211  
1212 0 $oq->_normalize();
1213  
1214 0 $$oq{dbtype} = $$oq{dbh}{Driver}{Name};
1215 0 ▶ $$oq{dbtype} = $$oq{dbh}->get_info(17) if $$oq{dbtype} eq 'ODBC';
Branch: 1 branch
BranchTrueFalseTotal
if $oq->{"dbtype"} eq "ODBC" 0 0 0
1216  
1217 0 return $oq;
1218 }
1219  
1220  
1221  
1222  
1223  
1224  
1225  
1226  
1227  
1228  
1229  
1230 1 our $filterGrammar = <<'TILEND';
1231 start: exp /^$/
1232  
1233 exp:
1234 '(' exp ')' logicOp exp
1235 | '(' exp ')'
1236 | comparisonExp logicOp exp
1237 | comparisonExp
1238  
1239 comparisonExp:
1240 namedFilter
1241 | colAlias compOp colAlias
1242 | colAlias compOp bindVal
1243  
1244 bindVal: float | quotedString
1245  
1246 logicOp:
1247 /and/i
1248 | /or/i
1249  
1250 namedFilter: /\w+/ '(' namedFilterArg(s? /,/) ')'
1251  
1252 namedFilterArg: quotedString | float | unquotedIdentifier
1253  
1254 unquotedIdentifier: /\w+/
1255  
1256 colAlias: '[' /\w+/ ']'
1257  
1258 float:
1259 /\-?\d*\.?\d+/
1260 | /\-?\d+\.?\d*/
1261  
1262 quotedString:
1263 /'.*?'/
1264 | /".*?"/
1265  
1266 compOp:
1267 '<=' | '>=' | '=' | '!=' | '<' | '>' |
1268 /contains/i | /not\ contains/i | /like/i | /not\ like/i
1269  
1270 TILEND
1271  
1272  
1273 1 our $sortGrammar = <<'TILEND';
1274 start: expList /^$/
1275  
1276 expList: expression(s? /,/)
1277  
1278 expression:
1279 namedSort opt(?)
1280 | colAlias opt(?)
1281  
1282 opt: /desc/i
1283  
1284 namedSort: /\w+/ '(' namedSortArg(s? /,/) ')'
1285 namedSortArg: quotedString | float
1286  
1287 colAlias: '[' /\w+/ ']'
1288  
1289 float:
1290 /\-?\d*\.?\d+/
1291 | /\-?\d+\.?\d*/
1292  
1293 quotedString:
1294 /'.*?(?<!\\)'/
1295 | /".*?(?<!\\)"/
1296  
1297 TILEND
1298  
1299 1 our (%cached_parsers, $oq, $translations);
1300 sub translator_callback {
1301 0 ▶ return $$translations{$_[0]}->($oq, @_) if exists $$translations{$_[0]};
Subroutine translator_callback: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if exists $$DBIx::OptimalQuery::translations{$_[0]} 0 0 0
1302 0 ▶ return $$translations{'*default*'}->($oq, @_) if exists $$translations{'*default*'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $DBIx::OptimalQuery::translations->{"*default*"} 0 0 0
1303 0 return 1;
1304 }
1305  
1306 sub parse {
1307 0 ▶ local $oq = shift;
Subroutine parse: not called CC=3 SCAR=24.8
Pod undocumented
1308 0 my $grammar = shift;
1309 0 my $string = shift;
1310 0 local $translations = shift;
1311 0 ▶ my $start_rule = shift || 'start';
Condition0%0/2
shift() || 'start'
l!l
00
MC/DC0%0/1
shift() || 'start'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1312 0 local $::RD_AUTOACTION = 'DBIx::OptimalQuery::translator_callback(@item);';
1313 0 ▶ $cached_parsers{$grammar} ||= Parse::RecDescent->new($grammar);
Condition0%0/3
$DBIx::OptimalQuery::cached_parsers{$grammar} ||= "Parse::RecDescent"->new($grammar)
l!l&&r!l&&!r
000
MC/DC0%0/2
$DBIx::OptimalQuery::cached_parsers{$grammar} ||= "Parse::RecDescent"->new($grammar)
$DBIx::OptimalQuery::cached_parsers{$grammar} "Parse::RecDescent"->new($grammar)
Truth table0%0/3
A ||= B
A= $DBIx::OptimalQuery::cached_parsers{$grammar}B= "Parse::RecDescent"->new($grammar)
ABresult
1X1
011
000
1314 0 return $cached_parsers{$grammar}->$start_rule($string);
1315 }
1316  
1317  
1318  
1319  
1320  
1321  
1322 # normalize member variables
1323 sub _normalize {
1324 0 ▶ my $oq = shift;
Subroutine _normalize: not called CC=38 SCAR=73.0
1325 0 ▶ $$oq{error_handler}->("DEBUG: \$oq->_normalize()\n") if $$oq{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $oq->{"debug"} 0 0 0
1326  
1327 0 ▶ $oq->{'AutoSetLongReadLen'} = 1 unless exists $oq->{'AutoSetLongReadLen'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $oq->{"AutoSetLongReadLen"} 0 0 0
1328  
1329 # make sure all option hash refs exist
1330 0 ▶ $oq->{'select'}->{$_}->[3] ||= {} for keys %{ $oq->{'select'} };
Condition0%0/2
$oq->{"select"}{$_}[3] ||= {}
l!l
00
MC/DC0%0/1
$oq->{"select"}{$_}[3] ||= {}
$oq->{"select"}{$_}[3]
Truth table0%0/2
A
A= $oq->{"select"}{$_}[3]
Aresult
11
00
1331 0 ▶ $oq->{'joins' }->{$_}->[3] ||= {} for keys %{ $oq->{'joins'} };
Condition0%0/2
$oq->{"joins"}{$_}[3] ||= {}
l!l
00
MC/DC0%0/1
$oq->{"joins"}{$_}[3] ||= {}
$oq->{"joins"}{$_}[3]
Truth table0%0/2
A
A= $oq->{"joins"}{$_}[3]
Aresult
11
00
1332  
1333  
1334 # since the sql & deps definitions can optionally be entered as arrays
1335 # turn all into arrays if not already
1336 0 for ( # key, index
1337 ['select', 0], ['select', 1],
1338 ['joins', 0], ['joins', 1], ['joins', 2],
1339 ['named_filters', 0], ['named_filters', 1],
1340 ['named_sorts', 0], ['named_sorts', 1] ) {
1341 0 my ($key, $i) = @$_;
1342 0 ▶ $oq->{$key} ||= {};
Condition0%0/2
$oq->{$key} ||= {}
l!l
00
MC/DC0%0/1
$oq->{$key} ||= {}
$oq->{$key}
Truth table0%0/2
A
A= $oq->{$key}
Aresult
11
00
1343 0 foreach my $alias (keys %{ $oq->{$key} }) {
1344 0 ▶ if (ref($oq->{$key}->{$alias}) eq 'ARRAY' &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $oq->{$key}{$alias} eq "ARRAY" and defined $oq->{$key}{$alias}[$i] and ref $oq->{$key}{$alias}[$i] ne "ARRAY") 0 0 0
Condition0%0/6
ref $oq->{$key}{$alias} eq "ARRAY" and defined $oq->{$key}{$alias}[$i] and ref $oq->{$key}{$alias}[$i] ne "ARRAY"
!ll&&!rl&&r
000
ref $oq->{$key}{$alias} eq "ARRAY" and defined $oq->{$key}{$alias}[$i]
!ll&&!rl&&r
000
MC/DC0%0/3
ref $oq->{$key}{$alias} eq "ARRAY" and defined $oq->{$key}{$alias}[$i] and ref $oq->{$key}{$alias}[$i] ne "ARRAY"
ref $oq->{$key}{$alias} eq "ARRAY" defined $oq->{$key}{$alias}[$i] ref $oq->{$key}{$alias}[$i] ne "ARRAY"
Truth table0%0/4
A and B and C
A= ref $oq->{$key}{$alias} eq "ARRAY"B= defined $oq->{$key}{$alias}[$i]C= ref $oq->{$key}{$alias}[$i] ne "ARRAY"
ABCresult
0XX0
10X0
1100
1111
1345 defined $oq->{$key}->{$alias}->[$i] &&
1346 ref($oq->{$key}->{$alias}->[$i]) ne 'ARRAY') {
1347 0 $oq->{$key}->{$alias}->[$i] = [$oq->{$key}->{$alias}->[$i]];
1348 }
1349 }
1350 }
1351  
1352 # make sure the following select options, if they exist are array references
1353 0 foreach my $col (keys %{ $oq->{'select'} }) {
1354 0 my $opts = $oq->{'select'}->{$col}->[3];
1355 0 foreach my $opt (qw( select_sql sort_sql filter_sql )) {
1356 $opts->{$opt} = [$opts->{$opt}]
1357 0 ▶ if exists $opts->{$opt} && ref($opts->{$opt}) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{$opt} and ref $opts->{$opt} ne "ARRAY" 0 0 0
Condition0%0/3
exists $opts->{$opt} and ref $opts->{$opt} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts->{$opt} and ref $opts->{$opt} ne "ARRAY"
exists $opts->{$opt} ref $opts->{$opt} ne "ARRAY"
Truth table0%0/3
A and B
A= exists $opts->{$opt}B= ref $opts->{$opt} ne "ARRAY"
ABresult
0X0
100
111
1358 }
1359  
1360 # make sure defined deps exist
1361 0 foreach my $dep (@{ $$oq{'select'}{$col}[0] }) {
1362 die "dep $dep for select $col does not exist"
1363 0 ▶ if defined $dep && ! exists $$oq{'joins'}{$dep};
Branch: 1 branch
BranchTrueFalseTotal
if defined $dep and not exists $oq->{"joins"}{$dep} 0 0 0
Condition0%0/3
defined $dep and not exists $oq->{"joins"}{$dep}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dep and not exists $oq->{"joins"}{$dep}
defined $dep not exists $oq->{"joins"}{$dep}
Truth table0%0/3
A and not(B)
A= defined $depB= not exists $oq->{"joins"}{$dep}
ABresult
0X0
100
111
1364 }
1365 }
1366  
1367 # look for new cursors and define parent child links if not already defined
1368 0 foreach my $join (keys %{ $oq->{'joins'} }) {
1369 0 my $opts = $oq->{'joins'}->{$join}->[3];
1370 0 ▶ if (exists $opts->{new_cursor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"new_cursor"}) 0 0 0
1371 0 ▶ if (ref($opts->{new_cursor}) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts->{'new_cursor'} ne 'HASH') { } 0 0 0
1372 0 $oq->_formulate_new_cursor($join);
1373 } else {
1374 die "could not find keys, join, and sql for new cursor in $join"
1375 unless exists $opts->{new_cursor}->{'keys'} &&
1376 exists $opts->{new_cursor}->{'join'} &&
1377 0 ▶ exists $opts->{new_cursor}->{'sql'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"new_cursor"}{"keys"} and exists $opts->{"new_cursor"}{"join"} and exists $opts->{"new_cursor"}{"sql"} 0 0 0
Condition0%0/6
exists $opts->{"new_cursor"}{"keys"} and exists $opts->{"new_cursor"}{"join"} and exists $opts->{"new_cursor"}{"sql"}
!ll&&!rl&&r
000
exists $opts->{"new_cursor"}{"keys"} and exists $opts->{"new_cursor"}{"join"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $opts->{"new_cursor"}{"keys"} and exists $opts->{"new_cursor"}{"join"} and exists $opts->{"new_cursor"}{"sql"}
exists $opts->{"new_cursor"}{"keys"} exists $opts->{"new_cursor"}{"join"} exists $opts->{"new_cursor"}{"sql"}
Truth table0%0/4
A and B and C
A= exists $opts->{"new_cursor"}{"keys"}B= exists $opts->{"new_cursor"}{"join"}C= exists $opts->{"new_cursor"}{"sql"}
ABCresult
0XX0
10X0
1100
1111
1378 }
1379 }
1380  
1381 # make sure defined deps exist
1382 0 foreach my $dep (@{ $$oq{'joins'}{$join}[0] }) {
1383 die "dep $dep for join $join does not exist"
1384 0 ▶ if defined $dep && ! exists $$oq{'joins'}{$dep};
Branch: 1 branch
BranchTrueFalseTotal
if defined $dep and not exists $oq->{"joins"}{$dep} 0 0 0
Condition0%0/3
defined $dep and not exists $oq->{"joins"}{$dep}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dep and not exists $oq->{"joins"}{$dep}
defined $dep not exists $oq->{"joins"}{$dep}
Truth table0%0/3
A and not(B)
A= defined $depB= not exists $oq->{"joins"}{$dep}
ABresult
0X0
100
111
1385 }
1386 }
1387  
1388 # make sure deps for named_sorts exist
1389 0 foreach my $named_sort (keys %{ $$oq{'named_sorts'} }) {
1390 0 foreach my $dep (@{ $$oq{'named_sorts'}{$named_sort}->[0] }) {
1391 die "dep $dep for named_sort $named_sort does not exist"
1392 0 ▶ if defined $dep && ! exists $$oq{'joins'}{$dep};
Branch: 1 branch
BranchTrueFalseTotal
if defined $dep and not exists $oq->{"joins"}{$dep} 0 0 0
Condition0%0/3
defined $dep and not exists $oq->{"joins"}{$dep}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dep and not exists $oq->{"joins"}{$dep}
defined $dep not exists $oq->{"joins"}{$dep}
Truth table0%0/3
A and not(B)
A= defined $depB= not exists $oq->{"joins"}{$dep}
ABresult
0X0
100
111
1393 }
1394 }
1395  
1396 # make sure deps for named_filter exist
1397 0 foreach my $named_filter (keys %{ $$oq{'named_filters'} }) {
1398 0 ▶ if (ref($$oq{'named_filters'}{$named_filter}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $oq->{"named_filters"}{$named_filter} eq "ARRAY") 0 0 0
1399 0 foreach my $dep (@{ $$oq{'named_filters'}{$named_filter}->[0] }) {
1400 die "dep $dep for named_sort $named_filter does not exist"
1401 0 ▶ if defined $dep && ! exists $$oq{'joins'}{$dep};
Branch: 1 branch
BranchTrueFalseTotal
if defined $dep and not exists $oq->{"joins"}{$dep} 0 0 0
Condition0%0/3
defined $dep and not exists $oq->{"joins"}{$dep}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dep and not exists $oq->{"joins"}{$dep}
defined $dep not exists $oq->{"joins"}{$dep}
Truth table0%0/3
A and not(B)
A= defined $depB= not exists $oq->{"joins"}{$dep}
ABresult
0X0
100
111
1402 }
1403 }
1404 }
1405  
1406 0 $oq->{'col_types'} = undef;
1407  
1408 0 return undef;
1409 }
1410  
1411  
1412  
1413  
1414  
1415  
1416  
1417 # defines how a child cursor joins to its parent cursor
1418 # by defining keys, join, sql in child cursor
1419 # called from the _normalize method
1420 sub _formulate_new_cursor {
1421 0 ▶ my $oq = shift;
Subroutine _formulate_new_cursor: not called CC=21 SCAR=61.4
1422 0 my $joinAlias = shift;
1423  
1424 0 ▶ $$oq{error_handler}->("DEBUG: \$oq->_formulate_new_cursor('$joinAlias')\n") if $$oq{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $oq->{"debug"} 0 0 0
1425  
1426 # vars to define
1427 0 my (@keys, $join, $sql, @sqlBinds);
1428  
1429 # get join definition
1430 0 my ($fromSql, @fromBinds) = @{ $oq->{joins}->{$joinAlias}->[1] };
1431  
1432 0 my ($whereSql, @whereBinds);
1433 0 ($whereSql, @whereBinds) = @{ $oq->{joins}->{$joinAlias}->[2] }
1434 0 ▶ if defined $oq->{joins}->{$joinAlias}->[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $oq->{"joins"}{$joinAlias}[2] 0 0 0
1435  
1436 # if NOT an SQL-92 type join
1437 0 ▶ if (defined $whereSql) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $whereSql) { } 0 0 0
1438 0 $whereSql =~ s/\(\+\)/\ /g; # remove outer join notation
1439 0 ▶ die "BAD_PARAMS - where binds not allowed in 'new_cursor' joins"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @whereBinds 0 0 0
1440 if scalar(@whereBinds);
1441 }
1442  
1443 # else is SQL-92 so separate out joins from table definition
1444 # do this by making it a pre SQL-92 type join
1445 # by defining $whereSql
1446 # and removing join sql from $fromSql
1447 else {
1448 0 $_ = $fromSql;
1449 0 m/\G\s*left\b/sicg;
1450 0 m/\G\s*join\b/sicg;
1451  
1452 # parse inline view
1453 0 ▶ if (m/\G\s*\(/scg) {
Branch: 2 branches
BranchTrueFalseTotal
if (/\G\s*\(/cgs) { } 0 0 0
elsif (/\G\s*(\w+)\b/cgs) { } 0 0 0
1454 0 $fromSql = '(';
1455 0 my $p=1;
1456 0 my $q;
1457 0 ▶ while ($p > 0 && m/\G(.)/scg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p > 0 and /\G(.)/cgs) 0 0 0
Condition0%0/3
$p > 0 and /\G(.)/cgs
!ll&&!rl&&r
000
MC/DC0%0/2
$p > 0 and /\G(.)/cgs
$p > 0 /\G(.)/cgs
Truth table0%0/3
A and B
A= $p > 0B= /\G(.)/cgs
ABresult
0X0
100
111
1458 0 my $c = $1;
1459 0 ▶ if ($q) { $q = '' if $c eq $q; } # if end of quote
Branch: 5 branches
BranchTrueFalseTotal
if ($q) { } 0 0 0
elsif ($c eq q['] or $c eq '"') { } 0 0 0
elsif ($c eq '(') { } 0 0 0
elsif ($c eq ')') { } 0 0 0
if $c eq $q 0 0 0
1460 0 ▶ elsif ($c eq "'" || $c eq '"') { $q = $c; } # if start of quote
Condition0%0/3
$c eq "'" or $c eq "\""
l!l&&r!l&&!r
000
MC/DC0%0/2
$c eq "'" or $c eq "\""
$c eq "'" $c eq "\""
Truth table0%0/3
A or B
A= $c eq "'"B= $c eq "\""
ABresult
1X1
011
000
1461 0 elsif ($c eq '(') { $p++; } # if left paren
1462 0 elsif ($c eq ')') { $p--; } # if right paren
1463 0 $fromSql .= $c;
1464 }
1465 }
1466  
1467 # parse table name
1468 elsif (m/\G\s*(\w+)\b/scg) {
1469 0 $fromSql = $1;
1470 }
1471  
1472 else {
1473 0 die "could not parse tablename";
1474 }
1475  
1476 # include alias if it exists
1477 0 ▶ if (m/\G\s*([\d\w\_]+)\s*/scg && lc($1) ne 'on') {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\s*([\d\w\_]+)\s*/cgs and lc $1 ne "on") 0 0 0
Condition0%0/3
/\G\s*([\d\w\_]+)\s*/cgs and lc $1 ne "on"
!ll&&!rl&&r
000
MC/DC0%0/2
/\G\s*([\d\w\_]+)\s*/cgs and lc $1 ne "on"
/\G\s*([\d\w\_]+)\s*/cgs lc $1 ne "on"
Truth table0%0/3
A and B
A= /\G\s*([\d\w\_]+)\s*/cgsB= lc $1 ne "on"
ABresult
0X0
100
111
1478 0 $fromSql .= ' '.$1;
1479 0 m/\G\s*on\b/cgi;
1480 }
1481  
1482 # get the whereSql
1483 0 ▶ if (m/\G\s*\((.*)\)\s*$/cgs) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\s*\((.*)\)\s*$/cgs) 0 0 0
1484 0 $whereSql = $1;
1485 }
1486 }
1487  
1488 # define sql & sqlBinds
1489 0 $sql = $fromSql;
1490 0 @sqlBinds = @fromBinds;
1491
1492 # parse $whereSql to create $join, and @keys
1493 0 foreach my $part (split /\b([\w\d\_]+\.[\w\d\_]+)\b/,$whereSql) {
1494 0 ▶ if ($part =~ /\b([\w\d\_]+)\.([\w\d\_]+)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /\b([\w\d\_]+)\.([\w\d\_]+)\b/) { } 0 0 0
1495 0 my $dep = $1;
1496 0 my $sql = $2;
1497 0 ▶ if ($dep eq $joinAlias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dep eq $joinAlias) { } 0 0 0
1498 0 $join .= $part;
1499 } else {
1500 0 push @keys, [$dep, $sql];
1501 0 $join .= '?';
1502 }
1503 } else {
1504 0 $join .= $part;
1505 }
1506 }
1507  
1508 # fill in options
1509 0 $oq->{joins}->{$joinAlias}->[3]->{'new_cursor'} = {
1510 'keys' => \@keys, 'join' => $join, 'sql' => [$sql, @sqlBinds] };
1511  
1512 0 return undef;
1513 }
1514  
1515  
1516  
1517  
1518 # make sure the join counts are the same
1519 # throws exception with error when there is a problem
1520 # this can be an expensive wasteful operation and should not be done in a production env
1521 sub check_join_counts {
1522 0 ▶ my $oq = shift;
Subroutine check_join_counts: not called CC=11 SCAR=48.8
Pod undocumented
1523  
1524 0 ▶ $$oq{error_handler}->("DEBUG: \$oq->check_join_counts()\n") if $$oq{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $oq->{"debug"} 0 0 0
1525  
1526  
1527 # since driving table count is computed first this will get set first
1528 0 my $drivingTableCount;
1529  
1530 0 foreach my $join (keys %{ $oq->{joins} }) {
1531 0 my ($cursors) = @{ $oq->_order_deps($join) };
1532 0 my @deps = map { @$_ } @$cursors; # flatten deps in cursors
1533 0 my $drivingTable = $deps[0];
1534  
1535 # now create from clause
1536 0 my ($fromSql, @fromBinds, @whereSql, @whereBinds);
1537 0 foreach my $joinAlias (@deps) {
1538 0 my ($sql, @sqlBinds) = @{ $oq->{joins}->{$joinAlias}->[1] };
1539  
1540 # if this is the driving table
1541 0 ▶ if (! $oq->{joins}->{$joinAlias}->[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $oq->{'joins'}{$joinAlias}[0]) { } 0 0 0
elsif (defined $oq->{'joins'}{$joinAlias}[2]) { } 0 0 0
1542 # alias it if not already aliased in sql
1543 0 ▶ $fromSql .= " $joinAlias" unless $sql =~ /\b$joinAlias\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sql =~ /\b$joinAlias\s*$/ 0 0 0
1544 }
1545  
1546 # if NOT sql-92 join
1547 elsif (defined $oq->{joins}->{$joinAlias}->[2]) {
1548 0 $fromSql .= ",\n $sql $joinAlias";
1549 0 push @fromBinds, @sqlBinds;
1550 0 my ($where_sql, @where_sqlBinds) = @{ $oq->{joins}->{$joinAlias}->[2] };
1551 0 push @whereSql, $where_sql;
1552 0 push @whereBinds, @where_sqlBinds;
1553 }
1554  
1555 # else this is an SQL-92 type join
1556 else {
1557 0 $fromSql .= "\n$sql ";
1558 }
1559 }
1560  
1561 0 ▶ my $where = 'WHERE '.join(' AND ', @whereSql) if @whereSql;
Branch: 1 branch
BranchTrueFalseTotal
if @whereSql 0 0 0
1562  
1563 0 my $sql = "
1564 SELECT count(*)
1565 FROM (
1566 SELECT $drivingTable.*
1567 FROM $fromSql
1568 $where
1569 ) OPTIMALQUERYCNTCK ";
1570 0 my @binds = (@fromBinds,@whereBinds);
1571 0 my $count;
1572 0 eval { ($count) = $oq->{dbh}->selectrow_array($sql, undef, @binds); };
1573 0 ▶ die "Problem executing ERROR: $@\nSQL: $sql\nBINDS: ".join(',', @binds)."\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1574 0 ▶ $drivingTableCount = $count unless defined $drivingTableCount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $drivingTableCount 0 0 0
1575 0 ▶ confess "BAD_JOIN_COUNT - driving table $drivingTable count ".
Branch: 1 branch
BranchTrueFalseTotal
if $count != $drivingTableCount 0 0 0
1576 "($drivingTableCount) != driving table joined with $join".
1577 " count ($count)" if $count != $drivingTableCount;
1578 }
1579  
1580 0 return undef;
1581 }
1582  
1583  
1584  
1585 =comment
1586 $oq->get_col_type($alias,$context);
1587 =cut
1588 sub type_map {
1589 0 ▶ my $oq = shift;
Subroutine type_map: not called CC=1 SCAR=6.9
Pod undocumented
1590 return {
1591 0 -1 => 'char',
1592 -4 => 'clob',
1593 -5 => 'num',
1594 -6 => 'num',
1595 -9 => 'char',
1596 0 => 'char',
1597 1 => 'char',
1598 3 => 'num', # is decimal type
1599 4 => 'num',
1600 6 => 'num', # float
1601 7 => 'num',
1602 8 => 'num',
1603 9 => 'date',
1604 11 => 'datetime',
1605 10 => 'char',
1606 12 => 'char',
1607 16 => 'date',
1608 30 => 'clob',
1609 40 => 'clob',
1610 91 => 'date',
1611 93 => 'date',
1612 95 => 'date',
1613 'INTEGER' => 'num',
1614 'TEXT' => 'char',
1615 'VARCHAR' => 'char',
1616 'varchar' => 'char'
1617 };
1618 }
1619  
1620 # $type = $oq->get_col_type($alias,$context);
1621 sub get_col_type {
1622 0 ▶ my $oq = shift;
Subroutine get_col_type: not called CC=4 SCAR=30.0
Pod undocumented
1623 0 my $alias = shift;
1624 0 ▶ my $context = shift || 'default';
Condition0%0/2
shift() || 'default'
l!l
00
MC/DC0%0/1
shift() || 'default'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1625 0 ▶ $$oq{error_handler}->("DEBUG: \$oq->get_col_type($alias, $context)\n") if $$oq{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $oq->{"debug"} 0 0 0
1626  
1627 return $oq->{'select'}->{$alias}->[3]->{'col_type'} ||
1628 0 ▶ $oq->get_col_types($context)->{$alias};
Condition0%0/3
$oq->{'select'}{$alias}[3]{'col_type'} || $oq->get_col_types($context)->{$alias}
l!l&&r!l&&!r
000
MC/DC0%0/2
$oq->{'select'}{$alias}[3]{'col_type'} || $oq->get_col_types($context)->{$alias}
$oq->{'select'}{$alias}[3]{'col_type'} $oq->get_col_types($context)->{$alias}
Truth table0%0/3
A || B
A= $oq->{'select'}{$alias}[3]{'col_type'}B= $oq->get_col_types($context)->{$alias}
ABresult
1X1
011
000
1629 }
1630  
1631 #{ ColAlias => Type, .. } = $oq->get_col_types($context)
1632 # where $content in ('default','sort','filter','select')
1633 sub get_col_types {
1634 0 ▶ my $oq = shift;
Subroutine get_col_types: not called CC=28 SCAR=67.0
Pod undocumented
1635 0 ▶ my $context = shift || 'default';
Condition0%0/2
shift() || 'default'
l!l
00
MC/DC0%0/1
shift() || 'default'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1636 0 ▶ $$oq{error_handler}->("DEBUG: \$oq->get_col_types($context)\n") if $$oq{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $oq->{"debug"} 0 0 0
1637 return $oq->{'col_types'}->{$context}
1638 0 ▶ if defined $oq->{'col_types'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $oq->{"col_types"} 0 0 0
1639  
1640 0 $oq->{'col_types'} = {
1641 'default' => {}, 'sort' => {},
1642 'filter' => {}, 'select' => {} };
1643  
1644 0 my (%deps, @selectColTypeOrder, @selectColAliasOrder, @select, @selectBinds, @where);
1645 0 foreach my $selectAlias (keys %{ $oq->{'select'} } ) {
1646 0 my $s = $oq->{'select'}->{$selectAlias};
1647  
1648 # did user already define this type?
1649 0 ▶ if (exists $s->[3]->{'col_type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $s->[3]{'col_type'}) { } 0 0 0
1650 0 $oq->{'col_types'}->{'default'}->{$selectAlias} = $s->[3]->{'col_type'};
1651 0 $oq->{'col_types'}->{'select' }->{$selectAlias} = $s->[3]->{'col_type'};
1652 0 $oq->{'col_types'}->{'filter' }->{$selectAlias} = $s->[3]->{'col_type'};
1653 0 $oq->{'col_types'}->{'sort' }->{$selectAlias} = $s->[3]->{'col_type'};
1654 }
1655  
1656 # else write sql to determine type with context
1657 else {
1658 0 $deps{$_} = 1 for @{ $s->[0] };
1659  
1660 0 foreach my $type (
1661 ['default', $s->[1]],
1662 ['select', $s->[3]->{'select_sql'}],
1663 ['filter', $s->[3]->{'filter_sql'}],
1664 ['sort', $s->[3]->{'sort_sql'}] ) {
1665 0 ▶ next if ! defined $type->[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type->[1] 0 0 0
1666 0 push @selectColTypeOrder, $type->[0];
1667 0 push @selectColAliasOrder, $selectAlias;
1668 0 my ($sql, @binds) = @{ $type->[1] };
1669 0 push @select, $sql;
1670 0 push @selectBinds, @binds;
1671  
1672 # this next one is needed for oracle so inline views don't get processed
1673 # kinda stupid if you ask me
1674 # don't bother though if there is binds
1675 # this isn't neccessary for mysql since an explicit limit is
1676 # defined latter
1677 0 ▶ if ($$oq{dbtype} eq 'Oracle' && $#binds == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oq->{"dbtype"} eq "Oracle" and $#binds == -1) 0 0 0
Condition0%0/3
$oq->{"dbtype"} eq "Oracle" and $#binds == -1
!ll&&!rl&&r
000
MC/DC0%0/2
$oq->{"dbtype"} eq "Oracle" and $#binds == -1
$oq->{"dbtype"} eq "Oracle" $#binds == -1
Truth table0%0/3
A and B
A= $oq->{"dbtype"} eq "Oracle"B= $#binds == -1
ABresult
0X0
100
111
1678 0 push @where, "to_char($sql) = NULL";
1679 }
1680 }
1681 }
1682 }
1683  
1684 # are there unknown deps?
1685 0 ▶ if (%deps) {
Branch: 1 branch
BranchTrueFalseTotal
if (%deps) 0 0 0
1686  
1687 # order and flatten deps
1688 0 my @deps = keys %deps;
1689 0 my ($deps) = @{ $oq->_order_deps(\@deps) };
1690  
1691  
1692 0 @deps = ();
1693 0 push @deps, @$_ for @$deps;
1694  
1695 # now create from clause
1696 0 my ($fromSql, @fromBinds);
1697 0 foreach my $joinAlias (@deps) {
1698 0 my ($sql, @sqlBinds) = @{ $oq->{joins}->{$joinAlias}->[1] };
1699 0 push @fromBinds, @sqlBinds;
1700  
1701 # if this is the driving table join
1702 0 ▶ if (! $oq->{joins}->{$joinAlias}->[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $oq->{'joins'}{$joinAlias}[0]) { } 0 0 0
elsif (defined $oq->{'joins'}{$joinAlias}[2]) { } 0 0 0
1703  
1704 # alias it if not already aliased in sql
1705 0 $fromSql .= $sql;
1706 0 ▶ $fromSql .= " $joinAlias" unless $sql =~ /\b$joinAlias\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sql =~ /\b$joinAlias\s*$/ 0 0 0
1707 }
1708  
1709 # if NOT sql-92 join
1710 elsif (defined $oq->{joins}->{$joinAlias}->[2]) {
1711 0 $fromSql .= ",\n $sql $joinAlias";
1712 }
1713  
1714 # else this is an SQL-92 type join
1715 else {
1716 0 $fromSql .= "\n$sql ";
1717 }
1718  
1719 }
1720  
1721 0 ▶ my $where = ' AND ' if @where;
Branch: 1 branch
BranchTrueFalseTotal
if @where 0 0 0
1722 0 $where .= join("\nAND ", @where);
1723  
1724 0 my @binds = (@selectBinds, @fromBinds);
1725 0 my $sql = "
1726 SELECT ".join(',', @select)."
1727 FROM $fromSql";
1728  
1729 0 ▶ if ($$oq{dbtype} eq 'Oracle' || $$oq{dbtype} eq 'Microsoft SQL Server') {
Branch: 2 branches
BranchTrueFalseTotal
if ($oq->{'dbtype'} eq 'Oracle' or $oq->{'dbtype'} eq 'Microsoft SQL Server') { } 0 0 0
elsif ($oq->{'dbtype'} eq 'mysql') { } 0 0 0
Condition0%0/3
$oq->{"dbtype"} eq "Oracle" or $oq->{"dbtype"} eq "Microsoft SQL Server"
l!l&&r!l&&!r
000
MC/DC0%0/2
$oq->{"dbtype"} eq "Oracle" or $oq->{"dbtype"} eq "Microsoft SQL Server"
$oq->{"dbtype"} eq "Oracle" $oq->{"dbtype"} eq "Microsoft SQL Server"
Truth table0%0/3
A or B
A= $oq->{"dbtype"} eq "Oracle"B= $oq->{"dbtype"} eq "Microsoft SQL Server"
ABresult
1X1
011
000
1730 0 $sql .= "
1731 WHERE 1=2
1732 $where ";
1733 }
1734  
1735 elsif ($$oq{dbtype} eq 'mysql') {
1736 0 $sql .= "
1737 LIMIT 0 ";
1738 }
1739  
1740 0 my $sth;
1741 0 eval {
1742 0 local $oq->{dbh}->{PrintError} = 0;
1743 0 local $oq->{dbh}->{RaiseError} = 1;
1744 0 $sth = $oq->{dbh}->prepare($sql);
1745 0 $sth->execute(@binds);
1746 0 ▶ }; if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1747 0 confess "SQL Error in get_col_types:\n$@\n$sql\n(".join(",",@binds).")";
1748 }
1749  
1750 # read types into col_types cache in object
1751 0 my $type_map = $oq->type_map();
1752 0 ▶ for (my $i=0; $i < scalar(@selectColAliasOrder); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @selectColAliasOrder) 0 0 0
1753 0 my $name = $selectColAliasOrder[$i];
1754 0 my $type_code = $sth->{TYPE}->[$i];
1755  
1756 # remove parenthesis in type_code from sqlite
1757 0 $type_code =~ s/\([^\)]*\)//;
1758
1759 0 ▶ my $type = $type_map->{$type_code} or
Branch: 1 branch
BranchTrueFalseTotal
my $type = $type_map->{$type_code} or die "could not find type code: $type_code for col $name" 0 0 0
1760 die "could not find type code: $type_code for col $name";
1761 0 $oq->{'col_types'}->{$selectColTypeOrder[$i]}->{$name} = $type;
1762  
1763 # set the type for select, filter, and sort to the default
1764 # unless they are already defined
1765 0 ▶ if ($selectColTypeOrder[$i] eq 'default') {
Branch: 1 branch
BranchTrueFalseTotal
if ($selectColTypeOrder[$i] eq "default") 0 0 0
1766 0 ▶ $oq->{'col_types'}->{'select' }->{$name} ||= $type;
Condition0%0/3
$oq->{"col_types"}{"select"}{$name} ||= $type
l!l&&r!l&&!r
000
MC/DC0%0/2
$oq->{"col_types"}{"select"}{$name} ||= $type
$oq->{"col_types"}{"select"}{$name} $type
Truth table0%0/3
A ||= B
A= $oq->{"col_types"}{"select"}{$name}B= $type
ABresult
1X1
011
000
1767 0 ▶ $oq->{'col_types'}->{'filter' }->{$name} ||= $type;
Condition0%0/3
$oq->{"col_types"}{"filter"}{$name} ||= $type
l!l&&r!l&&!r
000
MC/DC0%0/2
$oq->{"col_types"}{"filter"}{$name} ||= $type
$oq->{"col_types"}{"filter"}{$name} $type
Truth table0%0/3
A ||= B
A= $oq->{"col_types"}{"filter"}{$name}B= $type
ABresult
1X1
011
000
1768 0 ▶ $oq->{'col_types'}->{'sort' }->{$name} ||= $type;
Condition0%0/3
$oq->{"col_types"}{"sort"}{$name} ||= $type
l!l&&r!l&&!r
000
MC/DC0%0/2
$oq->{"col_types"}{"sort"}{$name} ||= $type
$oq->{"col_types"}{"sort"}{$name} $type
Truth table0%0/3
A ||= B
A= $oq->{"col_types"}{"sort"}{$name}B= $type
ABresult
1X1
011
000
1769 }
1770 }
1771  
1772 0 $sth->finish();
1773 }
1774  
1775 0 return $oq->{'col_types'}->{$context};
1776 }
1777  
1778  
1779  
1780  
1781 # prepare an sth
1782 sub prepare {
1783 0 ▶ my $oq = shift;
Subroutine prepare: not called CC=2 SCAR=17.9
Pod undocumented
1784 0 ▶ $$oq{error_handler}->("DEBUG: \$oq->prepare(".Dumper(\@_).")\n") if $$oq{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $oq->{"debug"} 0 0 0
1785 0 return DBIx::OptimalQuery::sth->new($oq,@_);
1786 }
1787  
1788  
1789  
1790 # returns ARRAYREF: [order,idx]
1791 # order is [ [dep1,dep2,dep3], [dep4,dep5,dep6] ], # cursor/dep order
1792 # idx is { dep1 => 0, dep4 => 1, .. etc .. } # index of what cursor dep is in
1793 sub _order_deps {
1794 0 ▶ my $oq = shift;
Subroutine _order_deps: not called CC=4 SCAR=30.0
1795 0 ▶ $$oq{error_handler}->("DEBUG: \$oq->_order_deps(".Dumper(\@_).")\n") if $$oq{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $oq->{"debug"} 0 0 0
1796 0 my $deps = shift;
1797 0 ▶ $deps = [$deps] unless ref($deps) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $deps eq "ARRAY" 0 0 0
1798  
1799 # @order is an array of array refs. Where each array ref represents deps
1800 # for a separate cursor
1801 # %idx is a hash of scalars where the hash key is the dep name and the
1802 # hash value is what index into order (which cursor number)
1803 # where you find the dep
1804 0 my (@order, %idx);
1805  
1806 # var to detect infinite recursion
1807 0 my $maxRecurse = 1000;
1808  
1809 # recursive function to order deps
1810 # each dep calls this again on all parent deps until all deps are fulfilled
1811 # then the dep is added
1812 # modfies @order & %idx
1813 0 my $place_missing_deps;
1814 $place_missing_deps = sub {
1815 0 ▶ my $dep = shift;
Subroutine __ANON__: not called CC=9 SCAR=45.0
1816  
1817 # detect infinite recursion
1818 0 $maxRecurse--;
1819 0 ▶ die "BAD_JOINS - could not link joins to meet all deps" if $maxRecurse == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $maxRecurse == 0 0 0 0
1820  
1821 # recursion to make sure parent deps are added first
1822 0 ▶ if (defined $oq->{'joins'}->{$dep}->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oq->{"joins"}{$dep}[0]) 0 0 0
1823 0 foreach my $parent_dep (@{ $oq->{'joins'}->{$dep}->[0] } ) {
1824 0 ▶ $place_missing_deps->($parent_dep) if ! exists $idx{$parent_dep};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $idx{$parent_dep} 0 0 0
1825 }
1826 }
1827  
1828 # at this point all parent deps have been added,
1829 # now add this dep if it has not already been added
1830 0 ▶ if (! exists $idx{$dep}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $idx{$dep}) 0 0 0
1831  
1832 # add new cursor if dep is main driving table or has option new_cursor
1833 0 ▶ if (! defined $oq->{'joins'}->{$dep}->[0] ||
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $oq->{'joins'}{$dep}[0] or exists $oq->{'joins'}{$dep}[3]{'new_cursor'}) { } 0 0 0
Condition0%0/3
not defined $oq->{"joins"}{$dep}[0] or exists $oq->{"joins"}{$dep}[3]{"new_cursor"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $oq->{"joins"}{$dep}[0] or exists $oq->{"joins"}{$dep}[3]{"new_cursor"}
not defined $oq->{"joins"}{$dep}[0] exists $oq->{"joins"}{$dep}[3]{"new_cursor"}
Truth table0%0/3
not(A) or B
A= not defined $oq->{"joins"}{$dep}[0]B= exists $oq->{"joins"}{$dep}[3]{"new_cursor"}
ABresult
1X1
011
000
1834 exists $oq->{'joins'}->{$dep}->[3]->{new_cursor}) {
1835 0 push @order, [$dep];
1836 0 $idx{$dep} = $#order;
1837 }
1838  
1839 # place dep in @order & %idx
1840 # uses the same cursor as its parent dep
1841 # this is found by looking at the parent_idx
1842 else {
1843 0 ▶ my $parent_idx = $idx{$oq->{'joins'}->{$dep}->[0]->[0]} || 0;
Condition0%0/2
$idx{$oq->{'joins'}{$dep}[0][0]} || 0
l!l
00
MC/DC0%0/1
$idx{$oq->{'joins'}{$dep}[0][0]} || 0
$idx{$oq->{'joins'}{$dep}[0][0]}
Truth table0%0/2
A
A= $idx{$oq->{'joins'}{$dep}[0][0]}
Aresult
11
00
1844 0 push @{ $order[ $parent_idx ] }, $dep;
1845 0 $idx{$dep} = $parent_idx;
1846 }
1847 }
1848 0 return undef;
1849 0 };
1850  
1851 0 $place_missing_deps->($_) for @$deps;
1852  
1853 0 return [\@order, \%idx];
1854 }
1855  
1856  
1857 1 1;
« blib/lib/DBIx/OptimalQuery.pm ↑ Summary blib/lib/CGI/OptimalQuery/SaveSearchTool.pm »