| 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.11.0'; # VERSION | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 261 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 261 | my( $class, $operator ) = @_; | |||||||||||||||||||||||||||
| 13 | 261 | return bless { operands => [], operator => $operator }, $class; | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | sub push_operand | ||||||||||||||||||||||||||||
| 17 | 259 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
push_operand: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | 259 | my( $self, $operand ) = @_; | |||||||||||||||||||||||||||
| 19 | 259 | ▶ | die 'attempt to insert more than two operands' if @{$self->{operands}} >= 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 259 | push @{$self->{operands}}, $operand; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub unshift_operand | ||||||||||||||||||||||||||||
| 24 | 185 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
unshift_operand: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 185 | my( $self, $operand ) = @_; | |||||||||||||||||||||||||||
| 26 | 185 | ▶ | die 'attempt to insert more than two operands' if @{$self->{operands}} >= 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 185 | unshift @{$self->{operands}}, $operand; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub operator | ||||||||||||||||||||||||||||
| 31 | 345 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
operator: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 345 | my( $self, $operator ) = @_; | |||||||||||||||||||||||||||
| 33 | 345 | my $previous_operator = $self->{operator}; | |||||||||||||||||||||||||||
| 34 | 345 | ▶ | $self->{operator} = $operator if defined $operator; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 345 | return $previous_operator; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub left | ||||||||||||||||||||||||||||
| 39 | 73 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
left: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 73 | my( $self, $operand ) = @_; | |||||||||||||||||||||||||||
| 41 | 73 | my $previous_operand = $self->{operands}[0]; | |||||||||||||||||||||||||||
| 42 | 73 | ▶ | $self->{operands}[0] = $operand if defined $operand; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 73 | return $previous_operand; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub right | ||||||||||||||||||||||||||||
| 47 | 9 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
right: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 9 | my( $self, $operand ) = @_; | |||||||||||||||||||||||||||
| 49 | 9 | my $previous_operand = $self->{operands}[1]; | |||||||||||||||||||||||||||
| 50 | 9 | ▶ | $self->{operands}[1] = $operand if defined $operand; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 9 | return $previous_operand; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub to_filter | ||||||||||||||||||||||||||||
| 55 | 184 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
to_filter: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 184 | my( $self ) = @_; | |||||||||||||||||||||||||||
| 57 | 184 | $self->validate; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 184 | my $operator = $self->{operator}; | |||||||||||||||||||||||||||
| 60 | 184 | my @operands; | |||||||||||||||||||||||||||
| 61 | 184 | for my $i (0..$#{$self->{operands}}) { | |||||||||||||||||||||||||||
| 62 | 368 | my $arg = $self->{operands}[$i]; | |||||||||||||||||||||||||||
| 63 | 368 | ▶ | 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 | 216 | $arg = $arg->to_filter; | |||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||
| 66 | 152 | $arg =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||
| 67 | 152 | $arg =~ s/"/\\"/g; | |||||||||||||||||||||||||||
| 68 | 152 | $arg = "\"$arg\""; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 368 | push @operands, $arg; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 184 | return "($operands[0] $operator $operands[1])"; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub to_SQL | ||||||||||||||||||||||||||||
| 77 | 93 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
to_SQL: called CC=15 SCAR=27.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 93 | my( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 79 | 93 | $self->validate; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 93 | ▶ | $options = {} unless $options; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | my( $delim, $placeholder ) = ( | ||||||||||||||||||||||||||||
| 83 | $options->{delim}, | ||||||||||||||||||||||||||||
| 84 | $options->{placeholder}, | ||||||||||||||||||||||||||||
| 85 | 93 | ); | |||||||||||||||||||||||||||
| 86 | 93 | ▶ | $delim = "'" unless $delim; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 93 | my $operator = $self->{operator}; | |||||||||||||||||||||||||||
| 89 | 93 | my @operands = @{$self->{operands}}; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Handle STARTS/ENDS WITH | ||||||||||||||||||||||||||||
| 92 | 93 | ▶ | 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 | 93 | my @values; | |||||||||||||||||||||||||||
| 104 | my @operands_now; | ||||||||||||||||||||||||||||
| 105 | 93 | for my $arg (@operands) { | |||||||||||||||||||||||||||
| 106 | 184 | my $sql = $arg; | |||||||||||||||||||||||||||
| 107 | 184 | ▶ | 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 | 107 | ( $sql, my $values ) = $arg->to_SQL( $options ); | |||||||||||||||||||||||||||
| 109 | 97 | ▶ | if( $arg->isa( OPTIMADE::Filter::Comparison:: ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | $sql = "($sql)"; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 97 | 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 | 174 | 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 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
modify: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 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 | 277 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=3 SCAR=12.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 145 | 277 | my $self = shift; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 277 | ▶ | 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 | 277 | ▶ | die 'operator undefined for OPTIMADE::Filter::Comparison' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | if !$self->operator; | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 6 | 1; | |||||||||||||||||||||||||||