DBManager/Filter/text.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 76 CC 9 SCAR 22.0
CC
9
Cov
0%
check
12.0
as_text
12.0
as_filter
12.0
CRAP 9.1
1 package QBit::Application::Model::DBManager::Filter::text;
2 $QBit::Application::Model::DBManager::Filter::text::VERSION = '0.019';
3 use qbit;
4  
5 use base qw(QBit::Application::Model::DBManager::Filter);
6  
7 sub need_tokens {return [qw(STRING NOT LIKE IN IS)]}
8  
9 sub nonterminals {
10 return {
11 strings => "STRING { [\$_[1]] }\n | STRING ',' strings { [\$_[1], \@{\$_[3]}] }\n ;",
12 string_list => "'[' strings ']' { \$_[2] }\n ;"
13 };
14 }
15  
16 sub expressions {
17 my ($self, $field_name) = @_;
18  
19 my $uc_field_name = uc($field_name);
20  
21 return [
22 "$uc_field_name '=' STRING { [$field_name => '=' => \$_[3]] }",
23 "$uc_field_name '<>' STRING { [$field_name => '<>' => \$_[3]] }",
24 "$uc_field_name LIKE STRING { [$field_name => 'LIKE' => \$_[3]] }",
25 "$uc_field_name NOT LIKE STRING { [$field_name => 'NOT LIKE' => \$_[4]] }",
26 "$uc_field_name 'IS' STRING { [$field_name => 'IS' => \$_[3]] }",
27 "$uc_field_name 'IS NOT' STRING { [$field_name => 'IS NOT' => \$_[3]] }",
28 "$uc_field_name '=' string_list { [$field_name => '=' => \$_[3]] }",
29 "$uc_field_name '<>' string_list { [$field_name => '<>' => \$_[3]] }",
30 "$uc_field_name IN string_list { [$field_name => 'IN' => \$_[3]] }",
31 "$uc_field_name NOT IN string_list { [$field_name => 'NOT IN' => \$_[4]] }",
32 ];
33 }
34  
35 sub check {
36 throw gettext('Bad data') unless !ref($_[1]->[2]) || ref($_[1]->[2]) eq 'ARRAY';
37 throw gettext('Bad operation "%s"', $_[1]->[1])
38 unless in_array($_[1]->[1], [qw(= <> LIKE IN IS), 'NOT LIKE', 'NOT IN', 'IS NOT']);
39 }
40  
41 sub as_text {
42 my $string;
43 if (ref($_[1]->[2]) eq 'ARRAY') {
44 $string = '[' . join(', ', map {s/'/\\'/g; "'$_'"} @{$_[1]->[2]}) . ']';
45 } elsif (defined($_[1]->[2])) {
46 $string = $_[1]->[2];
47 $string =~ s/'/\\'/g;
48 $string = "'$string'";
49 } else {
50 $string = 'NULL';
51 }
52 "$_[1]->[0] $_[1]->[1] $string";
53 }
54  
55 sub as_filter {
56 [
57 defined($_[2]->{'db_expr'})
58 ? $_[2]->{'db_expr'}
59 : $_[1]->[0] => $_[1]->[1] => \($_[1]->[1] =~ /LIKE/ ? __like_str($_[1]->[2]) : $_[1]->[2])
60 ];
61 }
62  
63 sub __like_str {
64 my ($text) = @_;
65  
66 $text =~ s/%/\\%/g;
67 $text =~ s/_/\\_/g;
68  
69 $text =~ s/\*/%/g;
70 $text =~ s/\?/_/g;
71  
72 $text = '%' . $text unless $text =~ s/^\^//;
73 $text = $text . '%' unless $text =~ s/([^\\])\$$/$1/;
74  
75 $text =~ s/^\Q%\^/%^/;
76 $text =~ s/\Q\$%\E$/\$%/;
77  
78 return $text;
79 }
80  
81 TRUE;