lib/DB/Object/Postgres/Tables.pm

filter: Statementstmt 8.4% 23 / 271 Branchbran 0.0% 0 / 166 Conditioncond 0.0% 0 / 135 MC/DCmcdc 0.0% 0 / 71 Subroutinesub 25.9% 7 / 27 Podpod 100% 19 / 19 totaltotal 7.1% 49 / 689 CC 145 SCAR 66.6
CC
145
Cov
4%
structure
2256.0
create
756.0
disable_trigger
132.0
CRAP 781.7
« lib/DB/Object.pm ↑ Summary blib/lib/DB/Object/Postgres/Query.pm »
1 # -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## SQL API Abstraction - ~/lib/DB/Object/Postgres/Tables.pm
4 ## Version v1.1.2
5 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2017/07/19
8 ## Modified 2026/10/01
9 ## All rights reserved
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 ## This package's purpose is to separate the object of the tables from the main
15 ## DB::Object package so that when they get DESTROY'ed, it does not interrupt
16 ## the SQL connection
17 ##----------------------------------------------------------------------------
18 package DB::Object::Postgres::Tables;
19 BEGIN
20 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 warnings::register_categories( 'DB::Object' );
24 1 ▶ use parent qw( DB::Object::Tables );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶ use vars qw( $VERSION $DEBUG $EXCEPTION_CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 our $DEBUG = 0;
27 1 our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
28 1 our $VERSION = 'v1.1.2';
29 };
30  
31 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 sub init
35 0 ▶{
Subroutine init: not called CC=2 SCAR=17.9
Pod documented
36 # return( shift->DB::Object::Tables::init( @_ ) );
37 0 my $self = shift( @_ );
38 0 $self->{_init_params_order} = [qw( dbo query_object )];
39 0 $self->{_init_strict_use_sub} = 1;
40 0 $self->{_exception_class} = $EXCEPTION_CLASS;
41 0 ▶ $self->DB::Object::Tables::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->DB::Object::Tables::init(@_) or return $self->pass_error 0 0 0
42 0 return( $self );
43 }
44  
45 # Inherited from DB::Object::Tables
46 # sub alter
47  
48 sub create
49 0 ▶{
Subroutine create: not called CC=27 SCAR=66.3
Pod documented
50 0 my $self = shift( @_ );
51 # $tbl->create( [ 'ROW 1', 'ROW 2'... ], { 'temporary' => 1, 'TYPE' => ISAM }, $obj );
52 0 ▶ my $data = shift( @_ ) || [];
Condition0%0/2
shift @_ || []
l!l
00
MC/DC0%0/1
shift @_ || []
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
53 0 ▶ my $opt = shift( @_ ) || {};
Condition0%0/2
shift @_ || {}
l!l
00
MC/DC0%0/1
shift @_ || {}
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
54 0 my $sth = shift( @_ );
55 0 my $table = $self->{table};
56 # Set temporary in the object, so we can use it to recreate the table creation info as string:
57 # $table->create( [ ... ], { ... }, $obj )->as_string();
58 0 my $temp = $self->{temporary} = delete( $opt->{temporary} );
59 # Check possible options
60 0 my $allowed =
61 {
62 'inherits' => qr/^\w+$/i,
63 'with oids' => qr//,
64 'without oids' => qr//,
65 'on commit' => qr/^(PRESERVE ROWS|DELETE ROWS|DROP)$/,
66 'comment' => qr//,
67 'tablespace' => qr/^\w+$/,
68 };
69 0 my @options = ();
70 0 my @errors = ();
71 # Avoid working for nothing, make this condition
72 0 ▶ if( %$opt )
Branch: 1 branch
BranchTrueFalseTotal
if (%$opt) 0 0 0
73 {
74 0 my %lc_opt = map{ lc( $_ ) => $opt->{ $_ } } keys( %$opt );
75 0 $opt = \%lc_opt;
76 0 foreach my $key ( keys( %$opt ) )
77 {
78 0 ▶ next if( $opt->{ $key } =~ /^\s*$/ || !exists( $allowed->{ $key } ) || $key eq "inherits" );
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{$key} =~ /^\s*$/ or not exists $allowed->{$key} or $key eq "inherits" 0 0 0
Condition0%0/6
$opt->{$key} =~ /^\s*$/ or not exists $allowed->{$key} or $key eq "inherits"
l!l&&r!l&&!r
000
$opt->{$key} =~ /^\s*$/ or not exists $allowed->{$key}
l!l&&r!l&&!r
000
MC/DC0%0/3
$opt->{$key} =~ /^\s*$/ or not exists $allowed->{$key} or $key eq "inherits"
$opt->{$key} =~ /^\s*$/ not exists $allowed->{$key} $key eq "inherits"
Truth table0%0/4
A or not(B) or C
A= $opt->{$key} =~ /^\s*$/B= not exists $allowed->{$key}C= $key eq "inherits"
ABCresult
1XX1
01X1
0011
0000
79 0 ▶ if( $opt->{ $key } !~ /$allowed->{ $key }/ )
Branch: 1 branch
BranchTrueFalseTotal
if (not $opt->{$key} =~ /$allowed->{$key}/) { } 0 0 0
80 {
81 0 push( @errors, $key );
82 }
83 else
84 {
85 0 push( @options, $key );
86 }
87 }
88 0 ▶ $opt->{ 'comment' } = "'" . quotemeta( $opt->{ 'comment' } ) . "'" if( exists( $opt->{ 'comment' } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt->{"comment"} 0 0 0
89 }
90 0 ▶ if( @errors )
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 0 0 0
91 {
92 0 warn( "The options '", join( ', ', @errors ), "' were either not recognized or malformed and thus were ignored.\n" );
93 }
94 # Check statement
95 0 my $select = '';
96 0 ▶ if( $sth && ref( $sth ) && ( $sth->isa( "DB::Object::Postgres::Statement" ) || $sth->can( 'as_string' ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($sth and ref $sth and $sth->isa("DB::Object::Postgres::Statement") || $sth->can("as_string")) 0 0 0
Condition0%0/9
$sth and ref $sth and $sth->isa("DB::Object::Postgres::Statement") || $sth->can("as_string")
!ll&&!rl&&r
000
$sth and ref $sth
!ll&&!rl&&r
000
$sth->isa("DB::Object::Postgres::Statement") or $sth->can("as_string")
l!l&&r!l&&!r
000
MC/DC0%0/4
$sth and ref $sth and $sth->isa("DB::Object::Postgres::Statement") || $sth->can("as_string")
$sth ref $sth $sth->isa("DB::Object::Postgres::Statement") $sth->can("as_string")
Truth table0%0/5
A and B and C or D
A= $sthB= ref $sthC= $sth->isa("DB::Object::Postgres::Statement")D= $sth->can("as_string")
ABCDresult
0XXX0
10XX0
11000
111X1
11011
97 {
98 0 $select = $sth->as_string();
99 0 ▶ if( $select !~ /^\s*(?:IGNORE|REPLACE)*\s*\bSELECT\s+/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($select =~ /^\s*(?:IGNORE|REPLACE)*\s*\bSELECT\s+/) 0 0 0
100 {
101 0 return( $self->error( "SELECT statement to use to create table is invalid:\n$select" ) );
102 }
103 }
104 0 ▶ if( $self->exists() == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exists == 0) { } 0 0 0
105 {
106 0 ▶ my $query = 'CREATE ' . ( $temp ? 'TEMPORARY ' : '' ) . "TABLE $table ";
Branch: 1 branch
BranchTrueFalseTotal
$temp ? : 0 0 0
107 # Structure of table if any -
108 # structure may very well be provided using a select statement, such as:
109 # CREATE TEMPORARY TABLE ploppy TYPE=HEAP COMMENT='this is kewl' MAX_ROWS=10 SELECT * FROM some_table LIMIT 0,0
110 0 ▶ my $def = "(\n" . CORE::join( ",\n", @$data ) . "\n)" if( $data && ref( $data ) && @$data );
Branch: 1 branch
BranchTrueFalseTotal
if $data and ref $data and @$data 0 0 0
Condition0%0/6
$data and ref $data and @$data
!ll&&!rl&&r
000
$data and ref $data
!ll&&!rl&&r
000
MC/DC0%0/3
$data and ref $data and @$data
$data ref $data @$data
Truth table0%0/4
A and B and C
A= $dataB= ref $dataC= @$data
ABCresult
0XX0
10X0
1100
1111
111 0 ▶ $def .= " INHERITS (" . $opt->{ 'inherits' } . ")" if( $opt->{ 'inherits' } );
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"inherits"} 0 0 0
112 0 my $tdef = CORE::join( ' ', map{ "\U$_\E = $opt->{ $_ }" } @options );
113 0 ▶ if( !$def && !$select )
Branch: 1 branch
BranchTrueFalseTotal
unless ($def or $select) 0 0 0
Condition0%0/3
$def or $select
l!l&&r!l&&!r
000
MC/DC0%0/2
$def or $select
$def $select
Truth table0%0/3
A or B
A= $defB= $select
ABresult
1X1
011
000
114 {
115 0 return( $self->error( "Lacking table '$table' structure information to create it." ) );
116 }
117 0 $query .= join( ' ', $def, $tdef, $select );
118 0 ▶ my $new = $self->database_object->prepare( $query ) ||
Condition0%0/2
$self->database_object->prepare($query) || (return $self->error("Error while preparing query to create table '${table}':\n$query", $self->database_object->errstr))
l!l
00
MC/DC0%0/1
$self->database_object->prepare($query) || (return $self->error("Error while preparing query to create table '${table}':\n$query", $self->database_object->errstr))
$self->database_object->prepare($query)
Truth table0%0/2
A
A= $self->database_object->prepare($query)
Aresult
11
00
119 return( $self->error( "Error while preparing query to create table '$table':\n$query", $self->database_object->errstr() ) );
120 # Trick so other method may follow, such as as_string(), fetchrow(), rows()
121 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
122 {
123 # print( STDERR "create(): wantarrays in void context.\n" );
124 0 ▶ $new->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$new->execute or return $self->error("Error while executing query to create table '${table}':\n$query", $new->errstr) 0 0 0
125 return( $self->error( "Error while executing query to create table '$table':\n$query", $new->errstr() ) );
126 }
127 0 $self->reset_structure;
128 0 $self->database_object->table_push( $table );
129 0 return( $new );
130 }
131 else
132 {
133 0 return( $self->error( "Table '$table' already exists." ) );
134 }
135 }
136  
137 sub create_info
138 0 ▶{
Subroutine create_info: not called CC=9 SCAR=45.0
Pod documented
139 0 my $self = shift( @_ );
140 0 my $table = $self->{table};
141 0 ▶ $self->structure || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->structure or return $self->pass_error 0 0 0
142 0 my $struct = $self->{structure};
143 0 my $fields = $self->{fields};
144 0 my $default = $self->{default};
145 0 my $primary = $self->{primary};
146 0 my @output = ();
147 0 foreach my $field ( sort{ $fields->{ $a } <=> $fields->{ $b } } keys( %$fields ) )
148 {
149 0 push( @output, "$field $struct->{ $field }" );
150 }
151 0 ▶ push( @output, "PRIMARY KEY(" . CORE::join( ',', @$primary ) . ")" ) if( $primary && @$primary );
Branch: 1 branch
BranchTrueFalseTotal
if $primary and @$primary 0 0 0
Condition0%0/3
$primary and @$primary
!ll&&!rl&&r
000
MC/DC0%0/2
$primary and @$primary
$primary @$primary
Truth table0%0/3
A and B
A= $primaryB= @$primary
ABresult
0X0
100
111
152 0 my $info = $self->stat( $table );
153 0 my @opt = ();
154 0 my $addons = $info->{create_options};
155 0 ▶ if( $addons )
Branch: 1 branch
BranchTrueFalseTotal
if ($addons) 0 0 0
156 {
157 0 $addons =~ s/(\A|\s+)([\w\_]+)\s*=\s*/$1\U$2\E=/g;
158 0 push( @opt, $addons );
159 }
160 0 ▶ push( @opt, "COMMENT='" . quotemeta( $info->{ 'comment' } ) . "'" ) if( $info->{comment} );
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"comment"} 0 0 0
161 0 my $str = "CREATE TABLE $table (\n\t" . CORE::join( ",\n\t", @output ) . "\n)";
162 0 ▶ $str .= ' ' . CORE::join( ' ', @opt ) if( @opt );
Branch: 1 branch
BranchTrueFalseTotal
if @opt 0 0 0
163 0 $str .= ';';
164 0 ▶ return( @output ? $str : undef() );
Branch: 1 branch
BranchTrueFalseTotal
@output ? : 0 0 0
165 }
166  
167 # NOTE: sub default is inherited from DB::Object::Tables
168 # sub default
169  
170 # <https://www.postgresql.org/docs/10/sql-altertable.html>
171 sub disable_trigger
172 0 ▶{
Subroutine disable_trigger: not called CC=11 SCAR=48.8
Pod documented
173 0 my $self = shift( @_ );
174 my $table = $self->{table} ||
175 0 ▶ return( $self->error( "No table was provided to disable trigger." ) );
Condition0%0/2
$self->{'table'} || (return $self->error('No table was provided to disable trigger.'))
l!l
00
MC/DC0%0/1
$self->{'table'} || (return $self->error('No table was provided to disable trigger.'))
$self->{'table'}
Truth table0%0/2
A
A= $self->{'table'}
Aresult
11
00
176 0 my $opts = $self->_get_args_as_hash( @_ );
177 0 ▶ $opts->{all} //= 0;
Condition0%0/2
$opts->{"all"} //= 0
l!l
00
MC/DC0%0/1
$opts->{"all"} //= 0
$opts->{"all"}
Truth table0%0/2
A
A= $opts->{"all"}
Aresult
11
00
178 0 ▶ my $dbo = $self->database_object ||
Condition0%0/2
$self->database_object || (return $self->error('No database handle could be found.'))
l!l
00
MC/DC0%0/1
$self->database_object || (return $self->error('No database handle could be found.'))
$self->database_object
Truth table0%0/2
A
A= $self->database_object
Aresult
11
00
179 return( $self->error( "No database handle could be found." ) );
180 # This feature exists only since version 8.1
181 0 ▶ unless( $dbo->version >= version->declare( '8.1' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbo->version >= "version"->declare("8.1")) 0 0 0
182 {
183 0 return( $self->error( "Disabling trigger on a table requires PostgreSQL version 8.1 or higher." ) );
184 }
185 0 my $query = 'ALTER TABLE ' . $table . ' DISABLE TRIGGER ';
186 0 ▶ if( defined( $opts->{name} ) && length( $opts->{name} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{'name'} and length $opts->{'name'}) { } 0 0 0
Condition0%0/3
defined $opts->{"name"} and length $opts->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts->{"name"} and length $opts->{"name"}
defined $opts->{"name"} length $opts->{"name"}
Truth table0%0/3
A and B
A= defined $opts->{"name"}B= length $opts->{"name"}
ABresult
0X0
100
111
187 {
188 0 $query .= $opts->{name};
189 }
190 else
191 {
192 0 ▶ $query .= $opts->{all} ? 'ALL' : 'USER';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'all'} ? : 0 0 0
193 }
194 0 ▶ my $sth = $dbo->prepare( $query ) ||
Condition0%0/2
$dbo->prepare($query) || (return $self->error("Error while preparing query to disable trigger for table '${table}':\n$query", $dbo->errstr))
l!l
00
MC/DC0%0/1
$dbo->prepare($query) || (return $self->error("Error while preparing query to disable trigger for table '${table}':\n$query", $dbo->errstr))
$dbo->prepare($query)
Truth table0%0/2
A
A= $dbo->prepare($query)
Aresult
11
00
195 return( $self->error( "Error while preparing query to disable trigger for table '$table':\n$query", $dbo->errstr() ) );
196 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
197 {
198 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to disable trigger for table '${table}':\n$query", $sth->errstr) 0 0 0
199 return( $self->error( "Error while executing query to disable trigger for table '$table':\n$query", $sth->errstr() ) );
200 }
201 0 return( $sth );
202 }
203  
204 sub drop
205 0 ▶{
Subroutine drop: not called CC=10 SCAR=47.0
Pod documented
206 0 my $self = shift( @_ );
207 my $table = $self->{table} ||
208 0 ▶ return( $self->error( "No table was provided to drop." ) );
Condition0%0/2
$self->{'table'} || (return $self->error('No table was provided to drop.'))
l!l
00
MC/DC0%0/1
$self->{'table'} || (return $self->error('No table was provided to drop.'))
$self->{'table'}
Truth table0%0/2
A
A= $self->{'table'}
Aresult
11
00
209 0 my $opts = $self->_get_args_as_hash( @_ );
210 0 ▶ my $dbo = $self->database_object ||
Condition0%0/2
$self->database_object || (return $self->error('No database handle could be found.'))
l!l
00
MC/DC0%0/1
$self->database_object || (return $self->error('No database handle could be found.'))
$self->database_object
Truth table0%0/2
A
A= $self->database_object
Aresult
11
00
211 return( $self->error( "No database handle could be found." ) );
212 0 my $query = 'DROP TABLE';
213 0 ▶ $query .= ' IF EXISTS' if( $opts->{ 'if-exists' } || $opts->{if_exists} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"if-exists"} or $opts->{"if_exists"} 0 0 0
Condition0%0/3
$opts->{"if-exists"} or $opts->{"if_exists"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"if-exists"} or $opts->{"if_exists"}
$opts->{"if-exists"} $opts->{"if_exists"}
Truth table0%0/3
A or B
A= $opts->{"if-exists"}B= $opts->{"if_exists"}
ABresult
1X1
011
000
214 0 $query .= " $table";
215 0 ▶ if( $opts->{cascade} )
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'cascade'}) { } 0 0 0
elsif ($opts->{'restrict'}) { } 0 0 0
216 {
217 0 $query .= ' CASCADE';
218 }
219 ## Default Postgres behavior
220 elsif( $opts->{restrict} )
221 {
222 0 $query .= ' RESTRICT';
223 }
224 0 ▶ my $sth = $dbo->prepare( $query ) ||
Condition0%0/2
$dbo->prepare($query) || (return $self->error("Error while preparing query to drop table '${table}':\n$query", $dbo->errstr))
l!l
00
MC/DC0%0/1
$dbo->prepare($query) || (return $self->error("Error while preparing query to drop table '${table}':\n$query", $dbo->errstr))
$dbo->prepare($query)
Truth table0%0/2
A
A= $dbo->prepare($query)
Aresult
11
00
225 return( $self->error( "Error while preparing query to drop table '$table':\n$query", $dbo->errstr() ) );
226 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
227 {
228 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to drop table '${table}':\n$query", $sth->errstr) 0 0 0
229 return( $self->error( "Error while executing query to drop table '$table':\n$query", $sth->errstr() ) );
230 }
231 0 $self->reset_structure;
232 0 return( $sth );
233 }
234  
235 # <https://www.postgresql.org/docs/10/sql-altertable.html>
236 sub enable_trigger
237 0 ▶{
Subroutine enable_trigger: not called CC=11 SCAR=48.8
Pod documented
238 0 my $self = shift( @_ );
239 my $table = $self->{table} ||
240 0 ▶ return( $self->error( "No table was provided to enable trigger." ) );
Condition0%0/2
$self->{'table'} || (return $self->error('No table was provided to enable trigger.'))
l!l
00
MC/DC0%0/1
$self->{'table'} || (return $self->error('No table was provided to enable trigger.'))
$self->{'table'}
Truth table0%0/2
A
A= $self->{'table'}
Aresult
11
00
241 0 my $opts = $self->_get_args_as_hash( @_ );
242 0 ▶ $opts->{all} //= 0;
Condition0%0/2
$opts->{"all"} //= 0
l!l
00
MC/DC0%0/1
$opts->{"all"} //= 0
$opts->{"all"}
Truth table0%0/2
A
A= $opts->{"all"}
Aresult
11
00
243 0 ▶ my $dbo = $self->database_object ||
Condition0%0/2
$self->database_object || (return $self->error('No database handle could be found.'))
l!l
00
MC/DC0%0/1
$self->database_object || (return $self->error('No database handle could be found.'))
$self->database_object
Truth table0%0/2
A
A= $self->database_object
Aresult
11
00
244 return( $self->error( "No database handle could be found." ) );
245 # This feature exists only since version 8.1
246 0 ▶ unless( $dbo->version >= version->declare( '8.1' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbo->version >= "version"->declare("8.1")) 0 0 0
247 {
248 0 return( $self->error( "Enabling trigger on a table requires PostgreSQL version 8.1 or higher." ) );
249 }
250 0 my $query = 'ALTER TABLE ' . $table . ' ENABLE TRIGGER ';
251 0 ▶ if( defined( $opts->{name} ) && length( $opts->{name} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{'name'} and length $opts->{'name'}) { } 0 0 0
Condition0%0/3
defined $opts->{"name"} and length $opts->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts->{"name"} and length $opts->{"name"}
defined $opts->{"name"} length $opts->{"name"}
Truth table0%0/3
A and B
A= defined $opts->{"name"}B= length $opts->{"name"}
ABresult
0X0
100
111
252 {
253 0 $query .= $opts->{name};
254 }
255 else
256 {
257 0 ▶ $query .= $opts->{all} ? 'ALL' : 'USER';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'all'} ? : 0 0 0
258 }
259 0 ▶ my $sth = $dbo->prepare( $query ) ||
Condition0%0/2
$dbo->prepare($query) || (return $self->error("Error while preparing query to disable trigger for table '${table}':\n$query", $self->database_object->errstr))
l!l
00
MC/DC0%0/1
$dbo->prepare($query) || (return $self->error("Error while preparing query to disable trigger for table '${table}':\n$query", $self->database_object->errstr))
$dbo->prepare($query)
Truth table0%0/2
A
A= $dbo->prepare($query)
Aresult
11
00
260 return( $self->error( "Error while preparing query to disable trigger for table '$table':\n$query", $self->database_object->errstr() ) );
261 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
262 {
263 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to disable trigger for table '${table}':\n$query", $sth->errstr) 0 0 0
264 return( $self->error( "Error while executing query to disable trigger for table '$table':\n$query", $sth->errstr() ) );
265 }
266 0 return( $sth );
267 }
268  
269 # NOTE: exists is inherited from DB::Object::Tables
270  
271 # NOTE: sub fields is inherited from DB::Object::Tables
272 # sub fields
273  
274 sub lock
275 0 ▶{
Subroutine lock: not called CC=10 SCAR=47.0
Pod documented
276 0 my $self = shift( @_ );
277 0 my $table = $self->{table};
278 0 ▶ my $opt = shift( @_ ) || 'SHARE';
Condition0%0/2
shift @_ || 'SHARE'
l!l
00
MC/DC0%0/1
shift @_ || 'SHARE'
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
279 0 ▶ my $nowait = shift( @_ ) || undef();
Condition0%0/2
shift @_ || undef()
l!l
00
MC/DC0%0/1
shift @_ || undef()
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
280 # If this is a SELECT ... FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE and FOR KEY SHAR, we redirect to the query builder
281 0 ▶ if( $opt =~ /^(?:For[[:blank:]\h]+)?(update|no[[:blank:]\h]+key[[:blank:]\h]+update|share|key[[:blank:]\h]+share)/i )
Branch: 2 branches
BranchTrueFalseTotal
if ($opt =~ /^(?:For[[:blank:]\h]+)?(update|no[[:blank:]\h]+key[[:blank:]\h]+update|share|key[[:blank:]\h]+share)/i) { } 0 0 0
elsif (not $opt =~ /^(ACCESS\s+SHARE|ROW\s+SHARE|ROW\s+EXCLUSIVE|SHARE\s+UPDATE\s+EXCLUSIVE|SHARE|SHARE\s+ROW\s+EXCLUSIVE|EXCLUSIVE|ACCESS\s+EXCLUSIVE)$/i) { } 0 0 0
282 {
283 0 return( $self->lock_select( $opt ) );
284 }
285 elsif( $opt !~ /^(ACCESS\s+SHARE|ROW\s+SHARE|ROW\s+EXCLUSIVE|SHARE\s+UPDATE\s+EXCLUSIVE|SHARE|SHARE\s+ROW\s+EXCLUSIVE|EXCLUSIVE|ACCESS\s+EXCLUSIVE)$/i )
286 {
287 0 return( $self->error( "Bad table '$table' locking option '$opt'." ) );
288 }
289 0 ▶ my $dbo = $self->database_object ||
Condition0%0/2
$self->database_object || (return $self->error('No database handle could be found.'))
l!l
00
MC/DC0%0/1
$self->database_object || (return $self->error('No database handle could be found.'))
$self->database_object
Truth table0%0/2
A
A= $self->database_object
Aresult
11
00
290 return( $self->error( "No database handle could be found." ) );
291 0 my $query = sprintf( 'LOCK TABLE %s IN %s MODE', $table, $opt );
292 0 ▶ $query .= " NOWAIT" if( $nowait );
Branch: 1 branch
BranchTrueFalseTotal
if $nowait 0 0 0
293 0 ▶ my $sth = $dbo->prepare( $query ) ||
Condition0%0/2
$dbo->prepare($query) || (return $self->error("Error while preparing query to do tables locking:\n$query", $dbo->errstr))
l!l
00
MC/DC0%0/1
$dbo->prepare($query) || (return $self->error("Error while preparing query to do tables locking:\n$query", $dbo->errstr))
$dbo->prepare($query)
Truth table0%0/2
A
A= $dbo->prepare($query)
Aresult
11
00
294 return( $self->error( "Error while preparing query to do tables locking:\n$query", $dbo->errstr() ) );
295 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
296 {
297 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to do tables locking:\n$query", $sth->errstr) 0 0 0
298 return( $self->error( "Error while executing query to do tables locking:\n$query", $sth->errstr() ) );
299 }
300 0 return( $sth );
301 }
302  
303 sub lock_select
304 0 ▶{
Subroutine lock_select: not called CC=5 SCAR=34.0
Pod documented
305 0 my $self = shift( @_ );
306 0 my $q = $self->_reset_query;
307 # Void
308 0 ▶ return( $q->lock( @_ ) ) if( !defined( wantarray() ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
309 0 ▶ if( wantarray() )
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
310 {
311 0 ▶ my( @val ) = $q->lock( @_ ) || return( $self->pass_error( $q->error ) );
Condition0%0/2
$q->lock(@_) || (return $self->pass_error($q->error))
l!l
00
MC/DC0%0/1
$q->lock(@_) || (return $self->pass_error($q->error))
$q->lock(@_)
Truth table0%0/2
A
A= $q->lock(@_)
Aresult
11
00
312 0 return( @val );
313 }
314 else
315 {
316 0 my $val = $q->lock( @_ );
317 0 ▶ return( $self->pass_error( $q->error ) ) if( !defined( $val ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
318 0 return( $val );
319 }
320 }
321  
322 # NOTE: sub name is inherited from DB::Object::Tables
323 # sub name
324  
325 # NOTE: sub null is inherited from DB::Object::Tables
326 # sub null
327  
328 # NOTE: sub primary is inherited from DB::Object::Tables
329 # sub primary
330  
331 sub on_conflict
332 0 ▶{
Subroutine on_conflict: not called CC=5 SCAR=34.0
Pod documented
333 0 my $self = shift( @_ );
334 0 my $q = $self->_reset_query;
335 # Void
336 0 ▶ return( $q->on_conflict( @_ ) ) if( !defined( wantarray() ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
337 0 ▶ if( wantarray() )
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
338 {
339 0 ▶ my( @val ) = $q->on_conflict( @_ ) || return( $self->pass_error( $q->error ) );
Condition0%0/2
$q->on_conflict(@_) || (return $self->pass_error($q->error))
l!l
00
MC/DC0%0/1
$q->on_conflict(@_) || (return $self->pass_error($q->error))
$q->on_conflict(@_)
Truth table0%0/2
A
A= $q->on_conflict(@_)
Aresult
11
00
340 0 return( @val );
341 }
342 else
343 {
344 0 my $val = $q->on_conflict( @_ );
345 0 ▶ return( $self->pass_error( $q->error ) ) if( !defined( $val ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
346 0 return( $val );
347 }
348 }
349  
350 0 ▶sub optimize { return( shift->error( "optimize() is not implemented PostgreSQL." ) ); }
Subroutine optimize: not called CC=1 SCAR=6.9
Pod documented
351  
352 0 ▶sub parent { return( shift->_set_get_scalar( 'parent', @_ ) ); }
Subroutine parent: not called CC=1 SCAR=6.9
Pod documented
353  
354 sub qualified_name
355 0 ▶{
Subroutine qualified_name: not called CC=5 SCAR=34.0
Pod documented
356 0 my $self = shift( @_ );
357 0 my @val = ();
358 0 ▶ my $dbo = $self->database_object ||
Condition0%0/2
$self->database_object || (return $self->error('No database handle could be found.'))
l!l
00
MC/DC0%0/1
$self->database_object || (return $self->error('No database handle could be found.'))
$self->database_object
Truth table0%0/2
A
A= $self->database_object
Aresult
11
00
359 return( $self->error( "No database handle could be found." ) );
360 0 ▶ CORE::push( @val, $dbo->database ) if( $self->{prefixed} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prefixed"} > 2 0 0 0
361 0 ▶ CORE::push( @val, $self->schema ) if( $self->{prefixed} > 1 && $self->schema );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prefixed"} > 1 and $self->schema 0 0 0
Condition0%0/3
$self->{"prefixed"} > 1 and $self->schema
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"prefixed"} > 1 and $self->schema
$self->{"prefixed"} > 1 $self->schema
Truth table0%0/3
A and B
A= $self->{"prefixed"} > 1B= $self->schema
ABresult
0X0
100
111
362 0 CORE::push( @val, $self->name );
363 0 return( CORE::join( '.', @val ) );
364 }
365  
366 sub rename
367 0 ▶{
Subroutine rename: not called CC=8 SCAR=42.8
Pod documented
368 0 my $self = shift( @_ );
369 my $table = $self->{table} ||
370 0 ▶ return( $self->error( 'No table was provided to rename' ) );
Condition0%0/2
$self->{'table'} || (return $self->error('No table was provided to rename'))
l!l
00
MC/DC0%0/1
$self->{'table'} || (return $self->error('No table was provided to rename'))
$self->{'table'}
Truth table0%0/2
A
A= $self->{'table'}
Aresult
11
00
371 0 ▶ my $new = shift( @_ ) ||
Condition0%0/2
shift @_ || (return $self->error("No new table name was provided to rename table '${table}'."))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error("No new table name was provided to rename table '${table}'."))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
372 return( $self->error( "No new table name was provided to rename table '$table'." ) );
373 0 ▶ if( $new !~ /^[\w\_]+$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($new =~ /^[\w\_]+$/) 0 0 0
374 {
375 0 return( $self->error( "Bad new table name '$new'." ) );
376 }
377 0 my $query = "ALTER TABLE $table RENAME TO $new";
378 0 ▶ my $dbo = $self->database_object ||
Condition0%0/2
$self->database_object || (return $self->error('No database handle could be found.'))
l!l
00
MC/DC0%0/1
$self->database_object || (return $self->error('No database handle could be found.'))
$self->database_object
Truth table0%0/2
A
A= $self->database_object
Aresult
11
00
379 return( $self->error( "No database handle could be found." ) );
380 0 ▶ my $sth = $dbo->prepare( $query ) ||
Condition0%0/2
$dbo->prepare($query) || (return $self->error("Error while preparing query to rename table '${table}' into '${new}':\n$query", $dbo->errstr))
l!l
00
MC/DC0%0/1
$dbo->prepare($query) || (return $self->error("Error while preparing query to rename table '${table}' into '${new}':\n$query", $dbo->errstr))
$dbo->prepare($query)
Truth table0%0/2
A
A= $dbo->prepare($query)
Aresult
11
00
381 return( $self->error( "Error while preparing query to rename table '$table' into '$new':\n$query", $dbo->errstr() ) );
382 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
383 {
384 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to rename table '${table}' into '${new}':\n$query", $sth->errstr) 0 0 0
385 return( $self->error( "Error while executing query to rename table '$table' into '$new':\n$query", $sth->errstr() ) );
386 }
387 0 $self->reset_structure;
388 0 return( $sth );
389 }
390  
391 0 ▶sub repair { return( shift->error( "repair() is not implemented PostgreSQL." ) ); }
Subroutine repair: not called CC=1 SCAR=6.9
Pod documented
392  
393 0 ▶sub reset { return( shift->DB::Object::Tables::reset( @_ ) ); }
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
394  
395 0 ▶sub stat { return( shift->error( "stat() is not implemented PostgreSQL." ) ); }
Subroutine stat: not called CC=1 SCAR=6.9
Pod documented
396  
397 sub structure
398 0 ▶{
Subroutine structure: not called CC=47 SCAR=77.2
Pod documented
399 0 my $self = shift( @_ );
400 0 ▶ return( $self->_clone( $self->{_cache_structure} ) ) if( $self->{_cache_structure} && !CORE::length( $self->{_reset_structure} // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_cache_structure"} and not length($self->{"_reset_structure"} // "") 0 0 0
Condition0%0/5
$self->{"_cache_structure"} and not length($self->{"_reset_structure"} // "")
!ll&&!rl&&r
000
$self->{"_reset_structure"} // ""
l!l
00
MC/DC0%0/2
$self->{"_cache_structure"} and not length($self->{"_reset_structure"} // "")
$self->{"_cache_structure"} $self->{"_reset_structure"}
Truth table0%0/3
A and not(B)
A= $self->{"_cache_structure"}B= $self->{"_reset_structure"}
ABresult
0X0
110
101
401 my $table = $self->{table} ||
402 0 ▶ return( $self->error( "No table provided to get its structure." ) );
Condition0%0/2
$self->{'table'} || (return $self->error('No table provided to get its structure.'))
l!l
00
MC/DC0%0/1
$self->{'table'} || (return $self->error('No table provided to get its structure.'))
$self->{'table'}
Truth table0%0/2
A
A= $self->{'table'}
Aresult
11
00
403 0 ▶ my $struct = $self->{structure} // {};
Condition0%0/2
$self->{"structure"} // {}
l!l
00
MC/DC0%0/1
$self->{"structure"} // {}
$self->{"structure"}
Truth table0%0/2
A
A= $self->{"structure"}
Aresult
11
00
404 0 ▶ my $fields = $self->{fields} // {};
Condition0%0/2
$self->{"fields"} // {}
l!l
00
MC/DC0%0/1
$self->{"fields"} // {}
$self->{"fields"}
Truth table0%0/2
A
A= $self->{"fields"}
Aresult
11
00
405 0 ▶ my $dbo = $self->database_object ||
Condition0%0/2
$self->database_object || (return $self->error('No database handle could be found.'))
l!l
00
MC/DC0%0/1
$self->database_object || (return $self->error('No database handle could be found.'))
$self->database_object
Truth table0%0/2
A
A= $self->database_object
Aresult
11
00
406 return( $self->error( "No database handle could be found." ) );
407 0 my $types_dict = $dbo->datatype_dict;
408 0 ▶ $self->_load_class( 'DB::Object::Fields::Field' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("DB::Object::Fields::Field") or return $self->pass_error 0 0 0
409 0 my $q = $self->_reset_query;
410 # <https://stackoverflow.com/questions/6777456/list-all-index-names-column-names-and-its-table-name-of-a-postgresql-database>
411  
412 # If we have a cache, use it instead of reprocessing it.
413 # <https://stackoverflow.com/questions/52376045/why-does-atttypmod-differ-from-character-maximum-length>
414 # my $query = <<EOT;
415 # SELECT
416 # a.table_schema AS "schema_name"
417 # ,CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'table' END as "table_type"
418 # ,a.column_name AS "field"
419 # ,a.ordinal_position AS "field_num"
420 # ,a.column_default AS "default"
421 # ,a.*
422 # FROM information_schema.columns a
423 # LEFT JOIN pg_catalog.pg_class c ON c.relname = a.table_name
424 # WHERE a.table_name = ?
425 # ORDER BY a.ordinal_position
426 # EOT
427 # <https://www.postgresql.org/docs/14/catalog-pg-attrdef.html>
428 # We could use:
429 # generate_subscripts(i.indkey, 1)
430 # instead of:
431 # generate_series(1,array_upper(string_to_array(i.indkey::text, ' ' )::int2[],1))
432 # but this is not supported by PostgreSQL v8.0; only from v8.4 onward
433 0 my $query = <<EOT;
434 SELECT
435 n.nspname AS "schema_name"
436 ,c.relname AS "table_name"
437 ,CASE c.relkind
438 WHEN 'r' THEN 'table'
439 WHEN 'v' THEN 'view'
440 WHEN 'm' THEN 'materialized view'
441 WHEN 's' THEN 'special'
442 WHEN 'f' THEN 'foreign table'
443 WHEN 'p' THEN 'table'
444 END as "table_type"
445 ,a.attname AS "field"
446 ,a.attnum AS "field_num"
447 ,CASE
448 WHEN a.atttypmod = -1 THEN null
449 WHEN t.oid IN (1042, 1043) THEN a.atttypmod - 4
450 WHEN t.oid IN (1560, 1562) THEN a.atttypmod
451 ELSE NULL
452 END AS "character_maximum_length"
453 ,CASE SUBSTRING(t.typname,1,1)
454 WHEN '_' THEN SUBSTRING(t.typname,2)
455 ELSE t.typname
456 END AS "data_type"
457 ,pg_catalog.format_type(a.atttypid,a.atttypmod) AS "format_type"
458 ,(a.attndims > 0 OR pg_catalog.format_type(a.atttypid,a.atttypmod) LIKE '%[]') AS "is_array"
459 ,CASE a.attnotnull
460 WHEN FALSE THEN TRUE
461 ELSE FALSE
462 END AS "is_nullable"
463 ,COALESCE(i.indisprimary,false) AS "is_primary"
464 ,COALESCE(i.indisunique,false) AS "is_unique"
465 ,r.oid IS NOT NULL AS "is_foreign"
466 ,r2.oid IS NOT NULL AS "is_check"
467 ,(SELECT pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid) FROM pg_attrdef
468 WHERE pg_attrdef.adrelid = c.oid
469 AND pg_attrdef.adnum = a.attnum) AS "default"
470 ,(SELECT pg_description.description FROM pg_description
471 WHERE pg_description.objoid = c.oid AND pg_description.objsubid = a.attnum) AS "comment"
472 ,(SELECT c2.relname FROM pg_inherits LEFT JOIN pg_class c2 ON c2.oid = pg_inherits.inhparent
473 WHERE pg_inherits.inhrelid = c.oid) AS "table_parent"
474 ,c2.relname AS "index_name"
475 ,i.indnatts AS "index_n_columns"
476 ,i.indkey AS "index_columns"
477 ,ARRAY(
478 SELECT pg_get_indexdef(i.indexrelid, k, TRUE)
479 FROM generate_series(1,array_upper(string_to_array(i.indkey::text, ' ' )::int2[],1)) AS s(k)
480 ORDER BY k
481 ) AS "index_keys"
482 ,r.conname AS "foreign_name"
483 ,CASE
484 WHEN r.conindid > 0 THEN (SELECT c3.relname FROM pg_catalog.pg_class c3 WHERE c3.oid = r.conindid)
485 ELSE NULL
486 END AS "foreign_index_name"
487 ,CASE
488 WHEN r.confrelid > 0 THEN (SELECT c4.relname FROM pg_catalog.pg_class c4 WHERE c4.oid = r.confrelid)
489 ELSE NULL
490 END AS "foreign_table"
491 ,CASE r.confupdtype
492 WHEN 'a' THEN 'nothing'
493 WHEN 'r' THEN 'restrict'
494 WHEN 'c' THEN 'cascade'
495 WHEN 'n' THEN 'null'
496 WHEN 'd' THEN 'default'
497 ELSE NULL
498 END AS "foreign_update_action"
499 ,CASE r.confdeltype
500 WHEN 'a' THEN 'nothing'
501 WHEN 'r' THEN 'restrict'
502 WHEN 'c' THEN 'cascade'
503 WHEN 'n' THEN 'null'
504 WHEN 'd' THEN 'default'
505 ELSE NULL
506 END AS "foreign_delete_action"
507 ,CASE r.confmatchtype
508 WHEN 'f' THEN 'full'
509 WHEN 'p' THEN 'partial'
510 WHEN 's' THEN 'simple'
511 ELSE NULL
512 END AS "foreign_match"
513 ,r.conkey AS "foreign_columns"
514 ,ARRAY(
515 SELECT a2.attname
516 FROM generate_series(1,array_upper(r.conkey,1)) AS z(j), pg_catalog.pg_attribute a2
517 WHERE a2.attrelid = c.oid AND a2.attnum = r.conkey[j]
518 ) AS "foreign_keys"
519 ,CASE
520 WHEN r.oid IS NOT NULL THEN pg_get_constraintdef(r.oid,TRUE)
521 ELSE NULL
522 END AS "foreign_expression"
523 ,r2.conname AS "check_name"
524 ,CASE
525 WHEN r2.conindid > 0 THEN (SELECT c3.relname FROM pg_catalog.pg_class c3 WHERE c3.oid = r2.conindid)
526 ELSE NULL
527 END AS "check_index_name"
528 ,r2.conkey AS "check_columns"
529 ,ARRAY(
530 SELECT a2.attname
531 FROM generate_series(1,array_upper(r2.conkey,1)) AS z(j), pg_catalog.pg_attribute a2
532 WHERE a2.attrelid = c.oid AND a2.attnum = r2.conkey[j]
533 ) AS "check_keys"
534 ,CASE
535 WHEN r2.oid IS NOT NULL THEN pg_get_constraintdef(r2.oid,TRUE)
536 ELSE NULL
537 END AS "check_expression"
538 FROM pg_catalog.pg_class c
539 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
540 LEFT JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
541 LEFT JOIN pg_catalog.pg_type t ON t.oid = a.atttypid
542 LEFT JOIN pg_catalog.pg_index i ON i.indrelid = c.oid AND a.attnum = ANY (i.indkey::int[])
543 LEFT JOIN pg_catalog.pg_class c2 ON c2.oid = i.indexrelid
544 LEFT JOIN pg_catalog.pg_constraint r ON r.conrelid = c.oid AND r.contype = 'f' AND a.attnum = ANY (r.conkey)
545 LEFT JOIN pg_catalog.pg_constraint r2 ON r2.conrelid = c.oid AND r2.contype = 'c' AND a.attnum = ANY (r2.conkey)
546 WHERE
547 c.relname = ? AND
548 a.attnum > 0 AND
549 NOT a.attisdropped
550 ORDER BY a.attnum
551 EOT
552 # http://www.postgresql.org/docs/9.3/interactive/infoschema-columns.html
553 # select * from information_schema.columns where table_name = 'address'
554 0 ▶ my $sth = $dbo->prepare_cached( $query ) ||
Condition0%0/2
$dbo->prepare_cached($query) || (return $self->error("Error while preparing query to get table '${table}' columns specification: ", $dbo->errstr))
l!l
00
MC/DC0%0/1
$dbo->prepare_cached($query) || (return $self->error("Error while preparing query to get table '${table}' columns specification: ", $dbo->errstr))
$dbo->prepare_cached($query)
Truth table0%0/2
A
A= $dbo->prepare_cached($query)
Aresult
11
00
555 return( $self->error( "Error while preparing query to get table '$table' columns specification: ", $dbo->errstr ) );
556 0 ▶ $sth->execute( $table ) ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($table) or return $self->error("Error while executing query to get table '${table}' columns specification: ", $sth->errstr) 0 0 0
557 return( $self->error( "Error while executing query to get table '$table' columns specification: ", $sth->errstr ) );
558 0 my $check = {};
559 0 my $foreign = {};
560 0 my $index = {};
561 0 my @primary = ();
562 0 my $ref = '';
563 0 my $c = 0;
564 # Mysql: field, type, null, key, default, extra
565 # Postgres: tablename, field, field_num, type, len, comment, is_nullable, key, foreign_key, default
566 0 ▶ while( $ref = $sth->fetchrow_hashref() )
Branch: 1 branch
BranchTrueFalseTotal
if ($ref = $sth->fetchrow_hashref) 0 0 0
567 {
568 my $def =
569 {
570 name => $ref->{field},
571 comment => $ref->{comment},
572 default => $ref->{default},
573 ( $ref->{check_name} ? ( check_name => $ref->{check_name} ) : () ),
574 ( $ref->{foreign_name} ? ( foreign_name => $ref->{foreign_name} ) : () ),
575 ( $ref->{index_name} ? ( index_name => $ref->{index_name} ) : () ),
576 is_array => ( $ref->{is_array} ? 1 : 0 ),
577 is_check => ( $ref->{is_check} ? 1 : 0 ),
578 is_foreign => ( $ref->{is_foreign} ? 1 : 0 ),
579 is_nullable => ( $ref->{is_nullable} ? 1 : 0 ),
580 is_primary => ( $ref->{is_primary} ? 1 : 0 ),
581 is_unique => ( $ref->{is_unique} ? 1 : 0 ),
582 pos => $ref->{field_num},
583 # query_object => $q,
584 size => $ref->{character_maximum_length},
585 # When the field is an array, the data type will start with an underscore
586 0 ▶ type => ( substr( $ref->{data_type}, 0, 1 ) eq '_' ? substr( $ref->{data_type}, 1 ) : $ref->{data_type} ),
Branch: 10 branches
BranchTrueFalseTotal
$ref->{'check_name'} ? : 0 0 0
$ref->{'foreign_name'} ? : 0 0 0
$ref->{'index_name'} ? : 0 0 0
$ref->{'is_array'} ? : 0 0 0
$ref->{'is_check'} ? : 0 0 0
$ref->{'is_foreign'} ? : 0 0 0
$ref->{'is_nullable'} ? : 0 0 0
$ref->{'is_primary'} ? : 0 0 0
$ref->{'is_unique'} ? : 0 0 0
substr($ref->{'data_type'}, 0, 1) eq '_' ? : 0 0 0
587 # table_object => $self,
588 };
589 0 ▶ $self->{type} = $ref->{table_type} if( !$self->{type} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"type"} 0 0 0
590 0 ▶ $self->{schema} = $ref->{schema_name} if( !$self->{schema} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"schema"} 0 0 0
591 0 ▶ $self->{parent} = $ref->{table_parent} if( !$self->{table_parent} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"table_parent"} 0 0 0
592 0 ▶ if( $ref->{index_name} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"index_name"} and not exists $$index{$ref->{"index_name"}}) 0 0 0
Condition0%0/3
$ref->{"index_name"} and not exists $$index{$ref->{"index_name"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$ref->{"index_name"} and not exists $$index{$ref->{"index_name"}}
$ref->{"index_name"} not exists $$index{$ref->{"index_name"}}
Truth table0%0/3
A and not(B)
A= $ref->{"index_name"}B= not exists $$index{$ref->{"index_name"}}
ABresult
0X0
100
111
593 !CORE::exists( $index->{ $ref->{index_name} } ) )
594 {
595 my $constraint = $self->new_index(
596 is_primary => $ref->{is_primary},
597 is_unique => $ref->{is_unique},
598 fields => $ref->{index_keys},
599 name => $ref->{index_name},
600 0 ▶ ) || return( $self->pass_error );
Condition0%0/2
$self->new_index('is_primary', $ref->{'is_primary'}, 'is_unique', $ref->{'is_unique'}, 'fields', $ref->{'index_keys'}, 'name', $ref->{'index_name'}) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new_index('is_primary', $ref->{'is_primary'}, 'is_unique', $ref->{'is_unique'}, 'fields', $ref->{'index_keys'}, 'name', $ref->{'index_name'}) || (return $self->pass_error)
$self->new_index('is_primary', $ref->{'is_primary'}, 'is_unique', $ref->{'is_unique'}, 'fields', $ref->{'index_keys'}, 'name', $ref->{'index_name'})
Truth table0%0/2
A
A= $self->new_index('is_primary', $ref->{'is_primary'}, 'is_unique', $ref->{'is_unique'}, 'fields', $ref->{'index_keys'}, 'name', $ref->{'index_name'})
Aresult
11
00
601 0 $index->{ $ref->{index_name} } = $constraint;
602 }
603 0 ▶ if( $ref->{check_name} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"check_name"} and not exists $$check{$ref->{"check_name"}}) 0 0 0
Condition0%0/3
$ref->{"check_name"} and not exists $$check{$ref->{"check_name"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$ref->{"check_name"} and not exists $$check{$ref->{"check_name"}}
$ref->{"check_name"} not exists $$check{$ref->{"check_name"}}
Truth table0%0/3
A and not(B)
A= $ref->{"check_name"}B= not exists $$check{$ref->{"check_name"}}
ABresult
0X0
100
111
604 !CORE::exists( $check->{ $ref->{check_name} } ) )
605 {
606 my $constraint = $self->new_check(
607 expr => $ref->{check_expression},
608 fields => $ref->{check_keys},
609 name => $ref->{check_name},
610 0 ▶ ) || return( $self->pass_error );
Condition0%0/2
$self->new_check('expr', $ref->{'check_expression'}, 'fields', $ref->{'check_keys'}, 'name', $ref->{'check_name'}) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new_check('expr', $ref->{'check_expression'}, 'fields', $ref->{'check_keys'}, 'name', $ref->{'check_name'}) || (return $self->pass_error)
$self->new_check('expr', $ref->{'check_expression'}, 'fields', $ref->{'check_keys'}, 'name', $ref->{'check_name'})
Truth table0%0/2
A
A= $self->new_check('expr', $ref->{'check_expression'}, 'fields', $ref->{'check_keys'}, 'name', $ref->{'check_name'})
Aresult
11
00
611 0 $check->{ $ref->{check_name} } = $constraint;
612 }
613 0 ▶ if( $ref->{foreign_name} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"foreign_name"} and not exists $$foreign{$ref->{"foreign_name"}}) 0 0 0
Condition0%0/3
$ref->{"foreign_name"} and not exists $$foreign{$ref->{"foreign_name"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$ref->{"foreign_name"} and not exists $$foreign{$ref->{"foreign_name"}}
$ref->{"foreign_name"} not exists $$foreign{$ref->{"foreign_name"}}
Truth table0%0/3
A and not(B)
A= $ref->{"foreign_name"}B= not exists $$foreign{$ref->{"foreign_name"}}
ABresult
0X0
100
111
614 !CORE::exists( $foreign->{ $ref->{foreign_name} } ) )
615 {
616 my $constraint = $self->new_foreign(
617 expr => $ref->{foreign_expression},
618 match => $ref->{foreign_match},
619 on_delete => $ref->{foreign_delete_action},
620 on_update => $ref->{foreign_update_action},
621 table => $ref->{foreign_table},
622 fields => $ref->{foreign_keys},
623 name => $ref->{foreign_name},
624 0 ▶ ) || return( $self->pass_error );
Condition0%0/2
$self->new_foreign('expr', $ref->{'foreign_expression'}, 'match', $ref->{'foreign_match'}, 'on_delete', $ref->{'foreign_delete_action'}, 'on_update', $ref->{'foreign_update_action'}, 'table', $ref->{'foreign_table'}, 'fields', $ref->{'foreign_keys'}, 'name', $ref->{'foreign_name'}) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new_foreign('expr', $ref->{'foreign_expression'}, 'match', $ref->{'foreign_match'}, 'on_delete', $ref->{'foreign_delete_action'}, 'on_update', $ref->{'foreign_update_action'}, 'table', $ref->{'foreign_table'}, 'fields', $ref->{'foreign_keys'}, 'name', $ref->{'foreign_name'}) || (return $self->pass_error)
$self->new_foreign('expr', $ref->{'foreign_expression'}, 'match', $ref->{'foreign_match'}, 'on_delete', $ref->{'foreign_delete_action'}, 'on_update', $ref->{'foreign_update_action'}, 'table', $ref->{'foreign_table'}, 'fields', $ref->{'foreign_keys'}, 'name', $ref->{'foreign_name'})
Truth table0%0/2
A
A= $self->new_foreign('expr', $ref->{'foreign_expression'}, 'match', $ref->{'foreign_match'}, 'on_delete', $ref->{'foreign_delete_action'}, 'on_update', $ref->{'foreign_update_action'}, 'table', $ref->{'foreign_table'}, 'fields', $ref->{'foreign_keys'}, 'name', $ref->{'foreign_name'})
Aresult
11
00
625 0 $foreign->{ $ref->{foreign_name} } = $constraint;
626 }
627  
628 0 my( $const_def, $dict );
629 0 ▶ if( $def->{is_array} &&
Branch: 2 branches
BranchTrueFalseTotal
if ($def->{'is_array'} and exists $$types_dict{"$def->{'type'}array"}) { } 0 0 0
elsif (exists $$types_dict{$def->{'type'}}) { } 0 0 0
Condition0%0/3
$def->{"is_array"} and exists $$types_dict{qq[$def->{"type"}array]}
!ll&&!rl&&r
000
MC/DC0%0/2
$def->{"is_array"} and exists $$types_dict{qq[$def->{"type"}array]}
$def->{"is_array"} exists $$types_dict{qq[$def->{"type"}array]}
Truth table0%0/3
A and B
A= $def->{"is_array"}B= exists $$types_dict{qq[$def->{"type"}array]}
ABresult
0X0
100
111
630 CORE::exists( $types_dict->{ "$def->{type}array" } ) )
631 {
632 0 $const_def = $types_dict->{ "$def->{type}array" };
633 }
634 elsif( CORE::exists( $types_dict->{ $def->{type} } ) )
635 {
636 0 $const_def = $types_dict->{ $def->{type} };
637 }
638 else
639 {
640 # Get the constant
641 0 DATA_TYPE_RE: foreach my $type ( keys( %$types_dict ) )
642 {
643 0 ▶ if( $def->{type} =~ /$types_dict->{ $type }->{re}/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"type"} =~ /$types_dict->{$type}{"re"}/i) 0 0 0
644 {
645 0 $const_def = $types_dict->{ $type };
646 0 last DATA_TYPE_RE;
647 }
648 }
649 }
650 0 ▶ if( defined( $const_def ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $const_def) 0 0 0
651 {
652 0 my $const_keys = [keys( %$const_def )];
653 0 my $dict = {};
654 0 @$dict{ @$const_keys } = @$const_def{ @$const_keys };
655 0 $def->{datatype} = $dict;
656 # $def->{type} = $dict->{type};
657 }
658 0 $def->{query_object} = $q;
659 0 $def->{table_object} = $self;
660 0 my @define = ( $def->{type} );
661 0 ▶ push( @define, "DEFAULT '$def->{default}'" ) if( defined( $def->{default} ) && length( $def->{default} // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $def->{"default"} and length($def->{"default"} // "") 0 0 0
Condition0%0/5
defined $def->{"default"} and length($def->{"default"} // "")
!ll&&!rl&&r
000
$def->{"default"} // ""
l!l
00
MC/DC0%0/2
defined $def->{"default"} and length($def->{"default"} // "")
defined $def->{"default"} $def->{"default"}
Truth table0%0/3
A and B
A= defined $def->{"default"}B= $def->{"default"}
ABresult
0X0
100
111
662 0 ▶ push( @define, "NOT NULL" ) if( !$def->{is_nullable} );
Branch: 1 branch
BranchTrueFalseTotal
unless $def->{"is_nullable"} 0 0 0
663 0 ▶ push( @primary, $def->{name} ) if( $ref->{key} );
Branch: 1 branch
BranchTrueFalseTotal
if $ref->{"key"} 0 0 0
664 0 $struct->{ $def->{name} } = CORE::join( ' ', @define );
665 0 $def->{debug} = $self->debug;
666 0 ▶ my $field = DB::Object::Fields::Field->new( %$def ) ||
Condition0%0/2
'DB::Object::Fields::Field'->new(%$def) || (return $self->pass_error('DB::Object::Fields::Field'->error))
l!l
00
MC/DC0%0/1
'DB::Object::Fields::Field'->new(%$def) || (return $self->pass_error('DB::Object::Fields::Field'->error))
'DB::Object::Fields::Field'->new(%$def)
Truth table0%0/2
A
A= 'DB::Object::Fields::Field'->new(%$def)
Aresult
11
00
667 return( $self->pass_error( DB::Object::Fields::Field->error ) );
668 0 $fields->{ $def->{name} } = $field;
669 }
670 0 $sth->finish;
671 0 ▶ if( @primary )
Branch: 1 branch
BranchTrueFalseTotal
if (@primary) 0 0 0
672 {
673 0 $self->{primary} = \@primary;
674 }
675 0 $self->{check} = $check;
676 0 $self->{fields} = $fields;
677 0 $self->{foreign} = $foreign;
678 0 $self->{indexes} = $index;
679 0 $self->{_cache_structure} = $struct;
680 0 return( $self->_clone( $struct ) );
681 }
682  
683 sub table_info
684 0 ▶{
Subroutine table_info: not called CC=2 SCAR=17.9
Pod documented
685 0 my $self = shift( @_ );
686 0 ▶ my $dbo = $self->database_object ||
Condition0%0/2
$self->database_object || (return $self->error('No database handle could be found.'))
l!l
00
MC/DC0%0/1
$self->database_object || (return $self->error('No database handle could be found.'))
$self->database_object
Truth table0%0/2
A
A= $self->database_object
Aresult
11
00
687 return( $self->error( "No database handle could be found." ) );
688 0 return( $dbo->table_info( @_ ) );
689 }
690  
691 sub unlock
692 0 ▶{
Subroutine unlock: not called CC=6 SCAR=37.4
Pod documented
693 0 my $self = shift( @_ );
694 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
695 {
696 0 return( $self->CORE::unlock( @_ ) );
697 }
698 0 ▶ my $dbo = $self->database_object ||
Condition0%0/2
$self->database_object || (return $self->error('No database handle could be found.'))
l!l
00
MC/DC0%0/1
$self->database_object || (return $self->error('No database handle could be found.'))
$self->database_object
Truth table0%0/2
A
A= $self->database_object
Aresult
11
00
699 return( $self->error( "No database handle could be found." ) );
700 0 my $query = 'UNLOCK TABLES';
701 0 ▶ my $sth = $dbo->prepare( $query ) ||
Condition0%0/2
$dbo->prepare($query) || (return $self->error("Error while preparing query to unlock tables:\n$query", $dbo->errstr))
l!l
00
MC/DC0%0/1
$dbo->prepare($query) || (return $self->error("Error while preparing query to unlock tables:\n$query", $dbo->errstr))
$dbo->prepare($query)
Truth table0%0/2
A
A= $dbo->prepare($query)
Aresult
11
00
702 return( $self->error( "Error while preparing query to unlock tables:\n$query", $dbo->errstr() ) );
703 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
704 {
705 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to unlock tables:\n$query", $sth->errstr) 0 0 0
706 return( $self->error( "Error while executing query to unlock tables:\n$query", $sth->errstr() ) );
707 }
708 0 return( $sth );
709 }
710  
711 # NOTE: DESTROY
712 DESTROY
713 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
714 # Do nothing
715 # DB::Object::Tables are never destroyed.
716 # They are just gateway to tables, and they are cached by DB::Object::Postgres::table()
717 # print( STDERR "DESTROY'ing table $self ($self->{ 'table' })\n" );
718 };
719  
720 1 1;
721 # NOTE: POD
722 __END__
« lib/DB/Object.pm ↑ Summary blib/lib/DB/Object/Postgres/Query.pm »