lib/DB/Object/Mysql/Tables.pm

filter: Statementstmt 7.8% 23 / 293 Branchbran 0.0% 0 / 194 Conditioncond 0.0% 0 / 114 MC/DCmcdc 0.0% 0 / 59 Subroutinesub 29.1% 7 / 24 Podpod 100% 15 / 15 totaltotal 6.4% 45 / 699 CC 152 SCAR 65.4
CC
152
Cov
4%
structure
1980.0
create
756.0
lock
380.0
CRAP 689.8
« blib/lib/DB/Object/Postgres/Query.pm ↑ Summary lib/DB/Object/SQLite/Query.pm »
1 # -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## SQL API Abstraction - ~/lib/DB/Object/Mysql/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::Mysql::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 $TYPE_TO_CONSTANT );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 # <https://dev.mysql.com/doc/refman/8.0/en/data-types.html>
27 # the 'constant' property in the dictionary hash is added in structure()
28 # See also: SELECT DISTINCT(data_type) FROM information_schema.columns ORDER by data_type
29 # but this does not provide a complete list of datatype
30 1 our $TYPE_TO_CONSTANT =
31 {
32 qr/^(bit)/ => { constant => '', name => 'SQL_BIT', type => 'bit' },
33 qr/^(tinyint)/ => { constant => '', name => 'SQL_TINYINT', type => 'tinyint' },
34 qr/^(smallint)/ => { constant => '', name => 'SQL_SMALLINT', type => 'smallint' },
35 qr/^(mediumint)/ => { constant => '', name => 'SQL_BIT', type => 'mediumint' },
36 qr/^(integer|int)/ => { constant => '', name => 'SQL_INTEGER', type => 'int' },
37 qr/^(bigint)/ => { constant => '', name => 'SQL_BIGINT', type => 'bigint' },
38 qr/^(dec|decimal)/ => { constant => '', name => 'SQL_DECIMAL', type => 'decimal' },
39 qr/^(float)/ => { constant => '', name => 'SQL_FLOAT', type => 'float' },
40 qr/^(double\s+precision|double)/ => { constant => '', name => 'SQL_DOUBLE', type => 'double' },
41 qr/^(date)\b/ => { constant => '', name => 'SQL_DATE', type => 'date' },
42 qr/^(datetime)/ => { constant => '', name => 'SQL_DATETIME', type => 'datetime' },
43 qr/^(timestamp)/ => { constant => '', name => 'SQL_TIMESTAMP', type => 'timestamp' },
44 qr/^(year)/ => { constant => '', name => 'SQL_INTERVAL_YEAR', type => 'year' },
45 qr/^(character|char)\b(?![[:blank:]]+varying)/ => { constant => '', name => 'SQL_VARCHAR', type => 'varchar' },
46 qr/^(character varying|varchar)/ => { constant => '', name => 'SQL_WVARCHAR', type => 'varchar' },
47 qr/^blob/ => { constant => '', name => 'SQL_BLOB', type => 'blob' },
48 qr/^text/ => { constant => '', name => 'SQL_LONGVARCHAR', type => 'text' },
49 qr/^binary/ => { constant => '', name => 'SQL_BINARY', type => 'binary' },
50 qr/^varbinary/ => { constant => '', name => 'SQL_VARBINARY', type => 'varbinary' },
51 qr/^(tinyblob)\b/ => { constant => '', name => 'SQL_BLOB', type => 'tinyblob' },
52 qr/^(mediumblob)\b/ => { constant => '', name => 'SQL_BLOB', type => 'mediumblob' },
53 qr/^(longlob)\b/ => { constant => '', name => 'SQL_BLOB', type => 'longlob' },
54 qr/^(tinytext)\b/ => { constant => '', name => 'SQL_LONGVARCHAR', type => 'tinytext' },
55 qr/^(mediumtext)\b/ => { constant => '', name => 'SQL_LONGVARCHAR', type => 'mediumtext' },
56 qr/^(longtext)\b/ => { constant => '', name => 'SQL_LONGVARCHAR', type => 'longtext' },
57 qr/^(enum)\b/ => { constant => '', name => 'SQL_UNKNOWN_TYPE', type => 'enum' },
58 qr/^(set)\b/ => { constant => '', name => 'SQL_UNKNOWN_TYPE', type => 'set' },
59 qr/^(geometry)\b/ => { constant => '', name => 'SQL_UNKNOWN_TYPE', type => 'geometry' },
60 qr/^(linestring)\b/ => { constant => '', name => 'SQL_UNKNOWN_TYPE', type => 'linestring' },
61 qr/^(point)\b/ => { constant => '', name => 'SQL_UNKNOWN_TYPE', type => 'point' },
62 qr/^(polygon)\b/ => { constant => '', name => 'SQL_UNKNOWN_TYPE', type => 'polygon' },
63 qr/^(curve)\b/ => { constant => '', name => 'SQL_UNKNOWN_TYPE', type => 'curve' },
64 qr/^(multipolygon)\b/ => { constant => '', name => 'SQL_UNKNOWN_TYPE', type => 'multipolygon' },
65 qr/^(multilinestring)\b/ => { constant => '', name => 'SQL_UNKNOWN_TYPE', type => 'multilinestring' },
66 qr/^(multipoint)\b/ => { constant => '', name => 'SQL_UNKNOWN_TYPE', type => 'multipoint' },
67 qr/^(geometrycollection)\b/ => { constant => '', name => 'SQL_UNKNOWN_TYPE', type => 'geometrycollection' },
68 qr/^(multicurve)\b/ => { constant => '', name => 'SQL_UNKNOWN_TYPE', type => 'multicurve' },
69 qr/^(json)\b/ => { constant => '', name => 'SQL_UNKNOWN_TYPE', type => 'json' },
70 };
71 1 our $DEBUG = 0;
72 1 our $VERSION = 'v1.1.2';
73 };
74  
75 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 sub init
79 0 ▶{
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
80 0 return( shift->DB::Object::Tables::init( @_ ) );
81 }
82  
83 # NOTE] sub alter is inherited from DB::Object::Tables
84 # sub alter
85  
86 sub check
87 0 ▶{
Subroutine check: not called CC=9 SCAR=45.0
Pod documented
88 0 my $self = shift( @_ );
89 my $table = $self->{table} ||
90 0 ▶ return( $self->error( 'No table was provided to check' ) );
Condition0%0/2
$self->{'table'} || (return $self->error('No table was provided to check'))
l!l
00
MC/DC0%0/1
$self->{'table'} || (return $self->error('No table was provided to check'))
$self->{'table'}
Truth table0%0/2
A
A= $self->{'table'}
Aresult
11
00
91 0 ▶ my $opt = shift( @_ ) if( @_ == 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 0 0
92 0 my %arg = ( @_ );
93 0 ▶ $opt = \%arg if( !$opt && %arg );
Branch: 1 branch
BranchTrueFalseTotal
if not $opt and %arg 0 0 0
Condition0%0/3
not $opt and %arg
!ll&&!rl&&r
000
MC/DC0%0/2
not $opt and %arg
not $opt %arg
Truth table0%0/3
not(A) and B
A= not $optB= %arg
ABresult
0X0
100
111
94 0 my $query = "CHECK TABLE $table";
95 0 ▶ $query .= " TYPE = QUICK" if( $opt->{ 'quick' } );
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"quick"} 0 0 0
96 0 ▶ my $sth = $self->prepare( $query ) ||
Condition0%0/2
$self->prepare($query) || (return $self->error("Error while preparing query to check table '${table}':\n$query\n", $self->errstr))
l!l
00
MC/DC0%0/1
$self->prepare($query) || (return $self->error("Error while preparing query to check table '${table}':\n$query\n", $self->errstr))
$self->prepare($query)
Truth table0%0/2
A
A= $self->prepare($query)
Aresult
11
00
97 return( $self->error( "Error while preparing query to check table '$table':\n$query\n", $self->errstr() ) );
98 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
99 {
100 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to check table '${table}':\n$query\n", $sth->errstr) 0 0 0
101 return( $self->error( "Error while executing query to check table '$table':\n$query\n", $sth->errstr() ) );
102 }
103 0 return( $sth );
104 }
105  
106 sub create
107 0 ▶{
Subroutine create: not called CC=27 SCAR=66.3
Pod documented
108 0 my $self = shift( @_ );
109 # $tbl->create( [ 'ROW 1', 'ROW 2'... ], { 'temporary' => 1, 'TYPE' => ISAM }, $obj );
110 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
111 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
112 0 my $sth = shift( @_ );
113 0 my $table = $self->{table};
114 # Set temporary in the object, so we can use it to recreate the table creation info as string:
115 # $table->create( [ ... ], { ... }, $obj )->as_string();
116 0 my $temp = $self->{temporary} = delete( $opt->{temporary} );
117 # Check possible options
118 0 my $allowed =
119 {
120 type => qr/^(ISAM|MYISAM|HEAP)$/i,
121 auto_increment => qr/^(1|0)$/,
122 avg_row_length => qr/^\d+$/,
123 checksum => qr/^(1|0)$/,
124 comment => qr//,
125 max_rows => qr/^\d+$/,
126 min_rows => qr/^\d+$/,
127 pack_keys => qr/^(1|0)$/,
128 password => qr//,
129 delay_key_write => qr/^\d+$/,
130 row_format => qr/^(default|dynamic|static|compressed)$/i,
131 raid_type => qr/^(?:1|STRIPED|RAID0|RAID_CHUNKS\s*=\s*\d+|RAID_CHUNKSIZE\s*=\s*\d+)$/,
132 };
133 0 my @options = ();
134 0 my @errors = ();
135 # Avoid working for nothing, make this condition
136 0 ▶ if( %$opt )
Branch: 1 branch
BranchTrueFalseTotal
if (%$opt) 0 0 0
137 {
138 0 my %lc_opt = map{ lc( $_ ) => $opt->{ $_ } } keys( %$opt );
139 0 $opt = \%lc_opt;
140 0 foreach my $key ( keys( %$opt ) )
141 {
142 0 ▶ next if( $opt->{ $key } =~ /^\s*$/ || !exists( $allowed->{ $key } ) );
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{$key} =~ /^\s*$/ or not exists $allowed->{$key} 0 0 0
Condition0%0/3
$opt->{$key} =~ /^\s*$/ or not exists $allowed->{$key}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{$key} =~ /^\s*$/ or not exists $allowed->{$key}
$opt->{$key} =~ /^\s*$/ not exists $allowed->{$key}
Truth table0%0/3
A or not(B)
A= $opt->{$key} =~ /^\s*$/B= not exists $allowed->{$key}
ABresult
1X1
011
000
143 0 ▶ if( $opt->{ $key } !~ /$allowed->{ $key }/ )
Branch: 1 branch
BranchTrueFalseTotal
if (not $opt->{$key} =~ /$allowed->{$key}/) { } 0 0 0
144 {
145 0 push( @errors, $key );
146 }
147 else
148 {
149 0 push( @options, $key );
150 }
151 }
152 0 ▶ $opt->{comment} = "'" . quotemeta( $opt->{comment} ) . "'" if( exists( $opt->{comment} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt->{"comment"} 0 0 0
153 0 ▶ $opt->{password} = "'" . $opt->{password} . "'" if( exists( $opt->{password} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt->{"password"} 0 0 0
154 }
155 0 ▶ if( @errors )
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 0 0 0
156 {
157 0 ▶ warn( "The options '", join( ', ', @errors ), "' were either not recognized or malformed and thus were ignored." ) if( $self->_is_warnings_enabled( 'DB::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("DB::Object") 0 0 0
158 }
159 # Check statement
160 0 my $select = '';
161 0 ▶ if( $sth && ref( $sth ) && ( $sth->isa( "DB::Object::Statement" ) || $sth->can( 'as_string' ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($sth and ref $sth and $sth->isa("DB::Object::Statement") || $sth->can("as_string")) 0 0 0
Condition0%0/9
$sth and ref $sth and $sth->isa("DB::Object::Statement") || $sth->can("as_string")
!ll&&!rl&&r
000
$sth and ref $sth
!ll&&!rl&&r
000
$sth->isa("DB::Object::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::Statement") || $sth->can("as_string")
$sth ref $sth $sth->isa("DB::Object::Statement") $sth->can("as_string")
Truth table0%0/5
A and B and C or D
A= $sthB= ref $sthC= $sth->isa("DB::Object::Statement")D= $sth->can("as_string")
ABCDresult
0XXX0
10XX0
11000
111X1
11011
162 {
163 0 $select = $sth->as_string();
164 0 ▶ if( $select !~ /^\s*(?:IGNORE|REPLACE)*\s*\bSELECT\s+/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($select =~ /^\s*(?:IGNORE|REPLACE)*\s*\bSELECT\s+/) 0 0 0
165 {
166 0 return( $self->error( "SELECT statement to use to create table is invalid:\n$select" ) );
167 }
168 }
169 0 ▶ if( $self->exists() == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exists == 0) { } 0 0 0
170 {
171 0 ▶ my $query = 'CREATE ' . ( $temp ? 'TEMPORARY ' : '' ) . "TABLE $table ";
Branch: 1 branch
BranchTrueFalseTotal
$temp ? : 0 0 0
172 # Structure of table if any -
173 # structure may very well be provided using a select statement, such as:
174 # CREATE TEMPORARY TABLE ploppy TYPE=HEAP COMMENT='this is kewl' MAX_ROWS=10 SELECT * FROM some_table LIMIT 0,0
175 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
176 0 my $tdef = CORE::join( ' ', map{ "\U$_\E = $opt->{ $_ }" } @options );
177 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
178 {
179 0 return( $self->error( "Lacking table '$table' structure information to create it." ) );
180 }
181 0 $query .= join( ' ', $def, $tdef, $select );
182 0 ▶ my $new = $self->prepare( $query ) ||
Condition0%0/2
$self->prepare($query) || (return $self->error("Error while preparing query to create table '${table}':\n$query", $self->errstr))
l!l
00
MC/DC0%0/1
$self->prepare($query) || (return $self->error("Error while preparing query to create table '${table}':\n$query", $self->errstr))
$self->prepare($query)
Truth table0%0/2
A
A= $self->prepare($query)
Aresult
11
00
183 return( $self->error( "Error while preparing query to create table '$table':\n$query", $self->errstr() ) );
184 # Trick so other method may follow, such as as_string(), fetchrow(), rows()
185 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
186 {
187 # print( STDERR "create(): wantarrays in void context.\n" );
188 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
189 return( $self->error( "Error while executing query to create table '$table':\n$query", $new->errstr() ) );
190 }
191 0 $self->reset_structure;
192 0 return( $new );
193 }
194 else
195 {
196 0 return( $self->error( "Table '$table' already exists." ) );
197 }
198 }
199  
200 sub create_info
201 0 ▶{
Subroutine create_info: not called CC=10 SCAR=47.0
Pod documented
202 0 my $self = shift( @_ );
203 0 my $table = $self->{table};
204 0 ▶ $self->structure || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->structure or return $self->pass_error 0 0 0
205 0 my $struct = $self->{structure};
206 0 my $fields = $self->{fields};
207 0 my $default = $self->{default};
208 0 my $primary = $self->{primary};
209 0 my @output = ();
210 0 foreach my $field ( sort{ $fields->{ $a } <=> $fields->{ $b } } keys( %$fields ) )
211 {
212 0 push( @output, "$field $struct->{ $field }" );
213 }
214 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
215 0 my $info = $self->stat( $table );
216 0 my @opt = ();
217 0 ▶ push( @opt, "TYPE = $info->{type}" ) if( $info->{type} );
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"type"} 0 0 0
218 0 my $addons = $info->{create_options};
219 0 ▶ if( $addons )
Branch: 1 branch
BranchTrueFalseTotal
if ($addons) 0 0 0
220 {
221 0 $addons =~ s/(\A|\s+)([\w\_]+)\s*=\s*/$1\U$2\E=/g;
222 0 push( @opt, $addons );
223 }
224 0 ▶ push( @opt, "COMMENT='" . quotemeta( $info->{ 'comment' } ) . "'" ) if( $info->{comment} );
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"comment"} 0 0 0
225 0 my $str = "CREATE TABLE $table (\n\t" . CORE::join( ",\n\t", @output ) . "\n)";
226 0 ▶ $str .= ' ' . CORE::join( ' ', @opt ) if( @opt );
Branch: 1 branch
BranchTrueFalseTotal
if @opt 0 0 0
227 0 $str .= ';';
228 0 ▶ return( @output ? $str : undef() );
Branch: 1 branch
BranchTrueFalseTotal
@output ? : 0 0 0
229 }
230  
231 # NOTE: sub default is inherited from DB::Object::Tables
232 # sub default
233  
234 sub drop
235 0 ▶{
Subroutine drop: not called CC=5 SCAR=34.0
Pod documented
236 0 my $self = shift( @_ );
237 my $table = $self->{table} ||
238 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
239 0 my $query = "DROP TABLE $table";
240 0 ▶ my $sth = $self->prepare( $query ) ||
Condition0%0/2
$self->prepare($query) || (return $self->error("Error while preparing query to drop table '${table}':\n$query", $self->errstr))
l!l
00
MC/DC0%0/1
$self->prepare($query) || (return $self->error("Error while preparing query to drop table '${table}':\n$query", $self->errstr))
$self->prepare($query)
Truth table0%0/2
A
A= $self->prepare($query)
Aresult
11
00
241 return( $self->error( "Error while preparing query to drop table '$table':\n$query", $self->errstr() ) );
242 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
243 {
244 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
245 return( $self->error( "Error while executing query to drop table '$table':\n$query", $sth->errstr() ) );
246 }
247 0 $self->reset_structure;
248 0 return( $sth );
249 }
250  
251 # NOTE: exists is inherited from DB::Object::Tables
252  
253 sub lock
254 0 ▶{
Subroutine lock: not called CC=19 SCAR=59.4
Pod documented
255 0 my $self = shift( @_ );
256 # There is two arguments, the first one does not look like an exiting table name and the second is a number...
257 # It pretty much looks like a statement lock
258 0 ▶ if( @_ == 2 && ( !$self->exists( $_[ 0 ] ) || $_[ 1 ] =~ /^\d+$/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and !$self->exists($_[0]) || $_[1] =~ /^\d+$/) 0 0 0
Condition0%0/6
@_ == 2 and !$self->exists($_[0]) || $_[1] =~ /^\d+$/
!ll&&!rl&&r
000
not $self->exists($_[0]) or $_[1] =~ /^\d+$/
l!l&&r!l&&!r
000
MC/DC0%0/3
@_ == 2 and !$self->exists($_[0]) || $_[1] =~ /^\d+$/
@_ == 2 not $self->exists($_[0]) $_[1] =~ /^\d+$/
Truth table0%0/4
A and not(B) or C
A= @_ == 2B= not $self->exists($_[0])C= $_[1] =~ /^\d+$/
ABCresult
0XX0
1000
11X1
1011
259 {
260 0 return( $self->SUPER::lock( @_ ) );
261 }
262 0 my @tables = ();
263 my $chk_opt = sub
264 0 ▶ {
Subroutine __ANON__: not called CC=5 SCAR=34.0
265 0 my $self = shift( @_ );
266 0 my $table = shift( @_ );
267 0 my $opt = shift( @_ );
268 0 my $alias = shift( @_ );
269 0 ▶ if( $opt !~ /^(READ|READ\s+LOCAL|(LOW_PRIORITY\s+)?WRITE)$/i )
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt =~ /^(READ|READ\s+LOCAL|(LOW_PRIORITY\s+)?WRITE)$/i) 0 0 0
270 {
271 0 return( $self->error( "Bad table '$table' locking option '$opt'." ) );
272 }
273 0 ▶ if( $alias )
Branch: 1 branch
BranchTrueFalseTotal
if ($alias) 0 0 0
274 {
275 0 ▶ if( $self->_simple_exist( $alias ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_simple_exist($alias)) { } 0 0 0
elsif (not $alias =~ /^[\w\_]+$/) { } 0 0 0
276 {
277 0 return( $self->error( "Alias '$alias' for table '$table' seems to match an already existing table." ) );
278 }
279 elsif( $alias !~ /^[\w\_]+$/ )
280 {
281 0 return( $self->error( "Illegal characters for table '$table' alias name '$alias'." ) );
282 }
283 }
284 0 return( 1 );
285 0 };
286 # No parameter, so we default to WRITE for read/write access, but with a low priority
287 0 ▶ if( !@_ )
Branch: 2 branches
BranchTrueFalseTotal
if (not @_) { } 0 0 0
elsif (@_ == 1) { } 0 0 0
288 {
289 0 push( @tables, "$self->{table} LOW_PRIORITY WRITE" );
290 }
291 elsif( @_ == 1 )
292 {
293 0 my $arg = shift( @_ );
294 0 my $alias = '';
295 0 my $opt = '';
296 # Array reference means 'table alias', 'access mode'
297 0 ▶ if( $self->_is_array( $arg ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_array($arg)) { } 0 0 0
298 {
299 0 ( $alias, $opt ) = @$arg;
300 }
301 # Otherwise just 'access mode'
302 else
303 {
304 0 $opt = $arg;
305 }
306 0 ▶ $opt ||= 'LOW_PRIORITY WRITE';
Condition0%0/2
$opt ||= "LOW_PRIORITY WRITE"
l!l
00
MC/DC0%0/1
$opt ||= "LOW_PRIORITY WRITE"
$opt
Truth table0%0/2
A
A= $opt
Aresult
11
00
307 0 ▶ $chk_opt->( $self, $self->{table}, $opt, $alias ) || return;
Branch: 1 branch
BranchTrueFalseTotal
&$chk_opt($self, $self->{"table"}, $opt, $alias) or return 0 0 0
308 0 my @lck = ( $self->{table} );
309 0 ▶ push( @lck, "AS $alias" ) if( $alias );
Branch: 1 branch
BranchTrueFalseTotal
if $alias 0 0 0
310 0 push( @lck, uc( $opt ) );
311 0 push( @tables, CORE::join( ' ', @lck ) );
312 }
313 else
314 {
315 0 my %arg = ( @_ );
316 0 my( $tbl, $value );
317 0 ▶ while( ( $tbl, $value ) = each( %arg ) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %arg) 0 0 0
318 {
319 0 my( $alias, $opt );
320 0 ▶ if( !$value )
Branch: 2 branches
BranchTrueFalseTotal
if (not $value) { } 0 0 0
elsif (ref $value) { } 0 0 0
321 {
322 0 $opt = 'LOW_PRIORITY WRITE';
323 }
324 elsif( ref( $value ) )
325 {
326 0 ( $alias, $opt ) = @$value;
327 }
328 else
329 {
330 0 $opt = $value;
331 }
332 0 ▶ $opt ||= 'LOW_PRIORITY WRITE';
Condition0%0/2
$opt ||= "LOW_PRIORITY WRITE"
l!l
00
MC/DC0%0/1
$opt ||= "LOW_PRIORITY WRITE"
$opt
Truth table0%0/2
A
A= $opt
Aresult
11
00
333 0 ▶ $chk_opt->( $self, $tbl, $opt, $alias ) || return;
Branch: 1 branch
BranchTrueFalseTotal
&$chk_opt($self, $tbl, $opt, $alias) or return 0 0 0
334 0 my @lck = ( $tbl );
335 0 ▶ push( @lck, "AS $alias" ) if( $alias );
Branch: 1 branch
BranchTrueFalseTotal
if $alias 0 0 0
336 0 push( @lck, uc( $opt ) );
337 0 push( @tables, CORE::join( ' ', @lck ) );
338 }
339 }
340 0 my $query = 'LOCK TABLES ' . CORE::join( ', ', @tables );
341 0 ▶ my $sth = $self->prepare( $query ) ||
Condition0%0/2
$self->prepare($query) || (return $self->error("Error while preparing query to do tables locking:\n$query", $self->errstr))
l!l
00
MC/DC0%0/1
$self->prepare($query) || (return $self->error("Error while preparing query to do tables locking:\n$query", $self->errstr))
$self->prepare($query)
Truth table0%0/2
A
A= $self->prepare($query)
Aresult
11
00
342 return( $self->error( "Error while preparing query to do tables locking:\n$query", $self->errstr() ) );
343 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
344 {
345 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
346 return( $self->error( "Error while executing query to do tables locking:\n$query", $sth->errstr() ) );
347 }
348 0 return( $sth );
349 }
350  
351 # NOTE: sub name is inherited from DB::Object::Tables
352 # sub name
353  
354 # NOTE: sub null is inherited from DB::Object::Tables
355 # sub null
356  
357 sub on_conflict
358 0 ▶{
Subroutine on_conflict: not called CC=5 SCAR=34.0
Pod documented
359 0 my $self = shift( @_ );
360 0 my $q = $self->_reset_query;
361 # Void
362 0 ▶ return( $q->on_conflict( @_ ) ) if( !defined( wantarray() ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
363 0 ▶ if( wantarray() )
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
364 {
365 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
366 0 return( @val );
367 }
368 else
369 {
370 0 my $val = $q->on_conflict( @_ );
371 0 ▶ return( $self->pass_error( $q->error ) ) if( !defined( $val ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
372 0 return( $val );
373 }
374 }
375  
376 sub optimize
377 0 ▶{
Subroutine optimize: not called CC=6 SCAR=37.4
Pod documented
378 0 my $self = shift( @_ );
379 my $table = $self->{table} ||
380 0 ▶ return( $self->error( 'No table was provided to optmize' ) );
Condition0%0/2
$self->{'table'} || (return $self->error('No table was provided to optmize'))
l!l
00
MC/DC0%0/1
$self->{'table'} || (return $self->error('No table was provided to optmize'))
$self->{'table'}
Truth table0%0/2
A
A= $self->{'table'}
Aresult
11
00
381 0 ▶ return( $self->error( "Table '$table' does not exist." ) ) if( !$self->exists( $table ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists($table) 0 0 0
382 0 my $query = "OPTIMIZE TABLE $table";
383 0 ▶ my $sth = $self->prepare( $query ) ||
Condition0%0/2
$self->prepare($query) || (return $self->error("Error while preparing query to optimize table '${table}':\n$query\n", $self->errstr))
l!l
00
MC/DC0%0/1
$self->prepare($query) || (return $self->error("Error while preparing query to optimize table '${table}':\n$query\n", $self->errstr))
$self->prepare($query)
Truth table0%0/2
A
A= $self->prepare($query)
Aresult
11
00
384 return( $self->error( "Error while preparing query to optimize table '$table':\n$query\n", $self->errstr() ) );
385 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
386 {
387 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to optimize table '${table}':\n$query\n", $sth->errstr) 0 0 0
388 return( $self->error( "Error while executing query to optimize table '$table':\n$query\n", $sth->errstr() ) );
389 }
390 0 return( $sth );
391 }
392  
393 # NOTE: sub primary is inherited from DB::Object::Tables
394 # sub primary
395  
396 sub qualified_name
397 0 ▶{
Subroutine qualified_name: not called CC=3 SCAR=24.8
Pod documented
398 0 my $self = shift( @_ );
399 0 my @val = ();
400 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
401 return( $self->error( "No database handle could be found." ) );
402 0 ▶ CORE::push( @val, $dbo->database ) if( $self->{prefixed} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prefixed"} > 2 0 0 0
403 0 CORE::push( @val, $self->name );
404 0 return( CORE::join( '.', @val ) );
405 }
406  
407 sub rename
408 0 ▶{
Subroutine rename: not called CC=7 SCAR=40.3
Pod documented
409 0 my $self = shift( @_ );
410 my $table = $self->{table} ||
411 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
412 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
413 return( $self->error( "No new table name was provided to rename table '$table'." ) );
414 0 ▶ if( $new !~ /^[\w\_]+$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($new =~ /^[\w\_]+$/) 0 0 0
415 {
416 0 return( $self->error( "Bad new table name '$new'." ) );
417 }
418 0 my $query = "ALTER TABLE $table RENAME TO $new";
419 0 ▶ my $sth = $self->prepare( $query ) ||
Condition0%0/2
$self->prepare($query) || (return $self->error("Error while preparing query to rename table '${table}' into '${new}':\n$query", $self->errstr))
l!l
00
MC/DC0%0/1
$self->prepare($query) || (return $self->error("Error while preparing query to rename table '${table}' into '${new}':\n$query", $self->errstr))
$self->prepare($query)
Truth table0%0/2
A
A= $self->prepare($query)
Aresult
11
00
420 return( $self->error( "Error while preparing query to rename table '$table' into '$new':\n$query", $self->errstr() ) );
421 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
422 {
423 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
424 return( $self->error( "Error while executing query to rename table '$table' into '$new':\n$query", $sth->errstr() ) );
425 }
426 0 $self->reset_structure;
427 0 return( $sth );
428 }
429  
430 sub repair
431 0 ▶{
Subroutine repair: not called CC=7 SCAR=40.3
Pod documented
432 0 my $self = shift( @_ );
433 my $table = $self->{table} ||
434 0 ▶ return( $self->error( 'No table was provided to repair' ) );
Condition0%0/2
$self->{'table'} || (return $self->error('No table was provided to repair'))
l!l
00
MC/DC0%0/1
$self->{'table'} || (return $self->error('No table was provided to repair'))
$self->{'table'}
Truth table0%0/2
A
A= $self->{'table'}
Aresult
11
00
435 0 ▶ return( $self->error( "Table '$table' does not exist." ) ) if( !$self->exists( $table ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists($table) 0 0 0
436 0 my $opts = $self->_get_args_as_hash( @_ );
437 0 my $query = "REPAIR TABLE $table";
438 0 ▶ $query .= ' TYPE = QUICK' if( $opts->{quick} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"quick"} 0 0 0
439 0 ▶ my $sth = $self->prepare( $query ) ||
Condition0%0/2
$self->prepare($query) || (return $self->error("Error while preparing query to repair table '${table}':\n$query\n", $self->errstr))
l!l
00
MC/DC0%0/1
$self->prepare($query) || (return $self->error("Error while preparing query to repair table '${table}':\n$query\n", $self->errstr))
$self->prepare($query)
Truth table0%0/2
A
A= $self->prepare($query)
Aresult
11
00
440 return( $self->error( "Error while preparing query to repair table '$table':\n$query\n", $self->errstr() ) );
441 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
442 {
443 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to repair table '${table}':\n$query\n", $sth->errstr) 0 0 0
444 return( $self->error( "Error while executing query to repair table '$table':\n$query\n", $sth->errstr() ) );
445 }
446 0 return( $sth );
447 }
448  
449 sub stat
450 0 ▶{
Subroutine stat: not called CC=13 SCAR=52.0
Pod documented
451 0 my $self = shift( @_ );
452 # If no $table argument is provided, we will stat all tables
453 0 my $table = shift( @_ );
454 0 my $db = $self->{database};
455 0 ▶ my $query = $table ? "SHOW TABLE STATUS FROM $db LIKE '$table'" : "SHOW TABLE STATUS FROM $db";
Branch: 1 branch
BranchTrueFalseTotal
$table ? : 0 0 0
456 0 ▶ my $sth = $self->prepare( $query ) ||
Branch: 1 branch
BranchTrueFalseTotal
$table ? : 0 0 0
Condition0%0/2
$self->prepare($query) || (return $self->error('Error while preparing query to get the status of table', $table ? " '${table}'" : 's', ":\n$query", $self->errstr))
l!l
00
MC/DC0%0/1
$self->prepare($query) || (return $self->error('Error while preparing query to get the status of table', $table ? " '${table}'" : 's', ":\n$query", $self->errstr))
$self->prepare($query)
Truth table0%0/2
A
A= $self->prepare($query)
Aresult
11
00
457 return( $self->error( "Error while preparing query to get the status of table", ( $table ? " '$table'" : 's' ), ":\n$query", $self->errstr() ) );
458 0 ▶ $sth->execute ||
Branch: 2 branches
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to get the status of table", $table ? " '${table}'" : "s", ":\n$query", $sth->errstr) 0 0 0
$table ? : 0 0 0
459 return( $self->error( "Error while executing query to get the status of table", ( $table ? " '$table'" : 's' ), ":\n$query", $sth->errstr ) );
460 0 my $tables = {};
461 0 my $ref = '';
462 0 ▶ while( $ref = $sth->fetchrow_hashref() )
Branch: 1 branch
BranchTrueFalseTotal
if ($ref = $sth->fetchrow_hashref) 0 0 0
463 {
464 0 my %data = map{ lc( $_ ) => $ref->{ $_ } } keys( %$ref );
465 0 my $name = $data{name};
466 # map{ $tables->{ $name }->{ $_ } = $data{ $_ } } keys( %data );
467 0 $tables->{ $name } = \%data;
468 }
469 0 $sth->finish;
470 0 ▶ return( wantarray() ? () : undef() ) if( !%$tables );
Branch: 2 branches
BranchTrueFalseTotal
unless %$tables 0 0 0
wantarray ? : 0 0 0
471 0 ▶ return( wantarray() ? %{ $tables->{ $table } } : $tables->{ $table } ) if( $table && exists( $tables->{ $table } ) );
Branch: 2 branches
BranchTrueFalseTotal
if $table and exists $tables->{$table} 0 0 0
wantarray ? : 0 0 0
Condition0%0/3
$table and exists $tables->{$table}
!ll&&!rl&&r
000
MC/DC0%0/2
$table and exists $tables->{$table}
$table exists $tables->{$table}
Truth table0%0/3
A and B
A= $tableB= exists $tables->{$table}
ABresult
0X0
100
111
472 0 ▶ return( wantarray() ? %$tables : $tables );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
473 }
474  
475 # TODO: Must implement a cache mechanism for DB::Object::Mysql::structure()
476 sub structure
477 0 ▶{
Subroutine structure: not called CC=44 SCAR=75.9
Pod documented
478 0 my $self = shift( @_ );
479 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
480 0 my $struct = $self->{structure};
481 0 my $fields = $self->{fields};
482 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
483 return( $self->error( "No database handle could be found." ) );
484 0 my $types_dict = $dbo->datatype_dict;
485 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
486 0 my $q = $self->_reset_query;
487 my $table = $self->{table} ||
488 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
489 0 ▶ my $sth1 = $dbo->prepare_cached( "SELECT * FROM information_schema.tables WHERE table_name = ?" ) ||
Condition0%0/2
$dbo->prepare_cached('SELECT * FROM information_schema.tables WHERE table_name = ?') || (return $self->error(qq[An error occured while preparing the sql query to get the details of table "$table": ], $dbo->errstr))
l!l
00
MC/DC0%0/1
$dbo->prepare_cached('SELECT * FROM information_schema.tables WHERE table_name = ?') || (return $self->error(qq[An error occured while preparing the sql query to get the details of table "$table": ], $dbo->errstr))
$dbo->prepare_cached('SELECT * FROM information_schema.tables WHERE table_name = ?')
Truth table0%0/2
A
A= $dbo->prepare_cached('SELECT * FROM information_schema.tables WHERE table_name = ?')
Aresult
11
00
490 return( $self->error( "An error occured while preparing the sql query to get the details of table \"$table\": ", $dbo->errstr() ) );
491 0 ▶ $sth1->execute( $table ) ||
Branch: 1 branch
BranchTrueFalseTotal
$sth1->execute($table) or return $self->error(qq[An error occured while executing the sql query to get the details of table "$table": ], $sth1->errstr) 0 0 0
492 return( $self->error( "An error occured while executing the sql query to get the details of table \"$table\": ", $sth1->errstr() ) );
493 0 my $def = $sth1->fetchrow_hashref;
494 0 $sth1->finish;
495 0 ▶ if( scalar( keys( %$def ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$def) { } 0 0 0
496 {
497 # Ensure all fields are in lower case.
498 0 foreach my $k ( keys( %$def ) )
499 {
500 # For example: AUTO_INCREMENT -> auto_increment
501 0 $def->{ lc( $k ) } = CORE::delete( $def->{ $k } );
502 }
503 0 $self->{type} = lc( $def->{table_type} );
504 0 ▶ $self->{type} = 'table' if( $self->{type} eq 'base table' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"type"} eq "base table" 0 0 0
505 }
506 else
507 {
508 0 ▶ warn( "No information found for table ${table}\n" ) if( $self->_is_warnings_enabled( 'DB::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("DB::Object") 0 0 0
509 }
510 # <https://dev.mysql.com/doc/refman/8.0/en/information-schema-columns-table.html>
511 0 my $query = <<EOT;
512 SELECT
513 a.column_name AS "field"
514 ,a.ordinal_position AS "field_num"
515 ,a.column_default AS "default"
516 ,a.*
517 FROM information_schema.columns a
518 WHERE table_name = ?
519 ORDER BY a.ordinal_position
520 EOT
521 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
522 return( $self->error( "Error while preparing query to get table '$table' columns specification: ", $dbo->errstr() ) );
523 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
524 return( $self->error( "Error while executing query to get table '$table' columns specification: ", $sth->errstr() ) );
525  
526 0 my @primary = ();
527 0 my $ref = '';
528 0 my $c = 0;
529 0 ▶ while( $ref = $sth->fetchrow_hashref() )
Branch: 1 branch
BranchTrueFalseTotal
if ($ref = $sth->fetchrow_hashref) 0 0 0
530 {
531 # Ensure all fields are in lower case.
532 0 foreach my $k ( keys( %$ref ) )
533 {
534 # For example: ORDINAL_POSITION -> ordinal_position
535 0 $ref->{ lc( $k ) } = CORE::delete( $ref->{ $k } );
536 }
537 my $def =
538 {
539 name => $ref->{field},
540 ( CORE::length( $ref->{column_comment} // '' ) ? ( comment => $ref->{column_comment} ) : () ),
541 default => $ref->{column_default},
542 # Damn MySQL not using boolean
543 is_nullable => ( $ref->{is_nullable} eq 'YES' ? 1 : 0 ),
544 is_primary => ( ( $ref->{column_key} // '' ) eq 'PRI' ? 1 : 0 ),
545 is_unique => ( ( $ref->{column_key} // '' ) eq 'UNI' ? 1 : 0 ),
546 pos => $ref->{field_num},
547 # query_object => $q,
548 size => $ref->{character_maximum_length},
549 type => $ref->{data_type},
550 # table_object => $self,
551 0 ▶ };
Branch: 4 branches
BranchTrueFalseTotal
length($ref->{'column_comment'} // '') ? : 0 0 0
$ref->{'is_nullable'} eq 'YES' ? : 0 0 0
($ref->{'column_key'} // '') eq 'PRI' ? : 0 0 0
($ref->{'column_key'} // '') eq 'UNI' ? : 0 0 0
Condition0%0/6
$ref->{"column_comment"} // ""
l!l
00
$ref->{"column_key"} // ""
l!l
00
$ref->{"column_key"} // ""
l!l
00
MC/DC0%0/1
$ref->{"column_comment"} // ""
$ref->{"column_comment"}
MC/DC0%0/1
$ref->{"column_key"} // ""
$ref->{"column_key"}
MC/DC0%0/1
$ref->{"column_key"} // ""
$ref->{"column_key"}
Truth table0%0/2
A
A= $ref->{"column_comment"}
Aresult
11
00
Truth table0%0/2
A
A= $ref->{"column_key"}
Aresult
11
00
Truth table0%0/2
A
A= $ref->{"column_key"}
Aresult
11
00
552  
553 0 my( $const_def, $dict );
554 0 ▶ if( CORE::exists( $types_dict->{ $def->{type} } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$types_dict{$def->{'type'}}) { } 0 0 0
555 {
556 0 $const_def = $types_dict->{ $def->{type} };
557 }
558 else
559 {
560 # Get the constant
561 0 DATA_TYPE_RE: foreach my $type ( keys( %$types_dict ) )
562 {
563 0 ▶ if( $def->{type} =~ /$types_dict->{ $type }->{re}/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"type"} =~ /$types_dict->{$type}{"re"}/i) 0 0 0
564 {
565 0 $const_def = $types_dict->{ $type };
566 0 last DATA_TYPE_RE;
567 }
568 }
569 }
570 0 ▶ if( defined( $const_def ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $const_def) 0 0 0
571 {
572 0 my $const_keys = [keys( %$const_def )];
573 0 my $dict = {};
574 0 @$dict{ @$const_keys } = @$const_def{ @$const_keys };
575 0 $def->{datatype} = $dict;
576 }
577 0 $def->{query_object} = $q;
578 0 $def->{table_object} = $self;
579 # The information schema field 'column_type' provides already what is needed, such as 'varchar(255)'
580 0 my @define = ( $ref->{column_type} );
581 0 ▶ push( @define, "NOT NULL" ) if( !$def->{is_nullable} );
Branch: 1 branch
BranchTrueFalseTotal
unless $def->{"is_nullable"} 0 0 0
582 # <https://stackoverflow.com/questions/68639745/how-to-get-a-default-column-value-from-mysql-5-7-information-schema-columns-with>
583 0 ▶ if( defined( $def->{default} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def->{"default"}) 0 0 0
584 {
585 0 my $value;
586 0 ▶ if( ( $ref->{data_type} eq 'datetime' || $ref->{data_type} eq 'timestamp' ) &&
Branch: 2 branches
BranchTrueFalseTotal
if ($ref->{'data_type'} eq 'datetime' || $ref->{'data_type'} eq 'timestamp' and $def->{'default'} eq 'CURRENT_TIMESTAMP') { } 0 0 0
elsif (not length($def->{'default'} // '')) { } 0 0 0
Condition0%0/6
$ref->{"data_type"} eq "datetime" || $ref->{"data_type"} eq "timestamp" and $def->{"default"} eq "CURRENT_TIMESTAMP"
!ll&&!rl&&r
000
$ref->{"data_type"} eq "datetime" or $ref->{"data_type"} eq "timestamp"
l!l&&r!l&&!r
000
MC/DC0%0/3
$ref->{"data_type"} eq "datetime" || $ref->{"data_type"} eq "timestamp" and $def->{"default"} eq "CURRENT_TIMESTAMP"
$ref->{"data_type"} eq "datetime" $ref->{"data_type"} eq "timestamp" $def->{"default"} eq "CURRENT_TIMESTAMP"
Truth table0%0/5
A or B and C
A= $ref->{"data_type"} eq "datetime"B= $ref->{"data_type"} eq "timestamp"C= $def->{"default"} eq "CURRENT_TIMESTAMP"
ABCresult
00X0
1X00
0100
1X11
0111
587 $def->{default} eq 'CURRENT_TIMESTAMP' )
588 {
589 0 $value = 'CURRENT_TIMESTAMP';
590 }
591 ▶ elsif( !length( $def->{default} // '' ) )
Condition0%0/2
$def->{"default"} // ""
l!l
00
MC/DC0%0/1
$def->{"default"} // ""
$def->{"default"}
Truth table0%0/2
A
A= $def->{"default"}
Aresult
11
00
592 {
593 0 $value = "''";
594 }
595 else
596 {
597 0 ▶ $value = $dbo->quote( $def->{default}, ( CORE::length( $def->{datatype}->{constant} // '' ) ? $def->{datatype}->{constant} : () ) );
Branch: 1 branch
BranchTrueFalseTotal
length($def->{'datatype'}{'constant'} // '') ? : 0 0 0
Condition0%0/2
$def->{"datatype"}{"constant"} // ""
l!l
00
MC/DC0%0/1
$def->{"datatype"}{"constant"} // ""
$def->{"datatype"}{"constant"}
Truth table0%0/2
A
A= $def->{"datatype"}{"constant"}
Aresult
11
00
598 }
599 0 push( @define, "DEFAULT ${value}" );
600 }
601 0 ▶ push( @define, 'AUTO_INCREMENT' ) if( lc( $ref->{extra} // '' ) eq 'auto_increment' );
Branch: 1 branch
BranchTrueFalseTotal
if lc($ref->{"extra"} // "") eq "auto_increment" 0 0 0
Condition0%0/2
$ref->{"extra"} // ""
l!l
00
MC/DC0%0/1
$ref->{"extra"} // ""
$ref->{"extra"}
Truth table0%0/2
A
A= $ref->{"extra"}
Aresult
11
00
602 0 ▶ if( defined( $ref->{extra} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref->{"extra"} and $ref->{"extra"} =~ /^DEFAULT_GENERATED[[:blank:]]+(ON[[:blank:]]+UPDATE[[:blank:]]+.*)$/i) 0 0 0
Condition0%0/3
defined $ref->{"extra"} and $ref->{"extra"} =~ /^DEFAULT_GENERATED[[:blank:]]+(ON[[:blank:]]+UPDATE[[:blank:]]+.*)$/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ref->{"extra"} and $ref->{"extra"} =~ /^DEFAULT_GENERATED[[:blank:]]+(ON[[:blank:]]+UPDATE[[:blank:]]+.*)$/i
defined $ref->{"extra"} $ref->{"extra"} =~ /^DEFAULT_GENERATED[[:blank:]]+(ON[[:blank:]]+UPDATE[[:blank:]]+.*)$/i
Truth table0%0/3
A and B
A= defined $ref->{"extra"}B= $ref->{"extra"} =~ /^DEFAULT_GENERATED[[:blank:]]+(ON[[:blank:]]+UPDATE[[:blank:]]+.*)$/i
ABresult
0X0
100
111
603 $ref->{extra} =~ /^DEFAULT_GENERATED[[:blank:]]+(ON[[:blank:]]+UPDATE[[:blank:]]+.*)$/i )
604 {
605 0 push( @define, $1 );
606 }
607 0 ▶ push( @primary, $def->{name} ) if( $ref->{column_key} eq 'PRI' );
Branch: 1 branch
BranchTrueFalseTotal
if $ref->{"column_key"} eq "PRI" 0 0 0
608 0 $struct->{ $def->{name} } = CORE::join( ' ', @define );
609 0 ▶ my $field = DB::Object::Fields::Field->new( %$def, debug => $self->debug ) ||
Condition0%0/2
'DB::Object::Fields::Field'->new(%$def, 'debug', $self->debug) || (return $self->pass_error('DB::Object::Fields::Field'->error))
l!l
00
MC/DC0%0/1
'DB::Object::Fields::Field'->new(%$def, 'debug', $self->debug) || (return $self->pass_error('DB::Object::Fields::Field'->error))
'DB::Object::Fields::Field'->new(%$def, 'debug', $self->debug)
Truth table0%0/2
A
A= 'DB::Object::Fields::Field'->new(%$def, 'debug', $self->debug)
Aresult
11
00
610 return( $self->pass_error( DB::Object::Fields::Field->error ) );
611 0 $fields->{ $def->{name} } = $field;
612 }
613 0 $sth->finish;
614 0 ▶ if( @primary )
Branch: 1 branch
BranchTrueFalseTotal
if (@primary) 0 0 0
615 {
616 0 $self->{primary} = \@primary;
617 }
618 0 $self->{fields} = $fields;
619 0 $self->{_cache_structure} = $struct;
620 0 return( $self->_clone( $struct ) );
621 }
622  
623 0 ▶sub table_info { return( shift->database_object->table_info( @_ ) ); }
Subroutine table_info: not called CC=1 SCAR=6.9
Pod documented
624  
625 sub unlock
626 0 ▶{
Subroutine unlock: not called CC=5 SCAR=34.0
Pod documented
627 0 my $self = shift( @_ );
628 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
629 {
630 0 return( $self->CORE::unlock( @_ ) );
631 }
632 0 my $query = 'UNLOCK TABLES';
633 0 ▶ my $sth = $self->prepare( $query ) ||
Condition0%0/2
$self->prepare($query) || (return $self->error("Error while preparing query to unlock tables:\n$query", $self->errstr))
l!l
00
MC/DC0%0/1
$self->prepare($query) || (return $self->error("Error while preparing query to unlock tables:\n$query", $self->errstr))
$self->prepare($query)
Truth table0%0/2
A
A= $self->prepare($query)
Aresult
11
00
634 return( $self->error( "Error while preparing query to unlock tables:\n$query", $self->errstr() ) );
635 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
636 {
637 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
638 return( $self->error( "Error while executing query to unlock tables:\n$query", $sth->errstr() ) );
639 }
640 0 return( $sth );
641 }
642  
643 DESTROY
644 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
645 # Do nothing
646 # DB::Object::Tables are never destroyed.
647 # They are just gateway to tables, and they are cached by DB::Object::table()
648 # print( STDERR "DESTROY'ing table $self ($self->{ 'table' })\n" );
649 };
650  
651 1 1;
652  
653 # NOTE: POD
654  
655 __END__
« blib/lib/DB/Object/Postgres/Query.pm ↑ Summary lib/DB/Object/SQLite/Query.pm »