DBBrowser/Table/Substatement/Condition.pm untested

filter: Statementstmt 0.0% 0 / 314 Branchbran 0.0% 0 / 180 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 557 CC 91 SCAR 60.8
CC
91
Cov
0%
__add_operator_and_value
600.0
__pattern_match
552.0
read_and_add_value
506.0
CRAP 435.5
1 package # hide from PAUSE
2 App::DBBrowser::Table::Substatement::Condition;
3  
4 use warnings;
5 use strict;
6 use 5.016;
7  
8 use List::MoreUtils qw( any uniq );
9  
10 use Term::Choose qw();
11 use Term::Form::ReadLine qw();
12  
13 use App::DBBrowser::Auxil;
14 use App::DBBrowser::Table::Extensions;
15  
16  
17 sub new {
18 my ( $class, $info, $options, $d ) = @_;
19 my $sf = {
20 i => $info,
21 o => $options,
22 d => $d
23 };
24 bless $sf, $class;
25 }
26  
27  
28 sub add_condition {
29 my ( $sf, $sql, $clause, $cols, $r_data ) = @_;
30 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
31 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
32 my $AND_OR = '';
33 my @bu;
34 my $stmt;
35 if ( $clause =~ s/_(when)\z//i ) {
36 $stmt = 'when_stmt';
37 $sql->{$stmt} = uc $1;
38 }
39 elsif( $clause =~ /^on\z/i ) {
40 $stmt = lc( $clause ) . '_stmt';
41 $sql->{$stmt} = uc $clause;
42 }
43 else {
44 $stmt = lc( $clause ) . '_stmt';
45 @bu = @{$sql->{'bu_' . $stmt}//[]};
46 $sql->{$stmt} = pop ( @bu ) // uc $clause;
47 }
48 my @pre = ( undef, $sf->{i}{ok} );
49 if ( $sf->{o}{enable}{extended_cols} ) {
50 push @pre, $sf->{i}{menu_addition};
51 }
52  
53 COL: while ( 1 ) {
54 my $info = $sf->__info_add_condition( $sql, $stmt, $r_data );
55 # Choose
56 my $col = $tc->choose(
57 [ @pre, @$cols ],
58 { %{$sf->{i}{lyt_h}}, info => $info }
59 );
60 $ax->print_sql_info( $info );
61 if ( ! defined $col ) {
62 if ( @bu ) {
63 $sql->{$stmt} = pop @bu;
64 next COL;
65 }
66 $sql->{'bu_' . $stmt} = [];
67 $sql->{$stmt} = '';
68 return;
69 }
70 if ( $col eq $sf->{i}{ok} ) {
71 if ( ! @bu ) {
72 $sql->{$stmt} = '';
73 }
74 else {
75 push @bu, $sql->{$stmt};
76 }
77 $sql->{'bu_' . $stmt} = [ @bu ];
78 return 1;
79 }
80 if ( $col eq $sf->{i}{menu_addition} ) {
81 $r_data->[-1][-1] = $sql->{$stmt} if @{$r_data//[]};
82 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
83 my $complex_col = $ext->column( $sql, $clause, $r_data, { add_parentheses => 1 } );
84 if ( ! defined $complex_col ) {
85 next COL;
86 }
87 $col = $complex_col;
88 }
89 push @bu, $sql->{$stmt};
90  
91 if ( $col eq ')' ) {
92 $sql->{$stmt} .= ")";
93 next COL;
94 }
95 if ( @bu == 1 || $sql->{$stmt} =~ /\(\z/ ) {
96 $AND_OR = '';
97 }
98 else {
99 my $info = $sf->__info_add_condition( $sql, $stmt, $r_data );
100 # Choose
101 my $choice = $tc->choose(
102 [ undef, "AND", "OR" ],
103 { %{$sf->{i}{lyt_h}}, info => $info }
104 );
105 $ax->print_sql_info( $info );
106 if ( ! defined $choice ) {
107 $sql->{$stmt} = pop @bu;
108 next COL;
109 }
110 $AND_OR = ' ' . $choice;
111 }
112 if ( $col eq '(' ) {
113 $sql->{$stmt} .= $AND_OR . " (";
114 next COL;
115 }
116 if ( $clause eq 'having' ) {
117 my $sa = App::DBBrowser::Table::Substatement::Aggregate->new( $sf->{i}, $sf->{o}, $sf->{d} );
118 $col = $sa->get_prepared_aggr_func( $sql, $clause, $col );
119 if ( ! defined $col ) {
120 $sql->{$stmt} = pop @bu;
121 next COL;
122 }
123 }
124 if ( $sql->{$stmt} =~ /\(\z/ ) {
125 $sql->{$stmt} .= $col;
126 }
127 else {
128 $sql->{$stmt} .= $AND_OR . ' ' . $col;
129 }
130 my $ok = $sf->__add_operator_and_value( $sql, $clause, $stmt, $col, $r_data );
131 if ( ! $ok ) {
132 $sql->{$stmt} = pop @bu;
133 next COL;
134 }
135 }
136 }
137  
138  
139 sub __add_operator_and_value {
140 my ( $sf, $sql, $clause, $stmt, $col, $r_data ) = @_;
141 my $driver = $sf->{i}{driver};
142 my $dbms = $sf->{i}{dbms};
143 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
144 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
145 my @operators = @{$sf->{o}{G}{operators}};
146 my $not_equal = ( any { $_ =~ /^\s?!=\s?\z/ } @operators ) ? "!=" : "<>";
147 if ( ! length $col ) {
148 $sql->{$stmt} =~ s/\s\z//;
149 @operators = ( "EXISTS", "NOT EXISTS" );
150 }
151 elsif ( $dbms eq 'SQLite' ) {
152 @operators = grep { ! /^(?:ANY|ALL)\z/ } @operators;
153 @operators = grep { ! /REGEXP/ } @operators if $driver eq 'ODBC';
154 }
155 elsif ( $dbms eq 'Firebird' ) {
156 @operators = uniq map { s/REGEXP(?:_i)?\z/SIMILAR TO/; $_ } @operators;
157 }
158 elsif ( $dbms eq 'Informix' ) {
159 @operators = uniq map { s/REGEXP(?:_i)?\z/MATCHES/; $_ } @operators;
160 }
161  
162 elsif ( $dbms eq 'MSSQL' ) {
163 my $major_server_version = $ax->major_server_version();
164 if ( $major_server_version < 17 ) {
165 @operators = grep { ! /REGEXP/ } @operators;
166 }
167 }
168 elsif ( $dbms eq 'other' ) {
169 @operators = grep { ! /REGEXP/ } @operators;
170 }
171 my $bu_stmt = $sql->{$stmt};
172  
173 OPERATOR: while( 1 ) {
174 my $operator;
175 #if ( @operators == 1 ) { ##
176 # $operator = $operators[0];
177 #}
178 #else {
179 my @pre = ( undef );
180 my $info = $sf->__info_add_condition( $sql, $stmt, $r_data );
181 # Choose
182 $operator = $tc->choose(
183 [ @pre, @operators ],
184 { %{$sf->{i}{lyt_h}}, info => $info }
185 );
186 $ax->print_sql_info( $info );
187 if ( ! defined $operator ) {
188 $sql->{$stmt} = $bu_stmt;
189 return;
190 }
191 #}
192 $operator =~ s/^\s+|\s+\z//g;
193 if ( $operator =~ /(?:REGEXP(?:_i)?|SIMILAR\sTO)\z/ ) {
194 my $not_match = $operator =~ /^NOT/ ? 1 : 0;
195 my $case_sensitive = $operator =~ /REGEXP_i\z/ ? 0 : 1;
196 my $regex_op = $sf->__pattern_match( $sql, $col, $not_match, $case_sensitive );
197 if ( ! $regex_op ) {
198 next OPERATOR if @operators > 1;
199 return;
200 }
201 $sql->{$stmt} =~ s/ (?: (?<=\() | \s ) \Q$col\E \z //x;
202 if ( $sql->{$stmt} =~ /\(\z/ ) {
203 $regex_op =~ s/^\s//;
204 }
205 $sql->{$stmt} .= $regex_op;
206 }
207 elsif ( $operator =~ /^(?:ALL|ANY)\z/) {
208 my @comb_op = ( "= $operator", "$not_equal $operator", "> $operator", "< $operator", ">= $operator", "<= $operator" );
209 my @pre = ( undef );
210 my $info = $sf->__info_add_condition( $sql, $stmt, $r_data );
211 # Choose
212 $operator = $tc->choose(
213 [ @pre, @comb_op ],
214 { %{$sf->{i}{lyt_h}}, info => $info }
215 );
216 $ax->print_sql_info( $info );
217 if ( ! defined $operator ) {
218 next OPERATOR if @operators > 1;
219 return;
220 }
221 $sql->{$stmt} .= ' ' . $operator;
222 }
223 elsif ( $operator =~ /^(?:NOT )?EXISTS\z/ ) {
224 if ( $sql->{$stmt} =~ /\(\z/ ) {
225 $sql->{$stmt} .= $operator;
226 }
227 else {
228 $sql->{$stmt} .= ' ' . $operator;
229 }
230 }
231 else {
232 $sql->{$stmt} .= ' ' . $operator;
233 }
234 my $ok = $sf->read_and_add_value( $sql, $clause, $stmt, $col, $operator, $r_data );
235 if ( $ok ) {
236 return 1;
237 }
238 else {
239 $sql->{$stmt} = $bu_stmt;
240 next OPERATOR if @operators > 1;
241 return;
242 }
243 }
244 }
245  
246  
247 sub __info_add_condition {
248 my ( $sf, $sql, $stmt, $r_data ) = @_;
249 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
250 my $info = $ax->get_sql_info( $sql );
251 if ( @{$r_data//[]} ) {
252 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
253 $r_data->[-1][-1] = $sql->{$stmt};
254 $info .= $ext->nested_func_info( $r_data );
255 }
256 return $info;
257 }
258  
259  
260 sub read_and_add_value {
261 my ( $sf, $sql, $clause, $stmt, $col, $operator, $r_data ) = @_;
262 my $dbms = $sf->{i}{dbms};
263 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
264 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
265 my $is_numeric = $ax->is_numeric( $sql, $col );
266 $r_data->[-1][-1] = $sql->{$stmt} if @{$r_data//[]};
267 if ( $operator =~ /^IS\s(?:NOT\s)?NULL\z/ ) {
268 return 1;
269 }
270 elsif ( $operator =~ /^(?:NOT\s)?IN\z/ ) {
271 my $bu_stmt = $sql->{$stmt};
272 $sql->{$stmt} .= ' (';
273 my @args;
274  
275 IN: while ( 1 ) {
276 # Readline
277 my $value = $ext->value( $sql, $clause, $r_data, $operator, { is_numeric => $is_numeric } );
278 if ( ! defined $value ) {
279 if ( ! @args ) {
280 $sql->{$stmt} = $bu_stmt;
281 return;
282 }
283 pop @args;
284 $sql->{$stmt} = $bu_stmt . ' (' . join ',', @args;
285 next IN;
286 }
287 if ( ! length $value || $value eq "''" ) {
288 if ( ! @args ) {
289 $sql->{$stmt} = $bu_stmt;
290 return;
291 }
292 if ( @args == 1 && $args[0] =~ /^\s*\((.+)\)\s*\z/ ) {
293 # if the only argument is a subquery:
294 # remove the parenthesis around the subquery
295 # because "IN ((subquery))" is not alowed
296 $sql->{$stmt} = $bu_stmt . ' (' . $1;
297 }
298 $sql->{$stmt} .= ')';
299 return 1;
300 }
301 push @args, $value;
302 $sql->{$stmt} = $bu_stmt . ' (' . join ',', @args;
303 $r_data->[-1][-1] = $sql->{$stmt} if @{$r_data//[]};
304 }
305 }
306 elsif ( $operator =~ /^(?:NOT\s)?BETWEEN\z/ ) {
307 # Readline
308 my $value_1 = $ext->value( $sql, $clause, $r_data, $operator, { is_numeric => $is_numeric } );
309 if ( ! defined $value_1 ) {
310 return;
311 }
312 my $bu_stmt = $sql->{$stmt};
313 $sql->{$stmt} .= ' ' . $value_1 . ' AND';
314 $r_data->[-1][-1] = $sql->{$stmt} if @{$r_data//[]};
315 # Readline
316 my $value_2 = $ext->value( $sql, $clause, $r_data, $operator, { is_numeric => $is_numeric } );
317 if ( ! defined $value_2 ) {
318 $sql->{$stmt} = $bu_stmt;
319 return;
320 }
321 $sql->{$stmt} .= ' ' . $value_2;
322 return 1;
323 }
324 elsif ( $operator =~ /(?:REGEXP(?:_i)?|SIMILAR\sTO|MATCHES|LIKE)\z/ ) {
325 # Readline
326 my $value = $ext->value( $sql, $clause, $r_data, $operator, { is_numeric => 0 } );
327 if ( ! defined $value ) {
328 return;
329 }
330 #if ( ! length $value ) {
331 # $value = "''";
332 #}
333 if ( $operator =~ /SIMILAR\sTO\z/ ) {
334 $sql->{$stmt} =~ s/ \? (?=\sESCAPE\s'\\'\z) /$value/x;
335 }
336 elsif ( $operator =~ /REGEXP(?:_i)?\z/ ) {
337 if ( $dbms eq 'SQLite' ) {
338 $sql->{$stmt} =~ s/ (?<=\sREGEXP\() \? (?=,\Q$col\E,[01]\)\z) /$value/x;
339 }
340 elsif ( $dbms =~ /^(?:DB2|Oracle|MSSQL)\z/ ) {
341 $sql->{$stmt} =~ s/ \? (?=,'[ci]'\)\z) /$value/x;
342 }
343 else {
344 $sql->{$stmt} .= ' ' . $value;
345 }
346 }
347 else {
348 $sql->{$stmt} .= ' ' . $value;
349 }
350 return 1;
351 }
352 else {
353 my $value;
354 if ( $clause eq 'on' ) {
355 $value = $sf->__choose_a_column( $sql, $clause, $stmt, $r_data );
356 }
357 else {
358 # Readline
359 $value = $ext->value( $sql, $clause, $r_data, $operator, { is_numeric => $is_numeric } );
360 }
361 if ( ! defined $value ) {
362 return;
363 }
364 $sql->{$stmt} .= ' ' . $value;
365 return 1;
366 }
367 }
368  
369  
370 sub __choose_a_column {
371 my ( $sf, $sql, $clause, $stmt, $r_data ) = @_;
372 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
373 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
374 my @pre = ( undef );
375 if ( $sf->{o}{enable}{extended_cols} ) {
376 push @pre, $sf->{i}{menu_addition};
377 }
378 my @choices = @{$sql->{cols_join_condition}};
379  
380 COL: while ( 1 ) {
381 my $info = $sf->__info_add_condition( $sql, $stmt, $r_data );
382 # Choose
383 my $col = $tc->choose(
384 [ @pre, @choices ],
385 { %{$sf->{i}{lyt_h}}, info => $info }
386 );
387 $ax->print_sql_info( $info );
388 if ( ! defined $col ) {
389 return;
390 }
391 if ( $col eq $sf->{i}{menu_addition} ) {
392 if ( @{$r_data//[]} ) {
393 $r_data->[-1][-1] = $sql->{$stmt};
394 }
395 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
396 my $complex_col = $ext->column( $sql, $clause, $r_data );
397 if ( ! defined $complex_col ) {
398 next COL;
399 }
400 $col = $complex_col;
401 }
402 return $col;
403 }
404 }
405  
406  
407 sub __pattern_match {
408 my ( $sf, $sql, $col, $not_match, $case_sensitive ) = @_;
409 my $dbms = $sf->{i}{dbms};
410 if ( $dbms eq 'SQLite' ) {
411 if ( $not_match ) {
412 return sprintf " NOT REGEXP(?,%s,%d)", $col, $case_sensitive;
413 }
414 else {
415 return sprintf " REGEXP(?,%s,%d)", $col, $case_sensitive;
416 }
417 }
418 elsif ( $dbms =~ /^(?:mysql|MariaDB)\z/ ) {
419 if ( $not_match ) {
420 return " $col NOT REGEXP" if ! $case_sensitive;
421 return " $col NOT REGEXP BINARY" if $case_sensitive;
422 }
423 else {
424 return " $col REGEXP" if ! $case_sensitive;
425 return " $col REGEXP BINARY" if $case_sensitive;
426 }
427 }
428 elsif ( $dbms eq 'Pg' ) {
429 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
430 my $col = $ax->pg_column_to_text( $sql, $col );
431 if ( $not_match ) {
432 return " $col !~*" if ! $case_sensitive;
433 return " $col !~" if $case_sensitive;
434 }
435 else {
436 return " $col ~*" if ! $case_sensitive;
437 return " $col ~" if $case_sensitive;
438 }
439 }
440 elsif ( $dbms eq 'Firebird' ) {
441 if ( $not_match ) {
442 return " $col NOT SIMILAR TO ? ESCAPE '\\'";
443 }
444 else {
445 return " $col SIMILAR TO ? ESCAPE '\\'";
446 }
447 }
448 elsif ( $dbms =~ /^(?:DB2|Oracle|MSSQL)\z/ ) {
449 if ( $not_match ) {
450 return " NOT REGEXP_LIKE($col,?,'i')" if ! $case_sensitive;
451 return " NOT REGEXP_LIKE($col,?,'c')" if $case_sensitive;
452 }
453 else {
454 return " REGEXP_LIKE($col,?,'i')" if ! $case_sensitive;
455 return " REGEXP_LIKE($col,?,'c')" if $case_sensitive;
456 }
457 }
458 }
459  
460  
461  
462 # The pattern must match the entire string:
463 #
464 # SIMILAR TO: % _ no default Escape
465 # MATCHES: * ? \
466 # LIKE: % _ \ SQLite, Firebird, DB2 and Oracle no default Escape
467  
468  
469  
470  
471 1;
472  
473 __END__