| 1 | package DBIO::PostgreSQL::Result; | ||||||||||||||||||||
| 2 | # ABSTRACT: PostgreSQL-specific Result component for DBIO | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 7 | ▶ | use base 'DBIO::Base'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 7 | __PACKAGE__->mk_classdata('_pg_schema_name'); | |||||||||||||||||||
| 10 | 7 | __PACKAGE__->mk_classdata('_pg_indexes' => {}); | |||||||||||||||||||
| 11 | 7 | __PACKAGE__->mk_classdata('_pg_triggers' => {}); | |||||||||||||||||||
| 12 | 7 | __PACKAGE__->mk_classdata('_pg_rls'); | |||||||||||||||||||
| 13 | 7 | __PACKAGE__->mk_classdata('_pg_check_constraints' => {}); | |||||||||||||||||||
| 14 | 7 | __PACKAGE__->mk_classdata('_pg_extra_ddl' => []); | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 11 | ▶ | sub pg_schema { | ||||||||||||||||||
|
Subroutine
pg_schema: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 19 | 11 | my ($class, $name) = @_; | |||||||||||||||||||
| 20 | 11 | ▶ | if (defined $name) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | 4 | $class->_pg_schema_name($name); | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | 11 | return $class->_pg_schema_name; | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 1 | ▶ | sub pg_qualified_table { | ||||||||||||||||||
|
Subroutine
pg_qualified_table: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 28 | 1 | my ($class) = @_; | |||||||||||||||||||
| 29 | 1 | my $schema = $class->_pg_schema_name; | |||||||||||||||||||
| 30 | 1 | my $table = $class->table; | |||||||||||||||||||
| 31 | 1 | ▶ | return $schema ? "${schema}.${table}" : $table; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 2 | ▶ | sub pg_index { | ||||||||||||||||||
|
Subroutine
pg_index: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 36 | 2 | my ($class, $name, $def) = @_; | |||||||||||||||||||
| 37 | 2 | ▶ | if ($def) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 2 | my $indexes = { %{ $class->_pg_indexes } }; | |||||||||||||||||||
| 39 | 2 | $indexes->{$name} = $def; | |||||||||||||||||||
| 40 | 2 | $class->_pg_indexes($indexes); | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | 2 | return $class->_pg_indexes->{$name}; | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 4 | ▶ | sub pg_indexes { | ||||||||||||||||||
|
Subroutine
pg_indexes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 47 | 4 | my ($class) = @_; | |||||||||||||||||||
| 48 | 4 | return { %{ $class->_pg_indexes } }; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 1 | ▶ | sub pg_trigger { | ||||||||||||||||||
|
Subroutine
pg_trigger: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 53 | 1 | my ($class, $name, $def) = @_; | |||||||||||||||||||
| 54 | 1 | ▶ | if ($def) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 1 | my $triggers = { %{ $class->_pg_triggers } }; | |||||||||||||||||||
| 56 | 1 | $triggers->{$name} = $def; | |||||||||||||||||||
| 57 | 1 | $class->_pg_triggers($triggers); | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | 1 | return $class->_pg_triggers->{$name}; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 3 | ▶ | sub pg_triggers { | ||||||||||||||||||
|
Subroutine
pg_triggers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 64 | 3 | my ($class) = @_; | |||||||||||||||||||
| 65 | 3 | return { %{ $class->_pg_triggers } }; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 5 | ▶ | sub pg_rls { | ||||||||||||||||||
|
Subroutine
pg_rls: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 70 | 5 | my ($class, $def) = @_; | |||||||||||||||||||
| 71 | 5 | ▶ | if ($def) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 1 | $class->_pg_rls($def); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | 5 | return $class->_pg_rls; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 4 | ▶ | sub pg_check_constraint { | ||||||||||||||||||
|
Subroutine
pg_check_constraint: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 79 | 4 | my ($class, $name, $def) = @_; | |||||||||||||||||||
| 80 | 4 | ▶ | if (defined $def) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 2 | ▶ | my $entry = ref $def eq 'HASH' ? { %$def } : { definition => $def }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 2 | ▶ | $entry->{constraint_name} //= $name; | ||||||||||||||||||
|
Condition66%2/3
$entry->{"constraint_name"} //= $name
MC/DC0%0/2
$entry->{"constraint_name"} //= $name
$entry->{"constraint_name"}
$name
Truth table66%2/3
A //= B
A= $entry->{"constraint_name"}B= $name
| |||||||||||||||||||||
| 83 | 2 | my $checks = { %{ $class->_pg_check_constraints } }; | |||||||||||||||||||
| 84 | 2 | $checks->{$name} = $entry; | |||||||||||||||||||
| 85 | 2 | $class->_pg_check_constraints($checks); | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | 4 | return $class->_pg_check_constraints->{$name}; | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 5 | ▶ | sub pg_check_constraints { | ||||||||||||||||||
|
Subroutine
pg_check_constraints: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 92 | 5 | my ($class) = @_; | |||||||||||||||||||
| 93 | 5 | return { %{ $class->_pg_check_constraints } }; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 9 | ▶ | sub pg_extra_ddl { | ||||||||||||||||||
|
Subroutine
pg_extra_ddl: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 98 | 9 | my ($class, @stmts) = @_; | |||||||||||||||||||
| 99 | 9 | ▶ | if (@stmts) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 3 | ▶ | my @flat = map { ref eq 'ARRAY' ? @$_ : $_ } @stmts; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 3 | my $cur = [ @{ $class->_pg_extra_ddl }, @flat ]; | |||||||||||||||||||
| 102 | 3 | $class->_pg_extra_ddl($cur); | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | 9 | return [ @{ $class->_pg_extra_ddl } ]; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 7 | 1; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =pod | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =encoding UTF-8 | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | =head1 NAME | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | DBIO::PostgreSQL::Result - PostgreSQL-specific Result component for DBIO | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =head1 VERSION | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | version 0.900001 | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =head1 DESCRIPTION | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | C<DBIO::PostgreSQL::Result> is a DBIO Result component that adds | ||||||||||||||||||||
| 127 | PostgreSQL-native metadata to a result class: the PostgreSQL schema | ||||||||||||||||||||
| 128 | (namespace) the table belongs to, custom indexes, triggers, and Row Level | ||||||||||||||||||||
| 129 | Security (RLS) configuration. | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | Load it with: | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | package MyApp::DB::Result::User; | ||||||||||||||||||||
| 134 | use base 'DBIO::Core'; | ||||||||||||||||||||
| 135 | __PACKAGE__->load_components('PostgreSQL::Result'); | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | __PACKAGE__->pg_schema('auth'); | ||||||||||||||||||||
| 138 | __PACKAGE__->table('users'); | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | The schema name from C<pg_schema> is used by L<DBIO::PostgreSQL::DDL> to | ||||||||||||||||||||
| 141 | qualify table names (e.g. C<auth.users>) in generated DDL. | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | =head1 METHODS | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =head2 pg_schema | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | __PACKAGE__->pg_schema('auth'); | ||||||||||||||||||||
| 148 | my $name = $class->pg_schema; | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | Get or set the PostgreSQL schema (namespace) for this result class. When set, | ||||||||||||||||||||
| 151 | L<DBIO::PostgreSQL::DDL> qualifies the table name as C<schema.table> in | ||||||||||||||||||||
| 152 | generated DDL. | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =head2 pg_qualified_table | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | my $fqn = $class->pg_qualified_table; # e.g. 'auth.users' | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | Returns the fully-qualified table name C<schema.table>, or just C<table> if no | ||||||||||||||||||||
| 159 | PostgreSQL schema has been set. | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =head2 pg_index | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | __PACKAGE__->pg_index('idx_users_tags' => { | ||||||||||||||||||||
| 164 | using => 'gin', | ||||||||||||||||||||
| 165 | columns => ['tags'], | ||||||||||||||||||||
| 166 | }); | ||||||||||||||||||||
| 167 | __PACKAGE__->pg_index('idx_users_active' => { | ||||||||||||||||||||
| 168 | columns => ['role'], | ||||||||||||||||||||
| 169 | where => "role != 'suspended'", | ||||||||||||||||||||
| 170 | }); | ||||||||||||||||||||
| 171 | __PACKAGE__->pg_index('idx_users_embedding' => { | ||||||||||||||||||||
| 172 | using => 'ivfflat', | ||||||||||||||||||||
| 173 | columns => ['embedding'], | ||||||||||||||||||||
| 174 | with => { lists => 100 }, | ||||||||||||||||||||
| 175 | }); | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | my $def = $class->pg_index('idx_users_tags'); | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | Get or set the definition for a named PostgreSQL index. The definition hashref | ||||||||||||||||||||
| 180 | accepts: | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =over 4 | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | =item C<columns> - ArrayRef of column names | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =item C<using> - index access method (C<btree>, C<gin>, C<gist>, C<brin>, C<hash>, C<ivfflat>, etc.) | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =item C<where> - partial index predicate (SQL expression string) | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =item C<expression> - expression index expression (replaces C<columns>) | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =item C<with> - storage parameter hashref (e.g. C<{ lists =E<gt> 100 }> for ivfflat) | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | =back | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | =head2 pg_indexes | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | my $all = $class->pg_indexes; # hashref of name => def | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | Returns a copy of all index definitions registered on this result class. | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | =head2 pg_trigger | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | __PACKAGE__->pg_trigger('users_modified_at' => { | ||||||||||||||||||||
| 205 | when => 'BEFORE', | ||||||||||||||||||||
| 206 | event => 'UPDATE', | ||||||||||||||||||||
| 207 | execute => 'auth.update_modified_at()', | ||||||||||||||||||||
| 208 | }); | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | my $def = $class->pg_trigger('users_modified_at'); | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | Get or set the definition for a named PostgreSQL trigger. The definition | ||||||||||||||||||||
| 213 | hashref accepts C<when> (C<BEFORE>/C<AFTER>/C<INSTEAD OF>), C<event> | ||||||||||||||||||||
| 214 | (C<INSERT>/C<UPDATE>/C<DELETE>/C<TRUNCATE>), and C<execute> (the function to | ||||||||||||||||||||
| 215 | call). | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | =head2 pg_triggers | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | my $all = $class->pg_triggers; # hashref of name => def | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | Returns a copy of all trigger definitions registered on this result class. | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | =head2 pg_rls | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | __PACKAGE__->pg_rls({ | ||||||||||||||||||||
| 226 | enable => 1, | ||||||||||||||||||||
| 227 | force => 1, | ||||||||||||||||||||
| 228 | policies => { | ||||||||||||||||||||
| 229 | users_own_data => { | ||||||||||||||||||||
| 230 | for => 'ALL', | ||||||||||||||||||||
| 231 | using => 'id = current_setting($$app.current_user_id$$)::uuid', | ||||||||||||||||||||
| 232 | }, | ||||||||||||||||||||
| 233 | }, | ||||||||||||||||||||
| 234 | }); | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | my $rls = $class->pg_rls; | ||||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | Get or set the Row Level Security configuration for this table. The hashref | ||||||||||||||||||||
| 239 | accepts: | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | =over 4 | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | =item C<enable> - boolean, generates C<ENABLE ROW LEVEL SECURITY> | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | =item C<force> - boolean, generates C<FORCE ROW LEVEL SECURITY> | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | =item C<policies> - hashref of policy name to policy definition (C<for>, C<using>, C<with_check>) | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | =back | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | =head2 pg_check_constraint | ||||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | __PACKAGE__->pg_check_constraint('run_status_invariant' => | ||||||||||||||||||||
| 254 | "(status IN ('Pending','Running','Done') AND error IS NULL)" | ||||||||||||||||||||
| 255 | . " OR status IN ('Blocked','Failed','Cancelled')" | ||||||||||||||||||||
| 256 | ); | ||||||||||||||||||||
| 257 | __PACKAGE__->pg_check_constraint('idx_name' => { | ||||||||||||||||||||
| 258 | constraint_name => 'idx_name', | ||||||||||||||||||||
| 259 | definition => 'CHECK (col > 0)', | ||||||||||||||||||||
| 260 | columns => ['col'], | ||||||||||||||||||||
| 261 | }); | ||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | my $def = $class->pg_check_constraint('run_status_invariant'); | ||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | Get or set a named CHECK constraint. Accepts either a plain string (the | ||||||||||||||||||||
| 266 | CHECK predicate expression) or a hashref with C<constraint_name>, | ||||||||||||||||||||
| 267 | C<definition> (which may already include C<CHECK>), and optional C<columns>. | ||||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | =head2 pg_check_constraints | ||||||||||||||||||||
| 270 | |||||||||||||||||||||
| 271 | my $all = $class->pg_check_constraints; | ||||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | Returns a copy of all CHECK constraint definitions registered on this result class. | ||||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | =head2 pg_extra_ddl | ||||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | __PACKAGE__->pg_extra_ddl( | ||||||||||||||||||||
| 278 | 'ALTER TABLE goldmine_site ADD CONSTRAINT goldmine_site_paypal_account_id_fkey' | ||||||||||||||||||||
| 279 | . ' FOREIGN KEY (paypal_account_id) REFERENCES goldmine_paypal_account (id)' | ||||||||||||||||||||
| 280 | . ' ON DELETE RESTRICT', | ||||||||||||||||||||
| 281 | ); | ||||||||||||||||||||
| 282 | |||||||||||||||||||||
| 283 | Appends raw SQL statements to be emitted after the table, indexes, triggers, | ||||||||||||||||||||
| 284 | and RLS DDL. Used for cross-table FKs that DBIO::PostgreSQL::DDL does not | ||||||||||||||||||||
| 285 | auto-generate from C<belongs_to>. Each call appends; arrayrefs are flattened. | ||||||||||||||||||||
| 286 | |||||||||||||||||||||
| 287 | =seealso | ||||||||||||||||||||
| 288 | |||||||||||||||||||||
| 289 | =over 4 | ||||||||||||||||||||
| 290 | |||||||||||||||||||||
| 291 | =item * L<DBIO::PostgreSQL> - the schema component (Database layer) | ||||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | =item * L<DBIO::PostgreSQL::PgSchema> - the PgSchema layer for enums, types, functions | ||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | =item * L<DBIO::PostgreSQL::DDL> - generates DDL from result class metadata | ||||||||||||||||||||
| 296 | |||||||||||||||||||||
| 297 | =back | ||||||||||||||||||||
| 298 | |||||||||||||||||||||
| 299 | =head1 AUTHOR | ||||||||||||||||||||
| 300 | |||||||||||||||||||||
| 301 | DBIO & DBIx::Class Authors | ||||||||||||||||||||
| 302 | |||||||||||||||||||||
| 303 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 304 | |||||||||||||||||||||
| 305 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||
| 306 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||
| 307 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||
| 308 | |||||||||||||||||||||
| 309 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 310 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 311 | |||||||||||||||||||||
| 312 | =cut | ||||||||||||||||||||