| 1 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Metabase::Query; | ||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Generic Metabase query language role | ||||||||||||||||||||||||||||
| 7 | 4 | our $VERSION = '1.003'; # VERSION | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use List::AllUtils qw/all/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 14 | # Operators and validator definitions | ||||||||||||||||||||||||||||
| 15 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | my %ops = ( | |||||||||||||||||||||||||||
| 18 | op_not => 'UP', # unary predicate | ||||||||||||||||||||||||||||
| 19 | op_or => 'PL', # predicate list | ||||||||||||||||||||||||||||
| 20 | op_and => 'PL', | ||||||||||||||||||||||||||||
| 21 | op_eq => 'FV', # field, value | ||||||||||||||||||||||||||||
| 22 | op_ne => 'FV', | ||||||||||||||||||||||||||||
| 23 | op_gt => 'FV', | ||||||||||||||||||||||||||||
| 24 | op_lt => 'FV', | ||||||||||||||||||||||||||||
| 25 | op_ge => 'FV', | ||||||||||||||||||||||||||||
| 26 | op_le => 'FV', | ||||||||||||||||||||||||||||
| 27 | op_like => 'FV', | ||||||||||||||||||||||||||||
| 28 | op_between => 'FLH', # field, value, value | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | my %validators = ( | ||||||||||||||||||||||||||||
| 32 | 12 | ▶ | PL => sub { all {_is_predicate($_)} @_ }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | UP => sub { @_ == 1 and _is_predicate($_[0]) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 39 | ▶ | FV => sub { @_ == 2 and _field_ok($_[0]) and _value_ok($_[1]) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
@_ == 2 and _field_ok($_[0])
MC/DC0%0/2
@_ == 2 and _field_ok($_[0])
@_ == 2
_field_ok($_[0])
Truth table33%1/3
A and B
A= @_ == 2B= _field_ok($_[0])
| |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | FLH => sub { @_ == 3 and _field_ok($_[0]) and all {_value_ok($_)} @_[1,2] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
Condition0%0/3
@_ == 3 and _field_ok($_[0])
MC/DC0%0/2
@_ == 3 and _field_ok($_[0])
@_ == 3
_field_ok($_[0])
Truth table0%0/3
A and B
A= @_ == 3B= _field_ok($_[0])
| |||||||||||||||||||||||||||||
| 36 | 4 | ); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 39 | # role parameters and attributes | ||||||||||||||||||||||||||||
| 40 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 4 | requires | |||||||||||||||||||||||||||
| 43 | 'translate_query', # convert to native form | ||||||||||||||||||||||||||||
| 44 | keys(%ops); # implement each op in native form | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | has '_method_table' => ( | ||||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||
| 48 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 49 | 6 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | # '-eq' => 'op_eq', etc. | ||||||||||||||||||||||||||||
| 51 | 6 | return { map { (my $n = $_) =~ s/op_/-/; ($n => $_) } keys %ops } | |||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||
| 53 | 4 | ); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | has '_validators' => ( | ||||||||||||||||||||||||||||
| 56 | is => 'ro', | ||||||||||||||||||||||||||||
| 57 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 58 | 6 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | # 'op_eq' => \&coderef | ||||||||||||||||||||||||||||
| 60 | 6 | return { map { $_ => $validators{$ops{$_}} } keys %ops } | |||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||
| 62 | 4 | ); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 65 | # public methods | ||||||||||||||||||||||||||||
| 66 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 51 | ▶ | sub dispatch_query_op { | ||||||||||||||||||||||||||
|
Subroutine
dispatch_query_op: called CC=5 SCAR=18.9
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 51 | my ($self, $predicate) = @_; | |||||||||||||||||||||||||||
| 71 | 51 | ▶ | if ( ! _is_predicate( $predicate ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | Carp::confess "dispatch_query_op() argument is not a valid predicate"; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 51 | my ($op_name, @args) = @$predicate; | |||||||||||||||||||||||||||
| 76 | 51 | my $op_method = $self->_method_table->{$op_name}; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 51 | ▶ | if ( ! $op_method ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | Carp::confess "Query operator '$op_name' is unknown.\n"; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 51 | ▶ | if ( ! @args ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | Carp::confess "No query arguments provided for $op_name\n"; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 51 | ▶ | if ( ! $self->_validators->{$op_method}->(@args) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | Carp::confess "Query arguments invalid for $op_name\: @args\n"; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 51 | return $self->$op_method(@args); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 41 | ▶ | sub get_native_query { | ||||||||||||||||||||||||||
|
Subroutine
get_native_query: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 41 | my ($self, $query) = @_; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Deal with old API -- convert K/V pairs to -eq joined with -and | ||||||||||||||||||||||||||||
| 96 | 41 | my %invalid = map { $_ => 1 } keys %$query; | |||||||||||||||||||||||||||
| 97 | 41 | delete $invalid{$_} for qw/-where -limit -order/; | |||||||||||||||||||||||||||
| 98 | 41 | ▶ | if ( %invalid ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 6 | my @pred; | |||||||||||||||||||||||||||
| 100 | 6 | for my $k ( keys %invalid ) { | |||||||||||||||||||||||||||
| 101 | 8 | ▶ | if ( substr($k, 0, 1) eq '-' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | Carp::confess "Invalid query parameter '$k'"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 8 | push @pred, [ -eq => $k => $query->{$k} ]; | |||||||||||||||||||||||||||
| 105 | 8 | delete $query->{$k} | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 6 | ▶ | if ( exists $query->{'-where'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 1 | $query->{'-where'} = [ -and => $query->{'-where'}, @pred ]; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | else { | ||||||||||||||||||||||||||||
| 111 | 5 | $query->{'-where'} = [ -and => @pred ]; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # XXX validate structure | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 41 | return $self->translate_query( $query ); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 121 | # private helper functions | ||||||||||||||||||||||||||||
| 122 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 72 | ▶ | sub _is_predicate { ref($_[0]) eq 'ARRAY' } | ||||||||||||||||||||||||||
|
Subroutine
_is_predicate: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 39 | ▶ | sub _field_ok { $_[0] =~ m{\A[a-z_.]+\z}i } | ||||||||||||||||||||||||||
|
Subroutine
_field_ok: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 39 | ▶ | sub _value_ok { ! ref $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
_value_ok: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 4 | 1; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||||||||