lib/DB/Object/Postgres/Tables.pm

filter: Statementstmt 8.7% 23 / 263 Branchbran 0.0% 0 / 168 Conditioncond 0.0% 0 / 117 MC/DCmcdc 0.0% 0 / 62 Subroutinesub 25.0% 7 / 28 Podpod 100% 20 / 20 totaltotal 7.6% 50 / 658 CC 137 SCAR 66.4
CC
137
Cov
4%
structure
2162.0
create
756.0
disable_trigger
110.0
CRAP 765.0
« blib/lib/DB/Object/Query.pm ↑ Summary blib/lib/DB/Object/Postgres/Query.pm »
1 # -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## Database Object Interface - ~/lib/DB/Object/Postgres/Tables.pm
4 ## Version v1.1.1
5 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2017/07/19
8 ## Modified 2026/08/05
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.1';
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=10 SCAR=47.0
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 # This feature exists only since version 8.1
179 0 ▶ unless( $self->database_object->version >= version->declare( '8.1' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->database_object->version >= "version"->declare("8.1")) 0 0 0
180 {
181 0 return( $self->error( "Disabling trigger on a table requires PostgreSQL version 8.1 or higher." ) );
182 }
183 0 my $query = 'ALTER TABLE ' . $table . ' DISABLE TRIGGER ';
184 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
185 {
186 0 $query .= $opts->{name};
187 }
188 else
189 {
190 0 ▶ $query .= $opts->{all} ? 'ALL' : 'USER';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'all'} ? : 0 0 0
191 }
192 0 ▶ my $sth = $self->database_object->prepare( $query ) ||
Condition0%0/2
$self->database_object->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
$self->database_object->prepare($query) || (return $self->error("Error while preparing query to disable trigger for 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
193 return( $self->error( "Error while preparing query to disable trigger for table '$table':\n$query", $self->database_object->errstr() ) );
194 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
195 {
196 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
197 return( $self->error( "Error while executing query to disable trigger for table '$table':\n$query", $sth->errstr() ) );
198 }
199 0 return( $sth );
200 }
201  
202 sub drop
203 0 ▶{
Subroutine drop: not called CC=9 SCAR=45.0
Pod documented
204 0 my $self = shift( @_ );
205 my $table = $self->{table} ||
206 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
207 0 my $opts = $self->_get_args_as_hash( @_ );
208 0 my $query = 'DROP TABLE';
209 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
210 0 $query .= " $table";
211 0 ▶ if( $opts->{cascade} )
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'cascade'}) { } 0 0 0
elsif ($opts->{'restrict'}) { } 0 0 0
212 {
213 0 $query .= ' CASCADE';
214 }
215 ## Default Postgres behavior
216 elsif( $opts->{restrict} )
217 {
218 0 $query .= ' RESTRICT';
219 }
220 0 ▶ my $sth = $self->database_object->prepare( $query ) ||
Condition0%0/2
$self->database_object->prepare($query) || (return $self->error("Error while preparing query to drop 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 drop 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
221 return( $self->error( "Error while preparing query to drop table '$table':\n$query", $self->database_object->errstr() ) );
222 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
223 {
224 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
225 return( $self->error( "Error while executing query to drop table '$table':\n$query", $sth->errstr() ) );
226 }
227 0 $self->reset_structure;
228 0 return( $sth );
229 }
230  
231 # <https://www.postgresql.org/docs/10/sql-altertable.html>
232 sub enable_trigger
233 0 ▶{
Subroutine enable_trigger: not called CC=10 SCAR=47.0
Pod documented
234 0 my $self = shift( @_ );
235 my $table = $self->{table} ||
236 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
237 0 my $opts = $self->_get_args_as_hash( @_ );
238 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
239 # This feature exists only since version 8.1
240 0 ▶ unless( $self->database_object->version >= version->declare( '8.1' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->database_object->version >= "version"->declare("8.1")) 0 0 0
241 {
242 0 return( $self->error( "Enabling trigger on a table requires PostgreSQL version 8.1 or higher." ) );
243 }
244 0 my $query = 'ALTER TABLE ' . $table . ' ENABLE TRIGGER ';
245 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
246 {
247 0 $query .= $opts->{name};
248 }
249 else
250 {
251 0 ▶ $query .= $opts->{all} ? 'ALL' : 'USER';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'all'} ? : 0 0 0
252 }
253 0 ▶ my $sth = $self->database_object->prepare( $query ) ||
Condition0%0/2
$self->database_object->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
$self->database_object->prepare($query) || (return $self->error("Error while preparing query to disable trigger for 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
254 return( $self->error( "Error while preparing query to disable trigger for table '$table':\n$query", $self->database_object->errstr() ) );
255 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
256 {
257 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
258 return( $self->error( "Error while executing query to disable trigger for table '$table':\n$query", $sth->errstr() ) );
259 }
260 0 return( $sth );
261 }
262  
263 sub exists
264 0 ▶{
Subroutine exists: not called CC=2 SCAR=17.9
Pod documented
265 0 my $self = shift( @_ );
266 0 ▶ return( $self->table_exists( @_ ? shift( @_ ) : $self->name ) );
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
267 }
268  
269 # NOTE: sub fields is inherited from DB::Object::Tables
270 # sub fields
271  
272 sub lock
273 0 ▶{
Subroutine lock: not called CC=9 SCAR=45.0
Pod documented
274 0 my $self = shift( @_ );
275 0 my $table = $self->{table};
276 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
277 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
278 # If this is a SELECT ... FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE and FOR KEY SHAR, we redirect to the query builder
279 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 SHARE|ROW SHARE|ROW EXCLUSIVE|SHARE UPDATE EXCLUSIVE|SHARE|SHARE ROW EXCLUSIVE|EXCLUSIVE|ACCESS EXCLUSIVE)$/i) { } 0 0 0
280 {
281 0 return( $self->lock_select( $opt ) );
282 }
283 elsif( $opt !~ /^(ACCESS SHARE|ROW SHARE|ROW EXCLUSIVE|SHARE UPDATE EXCLUSIVE|SHARE|SHARE ROW EXCLUSIVE|EXCLUSIVE|ACCESS EXCLUSIVE)$/i )
284 {
285 0 return( $self->error( "Bad table '$table' locking option '$opt'." ) );
286 }
287 0 my $query = sprintf( 'LOCK TABLE %s IN %s MODE', $table, $opt );
288 0 ▶ $query .= " NOWAIT" if( $nowait );
Branch: 1 branch
BranchTrueFalseTotal
if $nowait 0 0 0
289 0 ▶ my $sth = $self->database_object->prepare( $query ) ||
Condition0%0/2
$self->database_object->prepare($query) || (return $self->error("Error while preparing query to do tables locking:\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 do tables locking:\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
290 return( $self->error( "Error while preparing query to do tables locking:\n$query", $self->database_object->errstr() ) );
291 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
292 {
293 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
294 return( $self->error( "Error while executing query to do tables locking:\n$query", $sth->errstr() ) );
295 }
296 0 return( $sth );
297 }
298  
299 sub lock_select
300 0 ▶{
Subroutine lock_select: not called CC=5 SCAR=34.0
Pod documented
301 0 my $self = shift( @_ );
302 0 my $q = $self->_reset_query;
303 # Void
304 0 ▶ return( $q->lock( @_ ) ) if( !defined( wantarray() ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
305 0 ▶ if( wantarray() )
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
306 {
307 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
308 0 return( @val );
309 }
310 else
311 {
312 0 my $val = $q->lock( @_ );
313 0 ▶ return( $self->pass_error( $q->error ) ) if( !defined( $val ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
314 0 return( $val );
315 }
316 }
317  
318 # NOTE: sub name is inherited from DB::Object::Tables
319 # sub name
320  
321 # NOTE: sub null is inherited from DB::Object::Tables
322 # sub null
323  
324 # NOTE: sub primary is inherited from DB::Object::Tables
325 # sub primary
326  
327 sub on_conflict
328 0 ▶{
Subroutine on_conflict: not called CC=5 SCAR=34.0
Pod documented
329 0 my $self = shift( @_ );
330 0 my $q = $self->_reset_query;
331 # Void
332 0 ▶ return( $q->on_conflict( @_ ) ) if( !defined( wantarray() ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
333 0 ▶ if( wantarray() )
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
334 {
335 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
336 0 return( @val );
337 }
338 else
339 {
340 0 my $val = $q->on_conflict( @_ );
341 0 ▶ return( $self->pass_error( $q->error ) ) if( !defined( $val ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
342 0 return( $val );
343 }
344 }
345  
346 0 ▶sub optimize { return( shift->error( "optimize() is not implemented PostgreSQL." ) ); }
Subroutine optimize: not called CC=1 SCAR=6.9
Pod documented
347  
348 0 ▶sub parent { return( shift->_set_get_scalar( 'parent', @_ ) ); }
Subroutine parent: not called CC=1 SCAR=6.9
Pod documented
349  
350 sub qualified_name
351 0 ▶{
Subroutine qualified_name: not called CC=4 SCAR=30.0
Pod documented
352 0 my $self = shift( @_ );
353 0 my @val = ();
354 0 ▶ CORE::push( @val, $self->database_object->database ) if( $self->{prefixed} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prefixed"} > 2 0 0 0
355 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
356 0 CORE::push( @val, $self->name );
357 0 return( CORE::join( '.', @val ) );
358 }
359  
360 sub rename
361 0 ▶{
Subroutine rename: not called CC=7 SCAR=40.3
Pod documented
362 0 my $self = shift( @_ );
363 my $table = $self->{table} ||
364 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
365 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
366 return( $self->error( "No new table name was provided to rename table '$table'." ) );
367 0 ▶ if( $new !~ /^[\w\_]+$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($new =~ /^[\w\_]+$/) 0 0 0
368 {
369 0 return( $self->error( "Bad new table name '$new'." ) );
370 }
371 0 my $query = "ALTER TABLE $table RENAME TO $new";
372 0 ▶ my $sth = $self->database_object->prepare( $query ) ||
Condition0%0/2
$self->database_object->prepare($query) || (return $self->error("Error while preparing query to rename table '${table}' into '${new}':\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 rename table '${table}' into '${new}':\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
373 return( $self->error( "Error while preparing query to rename table '$table' into '$new':\n$query", $self->database_object->errstr() ) );
374 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
375 {
376 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
377 return( $self->error( "Error while executing query to rename table '$table' into '$new':\n$query", $sth->errstr() ) );
378 }
379 0 $self->reset_structure;
380 0 return( $sth );
381 }
382  
383 0 ▶sub repair { return( shift->error( "repair() is not implemented PostgreSQL." ) ); }
Subroutine repair: not called CC=1 SCAR=6.9
Pod documented
384  
385 0 ▶sub reset { return( shift->DB::Object::Tables::reset( @_ ) ); }
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
386  
387 0 ▶sub stat { return( shift->error( "stat() is not implemented PostgreSQL." ) ); }
Subroutine stat: not called CC=1 SCAR=6.9
Pod documented
388  
389 sub structure
390 0 ▶{
Subroutine structure: not called CC=46 SCAR=76.8
Pod documented
391 0 my $self = shift( @_ );
392 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
393 my $table = $self->{table} ||
394 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
395 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
396 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
397 0 my $types_dict = $self->database_object->datatype_dict;
398 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
399 0 my $q = $self->_reset_query;
400 # <https://stackoverflow.com/questions/6777456/list-all-index-names-column-names-and-its-table-name-of-a-postgresql-database>
401  
402 # If we have a cache, use it instead of reprocessing it.
403 # <https://stackoverflow.com/questions/52376045/why-does-atttypmod-differ-from-character-maximum-length>
404 # my $query = <<EOT;
405 # SELECT
406 # a.table_schema AS "schema_name"
407 # ,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"
408 # ,a.column_name AS "field"
409 # ,a.ordinal_position AS "field_num"
410 # ,a.column_default AS "default"
411 # ,a.*
412 # FROM information_schema.columns a
413 # LEFT JOIN pg_catalog.pg_class c ON c.relname = a.table_name
414 # WHERE a.table_name = ?
415 # ORDER BY a.ordinal_position
416 # EOT
417 # <https://www.postgresql.org/docs/14/catalog-pg-attrdef.html>
418 # We could use:
419 # generate_subscripts(i.indkey, 1)
420 # instead of:
421 # generate_series(1,array_upper(string_to_array(i.indkey::text, ' ' )::int2[],1))
422 # but this is not supported by PostgreSQL v8.0; only from v8.4 onward
423 0 my $query = <<EOT;
424 SELECT
425 n.nspname AS "schema_name"
426 ,c.relname AS "table_name"
427 ,CASE c.relkind
428 WHEN 'r' THEN 'table'
429 WHEN 'v' THEN 'view'
430 WHEN 'm' THEN 'materialized view'
431 WHEN 's' THEN 'special'
432 WHEN 'f' THEN 'foreign table'
433 WHEN 'p' THEN 'table'
434 END as "table_type"
435 ,a.attname AS "field"
436 ,a.attnum AS "field_num"
437 ,CASE
438 WHEN a.atttypmod = -1 THEN null
439 WHEN t.oid IN (1042, 1043) THEN a.atttypmod - 4
440 WHEN t.oid IN (1560, 1562) THEN a.atttypmod
441 ELSE NULL
442 END AS "character_maximum_length"
443 ,CASE SUBSTRING(t.typname,1,1)
444 WHEN '_' THEN SUBSTRING(t.typname,2)
445 ELSE t.typname
446 END AS "data_type"
447 ,pg_catalog.format_type(a.atttypid,a.atttypmod) AS "format_type"
448 ,(a.attndims > 0 OR pg_catalog.format_type(a.atttypid,a.atttypmod) LIKE '%[]') AS "is_array"
449 ,CASE a.attnotnull
450 WHEN FALSE THEN TRUE
451 ELSE FALSE
452 END AS "is_nullable"
453 ,COALESCE(i.indisprimary,false) AS "is_primary"
454 ,COALESCE(i.indisunique,false) AS "is_unique"
455 ,r.oid IS NOT NULL AS "is_foreign"
456 ,r2.oid IS NOT NULL AS "is_check"
457 ,(SELECT pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid) FROM pg_attrdef
458 WHERE pg_attrdef.adrelid = c.oid
459 AND pg_attrdef.adnum = a.attnum) AS "default"
460 ,(SELECT pg_description.description FROM pg_description
461 WHERE pg_description.objoid = c.oid AND pg_description.objsubid = a.attnum) AS "comment"
462 ,(SELECT c2.relname FROM pg_inherits LEFT JOIN pg_class c2 ON c2.oid = pg_inherits.inhparent
463 WHERE pg_inherits.inhrelid = c.oid) AS "table_parent"
464 ,c2.relname AS "index_name"
465 ,i.indnatts AS "index_n_columns"
466 ,i.indkey AS "index_columns"
467 ,ARRAY(
468 SELECT pg_get_indexdef(i.indexrelid, k, TRUE)
469 FROM generate_series(1,array_upper(string_to_array(i.indkey::text, ' ' )::int2[],1)) AS s(k)
470 ORDER BY k
471 ) AS "index_keys"
472 ,r.conname AS "foreign_name"
473 ,CASE
474 WHEN r.conindid > 0 THEN (SELECT c3.relname FROM pg_catalog.pg_class c3 WHERE c3.oid = r.conindid)
475 ELSE NULL
476 END AS "foreign_index_name"
477 ,CASE
478 WHEN r.confrelid > 0 THEN (SELECT c4.relname FROM pg_catalog.pg_class c4 WHERE c4.oid = r.confrelid)
479 ELSE NULL
480 END AS "foreign_table"
481 ,CASE r.confupdtype
482 WHEN 'a' THEN 'nothing'
483 WHEN 'r' THEN 'restrict'
484 WHEN 'c' THEN 'cascade'
485 WHEN 'n' THEN 'null'
486 WHEN 'd' THEN 'default'
487 ELSE NULL
488 END AS "foreign_update_action"
489 ,CASE r.confdeltype
490 WHEN 'a' THEN 'nothing'
491 WHEN 'r' THEN 'restrict'
492 WHEN 'c' THEN 'cascade'
493 WHEN 'n' THEN 'null'
494 WHEN 'd' THEN 'default'
495 ELSE NULL
496 END AS "foreign_delete_action"
497 ,CASE r.confmatchtype
498 WHEN 'f' THEN 'full'
499 WHEN 'p' THEN 'partial'
500 WHEN 's' THEN 'simple'
501 ELSE NULL
502 END AS "foreign_match"
503 ,r.conkey AS "foreign_columns"
504 ,ARRAY(
505 SELECT a2.attname
506 FROM generate_series(1,array_upper(r.conkey,1)) AS z(j), pg_catalog.pg_attribute a2
507 WHERE a2.attrelid = c.oid AND a2.attnum = r.conkey[j]
508 ) AS "foreign_keys"
509 ,CASE
510 WHEN r.oid IS NOT NULL THEN pg_get_constraintdef(r.oid,TRUE)
511 ELSE NULL
512 END AS "foreign_expression"
513 ,r2.conname AS "check_name"
514 ,CASE
515 WHEN r2.conindid > 0 THEN (SELECT c3.relname FROM pg_catalog.pg_class c3 WHERE c3.oid = r2.conindid)
516 ELSE NULL
517 END AS "check_index_name"
518 ,r2.conkey AS "check_columns"
519 ,ARRAY(
520 SELECT a2.attname
521 FROM generate_series(1,array_upper(r2.conkey,1)) AS z(j), pg_catalog.pg_attribute a2
522 WHERE a2.attrelid = c.oid AND a2.attnum = r2.conkey[j]
523 ) AS "check_keys"
524 ,CASE
525 WHEN r2.oid IS NOT NULL THEN pg_get_constraintdef(r2.oid,TRUE)
526 ELSE NULL
527 END AS "check_expression"
528 FROM pg_catalog.pg_class c
529 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
530 LEFT JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
531 LEFT JOIN pg_catalog.pg_type t ON t.oid = a.atttypid
532 LEFT JOIN pg_catalog.pg_index i ON i.indrelid = c.oid AND a.attnum = ANY (i.indkey::int[])
533 LEFT JOIN pg_catalog.pg_class c2 ON c2.oid = i.indexrelid
534 LEFT JOIN pg_catalog.pg_constraint r ON r.conrelid = c.oid AND r.contype = 'f' AND a.attnum = ANY (r.conkey)
535 LEFT JOIN pg_catalog.pg_constraint r2 ON r2.conrelid = c.oid AND r2.contype = 'c' AND a.attnum = ANY (r2.conkey)
536 WHERE
537 c.relname = ? AND
538 a.attnum > 0 AND
539 NOT a.attisdropped
540 ORDER BY a.attnum
541 EOT
542 # http://www.postgresql.org/docs/9.3/interactive/infoschema-columns.html
543 # select * from information_schema.columns where table_name = 'address'
544 0 ▶ my $sth = $self->database_object->prepare_cached( $query ) ||
Condition0%0/2
$self->database_object->prepare_cached($query) || (return $self->error("Error while preparing query to get table '${table}' columns specification: ", $self->database_object->errstr))
l!l
00
MC/DC0%0/1
$self->database_object->prepare_cached($query) || (return $self->error("Error while preparing query to get table '${table}' columns specification: ", $self->database_object->errstr))
$self->database_object->prepare_cached($query)
Truth table0%0/2
A
A= $self->database_object->prepare_cached($query)
Aresult
11
00
545 return( $self->error( "Error while preparing query to get table '$table' columns specification: ", $self->database_object->errstr ) );
546 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
547 return( $self->error( "Error while executing query to get table '$table' columns specification: ", $sth->errstr ) );
548 0 my $check = {};
549 0 my $foreign = {};
550 0 my $index = {};
551 0 my @primary = ();
552 0 my $ref = '';
553 0 my $c = 0;
554 # Mysql: field, type, null, key, default, extra
555 # Postgres: tablename, field, field_num, type, len, comment, is_nullable, key, foreign_key, default
556 0 ▶ while( $ref = $sth->fetchrow_hashref() )
Branch: 1 branch
BranchTrueFalseTotal
if ($ref = $sth->fetchrow_hashref) 0 0 0
557 {
558 my $def =
559 {
560 name => $ref->{field},
561 comment => $ref->{comment},
562 default => $ref->{default},
563 ( $ref->{check_name} ? ( check_name => $ref->{check_name} ) : () ),
564 ( $ref->{foreign_name} ? ( foreign_name => $ref->{foreign_name} ) : () ),
565 ( $ref->{index_name} ? ( index_name => $ref->{index_name} ) : () ),
566 is_array => ( $ref->{is_array} ? 1 : 0 ),
567 is_check => ( $ref->{is_check} ? 1 : 0 ),
568 is_foreign => ( $ref->{is_foreign} ? 1 : 0 ),
569 is_nullable => ( $ref->{is_nullable} ? 1 : 0 ),
570 is_primary => ( $ref->{is_primary} ? 1 : 0 ),
571 is_unique => ( $ref->{is_unique} ? 1 : 0 ),
572 pos => $ref->{field_num},
573 # query_object => $q,
574 size => $ref->{character_maximum_length},
575 # When the field is an array, the data type will start with an underscore
576 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
577 # table_object => $self,
578 };
579 0 ▶ $self->{type} = $ref->{table_type} if( !$self->{type} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"type"} 0 0 0
580 0 ▶ $self->{schema} = $ref->{schema_name} if( !$self->{schema} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"schema"} 0 0 0
581 0 ▶ $self->{parent} = $ref->{table_parent} if( !$self->{table_parent} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"table_parent"} 0 0 0
582 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
583 !CORE::exists( $index->{ $ref->{index_name} } ) )
584 {
585 my $constraint = $self->new_index(
586 is_primary => $ref->{is_primary},
587 is_unique => $ref->{is_unique},
588 fields => $ref->{index_keys},
589 name => $ref->{index_name},
590 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
591 0 $index->{ $ref->{index_name} } = $constraint;
592 }
593 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
594 !CORE::exists( $check->{ $ref->{check_name} } ) )
595 {
596 my $constraint = $self->new_check(
597 expr => $ref->{check_expression},
598 fields => $ref->{check_keys},
599 name => $ref->{check_name},
600 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
601 0 $check->{ $ref->{check_name} } = $constraint;
602 }
603 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
604 !CORE::exists( $foreign->{ $ref->{foreign_name} } ) )
605 {
606 my $constraint = $self->new_foreign(
607 expr => $ref->{foreign_expression},
608 match => $ref->{foreign_match},
609 on_delete => $ref->{foreign_delete_action},
610 on_update => $ref->{foreign_update_action},
611 table => $ref->{foreign_table},
612 fields => $ref->{foreign_keys},
613 name => $ref->{foreign_name},
614 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
615 0 $foreign->{ $ref->{foreign_name} } = $constraint;
616 }
617  
618 0 my( $const_def, $dict );
619 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
620 CORE::exists( $types_dict->{ "$def->{type}array" } ) )
621 {
622 0 $const_def = $types_dict->{ "$def->{type}array" };
623 }
624 elsif( CORE::exists( $types_dict->{ $def->{type} } ) )
625 {
626 0 $const_def = $types_dict->{ $def->{type} };
627 }
628 else
629 {
630 # Get the constant
631 0 DATA_TYPE_RE: foreach my $type ( keys( %$types_dict ) )
632 {
633 0 ▶ if( $def->{type} =~ /$types_dict->{ $type }->{re}/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"type"} =~ /$types_dict->{$type}{"re"}/i) 0 0 0
634 {
635 0 $const_def = $types_dict->{ $type };
636 0 last DATA_TYPE_RE;
637 }
638 }
639 }
640 0 ▶ if( defined( $const_def ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $const_def) 0 0 0
641 {
642 0 my $const_keys = [keys( %$const_def )];
643 0 my $dict = {};
644 0 @$dict{ @$const_keys } = @$const_def{ @$const_keys };
645 0 $def->{datatype} = $dict;
646 # $def->{type} = $dict->{type};
647 }
648 0 $def->{query_object} = $q;
649 0 $def->{table_object} = $self;
650 0 my @define = ( $def->{type} );
651 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
652 0 ▶ push( @define, "NOT NULL" ) if( !$def->{is_nullable} );
Branch: 1 branch
BranchTrueFalseTotal
unless $def->{"is_nullable"} 0 0 0
653 0 ▶ push( @primary, $def->{name} ) if( $ref->{key} );
Branch: 1 branch
BranchTrueFalseTotal
if $ref->{"key"} 0 0 0
654 0 $struct->{ $def->{name} } = CORE::join( ' ', @define );
655 0 $def->{debug} = $self->debug;
656 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
657 return( $self->pass_error( DB::Object::Fields::Field->error ) );
658 0 $fields->{ $def->{name} } = $field;
659 }
660 0 $sth->finish;
661 0 ▶ if( @primary )
Branch: 1 branch
BranchTrueFalseTotal
if (@primary) 0 0 0
662 {
663 0 $self->{primary} = \@primary;
664 }
665 0 $self->{check} = $check;
666 0 $self->{fields} = $fields;
667 0 $self->{foreign} = $foreign;
668 0 $self->{indexes} = $index;
669 0 $self->{_cache_structure} = $struct;
670 0 return( $self->_clone( $struct ) );
671 }
672  
673 0 ▶sub table_info { return( shift->database_object->table_info( @_ ) ); }
Subroutine table_info: not called CC=1 SCAR=6.9
Pod documented
674  
675 sub unlock
676 0 ▶{
Subroutine unlock: not called CC=5 SCAR=34.0
Pod documented
677 0 my $self = shift( @_ );
678 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
679 {
680 0 return( $self->CORE::unlock( @_ ) );
681 }
682 0 my $query = 'UNLOCK TABLES';
683 0 ▶ my $sth = $self->database_object->prepare( $query ) ||
Condition0%0/2
$self->database_object->prepare($query) || (return $self->error("Error while preparing query to unlock tables:\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 unlock tables:\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
684 return( $self->error( "Error while preparing query to unlock tables:\n$query", $self->database_object->errstr() ) );
685 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
686 {
687 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
688 return( $self->error( "Error while executing query to unlock tables:\n$query", $sth->errstr() ) );
689 }
690 0 return( $sth );
691 }
692  
693 # NOTE: DESTROY
694 DESTROY
695 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
696 # Do nothing
697 # DB::Object::Tables are never destroyed.
698 # They are just gateway to tables, and they are cached by DB::Object::Postgres::table()
699 # print( STDERR "DESTROY'ing table $self ($self->{ 'table' })\n" );
700 };
701  
702 1 1;
703 # NOTE: POD
704 __END__
« blib/lib/DB/Object/Query.pm ↑ Summary blib/lib/DB/Object/Postgres/Query.pm »