| 1 | package DBIO::PostgreSQL::JSONB; | ||
| 2 | # ABSTRACT: JSONB path expression helpers for DBIO::PostgreSQL queries | ||
| 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 | |||
| 7 | 2 | ▶ | use DBIO::PostgreSQL::JSONB::Op (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 2 | ▶ | use Exporter 'import'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | our @EXPORT_OK = qw(jsonb); | |
| 11 | |||
| 12 | 14 | ▶ | sub jsonb { |
|
Subroutine
jsonb: called CC=1 SCAR=0.0
Pod
documented
| |||
| 13 | 14 | my ( $col, @path ) = @_; | |
| 14 | 14 | return DBIO::PostgreSQL::JSONB::Op->new( col => $col, path => \@path ); | |
| 15 | } | ||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | 2 | 1; | |
| 20 | |||
| 21 | __END__ | ||
| 22 | |||
| 23 | =pod | ||
| 24 | |||
| 25 | =encoding UTF-8 | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | DBIO::PostgreSQL::JSONB - JSONB path expression helpers for DBIO::PostgreSQL queries | ||
| 30 | |||
| 31 | =head1 VERSION | ||
| 32 | |||
| 33 | version 0.900001 | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | use DBIO::PostgreSQL::JSONB qw(jsonb); | ||
| 38 | |||
| 39 | # Equality — single key | ||
| 40 | $rs->search( jsonb('me.data', 'status')->eq('active') ); | ||
| 41 | # WHERE (me.data->>'status') = ? | ||
| 42 | |||
| 43 | # Equality — nested path | ||
| 44 | $rs->search( jsonb('me.config', 'theme', 'color')->eq('dark') ); | ||
| 45 | # WHERE (me.config#>>'{theme,color}') = ? | ||
| 46 | |||
| 47 | # Inequality | ||
| 48 | $rs->search( jsonb('me.data', 'role')->ne('guest') ); | ||
| 49 | # WHERE (me.data->>'role') != ? | ||
| 50 | |||
| 51 | # Numeric comparisons (value extracted as text — cast in SQL if needed) | ||
| 52 | $rs->search( jsonb('me.stats', 'score')->gt(100) ); | ||
| 53 | $rs->search( jsonb('me.stats', 'score')->ge(100) ); | ||
| 54 | $rs->search( jsonb('me.stats', 'score')->lt(50) ); | ||
| 55 | $rs->search( jsonb('me.stats', 'score')->le(50) ); | ||
| 56 | |||
| 57 | # Pattern matching | ||
| 58 | $rs->search( jsonb('me.data', 'name')->like('John%') ); | ||
| 59 | $rs->search( jsonb('me.data', 'name')->ilike('%smith%') ); | ||
| 60 | |||
| 61 | # NULL checks | ||
| 62 | $rs->search( jsonb('me.data', 'avatar')->is_null ); | ||
| 63 | $rs->search( jsonb('me.data', 'email')->is_not_null ); | ||
| 64 | |||
| 65 | # ORDER BY | ||
| 66 | $rs->search( {}, { order_by => jsonb('me.score', 'total')->as_order } ); | ||
| 67 | $rs->search( {}, { order_by => { -desc => jsonb('me.score', 'total')->as_order } } ); | ||
| 68 | |||
| 69 | # Combined with containment operators from DBIO::PostgreSQL::SQLMaker: | ||
| 70 | $rs->search([ | ||
| 71 | jsonb('me.data', 'status')->eq('published'), | ||
| 72 | { 'me.data' => { '@>' => { featured => \1 } } }, | ||
| 73 | ]); | ||
| 74 | |||
| 75 | =head1 DESCRIPTION | ||
| 76 | |||
| 77 | Provides the C<jsonb()> helper function that builds text-extraction path | ||
| 78 | expressions for PostgreSQL JSONB columns. The generated SQL uses C<<< ->> >>> | ||
| 79 | (single-level) or C<<< #>> >>> (multi-level) to extract a text value, which | ||
| 80 | can then be compared with standard SQL operators. | ||
| 81 | |||
| 82 | This module covers the path-extraction side of JSONB querying. The comparison | ||
| 83 | methods themselves live on L<DBIO::PostgreSQL::JSONB::Op>, the operator object | ||
| 84 | returned by C<jsonb()>. | ||
| 85 | |||
| 86 | For containment and key-existence operators (C<<< @> >>>, C<?>, etc.) see | ||
| 87 | L<DBIO::PostgreSQL::SQLMaker>, which handles those transparently in | ||
| 88 | C<search()> without any extra import. | ||
| 89 | |||
| 90 | =head1 METHODS | ||
| 91 | |||
| 92 | =head2 jsonb | ||
| 93 | |||
| 94 | use DBIO::PostgreSQL::JSONB qw(jsonb); | ||
| 95 | |||
| 96 | my $expr = jsonb($column, @path); | ||
| 97 | |||
| 98 | Returns a L<DBIO::PostgreSQL::JSONB::Op> object representing a text-extraction | ||
| 99 | path into a JSONB column. Call a comparison method on the returned object to | ||
| 100 | produce a condition fragment suitable for passing to C<search()>. | ||
| 101 | |||
| 102 | C<$column> may include a table alias prefix (e.g. C<'me.data'>). | ||
| 103 | |||
| 104 | # Single-level: uses ->> | ||
| 105 | jsonb('me.data', 'status') # (me.data->>'status') | ||
| 106 | |||
| 107 | # Multi-level: uses #>> | ||
| 108 | jsonb('me.config', 'theme', 'color') # (me.config#>>'{theme,color}') | ||
| 109 | |||
| 110 | Path elements containing single quotes are escaped with standard SQL | ||
| 111 | C<''> doubling. | ||
| 112 | |||
| 113 | =head1 SEE ALSO | ||
| 114 | |||
| 115 | =over 4 | ||
| 116 | |||
| 117 | =item * L<DBIO::PostgreSQL::JSONB::Op> — operator object returned by C<jsonb()>, with all the comparison methods | ||
| 118 | |||
| 119 | =item * L<DBIO::PostgreSQL::SQLMaker> — JSONB operator support (C<<< @> >>>, C<?>, etc.) | ||
| 120 | |||
| 121 | =item * L<DBIO::PostgreSQL::Storage> — PostgreSQL storage layer | ||
| 122 | |||
| 123 | =back | ||
| 124 | |||
| 125 | =head1 AUTHOR | ||
| 126 | |||
| 127 | DBIO & DBIx::Class Authors | ||
| 128 | |||
| 129 | =head1 COPYRIGHT AND LICENSE | ||
| 130 | |||
| 131 | Copyright (C) 2026 DBIO Authors | ||
| 132 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||
| 133 | Based on DBIx::Class, heavily modified. | ||
| 134 | |||
| 135 | This is free software; you can redistribute it and/or modify it under | ||
| 136 | the same terms as the Perl 5 programming language system itself. | ||
| 137 | |||
| 138 | =cut | ||