| 1 | package OPTIMADE::Filter::Property; | ||||||||||
| 2 | |||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 7 | ▶ | use parent 'OPTIMADE::Filter::Modifiable'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 7 | ▶ | use Scalar::Util qw(blessed); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 7 | our $VERSION = '0.11.0'; # VERSION | |||||||||
| 10 | |||||||||||
| 11 | 2768 | ▶ | use overload '@{}' => sub { return $_[0]->{name} }, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 555 | ▶ | '""' => sub { return $_[0]->to_filter }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 0 | ▶ | '==' => sub { return $_[0]->_eq( $_[1] ) }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 14 | 7 | ▶ | 'eq' => sub { return $_[0]->_eq( $_[1] ) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 7 | our $identifier_re = q/([a-z_][a-z0-9_]*)/; | |||||||||
| 17 | |||||||||||
| 18 | 272 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 19 | 272 | my $class = shift; | |||||||||
| 20 | 272 | return bless { name => \@_ }, $class; | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | sub to_filter | ||||||||||
| 24 | 817 | ▶ | { | ||||||||
|
Subroutine
to_filter: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 25 | 817 | my( $self ) = @_; | |||||||||
| 26 | |||||||||||
| 27 | # Validate | ||||||||||
| 28 | 817 | $self->validate; | |||||||||
| 29 | 817 | for my $name (@$self) { | |||||||||
| 30 | 826 | my $lc_name = lc $name; | |||||||||
| 31 | 826 | ▶ | next if $lc_name =~ /^$identifier_re$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 1 | die "name '$lc_name' does not match identifier syntax: $identifier_re"; | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 816 | return join '.', map { lc } @$self; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | sub to_SQL | ||||||||||
| 39 | 100 | ▶ | { | ||||||||
|
Subroutine
to_SQL: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||
| 40 | 100 | my( $self, $options ) = @_; | |||||||||
| 41 | |||||||||||
| 42 | 100 | ▶ | $options = {} unless $options; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | my( $delim, $placeholder ) = ( | ||||||||||
| 44 | $options->{delim}, | ||||||||||
| 45 | $options->{placeholder}, | ||||||||||
| 46 | 100 | ); | |||||||||
| 47 | 100 | ▶ | $delim = "'" unless $delim; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | |||||||||||
| 49 | # Validate | ||||||||||
| 50 | 100 | $self->validate; | |||||||||
| 51 | 100 | ▶ | if( @$self > 2 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | die 'no SQL representation for properties of more than two ' . | |||||||||
| 53 | "identifiers\n"; | ||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | # Construct the SQL | ||||||||||
| 57 | 100 | my $sql = join '.', map { "${delim}$_${delim}" } @$self; | |||||||||
| 58 | |||||||||||
| 59 | 100 | ▶ | if( wantarray ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 99 | return ( $sql, [] ); | |||||||||
| 61 | } else { | ||||||||||
| 62 | 1 | return $sql; | |||||||||
| 63 | } | ||||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | sub modify | ||||||||||
| 67 | 7 | ▶ | { | ||||||||
|
Subroutine
modify: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 68 | 7 | my $self = shift; | |||||||||
| 69 | 7 | my $code = shift; | |||||||||
| 70 | |||||||||||
| 71 | 7 | return $code->( $self, @_ ); | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | sub validate | ||||||||||
| 75 | 917 | ▶ | { | ||||||||
|
Subroutine
validate: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||
| 76 | 917 | my $self = shift; | |||||||||
| 77 | 917 | ▶ | die 'name undefined for OPTIMADE::Filter::Property' if !@$self; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | sub _eq | ||||||||||
| 81 | 1 | ▶ | { | ||||||||
|
Subroutine
_eq: called CC=8 SCAR=22.5
| |||||||||||
| 82 | 1 | my( $a, $b ) = @_; | |||||||||
| 83 | |||||||||||
| 84 | 1 | ▶ | return '' if !blessed( $b ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 1 | ▶ | return '' if !$b->isa( OPTIMADE::Filter::Property:: ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | |||||||||||
| 87 | 1 | ▶ | return '' if @$a != @$b; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 1 | for my $i (0..$#$a) { | |||||||||
| 89 | 2 | ▶ | return '' if defined $a->[$i] ^ defined $b->[$i]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 2 | ▶ | next if !defined $a->[$i]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 2 | ▶ | return '' if $a->[$i] ne $b->[$i]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | } | ||||||||||
| 93 | 1 | return 1; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 7 | 1; | |||||||||