| 1 | package DBIO::PostgreSQL::Storage; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: PostgreSQL storage layer for DBIO | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use base qw/DBIO::Storage::DBI/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 8 | __PACKAGE__->register_driver('Pg' => __PACKAGE__); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub dbio_deploy_class { 'DBIO::PostgreSQL::Deploy' } | ||||||||||||||||||||||||||
|
Subroutine
dbio_deploy_class: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 8 | __PACKAGE__->register_type('jsonb', { | |||||||||||||||||||||||||||
| 14 | cake_options => [qw(inflate_json inflate_jsonb)], | ||||||||||||||||||||||||||||
| 15 | components => ['InflateColumn::Serializer'], | ||||||||||||||||||||||||||||
| 16 | col_attrs => { serializer_class => 'JSON' }, | ||||||||||||||||||||||||||||
| 17 | }); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 8 | ▶ | use Scope::Guard (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 8 | ▶ | use Context::Preserve 'preserve_context'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 8 | ▶ | use DBIO::Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 8 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # PostgreSQL defaults | ||||||||||||||||||||||||||||
| 26 | 8 | __PACKAGE__->sql_maker_class('DBIO::PostgreSQL::SQLMaker'); | |||||||||||||||||||||||||||
| 27 | 8 | __PACKAGE__->sql_quote_char('"'); | |||||||||||||||||||||||||||
| 28 | 8 | __PACKAGE__->datetime_parser_type('DateTime::Format::Pg'); | |||||||||||||||||||||||||||
| 29 | 8 | __PACKAGE__->_use_multicolumn_in(1); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Async is an explicit, per-connection mode (core ADR 0030): a connection opened | ||||||||||||||||||||||||||||
| 32 | # with connect(..., { async => 'ev' }) resolves the 'ev' mode through the mode | ||||||||||||||||||||||||||||
| 33 | # registry to the native EV backend below. Registering on this driver storage | ||||||||||||||||||||||||||||
| 34 | # class MRO-shadows the generic modes (forked/future_io) registered on the base | ||||||||||||||||||||||||||||
| 35 | # storage, so 'ev' picks the PostgreSQL-specific backend. There is no auto- | ||||||||||||||||||||||||||||
| 36 | # fallback and no silent degrade: a mode that is neither requested nor registered | ||||||||||||||||||||||||||||
| 37 | # is simply unavailable, and requesting an unregistered/uninstalled mode croaks | ||||||||||||||||||||||||||||
| 38 | # (see DBIO::Storage::DBI::_async_storage). This only stores the class name -- | ||||||||||||||||||||||||||||
| 39 | # DBIO::PostgreSQL::EV::Storage (the optional dbio-postgresql-ev dist) is loaded | ||||||||||||||||||||||||||||
| 40 | # lazily on first use, so registering it here is safe when the dist is absent. | ||||||||||||||||||||||||||||
| 41 | 8 | __PACKAGE__->register_async_mode( ev => 'DBIO::PostgreSQL::EV::Storage' ); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # This is the ONLY async registration this driver needs, and it stays as-is | ||||||||||||||||||||||||||||
| 44 | # under the storage-layer composition model (core karr #70): a mode maps to a | ||||||||||||||||||||||||||||
| 45 | # transport BASE class, once, on this concrete driver storage. PostgreSQL | ||||||||||||||||||||||||||||
| 46 | # EXTENSIONS (AGE, PostGIS, ...) no longer subclass storage_type nor add shadow | ||||||||||||||||||||||||||||
| 47 | # register_async_mode / ::Async registrations -- they register plain storage | ||||||||||||||||||||||||||||
| 48 | # LAYERS (DBIO::Schema::register_storage_layer) whose async mirrors compose ON | ||||||||||||||||||||||||||||
| 49 | # TOP of the resolved transport via DBIO::Storage::Composed. Composition rides | ||||||||||||||||||||||||||||
| 50 | # on top of this base; the mode->transport-base map does not grow per extension. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 66 | ▶ | sub sql_maker { | ||||||||||||||||||||||||||
|
Subroutine
sql_maker: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 66 | my $self = shift; | |||||||||||||||||||||||||||
| 54 | 66 | my $sm = $self->next::method(@_); | |||||||||||||||||||||||||||
| 55 | # PostgreSQL always uses double-quote identifier quoting — ensure it is | ||||||||||||||||||||||||||||
| 56 | # active even when the caller did not pass quote_names in connect_info. | ||||||||||||||||||||||||||||
| 57 | 66 | ▶ | $sm->{quote_char} //= $self->sql_quote_char; | ||||||||||||||||||||||||||
|
Condition33%1/3
$sm->{"quote_char"} //= $self->sql_quote_char
MC/DC0%0/2
$sm->{"quote_char"} //= $self->sql_quote_char
$sm->{"quote_char"}
$self->sql_quote_char
Truth table33%1/3
A //= B
A= $sm->{"quote_char"}B= $self->sql_quote_char
| |||||||||||||||||||||||||||||
| 58 | 66 | ▶ | $sm->{name_sep} //= $self->sql_name_sep; | ||||||||||||||||||||||||||
|
Condition33%1/3
$sm->{"name_sep"} //= $self->sql_name_sep
MC/DC0%0/2
$sm->{"name_sep"} //= $self->sql_name_sep
$sm->{"name_sep"}
$self->sql_name_sep
Truth table33%1/3
A //= B
A= $sm->{"name_sep"}B= $self->sql_name_sep
| |||||||||||||||||||||||||||||
| 59 | 66 | $sm; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub _determine_supports_insert_returning { | ||||||||||||||||||||||||||
|
Subroutine
_determine_supports_insert_returning: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | return shift->_server_info->{normalized_dbms_version} >= 8.002 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | ? 1 | ||||||||||||||||||||||||||||
| 65 | : 0 | ||||||||||||||||||||||||||||
| 66 | ; | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub with_deferred_fk_checks { | ||||||||||||||||||||||||||
|
Subroutine
with_deferred_fk_checks: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 0 | my ($self, $sub) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | my $txn_scope_guard = $self->txn_scope_guard; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | $self->_do_query('SET CONSTRAINTS ALL DEFERRED'); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my $sg = Scope::Guard->new(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 78 | 0 | $self->_do_query('SET CONSTRAINTS ALL IMMEDIATE'); | |||||||||||||||||||||||||||
| 79 | 0 | }); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | return preserve_context { $sub->() } after => sub { $txn_scope_guard->commit }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # only used when INSERT ... RETURNING is disabled | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub last_insert_id { | ||||||||||||||||||||||||||
|
Subroutine
last_insert_id: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 0 | my ($self, $source, @cols) = @_; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my @values; | |||||||||||||||||||||||||||
| 90 | 0 | my $col_info = $source->columns_info(\@cols); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | for my $col (@cols) { | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | my $seq = ( $col_info->{$col}{sequence} ||= $self->dbh_do('_dbh_get_autoinc_seq', $source, $col) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$col_info->{$col}{"sequence"} ||= $self->dbh_do("_dbh_get_autoinc_seq", $source, $col)
MC/DC0%0/2
$col_info->{$col}{"sequence"} ||= $self->dbh_do("_dbh_get_autoinc_seq", $source, $col)
$col_info->{$col}{"sequence"}
$self->dbh_do("_dbh_get_autoinc_seq", $source, $col)
Truth table0%0/3
A ||= B
A= $col_info->{$col}{"sequence"}B= $self->dbh_do("_dbh_get_autoinc_seq", $source, $col)
| |||||||||||||||||||||||||||||
| 94 | or $self->throw_exception( sprintf( | ||||||||||||||||||||||||||||
| 95 | "Could not determine sequence for column '%s.%s', please consider adding a " | ||||||||||||||||||||||||||||
| 96 | . "schema-qualified sequence to its column info", | ||||||||||||||||||||||||||||
| 97 | $source->name, | ||||||||||||||||||||||||||||
| 98 | $col, | ||||||||||||||||||||||||||||
| 99 | )); | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | push @values, $self->_dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq}); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | return @values; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub _sequence_fetch { | ||||||||||||||||||||||||||
|
Subroutine
_sequence_fetch: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 108 | 0 | my ($self, $function, $sequence) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $self->throw_exception('No sequence to fetch') unless $sequence; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my ($val) = $self->_get_dbh->selectrow_array( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | sprintf("select %s('%s')", $function, (ref $sequence eq 'SCALAR') ? $$sequence : $sequence) | ||||||||||||||||||||||||||||
| 114 | ); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | return $val; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub _dbh_get_autoinc_seq { | ||||||||||||||||||||||||||
|
Subroutine
_dbh_get_autoinc_seq: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 120 | 0 | my ($self, $dbh, $source, $col) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | my $schema; | |||||||||||||||||||||||||||
| 123 | 0 | my $table = $source->name; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | $table = $$table if ref $table eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | if ($table =~ /^(.+)\.(.+)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | ($schema, $table) = ($1, $2); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | my $seq_expr = $self->_dbh_get_column_default($dbh, $schema, $table, $col); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | unless (defined $seq_expr and $seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $seq_expr and $seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i
MC/DC0%0/2
defined $seq_expr and $seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i
defined $seq_expr
$seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i
Truth table0%0/3
A and B
A= defined $seq_exprB= $seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i
| |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | $seq_expr = '' unless defined $seq_expr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | $self->throw_exception( sprintf( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | "No sequence found for '%s%s.%s', check the RDBMS table definition or explicitly set the " | ||||||||||||||||||||||||||||
| 137 | . "'sequence' for this column in %s", | ||||||||||||||||||||||||||||
| 138 | $schema ? "$schema." : '', | ||||||||||||||||||||||||||||
| 139 | $table, | ||||||||||||||||||||||||||||
| 140 | $col, | ||||||||||||||||||||||||||||
| 141 | $source->source_name, | ||||||||||||||||||||||||||||
| 142 | )); | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | return $1; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | sub _dbh_get_column_default { | ||||||||||||||||||||||||||
|
Subroutine
_dbh_get_column_default: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 149 | 0 | my ($self, $dbh, $schema, $table, $col) = @_; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | my $sqlmaker = $self->sql_maker; | |||||||||||||||||||||||||||
| 152 | 0 | local $sqlmaker->{bindtype} = 'normal'; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | my ($where, @bind) = $sqlmaker->where({ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $schema and length $schema
MC/DC0%0/2
defined $schema and length $schema
defined $schema
length $schema
Truth table0%0/3
A and B
A= defined $schemaB= length $schema
| |||||||||||||||||||||||||||||
| 155 | 'a.attnum' => {'>', 0}, | ||||||||||||||||||||||||||||
| 156 | 'c.relname' => $table, | ||||||||||||||||||||||||||||
| 157 | 'a.attname' => $col, | ||||||||||||||||||||||||||||
| 158 | -not_bool => 'a.attisdropped', | ||||||||||||||||||||||||||||
| 159 | (defined $schema && length $schema) | ||||||||||||||||||||||||||||
| 160 | ? ('n.nspname' => $schema) | ||||||||||||||||||||||||||||
| 161 | : (-bool => \'pg_catalog.pg_table_is_visible(c.oid)') | ||||||||||||||||||||||||||||
| 162 | }); | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | my ($seq_expr) = $dbh->selectrow_array(<<"EOS", undef, @bind); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | SELECT | ||||||||||||||||||||||||||||
| 167 | (SELECT pg_catalog.pg_get_expr(d.adbin, d.adrelid) | ||||||||||||||||||||||||||||
| 168 | FROM pg_catalog.pg_attrdef d | ||||||||||||||||||||||||||||
| 169 | WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) | ||||||||||||||||||||||||||||
| 170 | FROM pg_catalog.pg_class c | ||||||||||||||||||||||||||||
| 171 | LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace | ||||||||||||||||||||||||||||
| 172 | JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid | ||||||||||||||||||||||||||||
| 173 | $where | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | EOS | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | return $seq_expr; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | sub sqlt_type { 'PostgreSQL' } | ||||||||||||||||||||||||||
|
Subroutine
sqlt_type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | sub _explain_sql { "EXPLAIN ANALYZE $_[1]" } | ||||||||||||||||||||||||||
|
Subroutine
_explain_sql: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 2 | ▶ | sub _minmax_operator_for_datatype { | ||||||||||||||||||||||||||
|
Subroutine
_minmax_operator_for_datatype: called CC=4 SCAR=15.8
| |||||||||||||||||||||||||||||
| 185 | #my ($self, $datatype, $want_max) = @_; | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 2 | ▶ | return ($_[2] ? 'BOOL_OR' : 'BOOL_AND') | ||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%1/2
$_[1] || ''
MC/DC0%0/1
$_[1] || ''
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||||||||||||||
| 188 | if ($_[1] || '') =~ /\Abool(?:ean)?\z/i; | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | shift->next::method(@_); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | sub bind_attribute_by_data_type { | ||||||||||||||||||||||||||
|
Subroutine
bind_attribute_by_data_type: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 195 | 0 | my ($self, $data_type) = @_; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | ▶ | if ($self->_is_binary_lob_type($data_type)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | ▶ | unless ($DBD::Pg::__DBIO_DBD_VERSION_CHECK_DONE__) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | if ($self->_server_info->{normalized_dbms_version} >= 9.0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 200 | 0 | ▶ | try { DBD::Pg->VERSION('2.17.2'); 1 } or carp( | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | __PACKAGE__ . ': BYTEA columns are known to not work on Pg >= 9.0 with DBD::Pg < 2.17.2' | ||||||||||||||||||||||||||||
| 202 | ); | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | 0 | ▶ | elsif (not try { DBD::Pg->VERSION('2.9.2'); 1 }) { carp( | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 205 | __PACKAGE__ . ': DBD::Pg 2.9.2 or greater is strongly recommended for BYTEA column support' | ||||||||||||||||||||||||||||
| 206 | )} | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 0 | $DBD::Pg::__DBIO_DBD_VERSION_CHECK_DONE__ = 1; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 0 | return { pg_type => DBD::Pg::PG_BYTEA() }; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | else { | ||||||||||||||||||||||||||||
| 214 | 0 | return undef; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # Savepoints via DBD::Pg native methods | ||||||||||||||||||||||||||||
| 219 | 0 | ▶ | sub _exec_svp_begin { | ||||||||||||||||||||||||||
|
Subroutine
_exec_svp_begin: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 220 | 0 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 221 | 0 | $self->_dbh->pg_savepoint($name); | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | ▶ | sub _exec_svp_release { | ||||||||||||||||||||||||||
|
Subroutine
_exec_svp_release: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 225 | 0 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 226 | 0 | $self->_dbh->pg_release($name); | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 0 | ▶ | sub _exec_svp_rollback { | ||||||||||||||||||||||||||
|
Subroutine
_exec_svp_rollback: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 230 | 0 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 231 | 0 | $self->_dbh->pg_rollback_to($name); | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | # Override deployment to use DBIO::PostgreSQL::DDL instead of SQL::Translator | ||||||||||||||||||||||||||||
| 236 | 0 | ▶ | sub deploy { | ||||||||||||||||||||||||||
|
Subroutine
deploy: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 237 | 0 | my ($self, $schema, $type, $sqltargs, $dir) = @_; | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 0 | ▶ | if ($schema->can('pg_deploy')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 0 | $schema->pg_deploy->install; | |||||||||||||||||||||||||||
| 241 | 0 | return; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | # Fallback to parent (SQL::Translator) for schemas without PostgreSQL component | ||||||||||||||||||||||||||||
| 245 | 0 | $self->next::method($schema, $type, $sqltargs, $dir); | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | ▶ | sub deployment_statements { | ||||||||||||||||||||||||||
|
Subroutine
deployment_statements: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 250 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 251 | 0 | my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | # If schema has PostgreSQL component, generate native DDL | ||||||||||||||||||||||||||||
| 254 | 0 | ▶ | if ($schema->can('pg_install_ddl')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | 0 | return $schema->pg_install_ddl; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | # Fallback to SQL::Translator | ||||||||||||||||||||||||||||
| 259 | 0 | ▶ | $sqltargs ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$sqltargs ||= {}
MC/DC0%0/1
$sqltargs ||= {}
$sqltargs
Truth table0%0/2
A
A= $sqltargs
| |||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 0 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not exists $sqltargs->{"producer_args"}{"postgres_version"} and my $dver = $self->_server_info->{"normalized_dbms_version"}
MC/DC0%0/2
not exists $sqltargs->{"producer_args"}{"postgres_version"} and my $dver = $self->_server_info->{"normalized_dbms_version"}
not exists $sqltargs->{"producer_args"}{"postgres_version"}
my $dver = $self->_server_info->{"normalized_dbms_version"}
Truth table0%0/3
not(A) and B
A= not exists $sqltargs->{"producer_args"}{"postgres_version"}B= my $dver = $self->_server_info->{"normalized_dbms_version"}
| |||||||||||||||||||||||||||||
| 262 | ! exists $sqltargs->{producer_args}{postgres_version} | ||||||||||||||||||||||||||||
| 263 | and | ||||||||||||||||||||||||||||
| 264 | my $dver = $self->_server_info->{normalized_dbms_version} | ||||||||||||||||||||||||||||
| 265 | ) { | ||||||||||||||||||||||||||||
| 266 | 0 | $sqltargs->{producer_args}{postgres_version} = $dver; | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 0 | $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest); | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 0 | ▶ | sub cake_defaults { | ||||||||||||||||||||||||||
|
Subroutine
cake_defaults: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 275 | return ( | ||||||||||||||||||||||||||||
| 276 | 0 | inflate_jsonb => 1, | |||||||||||||||||||||||||||
| 277 | inflate_datetime => 1, | ||||||||||||||||||||||||||||
| 278 | retrieve_defaults => 1, | ||||||||||||||||||||||||||||
| 279 | ); | ||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 8 | 1; | |||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | __END__ | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | =pod | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | =head1 NAME | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | DBIO::PostgreSQL::Storage - PostgreSQL storage layer for DBIO | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | =head1 VERSION | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | version 0.900001 | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | C<DBIO::PostgreSQL::Storage> is the DBI storage class for PostgreSQL. It | ||||||||||||||||||||||||||||
| 301 | extends L<DBIO::Storage::DBI> with PostgreSQL-specific behaviour: | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | =over 4 | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =item * | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | C<INSERT ... RETURNING> for efficient last-insert-id retrieval (PostgreSQL 8.2+). | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | =item * | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | Deferred foreign key checks via C<SET CONSTRAINTS ALL DEFERRED>. | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =item * | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | Sequence lookup via C<pg_catalog> when C<RETURNING> is not available. | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | =item * | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | C<BYTEA> column binding through C<DBD::Pg> native type constants. | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | =item * | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | Native savepoint support via C<pg_savepoint>, C<pg_release>, and | ||||||||||||||||||||||||||||
| 324 | C<pg_rollback_to>. | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | =item * | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | Deployment via L<DBIO::PostgreSQL::DDL> when the schema has the | ||||||||||||||||||||||||||||
| 329 | L<DBIO::PostgreSQL> component loaded, falling back to SQL::Translator | ||||||||||||||||||||||||||||
| 330 | otherwise. | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | =item * | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | Opt-in async via C<< MyApp::Schema->connect($dsn, $user, $pass, { async => 'ev' }) >>. | ||||||||||||||||||||||||||||
| 335 | The C<ev> mode resolves (core ADR 0030 mode registry) to the native EV backend | ||||||||||||||||||||||||||||
| 336 | from the optional L<DBIO::PostgreSQL::EV> dist, loaded lazily on first use. Without | ||||||||||||||||||||||||||||
| 337 | that dist installed, requesting the mode croaks with a clear C<install> message — | ||||||||||||||||||||||||||||
| 338 | there is no silent fallback or degrade. | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | =back | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | This class is registered as the driver for the C<Pg> DBD and is selected | ||||||||||||||||||||||||||||
| 343 | automatically when L<DBIO::PostgreSQL> is loaded into a schema class. | ||||||||||||||||||||||||||||
| 344 | Schema-introspection and migration planning live in | ||||||||||||||||||||||||||||
| 345 | L<DBIO::PostgreSQL::Introspect>, L<DBIO::PostgreSQL::Diff>, and | ||||||||||||||||||||||||||||
| 346 | L<DBIO::PostgreSQL::Deploy>; this class stays focused on the live DBI storage | ||||||||||||||||||||||||||||
| 347 | behaviour. | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =head1 METHODS | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | =head2 with_deferred_fk_checks | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | $storage->with_deferred_fk_checks(sub { ... }); | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | Wraps the given coderef in a transaction with C<SET CONSTRAINTS ALL DEFERRED>, | ||||||||||||||||||||||||||||
| 356 | restoring immediate constraint checking afterwards. Use this when bulk-loading | ||||||||||||||||||||||||||||
| 357 | data that temporarily violates referential integrity. | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =head2 last_insert_id | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | my @ids = $storage->last_insert_id($source, @cols); | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | Returns the last inserted value(s) for the given column(s) by querying the | ||||||||||||||||||||||||||||
| 364 | associated sequence via C<pg_catalog>. Only called when C<INSERT ... RETURNING> | ||||||||||||||||||||||||||||
| 365 | is unavailable (PostgreSQL older than 8.2). The sequence is determined from the | ||||||||||||||||||||||||||||
| 366 | column default expression. | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | =head2 bind_attribute_by_data_type | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | my $attr = $storage->bind_attribute_by_data_type($data_type); | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | Returns C<{ pg_type =E<gt> DBD::Pg::PG_BYTEA() }> for binary LOB types so that | ||||||||||||||||||||||||||||
| 373 | C<DBD::Pg> handles C<BYTEA> columns correctly. Also checks that a compatible | ||||||||||||||||||||||||||||
| 374 | C<DBD::Pg> version is installed for the connected PostgreSQL server version. | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =head2 deploy | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | $storage->deploy($schema, $type, $sqltargs, $dir); | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | Deploys the schema. When the schema has the L<DBIO::PostgreSQL> component | ||||||||||||||||||||||||||||
| 381 | loaded (i.e. C<$schema-E<gt>can('pg_deploy')> is true), delegates to | ||||||||||||||||||||||||||||
| 382 | L<DBIO::PostgreSQL::Deploy/install>. Otherwise falls back to the parent | ||||||||||||||||||||||||||||
| 383 | SQL::Translator-based deployment. | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | =head2 deployment_statements | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | my $sql = $storage->deployment_statements($schema, ...); | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | Generates DDL statements for deployment. When the schema has | ||||||||||||||||||||||||||||
| 390 | C<pg_install_ddl>, returns native PostgreSQL DDL from | ||||||||||||||||||||||||||||
| 391 | L<DBIO::PostgreSQL::DDL>. Otherwise falls back to SQL::Translator, passing | ||||||||||||||||||||||||||||
| 392 | the detected server version as C<postgres_version> to the producer. | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | =head2 cake_defaults | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | Returns PostgreSQL-optimized defaults for L<DBIO::Cake>. | ||||||||||||||||||||||||||||
| 397 | Activated via C<use DBIO::Cake '-Pg'>. | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | =over 4 | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =item * C<inflate_jsonb> — on (PostgreSQL standard is jsonb, not json) | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =item * C<inflate_datetime> — on | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | =item * C<retrieve_defaults> — on (PostgreSQL generates UUIDs, serials, NOW(), etc.) | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | =back | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | =seealso | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | =over 4 | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | =item * L<DBIO::PostgreSQL> - schema component that activates this storage class | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | =item * L<DBIO::PostgreSQL::Deploy> - high-level deploy/upgrade orchestration | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | =item * L<DBIO::PostgreSQL::DDL> - native PostgreSQL DDL generator | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | =back | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 428 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||
| 429 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 432 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =cut | ||||||||||||||||||||||||||||