| 1 | package DBIO::PostgreSQL::JSONB::Op; | ||||||||||
| 2 | # ABSTRACT: JSONB path-extraction operator object built by jsonb() | ||||||||||
| 3 | |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | |||||||||||
| 9 | 14 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 10 | 14 | my ( $class, %args ) = @_; | |||||||||
| 11 | 14 | return bless \%args, $class; | |||||||||
| 12 | } | ||||||||||
| 13 | |||||||||||
| 14 | # Returns the SQL text-extraction expression: (col->>'key') or (col#>>'{a,b}') | ||||||||||
| 15 | 14 | ▶ | sub _text_sql { | ||||||||
|
Subroutine
_text_sql: called CC=3 SCAR=11.0
| |||||||||||
| 16 | 14 | my ($self) = @_; | |||||||||
| 17 | |||||||||||
| 18 | 14 | my $col = $self->{col}; | |||||||||
| 19 | 14 | my @path = @{ $self->{path} }; | |||||||||
| 20 | |||||||||||
| 21 | 14 | ▶ | Carp::croak('jsonb() requires at least one path element') unless @path; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | |||||||||||
| 23 | # Escape single quotes in path elements (standard SQL string escaping) | ||||||||||
| 24 | 14 | my @safe = map { ( my $k = $_ ) =~ s/'/''/g; $k } @path; | |||||||||
| 25 | |||||||||||
| 26 | 14 | ▶ | return @safe == 1 | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | ? sprintf( "(%s->>'%s')", $col, $safe[0] ) | ||||||||||
| 28 | : sprintf( "(%s#>>'{%s}')", $col, join( ',', @safe ) ); | ||||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 10 | ▶ | sub _op { | ||||||||
|
Subroutine
_op: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 10 | my ( $self, $op, $val ) = @_; | |||||||||
| 33 | 10 | return \[ sprintf( '%s %s ?', $self->_text_sql, $op ), $val ]; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 3 | ▶ | sub eq { $_[0]->_op( '=', $_[1] ) } | ||||||||
|
Subroutine
eq: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | 1 | ▶ | sub ne { $_[0]->_op( '!=', $_[1] ) } | ||||||||
|
Subroutine
ne: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | 1 | ▶ | sub lt { $_[0]->_op( '<', $_[1] ) } | ||||||||
|
Subroutine
lt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 43 | |||||||||||
| 44 | |||||||||||
| 45 | 1 | ▶ | sub le { $_[0]->_op( '<=', $_[1] ) } | ||||||||
|
Subroutine
le: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | 1 | ▶ | sub gt { $_[0]->_op( '>', $_[1] ) } | ||||||||
|
Subroutine
gt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 49 | |||||||||||
| 50 | |||||||||||
| 51 | 1 | ▶ | sub ge { $_[0]->_op( '>=', $_[1] ) } | ||||||||
|
Subroutine
ge: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 52 | |||||||||||
| 53 | |||||||||||
| 54 | 1 | ▶ | sub like { $_[0]->_op( 'LIKE', $_[1] ) } | ||||||||
|
Subroutine
like: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | 1 | ▶ | sub ilike { $_[0]->_op( 'ILIKE', $_[1] ) } | ||||||||
|
Subroutine
ilike: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 58 | |||||||||||
| 59 | |||||||||||
| 60 | 1 | ▶ | sub is_null { | ||||||||
|
Subroutine
is_null: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 61 | 1 | my ($self) = @_; | |||||||||
| 62 | 1 | return \[ $self->_text_sql . ' IS NULL' ]; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | |||||||||||
| 66 | 1 | ▶ | sub is_not_null { | ||||||||
|
Subroutine
is_not_null: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 67 | 1 | my ($self) = @_; | |||||||||
| 68 | 1 | return \[ $self->_text_sql . ' IS NOT NULL' ]; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | 2 | ▶ | sub as_order { | ||||||||
|
Subroutine
as_order: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 73 | 2 | my ($self) = @_; | |||||||||
| 74 | 2 | return \$self->_text_sql; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | |||||||||||
| 78 | |||||||||||
| 79 | 2 | 1; | |||||||||
| 80 | |||||||||||
| 81 | __END__ | ||||||||||
| 82 | |||||||||||
| 83 | =pod | ||||||||||
| 84 | |||||||||||
| 85 | =encoding UTF-8 | ||||||||||
| 86 | |||||||||||
| 87 | =head1 NAME | ||||||||||
| 88 | |||||||||||
| 89 | DBIO::PostgreSQL::JSONB::Op - JSONB path-extraction operator object built by jsonb() | ||||||||||
| 90 | |||||||||||
| 91 | =head1 VERSION | ||||||||||
| 92 | |||||||||||
| 93 | version 0.900001 | ||||||||||
| 94 | |||||||||||
| 95 | =head1 DESCRIPTION | ||||||||||
| 96 | |||||||||||
| 97 | Operator object representing a text-extraction path into a PostgreSQL JSONB | ||||||||||
| 98 | column. You do not construct this class directly — use the C<jsonb()> helper | ||||||||||
| 99 | from L<DBIO::PostgreSQL::JSONB>: | ||||||||||
| 100 | |||||||||||
| 101 | use DBIO::PostgreSQL::JSONB qw(jsonb); | ||||||||||
| 102 | |||||||||||
| 103 | my $expr = jsonb('me.data', 'status'); # DBIO::PostgreSQL::JSONB::Op | ||||||||||
| 104 | $rs->search( $expr->eq('active') ); # WHERE (me.data->>'status') = ? | ||||||||||
| 105 | |||||||||||
| 106 | Each comparison method returns a SQL::Abstract literal-with-bind fragment | ||||||||||
| 107 | suitable for passing to C<search()>. | ||||||||||
| 108 | |||||||||||
| 109 | =head1 METHODS | ||||||||||
| 110 | |||||||||||
| 111 | =head2 eq | ||||||||||
| 112 | |||||||||||
| 113 | jsonb('me.data', 'status')->eq('active') | ||||||||||
| 114 | # WHERE (me.data->>'status') = ? | ||||||||||
| 115 | |||||||||||
| 116 | =head2 ne | ||||||||||
| 117 | |||||||||||
| 118 | jsonb('me.data', 'status')->ne('deleted') | ||||||||||
| 119 | # WHERE (me.data->>'status') != ? | ||||||||||
| 120 | |||||||||||
| 121 | =head2 lt | ||||||||||
| 122 | |||||||||||
| 123 | jsonb('me.stats', 'score')->lt(50) | ||||||||||
| 124 | # WHERE (me.stats->>'score') < ? | ||||||||||
| 125 | |||||||||||
| 126 | =head2 le | ||||||||||
| 127 | |||||||||||
| 128 | jsonb('me.stats', 'score')->le(100) | ||||||||||
| 129 | # WHERE (me.stats->>'score') <= ? | ||||||||||
| 130 | |||||||||||
| 131 | =head2 gt | ||||||||||
| 132 | |||||||||||
| 133 | jsonb('me.stats', 'score')->gt(100) | ||||||||||
| 134 | # WHERE (me.stats->>'score') > ? | ||||||||||
| 135 | |||||||||||
| 136 | =head2 ge | ||||||||||
| 137 | |||||||||||
| 138 | jsonb('me.stats', 'score')->ge(100) | ||||||||||
| 139 | # WHERE (me.stats->>'score') >= ? | ||||||||||
| 140 | |||||||||||
| 141 | =head2 like | ||||||||||
| 142 | |||||||||||
| 143 | jsonb('me.data', 'name')->like('John%') | ||||||||||
| 144 | # WHERE (me.data->>'name') LIKE ? | ||||||||||
| 145 | |||||||||||
| 146 | =head2 ilike | ||||||||||
| 147 | |||||||||||
| 148 | jsonb('me.data', 'name')->ilike('%smith%') | ||||||||||
| 149 | # WHERE (me.data->>'name') ILIKE ? | ||||||||||
| 150 | |||||||||||
| 151 | =head2 is_null | ||||||||||
| 152 | |||||||||||
| 153 | jsonb('me.data', 'avatar')->is_null | ||||||||||
| 154 | # WHERE (me.data->>'avatar') IS NULL | ||||||||||
| 155 | |||||||||||
| 156 | Returns a condition fragment that checks whether the path resolves to SQL | ||||||||||
| 157 | NULL (i.e. the key is absent or the value is JSON null). | ||||||||||
| 158 | |||||||||||
| 159 | =head2 is_not_null | ||||||||||
| 160 | |||||||||||
| 161 | jsonb('me.data', 'email')->is_not_null | ||||||||||
| 162 | # WHERE (me.data->>'email') IS NOT NULL | ||||||||||
| 163 | |||||||||||
| 164 | =head2 as_order | ||||||||||
| 165 | |||||||||||
| 166 | $rs->search( {}, { order_by => jsonb('me.score', 'total')->as_order } ) | ||||||||||
| 167 | # ORDER BY (me.score->>'total') | ||||||||||
| 168 | |||||||||||
| 169 | Returns a scalar ref suitable for use as an C<order_by> value. Combine with | ||||||||||
| 170 | C<-asc>/C<-desc> wrappers as usual: | ||||||||||
| 171 | |||||||||||
| 172 | { order_by => { -desc => jsonb('me.score', 'total')->as_order } } | ||||||||||
| 173 | # ORDER BY (me.score->>'total') DESC | ||||||||||
| 174 | |||||||||||
| 175 | =head1 SEE ALSO | ||||||||||
| 176 | |||||||||||
| 177 | =over 4 | ||||||||||
| 178 | |||||||||||
| 179 | =item * L<DBIO::PostgreSQL::JSONB> — the C<jsonb()> helper that builds these objects | ||||||||||
| 180 | |||||||||||
| 181 | =back | ||||||||||
| 182 | |||||||||||
| 183 | =head1 AUTHOR | ||||||||||
| 184 | |||||||||||
| 185 | DBIO & DBIx::Class Authors | ||||||||||
| 186 | |||||||||||
| 187 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 188 | |||||||||||
| 189 | Copyright (C) 2026 DBIO Authors | ||||||||||
| 190 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||
| 191 | Based on DBIx::Class, heavily modified. | ||||||||||
| 192 | |||||||||||
| 193 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 194 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 195 | |||||||||||
| 196 | =cut | ||||||||||