| 1 | package OPTIMADE::Filter::Comparison; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use parent 'OPTIMADE::Filter::Modifiable'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 6 | our $VERSION = '0.10.2'; # VERSION | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub new { | ||||||||||||||||||||||||||||
| 12 | 173 | ▶ | my( $class, $operator ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 173 | return bless { operands => [], operator => $operator }, $class; | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | sub push_operand | ||||||||||||||||||||||||||||
| 17 | { | ||||||||||||||||||||||||||||
| 18 | 175 | ▶ | my( $self, $operand ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
push_operand: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 175 | ▶ | die 'attempt to insert more than two operands' if @{$self->{operands}} >= 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 175 | push @{$self->{operands}}, $operand; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub unshift_operand | ||||||||||||||||||||||||||||
| 24 | { | ||||||||||||||||||||||||||||
| 25 | 169 | ▶ | my( $self, $operand ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
unshift_operand: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 169 | ▶ | die 'attempt to insert more than two operands' if @{$self->{operands}} >= 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 169 | unshift @{$self->{operands}}, $operand; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub operator | ||||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||
| 32 | 247 | ▶ | my( $self, $operator ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
operator: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 247 | my $previous_operator = $self->{operator}; | |||||||||||||||||||||||||||
| 34 | 247 | ▶ | $self->{operator} = $operator if defined $operator; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 247 | return $previous_operator; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub left | ||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||
| 40 | 1 | ▶ | my( $self, $operand ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
left: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 1 | my $previous_operand = $self->{operands}[0]; | |||||||||||||||||||||||||||
| 42 | 1 | ▶ | $self->{operands}[0] = $operand if defined $operand; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 1 | return $previous_operand; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub right | ||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | 5 | ▶ | my( $self, $operand ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
right: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | 5 | my $previous_operand = $self->{operands}[1]; | |||||||||||||||||||||||||||
| 50 | 5 | ▶ | $self->{operands}[1] = $operand if defined $operand; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 5 | return $previous_operand; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub to_filter | ||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||
| 56 | 164 | ▶ | my( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_filter: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 164 | $self->validate; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 164 | my $operator = $self->{operator}; | |||||||||||||||||||||||||||
| 60 | 164 | my @operands; | |||||||||||||||||||||||||||
| 61 | 164 | for my $i (0..$#{$self->{operands}}) { | |||||||||||||||||||||||||||
| 62 | 328 | my $arg = $self->{operands}[$i]; | |||||||||||||||||||||||||||
| 63 | 328 | ▶ | if( blessed $arg && $arg->can( 'to_filter' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($arg) and $arg->can("to_filter")
MC/DC50%1/2
builtin::blessed($arg) and $arg->can("to_filter")
builtin::blessed($arg)
$arg->can("to_filter")
Truth table66%2/3
A and B
A= builtin::blessed($arg)B= $arg->can("to_filter")
| |||||||||||||||||||||||||||||
| 64 | 176 | $arg = $arg->to_filter; | |||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||
| 66 | 152 | $arg =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||
| 67 | 152 | $arg =~ s/"/\\"/g; | |||||||||||||||||||||||||||
| 68 | 152 | $arg = "\"$arg\""; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 328 | push @operands, $arg; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 164 | return "($operands[0] $operator $operands[1])"; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub to_SQL | ||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 83 | ▶ | my( $self, $options ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_SQL: called CC=15 SCAR=27.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 83 | $self->validate; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 83 | ▶ | $options = {} unless $options; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | my( $delim, $placeholder ) = ( | ||||||||||||||||||||||||||||
| 83 | $options->{delim}, | ||||||||||||||||||||||||||||
| 84 | $options->{placeholder}, | ||||||||||||||||||||||||||||
| 85 | 83 | ); | |||||||||||||||||||||||||||
| 86 | 83 | ▶ | $delim = "'" unless $delim; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 83 | my $operator = $self->{operator}; | |||||||||||||||||||||||||||
| 89 | 83 | my @operands = @{$self->{operands}}; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Handle STARTS/ENDS WITH | ||||||||||||||||||||||||||||
| 92 | 83 | ▶ | if( $operator eq 'CONTAINS' ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 93 | 3 | $operator = 'LIKE'; | |||||||||||||||||||||||||||
| 94 | 3 | ▶ | $operands[1] = '%' . $operands[1] . '%' if !blessed $operands[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } elsif( $operator =~ /^STARTS( WITH)?$/ ) { | ||||||||||||||||||||||||||||
| 96 | 4 | $operator = 'LIKE'; | |||||||||||||||||||||||||||
| 97 | 4 | ▶ | $operands[1] = $operands[1] . '%' if !blessed $operands[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } elsif( $operator =~ /^ENDS( WITH)?$/ ) { | ||||||||||||||||||||||||||||
| 99 | 1 | $operator = 'LIKE'; | |||||||||||||||||||||||||||
| 100 | 1 | ▶ | $operands[1] = '%' . $operands[1] if !blessed $operands[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 83 | my @values; | |||||||||||||||||||||||||||
| 104 | my @operands_now; | ||||||||||||||||||||||||||||
| 105 | 83 | for my $arg (@operands) { | |||||||||||||||||||||||||||
| 106 | 166 | my $sql = $arg; | |||||||||||||||||||||||||||
| 107 | 166 | ▶ | if( blessed $arg && $arg->can( 'to_SQL' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($arg) and $arg->can("to_SQL")
MC/DC100%2/2
builtin::blessed($arg) and $arg->can("to_SQL")
builtin::blessed($arg)
$arg->can("to_SQL")
Truth table100%3/3
A and B
A= builtin::blessed($arg)B= $arg->can("to_SQL")
| |||||||||||||||||||||||||||||
| 108 | 89 | ( $sql, my $values ) = $arg->to_SQL( $options ); | |||||||||||||||||||||||||||
| 109 | 89 | ▶ | if( $arg->isa( OPTIMADE::Filter::Comparison:: ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | $sql = "($sql)"; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 89 | push @values, @$values; | |||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||
| 114 | 77 | push @values, $arg; | |||||||||||||||||||||||||||
| 115 | 77 | ▶ | if( $placeholder ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 16 | $sql = $placeholder; | |||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||
| 118 | 61 | $sql =~ s/"/""/g; | |||||||||||||||||||||||||||
| 119 | 61 | $sql = "\"$sql\""; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 166 | push @operands_now, $sql; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 83 | @operands = @operands_now; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 83 | ▶ | if( wantarray ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 69 | return ( "$operands[0] $operator $operands[1]", \@values ); | |||||||||||||||||||||||||||
| 128 | } else { | ||||||||||||||||||||||||||||
| 129 | 14 | return "$operands[0] $operator $operands[1]"; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub modify | ||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||
| 135 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
modify: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 136 | 5 | my $code = shift; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 10 | $self->{operands} = [ map { OPTIMADE::Filter::Modifiable::modify( $_, $code, @_ ) } | |||||||||||||||||||||||||||
| 139 | 5 | @{$self->{operands}} ]; | |||||||||||||||||||||||||||
| 140 | 5 | return $code->( $self, @_ ); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub validate | ||||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||
| 145 | 247 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=3 SCAR=12.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 247 | ▶ | if( @{$self->{operands}} != 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | die 'number of operands for OPTIMADE::Filter::Comparison must be 2, ' . | ||||||||||||||||||||||||||||
| 149 | 0 | 'got ' . @{$self->{operands}}; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 247 | ▶ | die 'operator undefined for OPTIMADE::Filter::Comparison' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | if !$self->operator; | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 6 | 1; | |||||||||||||||||||||||||||