| 1 | package DBIO::PostgreSQL::SQLMaker; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: PostgreSQL-specific SQL generation for DBIO | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use base 'DBIO::SQLMaker'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | my $JSON = JSON::MaybeXS->new( utf8 => 1, canonical => 1 ); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | my %JSONB_OP = ( | |||||||||||||||||||||||||||
| 14 | '@>' => '_where_op_jsonb_contains', | ||||||||||||||||||||||||||||
| 15 | '<@' => '_where_op_jsonb_contains', | ||||||||||||||||||||||||||||
| 16 | '@?' => '_where_op_jsonb_path', | ||||||||||||||||||||||||||||
| 17 | '@@' => '_where_op_jsonb_path', | ||||||||||||||||||||||||||||
| 18 | '?' => '_where_op_jsonb_exists', | ||||||||||||||||||||||||||||
| 19 | '?|' => '_where_op_jsonb_exists', | ||||||||||||||||||||||||||||
| 20 | '?&' => '_where_op_jsonb_exists', | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.8
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 25 | 3 | ▶ | my %opts = ref $_[0] eq 'HASH' ? %{ $_[0] } : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Register the JSONB operators with the new-style expand_op mechanism (the | ||||||||||||||||||||||||||||
| 28 | # base DBIO::SQLMaker disables the old special_ops system). Each handler | ||||||||||||||||||||||||||||
| 29 | # keeps its original ($self, $col, $op, $val) -> ($sql, @bind) signature; we | ||||||||||||||||||||||||||||
| 30 | # wrap its output in a -literal node. | ||||||||||||||||||||||||||||
| 31 | 3 | for my $op (keys %JSONB_OP) { | |||||||||||||||||||||||||||
| 32 | 21 | my $meth = $JSONB_OP{$op}; | |||||||||||||||||||||||||||
| 33 | 15 | ▶ | $opts{expand_op}{$op} ||= sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 15 | my ($self, $op, $v, $k) = @_; | |||||||||||||||||||||||||||
| 35 | 15 | return +{ -literal => [ $self->$meth($k, $op, $v) ] }; | |||||||||||||||||||||||||||
| 36 | 21 | ▶ | }; | ||||||||||||||||||||||||||
|
Condition33%1/3
$opts{"expand_op"}{$op} ||= sub {
MC/DC0%0/2
$opts{"expand_op"}{$op} ||= sub {
my($self, $op, $v, $k) = @_;
return {"-literal", [$self->$meth($k, $op, $v)]};
}
$opts{"expand_op"}{$op}
sub {
my($self, $op, $v, $k) = @_;
return {"-literal", [$self->$meth($k, $op, $v)]};
}
Truth table33%1/3
A ||= B
A= $opts{"expand_op"}{$op}B= sub {
my($self, $op, $v, $k) = @_;
return {"-literal", [$self->$meth($k, $op, $v)]};
}
| |||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 3 | $class->next::method( \%opts ); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 8 | ▶ | sub _where_op_jsonb_contains { | ||||||||||||||||||||||||||
|
Subroutine
_where_op_jsonb_contains: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 44 | 8 | my ( $self, $col, $op, $val ) = @_; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 8 | my $quoted = $self->_quote($col); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 8 | ▶ | if ( ref $val eq 'SCALAR' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 1 | return ( sprintf( '%s %s %s', $quoted, $op, $$val ), () ); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 7 | ▶ | my $json = | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $val eq "HASH" or ref $val eq "ARRAY"
MC/DC100%2/2
ref $val eq "HASH" or ref $val eq "ARRAY"
ref $val eq "HASH"
ref $val eq "ARRAY"
Truth table100%3/3
A or B
A= ref $val eq "HASH"B= ref $val eq "ARRAY"
| |||||||||||||||||||||||||||||
| 53 | ( ref $val eq 'HASH' || ref $val eq 'ARRAY' ) | ||||||||||||||||||||||||||||
| 54 | ? $JSON->encode($val) | ||||||||||||||||||||||||||||
| 55 | : $val; # already a JSON string | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 7 | return ( sprintf( '%s %s ?::jsonb', $quoted, $op ), $json ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | sub _where_op_jsonb_path { | ||||||||||||||||||||||||||
|
Subroutine
_where_op_jsonb_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 2 | my ( $self, $col, $op, $val ) = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | my $quoted = $self->_quote($col); | |||||||||||||||||||||||||||
| 65 | 2 | return ( sprintf( '%s %s ?::jsonpath', $quoted, $op ), $val ); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 5 | ▶ | sub _where_op_jsonb_exists { | ||||||||||||||||||||||||||
|
Subroutine
_where_op_jsonb_exists: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 70 | 5 | my ( $self, $col, $op, $val ) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 5 | my $quoted = $self->_quote($col); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 5 | ▶ | if ( $op eq '?' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 1 | return ( sprintf( 'jsonb_exists(%s, ?)', $quoted ), $val ); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 4 | ▶ | my @keys = ref $val eq 'ARRAY' ? @$val : ($val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 4 | my $arr = 'ARRAY[' . join( ', ', ('?') x scalar @keys ) . ']'; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 4 | ▶ | if ( $op eq '?|' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 2 | return ( sprintf( 'jsonb_exists_any(%s, %s)', $quoted, $arr ), @keys ); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | else { # ?& | ||||||||||||||||||||||||||||
| 85 | 2 | return ( sprintf( 'jsonb_exists_all(%s, %s)', $quoted, $arr ), @keys ); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 4 | 1; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | __END__ | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =pod | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 NAME | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | DBIO::PostgreSQL::SQLMaker - PostgreSQL-specific SQL generation for DBIO | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head1 VERSION | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | version 0.900001 | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | L<DBIO::SQLMaker> subclass for PostgreSQL. Extends standard SQL generation | ||||||||||||||||||||||||||||
| 110 | with native support for PostgreSQL JSONB operators. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Used automatically by L<DBIO::PostgreSQL::Storage> — the C<sql_maker_class> | ||||||||||||||||||||||||||||
| 113 | is set on the storage and this class is instantiated transparently whenever a | ||||||||||||||||||||||||||||
| 114 | PostgreSQL connection is opened. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head2 Containment: C<@>> and C<<@> | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Tests whether a JSONB value contains (or is contained by) another JSONB | ||||||||||||||||||||||||||||
| 119 | value. The RHS hashref or arrayref is JSON-encoded automatically. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | $rs->search({ 'me.data' => { '@>' => { status => 'active' } } }); | ||||||||||||||||||||||||||||
| 122 | # WHERE "me"."data" @> '{"status":"active"}'::jsonb | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | $rs->search({ 'me.tags' => { '<@' => ['a', 'b'] } }); | ||||||||||||||||||||||||||||
| 125 | # WHERE "me"."tags" <@ '["a","b"]'::jsonb | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head2 Key existence: C<?>, C<?|>, C<?&> | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Tests whether a JSONB object has a specific key (or any/all keys from a list). | ||||||||||||||||||||||||||||
| 130 | These operators are rewritten as C<jsonb_exists*()> functions to avoid | ||||||||||||||||||||||||||||
| 131 | conflicts with DBI's C<?> placeholder syntax. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | $rs->search({ 'me.data' => { '?' => 'email' } }); | ||||||||||||||||||||||||||||
| 134 | # WHERE jsonb_exists("me"."data", ?) | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | $rs->search({ 'me.data' => { '?|' => [qw(email phone)] } }); | ||||||||||||||||||||||||||||
| 137 | # WHERE jsonb_exists_any("me"."data", ARRAY[?, ?]) | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | $rs->search({ 'me.data' => { '?&' => [qw(name email)] } }); | ||||||||||||||||||||||||||||
| 140 | # WHERE jsonb_exists_all("me"."data", ARRAY[?, ?]) | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head2 JSONPath: C<@?> and C<@@> (PostgreSQL 12+) | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Evaluates a JSONPath expression against a JSONB value. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | $rs->search({ 'me.data' => { '@?' => '$.status == "active"' } }); | ||||||||||||||||||||||||||||
| 147 | # WHERE "me"."data" @? '$.status == "active"'::jsonpath | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | $rs->search({ 'me.data' => { '@@' => '$.score > 10' } }); | ||||||||||||||||||||||||||||
| 150 | # WHERE "me"."data" @@ '$.score > 10'::jsonpath | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head2 Path extraction | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | For comparing individual fields within a JSONB value, see | ||||||||||||||||||||||||||||
| 155 | L<DBIO::PostgreSQL::JSONB> which provides the C<jsonb()> path expression | ||||||||||||||||||||||||||||
| 156 | helper: | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | use DBIO::PostgreSQL::JSONB qw(jsonb); | ||||||||||||||||||||||||||||
| 159 | $rs->search( jsonb('me.data', 'status')->eq('active') ); | ||||||||||||||||||||||||||||
| 160 | # WHERE (me.data->>'status') = ? | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head1 METHODS | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head2 new | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Extends the base constructor to register JSONB C<special_ops>. Called | ||||||||||||||||||||||||||||
| 167 | automatically by L<DBIO::PostgreSQL::Storage> — no need to instantiate this | ||||||||||||||||||||||||||||
| 168 | class directly. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head2 _where_op_jsonb_contains | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Handles the C<@>> (contains) and C<<@> (contained by) operators. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | The RHS may be: | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =over 4 | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =item * B<hashref or arrayref> — JSON-encoded automatically and bound with a | ||||||||||||||||||||||||||||
| 179 | C<::jsonb> cast: | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | { 'me.data' => { '@>' => { status => 'active' } } } | ||||||||||||||||||||||||||||
| 182 | # WHERE "me"."data" @> '{"status":"active"}'::jsonb | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | { 'me.tags' => { '@>' => ['admin', 'user'] } } | ||||||||||||||||||||||||||||
| 185 | # WHERE "me"."tags" @> '["admin","user"]'::jsonb | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | { 'me.data' => { '<@' => { role => 'guest' } } } | ||||||||||||||||||||||||||||
| 188 | # WHERE "me"."data" <@ '{"role":"guest"}'::jsonb | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =item * B<plain string> — treated as a pre-encoded JSON string, bound as-is: | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | { 'me.data' => { '@>' => '{"status":"active"}' } } | ||||||||||||||||||||||||||||
| 193 | # WHERE "me"."data" @> '{"status":"active"}'::jsonb | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =item * B<scalar ref> — embedded as literal SQL without binding (use for | ||||||||||||||||||||||||||||
| 196 | sub-selects or other column references): | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | { 'me.data' => { '@>' => \'other_col' } } | ||||||||||||||||||||||||||||
| 199 | # WHERE "me"."data" @> other_col | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =back | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head2 _where_op_jsonb_path | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Handles C<@?> (jsonpath predicate) and C<@@> (jsonpath match) operators | ||||||||||||||||||||||||||||
| 206 | (PostgreSQL 12+). The RHS is bound as a C<::jsonpath> cast. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | { 'me.data' => { '@?' => '$.status == "active"' } } | ||||||||||||||||||||||||||||
| 209 | # WHERE "me"."data" @? '$.status == "active"'::jsonpath | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | { 'me.data' => { '@@' => '$.score > 10' } } | ||||||||||||||||||||||||||||
| 212 | # WHERE "me"."data" @@ '$.score > 10'::jsonpath | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =head2 _where_op_jsonb_exists | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | Handles the C<?> (key exists), C<?|> (any key exists), and C<?&> (all keys | ||||||||||||||||||||||||||||
| 217 | exist) operators. Because C<?> would conflict with DBI's placeholder syntax, | ||||||||||||||||||||||||||||
| 218 | these operators are rewritten as PostgreSQL functions. | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | { 'me.data' => { '?' => 'email' } } | ||||||||||||||||||||||||||||
| 221 | # WHERE jsonb_exists("me"."data", ?) | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | { 'me.data' => { '?|' => [qw(email phone)] } } | ||||||||||||||||||||||||||||
| 224 | # WHERE jsonb_exists_any("me"."data", ARRAY[?, ?]) | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | { 'me.data' => { '?&' => [qw(name email)] } } | ||||||||||||||||||||||||||||
| 227 | # WHERE jsonb_exists_all("me"."data", ARRAY[?, ?]) | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | A single string is accepted for C<?|> and C<?&> as a convenience (treated as | ||||||||||||||||||||||||||||
| 230 | a one-element list): | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | { 'me.data' => { '?|' => 'email' } } | ||||||||||||||||||||||||||||
| 233 | # WHERE jsonb_exists_any("me"."data", ARRAY[?]) | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =over 4 | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =item * L<DBIO::PostgreSQL::Storage> — PostgreSQL storage (uses this SQL maker) | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =item * L<DBIO::PostgreSQL::JSONB> — path expression DSL for JSONB columns | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =item * L<DBIO::SQLMaker> — base SQL maker class | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =back | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 254 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||
| 255 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 258 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | =cut | ||||||||||||||||||||||||||||