| 1 | package DBIO::SQLite::JSON::Op; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: SQLite JSON path-extraction operator object built by json() | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 15 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 15 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 14 | 15 | return bless \%args, $class; | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # The selectors build the extraction expression. The path is bound, never | ||||||||||||||||||||||||||||
| 18 | # interpolated -- a JSON path is caller data and belongs in a placeholder, not | ||||||||||||||||||||||||||||
| 19 | # in the SQL string. Each returns a fresh Op carrying ($sql, @bind) so the | ||||||||||||||||||||||||||||
| 20 | # column-only object stays reusable, exactly like Vector::Op's distance methods. | ||||||||||||||||||||||||||||
| 21 | 27 | ▶ | sub _select { | ||||||||||||||||||||||||||
|
Subroutine
_select: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 27 | my ($self, $sql, @bind) = @_; | |||||||||||||||||||||||||||
| 23 | return bless { | ||||||||||||||||||||||||||||
| 24 | col => $self->{col}, | ||||||||||||||||||||||||||||
| 25 | 27 | sql => $sql, | |||||||||||||||||||||||||||
| 26 | bind => \@bind, | ||||||||||||||||||||||||||||
| 27 | }, ref $self; | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 8 | ▶ | sub extract { | ||||||||||||||||||||||||||
|
Subroutine
extract: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 8 | my ($self, $path) = @_; | |||||||||||||||||||||||||||
| 32 | 8 | return $self->_select( sprintf('json_extract(%s, ?)', $self->{col}), $path ); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 8 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 8 | my ($self, $path) = @_; | |||||||||||||||||||||||||||
| 38 | 8 | return $self->_select( sprintf('%s -> ?', $self->{col}), $path ); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 11 | ▶ | sub get_text { | ||||||||||||||||||||||||||
|
Subroutine
get_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 11 | my ($self, $path) = @_; | |||||||||||||||||||||||||||
| 44 | 11 | return $self->_select( sprintf('%s ->> ?', $self->{col}), $path ); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 28 | ▶ | sub _require_selector { | ||||||||||||||||||||||||||
|
Subroutine
_require_selector: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 28 | my ($self) = @_; | |||||||||||||||||||||||||||
| 50 | Carp::croak( | ||||||||||||||||||||||||||||
| 51 | 'no JSON path selected — call a selector (extract/get/get_text) first' | ||||||||||||||||||||||||||||
| 52 | 28 | ▶ | ) unless defined $self->{sql}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 13 | ▶ | sub _op { | ||||||||||||||||||||||||||
|
Subroutine
_op: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 13 | my ($self, $op, $val) = @_; | |||||||||||||||||||||||||||
| 57 | 13 | $self->_require_selector; | |||||||||||||||||||||||||||
| 58 | 12 | return \[ sprintf('(%s) %s ?', $self->{sql}, $op), @{ $self->{bind} }, $val ]; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 8 | ▶ | sub eq { $_[0]->_op('=', $_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
eq: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | sub ne { $_[0]->_op('!=', $_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
ne: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub lt { $_[0]->_op('<', $_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
lt: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | sub le { $_[0]->_op('<=', $_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
le: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 2 | ▶ | sub gt { $_[0]->_op('>', $_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
gt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub ge { $_[0]->_op('>=', $_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
ge: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | sub like { $_[0]->_op('LIKE', $_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
like: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 3 | ▶ | sub is_null { | ||||||||||||||||||||||||||
|
Subroutine
is_null: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 84 | 3 | $self->_require_selector; | |||||||||||||||||||||||||||
| 85 | 3 | return \[ sprintf('(%s) IS NULL', $self->{sql}), @{ $self->{bind} } ]; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 2 | ▶ | sub is_not_null { | ||||||||||||||||||||||||||
|
Subroutine
is_not_null: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 91 | 2 | $self->_require_selector; | |||||||||||||||||||||||||||
| 92 | 2 | return \[ sprintf('(%s) IS NOT NULL', $self->{sql}), @{ $self->{bind} } ]; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 4 | ▶ | sub as_order { | ||||||||||||||||||||||||||
|
Subroutine
as_order: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 98 | 4 | $self->_require_selector; | |||||||||||||||||||||||||||
| 99 | 4 | return \[ $self->{sql}, @{ $self->{bind} } ]; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 6 | ▶ | sub as_select { | ||||||||||||||||||||||||||
|
Subroutine
as_select: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 6 | my ($self, $alias) = @_; | |||||||||||||||||||||||||||
| 105 | 6 | $self->_require_selector; | |||||||||||||||||||||||||||
| 106 | 6 | ▶ | Carp::croak('as_select requires an alias') unless defined $alias and length $alias; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $alias and length $alias
MC/DC0%0/2
defined $alias and length $alias
defined $alias
length $alias
Truth table33%1/3
A and B
A= defined $aliasB= length $alias
| |||||||||||||||||||||||||||||
| 107 | 6 | return \[ sprintf('%s AS %s', $self->{sql}, $alias), @{ $self->{bind} } ]; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | 1; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||