lib/DB/Object/Fields/Field.pm

filter: Statementstmt 47.9% 128 / 267 Branchbran 33.6% 33 / 98 Conditioncond 26.2% 16 / 61 MC/DCmcdc 12.9% 4 / 31 Subroutinesub 50.0% 40 / 80 Podpod 89.4% 34 / 38 totaltotal 44.3% 255 / 575 CC 75 SCAR 34.5
CC
75
Cov
42%
THAW
132.0
FREEZE
90.0
_find_siblings
56.0
CRAP 31.5
« blib/lib/DB/Object/Mysql.pm ↑ Summary lib/DB/Object/Cache/Tables.pm »
1 ##----------------------------------------------------------------------------
2 ## Database Object Interface - ~/lib/DB/Object/Fields/Field.pm
3 ## Version v1.4.0
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2020/01/01
7 ## Modified 2026/03/27
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package DB::Object::Fields::Field;
14 BEGIN
15 4 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 warnings::register_categories( 'DB::Object' );
19 4 ▶ use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶ use vars qw( $VERSION $FIELD_NAMES $EXCEPTION_CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶ use DB::Object::Fields::Overloaded;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶ use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶ use Module::Generic::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶ use overload (
Subroutine BEGIN: called CC=1 SCAR=0.1
26 '""' => 'as_string',
27 1 ▶ 'bool' => sub{1},
Subroutine __ANON__: called CC=1 SCAR=0.0
28 2 ▶ '+' => sub{ &_op_overload( @_, '+' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 1 ▶ '-' => sub{ &_op_overload( @_, '-' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 1 ▶ '*' => sub{ &_op_overload( @_, '*' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 1 ▶ '/' => sub{ &_op_overload( @_, '/' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 1 ▶ '%' => sub{ &_op_overload( @_, '%' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 1 ▶ '<' => sub{ &_op_overload( @_, '<' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 1 ▶ '>' => sub{ &_op_overload( @_, '>' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 1 ▶ '<=' => sub{ &_op_overload( @_, '<=' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
36 1 ▶ '>=' => sub{ &_op_overload( @_, '>=' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 # In most SQL driver, '<>' is more portable tan '!='
38 1 ▶ '!=' => sub{ &_op_overload( @_, '<>' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 3 ▶ '<<' => sub{ &_op_overload( @_, '<<' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 1 ▶ '>>' => sub{ &_op_overload( @_, '>>' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 0 ▶ 'lt' => sub{ &_op_overload( @_, '<' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 0 ▶ 'gt' => sub{ &_op_overload( @_, '>' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 0 ▶ 'le' => sub{ &_op_overload( @_, '<=' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 ▶ 'ge' => sub{ &_op_overload( @_, '>=' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 ▶ 'ne' => sub{ &_op_overload( @_, '<>' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 1 ▶ '&' => sub{ &_op_overload( @_, '&' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 1 ▶ '^' => sub{ &_op_overload( @_, '^' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 1 ▶ '|' => sub{ &_op_overload( @_, '|' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 1 ▶ '==' => sub{ &_op_overload( @_, '=' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
50 1 ▶ 'eq' => sub{ &_op_overload( @_, 'IS' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 # Full Text Search operator
52 0 ▶ '~~' => sub{ &_op_overload( @_, '@@' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 4 fallback => 1,
54 4 );
55 4 ▶ use Wanted;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 4 our $VERSION = 'v1.4.0';
57 4 our $FIELD_NAMES = [qw(
58 check_name comment datatype default foreign_name index_name is_array is_check
59 is_foreign is_nullable is_primary is_unique name pos prefixed size type
60 )];
61 4 our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
62 };
63  
64 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 sub init
68 1 ▶{
Subroutine init: called CC=2 SCAR=6.9
Pod documented
69 1 my $self = shift( @_ );
70 1 $self->{check_name} = undef;
71 1 $self->{comment} = undef;
72 1 $self->{datatype} = undef;
73 1 $self->{default} = undef;
74 1 $self->{foreign_name} = undef;
75 1 $self->{index_name} = undef;
76 1 $self->{is_array} = undef;
77 1 $self->{is_check} = undef;
78 1 $self->{is_foreign} = undef;
79 1 $self->{is_nullable} = undef;
80 1 $self->{is_primary} = undef;
81 1 $self->{is_unique} = undef;
82 1 $self->{name} = undef;
83 1 $self->{pos} = undef;
84 1 $self->{prefixed} = 0;
85 1 $self->{query_object} = undef;
86 1 $self->{size} = undef;
87 1 $self->{table_object} = undef;
88 1 $self->{type} = undef;
89 1 $self->{_init_params_order} = [qw( table_object query_object default pos type prefixed name )];
90 1 $self->{_init_strict_use_sub} = 1;
91 1 $self->{_exception_class} = $EXCEPTION_CLASS;
92 1 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 1 0 1
93 1 $self->{_fields} = $FIELD_NAMES;
94 # return( $self->error( "No table object was provided." ) ) if( !$self->{table_object} );
95 # return( $self->error( "Table object provided is not an object." ) ) if( !$self->_is_object( $self->{table_object} ) );
96 # return( $self->error( "Table object provided is not a DB::Object::Tables object." ) ) if( !$self->{table_object}->isa( 'DB::Object::Tables' ) );
97 # return( $self->error( "No name was provided for this field." ) ) if( !$self->{name} );
98 1 return( $self );
99 }
100  
101 0 ▶sub as_string { return( shift->name ); }
Subroutine as_string: not called CC=1 SCAR=6.9
Pod documented
102  
103 # We attach this field to its table object
104 sub attach
105 0 ▶{
Subroutine attach: not called CC=4 SCAR=30.0
Pod documented
106 0 my $self = shift( @_ );
107 0 ▶ my $tbl = shift( @_ ) ||
Condition0%0/2
shift @_ || (return $self->error('No DB::Object::Tables object was provided.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No DB::Object::Tables object was provided.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
108 return( $self->error( "No DB::Object::Tables object was provided." ) );
109 0 my $base_class = $self->base_class( $tbl );
110 0 ▶ unless( $self->_is_a( $tbl => "${base_class}::Tables" ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($tbl, "${base_class}::Tables")) 0 0 0
111 {
112 0 return( $self->error( "Value provided is not a ${base_class}::Tables object." ) );
113 }
114 0 ▶ $self->table_object( $tbl ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->table_object($tbl) or return $self->pass_error 0 0 0
115 0 $self->debug( $tbl->debug );
116 0 return( $self );
117 }
118  
119 sub base_class
120 0 ▶{
Subroutine base_class: not called CC=3 SCAR=20.6
Pod documented
121 0 my $self = shift( @_ );
122 0 ▶ my $this = @_ ? shift( @_ ) : $self;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
123 0 ▶ my $class = ref( $this ) ? ref( $this ) : $this;
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 0 0 0
124 4 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
125 0 my @supported_classes = CORE::values( %$DB::Object::DRIVER2PACK );
126 0 push( @supported_classes, 'DB::Object' );
127 0 my $ok_classes = CORE::join( '|', CORE::map{ CORE::quotemeta( $_ ) } @supported_classes );
128 0 my $base_class = ( $class =~ /^($ok_classes)/ )[0];
129 0 return( $base_class );
130 }
131  
132 sub clone
133 0 ▶{
Subroutine clone: not called CC=4 SCAR=30.0
Pod documented
134 0 my $self = shift( @_ );
135 # Fields to clone: we explicitly want to avoid cloning query_object, because it is not necessary and because it contains an object to the database connection that we do not want to clone.
136 # Likewise, the query object, we do not want to clone either.
137 # my $keys = [qw( default is_nullable name pos prefixed size type )];
138 0 my $keys = $self->_fields;
139 0 ▶ $keys = $self->new_array( $FIELD_NAMES ) if( !scalar( @$keys ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$keys 0 0 0
140 0 my $hash = {};
141 0 @$hash{ @$keys } = @$self{ @$keys };
142 # $hash->{datatype} = {};
143 # for( qw( alias constant name re type ) )
144 # {
145 # $hash->{datatype}->{ $_ } = $self->datatype->$_;
146 # }
147 0 my $dt = $self->datatype;
148 $hash->{datatype} =
149 {
150 alias => $dt->alias->clone,
151 constant => $dt->{constant},
152 name => $dt->{name},
153 re => $dt->{re},
154 type => $dt->{type},
155 0 };
156 0 ▶ $self->_load_class( 'Clone' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Clone") or return $self->pass_error 0 0 0
157 0 my $copy = Clone::clone( $hash );
158 0 $copy->{query_object} = $self->query_object;
159 0 $copy->{table_object} = $self->table_object;
160 0 $copy->{debug} = $self->debug;
161 0 ▶ my $new = $self->new( %$copy ) || return( $self->pass_error );
Condition0%0/2
$self->new(%$copy) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new(%$copy) || (return $self->pass_error)
$self->new(%$copy)
Truth table0%0/2
A
A= $self->new(%$copy)
Aresult
11
00
162 0 return( $new );
163 }
164  
165 0 ▶sub check_name { return( shift->_set_get_scalar( 'check_name', @_ ) ); }
Subroutine check_name: not called CC=1 SCAR=6.9
Pod documented
166  
167 0 ▶sub comment { return( shift->_set_get_scalar( 'comment', @_ ) ); }
Subroutine comment: not called CC=1 SCAR=6.9
Pod documented
168  
169 # A data type constant
170 # sub constant { return( shift->_set_get_hash_as_object( 'constant', @_ ) ); }
171 0 ▶sub constant { return( shift->datatype->constant( @_ ) ); }
Subroutine constant: not called CC=1 SCAR=6.9
Pod documented
172  
173 0 ▶sub database { return( shift->database_object->database ); }
Subroutine database: not called CC=1 SCAR=6.9
Pod documented
174  
175 20 ▶sub database_object { return( shift->table_object->database_object ); }
Subroutine database_object: called CC=1 SCAR=0.0
Pod documented
176  
177 20 ▶sub datatype { return( shift->_set_get_class( 'datatype', {
Subroutine datatype: called CC=1 SCAR=0.0
Pod documented
178 alias => { type => 'array_as_object' },
179 constant => { type => 'scalar' },
180 name => { type => 'scalar_as_object' },
181 re => { type => 'object', package => 'Regexp' },
182 type => { type => 'scalar_as_object' },
183 }, @_ ) ); }
184  
185 0 ▶sub default { return( shift->_set_get_scalar( 'default', @_ ) ); }
Subroutine default: not called CC=1 SCAR=6.9
Pod documented
186  
187 0 ▶sub foreign_name { return( shift->_set_get_scalar( 'foreign_name', @_ ) ); }
Subroutine foreign_name: not called CC=1 SCAR=6.9
Pod documented
188  
189 0 ▶sub first { return( shift->_find_siblings(1) ); }
Subroutine first: not called CC=1 SCAR=6.9
Pod documented
190  
191 0 ▶sub index_name { return( shift->_set_get_scalar( 'index_name', @_ ) ); }
Subroutine index_name: not called CC=1 SCAR=6.9
Pod documented
192  
193 0 ▶sub is_array { return( shift->_set_get_boolean( 'is_array', @_ ) ); }
Subroutine is_array: not called CC=1 SCAR=6.9
Pod documented
194  
195 0 ▶sub is_check { return( shift->_set_get_boolean( 'is_check', @_ ) ); }
Subroutine is_check: not called CC=1 SCAR=6.9
Pod documented
196  
197 0 ▶sub is_foreign { return( shift->_set_get_boolean( 'is_foreign', @_ ) ); }
Subroutine is_foreign: not called CC=1 SCAR=6.9
Pod documented
198  
199 0 ▶sub is_nullable { return( shift->_set_get_boolean( 'is_nullable', @_ ) ); }
Subroutine is_nullable: not called CC=1 SCAR=6.9
Pod documented
200  
201 0 ▶sub is_primary { return( shift->_set_get_boolean( 'is_primary', @_ ) ); }
Subroutine is_primary: not called CC=1 SCAR=6.9
Pod documented
202  
203 0 ▶sub is_unique { return( shift->_set_get_boolean( 'is_unique', @_ ) ); }
Subroutine is_unique: not called CC=1 SCAR=6.9
Pod documented
204  
205 sub last
206 0 ▶{
Subroutine last: not called CC=1 SCAR=6.9
Pod documented
207 0 my $self = shift( @_ );
208 0 my $fields = $self->table_object->fields;
209 0 my $pos = scalar( keys( %$fields ) );
210 0 return( $self->_find_siblings( $pos ) );
211 }
212  
213 sub name
214 21 ▶{
Subroutine name: called CC=10 SCAR=33.2
Pod documented
215 21 my $self = shift( @_ );
216 4 ▶ no overloading;
Subroutine BEGIN: called CC=1 SCAR=0.0
217 21 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 20 21
218 {
219 1 my $name = shift( @_ );
220 1 ▶ if( !defined( $name ) || !CORE::length( $name // '' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name and length($name // "")) 0 1 1
Condition40%2/5
defined $name and length($name // "")
!ll&&!rl&&r
001
$name // ""
l!l
10
MC/DC0%0/2
defined $name and length($name // "")
defined $name $name
Truth table33%1/3
A and B
A= defined $nameB= $name
ABresult
0X0
100
111
221 {
222 0 return( $self->error( "You can not set the table column name to be undefined or an empty string." ) );
223 }
224 1 $self->{name} = $name;
225 }
226 21 my $name = $self->{name};
227 21 my $alias = $self->query_object->table_alias;
228 21 ▶ if( $self->{prefixed} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'prefixed'}) { } 0 21 21
229 {
230 0 my @prefix = ();
231 0 ▶ if( length( $alias ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $alias) { } 0 0 0
232 {
233 0 CORE::push( @prefix, $alias );
234 }
235 else
236 {
237 # if the value is higher than 1, we also add the database name as a prefix
238 # For example $tbl->fields->some_field->prefixed(2)->name
239 0 ▶ push( @prefix, $self->database ) if( $self->{prefixed} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prefixed"} > 2 0 0 0
240 0 ▶ push( @prefix, $self->table_object->schema ) if( $self->{prefixed} > 1 && CORE::length( $self->table_object->schema ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prefixed"} > 1 and length $self->table_object->schema 0 0 0
Condition0%0/3
$self->{"prefixed"} > 1 and length $self->table_object->schema
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"prefixed"} > 1 and length $self->table_object->schema
$self->{"prefixed"} > 1 length $self->table_object->schema
Truth table0%0/3
A and B
A= $self->{"prefixed"} > 1B= length $self->table_object->schema
ABresult
0X0
100
111
241 0 push( @prefix, $self->table );
242 }
243 0 push( @prefix, $name );
244 0 return( join( '.', @prefix ) );
245 }
246 else
247 {
248 21 return( $name );
249 }
250 }
251  
252 sub next
253 0 ▶{
Subroutine next: not called CC=1 SCAR=6.9
Pod documented
254 0 my $self = shift( @_ );
255 0 return( $self->_find_siblings( $self->pos + 1 ) );
256 }
257  
258 0 ▶sub pos { return( shift->_set_get_scalar( 'pos', @_ ) ); }
Subroutine pos: not called CC=1 SCAR=6.9
Pod documented
259  
260 sub prefixed
261 0 ▶{
Subroutine prefixed: not called CC=5 SCAR=34.0
Pod documented
262 0 my $self = shift( @_ );
263 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
264 {
265 0 ▶ $self->{prefixed} = ( $_[0] =~ /^\d+$/ ? $_[0] : ( $_[0] ? 1 : 0 ) );
Branch: 2 branches
BranchTrueFalseTotal
$_[0] =~ /^\d+$/ ? : 0 0 0
$_[0] ? : 0 0 0
266 }
267 else
268 {
269 0 $self->{prefixed} = 1;
270 }
271 0 ▶ return( want( 'OBJECT' ) ? $self : $self->{prefixed} );
Branch: 1 branch
BranchTrueFalseTotal
want('OBJECT') ? : 0 0 0
272 }
273  
274 sub prev
275 0 ▶{
Subroutine prev: not called CC=1 SCAR=6.9
Pod documented
276 0 my $self = shift( @_ );
277 0 return( $self->_find_siblings( $self->pos - 1 ) );
278 }
279  
280 # sub query_object { return( shift->_set_get_object_without_init( 'query_object', 'DB::Object::Query', @_ ) ); }
281 42 ▶sub query_object { return( shift->table_object->query_object ); }
Subroutine query_object: called CC=1 SCAR=0.0
Pod documented
282  
283 0 ▶sub schema { return( shift->table_object->schema ); }
Subroutine schema: not called CC=1 SCAR=6.9
Pod documented
284  
285 0 ▶sub size { return( shift->_set_get_number( 'size', @_ ) ); }
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
286  
287 0 ▶sub table { return( shift->table_object->name ); }
Subroutine table: not called CC=1 SCAR=6.9
Pod documented
288  
289 0 ▶sub table_name { return( shift->table_object->name ); }
Subroutine table_name: not called CC=1 SCAR=6.9
Pod documented
290  
291 63 ▶sub table_object { return( shift->_set_get_object_without_init( 'table_object', 'DB::Object::Tables', @_ ) ); }
Subroutine table_object: called CC=1 SCAR=0.0
Pod documented
292  
293 0 ▶sub type { return( shift->_set_get_scalar( 'type', @_ ) ); }
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
294  
295 0 ▶sub _fields { return( shift->_set_get_array_as_object( '_fields', @_ ) ); }
Subroutine _fields: not called CC=1 SCAR=6.9
296  
297 sub _find_siblings
298 0 ▶{
Subroutine _find_siblings: not called CC=7 SCAR=40.3
299 0 my $self = shift( @_ );
300 0 my $pos = shift( @_ );
301 0 ▶ return( $self->error( "No field position provided." ) ) if( !CORE::length( $pos ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $pos 0 0 0
302 0 ▶ return if( $pos < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 0 0 0
303 0 my $fields = $self->table_object->fields;
304 0 my $next_field;
305 0 foreach my $f ( sort{ $fields->{ $a } <=> $fields->{ $b } } keys( %$fields ) )
306 {
307 0 ▶ if( $fields->{ $f } == $pos )
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->{$f} == $pos) 0 0 0
308 {
309 0 $next_field = $f;
310 0 CORE::last;
311 }
312 }
313 0 ▶ return if( !defined( $next_field ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next_field 0 0 0
314 0 ▶ my $o = $self->table_object->fields_object->_initiate_field_object( $next_field ) ||
Condition0%0/2
$self->table_object->fields_object->_initiate_field_object($next_field) || (return $self->pass_error($self->table_object->fields_object->error))
l!l
00
MC/DC0%0/1
$self->table_object->fields_object->_initiate_field_object($next_field) || (return $self->pass_error($self->table_object->fields_object->error))
$self->table_object->fields_object->_initiate_field_object($next_field)
Truth table0%0/2
A
A= $self->table_object->fields_object->_initiate_field_object($next_field)
Aresult
11
00
315 return( $self->pass_error( $self->table_object->fields_object->error ) );
316 0 return( $o );
317 }
318  
319 # Ref:
320 # <https://www.postgresql.org/docs/10/functions-comparison.html>
321 # <https://www.postgresql.org/docs/10/functions-math.html>
322 # <https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html>
323 # <https://sqlite.org/lang_expr.html>
324 sub _op_overload
325 20 ▶{
Subroutine _op_overload: called CC=29 SCAR=39.4
326 20 my( $self, $val, $swap, $op ) = @_;
327 20 ▶ if( $self->_is_a( $val => [qw( DB::Object::IN DB::Object::LIKE )] ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($val, ["DB::Object::IN", "DB::Object::LIKE"])) 0 20 20
328 {
329 0 return( $val->_opt_overload( $self, 1, $op ) );
330 }
331  
332 20 my $field = $self->name;
333 20 my $map =
334 {
335 '!=' => '<>',
336 'lt' => '<',
337 'gt' => '>',
338 'le' => '<=',
339 'ge' => '>=',
340 # '=' works for all types, but IS does not work with everything.
341 # For example:
342 # select * from ip_table where ip_addr IS inet '192.168.2.12' OR inet '192.168.2.12' << ip_addr
343 # does not work, but
344 # select * from ip_table where ip_addr = inet '192.168.2.12' OR inet '192.168.2.12' << ip_addr
345 # works better
346 '==' => '=',
347 };
348 20 ▶ $op = $map->{ $op } if( exists( $map->{ $op } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $map->{$op} 0 20 20
349 20 my $dbo = $self->database_object;
350 20 my $qo = $self->query_object;
351 20 my $placeholder_re = $dbo->_placeholder_regexp;
352 20 my $const = $self->datatype->constant;
353 # When the RHS is NULL, SQL requires IS NULL / IS NOT NULL - never = NULL or <> NULL,
354 # as those predicates always evaluate to NULL (never TRUE) per the SQL standard.
355 20 ▶ if( !ref( $val ) && uc( $val ) eq 'NULL' )
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $val and uc $val eq "NULL") 2 18 20
Condition100%3/3
not ref $val and uc $val eq "NULL"
!ll&&!rl&&r
1172
MC/DC100%2/2
not ref $val and uc $val eq "NULL"
not ref $val uc $val eq "NULL"
Truth table100%3/3
not(A) and B
A= not ref $valB= uc $val eq "NULL"
ABresult
0X0
100
111
356 {
357 2 ▶ my $null_expr = $op eq '<>' ? "${field} IS NOT NULL" : "${field} IS NULL";
Branch: 1 branch
BranchTrueFalseTotal
$op eq '<>' ? : 0 2 2
358 2 my $null_overloaded = DB::Object::Fields::Overloaded->new(
359 expression => $null_expr,
360 field => $self,
361 query_object => $qo,
362 debug => $self->debug,
363 );
364 2 ▶ weaken( $null_overloaded->{query_object} ) if( $null_overloaded && $null_overloaded->{query_object} );
Branch: 1 branch
BranchTrueFalseTotal
if $null_overloaded and $null_overloaded->{"query_object"} 2 0 2
Condition33%1/3
$null_overloaded and $null_overloaded->{"query_object"}
!ll&&!rl&&r
002
MC/DC0%0/2
$null_overloaded and $null_overloaded->{"query_object"}
$null_overloaded $null_overloaded->{"query_object"}
Truth table33%1/3
A and B
A= $null_overloadedB= $null_overloaded->{"query_object"}
ABresult
0X0
100
111
365 2 return( $null_overloaded );
366 }
367 # If the value specified in the operation is a placeholder, or a field object or a statement object, we do not want to quote process it
368 18 ▶ unless( $val =~ /$placeholder_re/ ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($val =~ /$placeholder_re/ or $self->_is_object($val) and $val->isa("DB::Object::Fields::Field") || $val->isa("DB::Object::Statement") or $dbo->placeholder->has(\$val) or $self->_is_scalar($val) or uc $val eq "NULL") 16 2 18
Condition50%9/18
$val =~ /$placeholder_re/ or $self->_is_object($val) and $val->isa("DB::Object::Fields::Field") || $val->isa("DB::Object::Statement") or $dbo->placeholder->has(\$val) or $self->_is_scalar($val) or uc $val eq "NULL"
l!l&&r!l&&!r
2016
$val =~ /$placeholder_re/ or $self->_is_object($val) and $val->isa("DB::Object::Fields::Field") || $val->isa("DB::Object::Statement") or $dbo->placeholder->has(\$val) or $self->_is_scalar($val)
l!l&&r!l&&!r
1116
$val =~ /$placeholder_re/ or $self->_is_object($val) and $val->isa("DB::Object::Fields::Field") || $val->isa("DB::Object::Statement") or $dbo->placeholder->has(\$val)
l!l&&r!l&&!r
0117
$val =~ /$placeholder_re/ or $self->_is_object($val) and $val->isa("DB::Object::Fields::Field") || $val->isa("DB::Object::Statement")
l!l&&r!l&&!r
0018
$self->_is_object($val) and $val->isa("DB::Object::Fields::Field") || $val->isa("DB::Object::Statement")
!ll&&!rl&&r
1800
$val->isa("DB::Object::Fields::Field") or $val->isa("DB::Object::Statement")
l!l&&r!l&&!r
000
MC/DC28%2/7
$val =~ /$placeholder_re/ or $self->_is_object($val) and $val->isa("DB::Object::Fields::Field") || $val->isa("DB::Object::Statement") or $dbo->placeholder->has(\$val) or $self->_is_scalar($val) or uc $val eq "NULL"
$val =~ /$placeholder_re/ $self->_is_object($val) $val->isa("DB::Object::Fields::Field") $val->isa("DB::Object::Statement") $dbo->placeholder->has(\$val) $self->_is_scalar($val) uc $val eq "NULL"
Truth table27%3/11
A or B and C or D or E or F or G
A= $val =~ /$placeholder_re/B= $self->_is_object($val)C= $val->isa("DB::Object::Fields::Field")D= $val->isa("DB::Object::Statement")E= $dbo->placeholder->has(\$val)F= $self->_is_scalar($val)G= uc $val eq "NULL"
ABCDEFGresult
1XXXXXX1
011XXXX1
0101XXX1
00XX1XX1
01001XX1
00XX01X1
010001X1
00XX0011
01000011
00XX0000
01000000
369 ( $self->_is_object( $val ) &&
370 (
371 $val->isa( 'DB::Object::Fields::Field' ) ||
372 $val->isa( 'DB::Object::Statement' )
373 )
374 ) ||
375 $dbo->placeholder->has( \$val ) ||
376 $self->_is_scalar( $val ) ||
377 uc( $val ) eq 'NULL' )
378 {
379 16 ▶ $val = $dbo->quote( $val, $const ) if( $dbo );
Branch: 1 branch
BranchTrueFalseTotal
if $dbo 16 0 16
380 }
381  
382 18 my $types;
383 # If the value is a statement object, stringify it, surround it with parenthesis and use it
384 18 ▶ if( $self->_is_a( $val, 'DB::Object::Statement' ) )
Branch: 4 branches
BranchTrueFalseTotal
if ($self->_is_a($val, 'DB::Object::Statement')) { } 0 18 18
elsif ($dbo->placeholder->has($self->_is_scalar($val) ? $val : \$val)) { } 1 17 18
elsif ($val =~ /$placeholder_re/) { } 0 17 17
elsif ($self->_is_scalar($val)) { } 1 16 17
385 {
386 0 $qo->elements->merge( $val->query_object->elements );
387 0 $val = '(' . $val->as_string . ')';
388 }
389 ▶ elsif( $dbo->placeholder->has( $self->_is_scalar( $val ) ? $val : \$val ) )
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_scalar($val) ? : 1 17 18
390 {
391 1 ▶ $types = $dbo->placeholder->replace( $self->_is_scalar( $val ) ? $val : \$val );
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_scalar($val) ? : 0 1 1
392 }
393 # A placeholder (pure: ?) or embedded (e.g. UPPER(?), COALESCE(?,?)):
394 # count the number of placeholders in $val and register one empty type per bind slot.
395 elsif( $val =~ /$placeholder_re/ )
396 {
397 0 my @matches = ( $val =~ /$placeholder_re/g );
398 0 $types = Module::Generic::Array->new( [ ( '' ) x scalar( @matches ) ] );
399 }
400 elsif( $self->_is_scalar( $val ) )
401 {
402 1 $val = $$val;
403 }
404 # return( DB::Object::Fields::Overloaded->new(
405 # expression =>
406 # (
407 # $swap
408 # ? "${val} ${op} ${field}"
409 # : "${field} ${op} ${val}"
410 # ),
411 # field => $self,
412 # # binded => ( $val =~ /^$placeholder_re$/ || $types ) ? 1 : 0,
413 # ( $val =~ /^$placeholder_re$/ ? ( placeholder => $val ) : () ),
414 # type => $self->type,
415 # # query_object => $self->query_object,
416 # debug => $self->debug,
417 # ( $val !~ /^$placeholder_re$/ ? ( value => $val ) : () ),
418 # # binded_offset => ( $val =~ /^$placeholder_re$/ && defined( $+{offset} ) ) ? ( $+{offset} - 1 ) : undef,
419 # # types => $types,
420 # ) );
421 # Capture the placeholder before calling new() - $1 can be clobbered by any
422 # subsequent regex match inside the argument list evaluation or inside new() itself.
423 18 ▶ my $captured_placeholder = ( $val =~ /($placeholder_re)/ ) ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /($placeholder_re)/ ? : 1 17 18
424 18 ▶ my $over = DB::Object::Fields::Overloaded->new(
Branch: 4 branches
BranchTrueFalseTotal
$swap ? : 3 15 18
defined $captured_placeholder ? : 1 17 18
defined $const ? : 0 18 18
!defined($captured_placeholder) ? : 17 1 18
425 expression =>
426 (
427 $swap
428 ? "${val} ${op} ${field}"
429 : "${field} ${op} ${val}"
430 ),
431 field => $self,
432 # query_object must be initialised before placeholder, because the placeholder
433 # setter calls $self->query_object->database_object->_placeholder_regexp
434 query_object => $qo,
435 debug => $self->debug,
436 ( defined( $captured_placeholder ) ? ( placeholder => $captured_placeholder ) : () ),
437 # Actually type() will return us the actual data type, not the driver constant
438 # type => $self->type,
439 ( defined( $const ) ? ( type => $const ) : () ),
440 ( !defined( $captured_placeholder ) ? ( value => $val ) : () ),
441 # binded_offset => ( defined( $captured_placeholder ) && defined( $+{index} ) ) ? ( $+{index} - 1 ) : undef,
442 # types => $types,
443 );
444 18 ▶ weaken( $over->{query_object} ) if( $over && $over->{query_object} );
Branch: 1 branch
BranchTrueFalseTotal
if $over and $over->{"query_object"} 18 0 18
Condition33%1/3
$over and $over->{"query_object"}
!ll&&!rl&&r
0018
MC/DC0%0/2
$over and $over->{"query_object"}
$over $over->{"query_object"}
Truth table33%1/3
A and B
A= $overB= $over->{"query_object"}
ABresult
0X0
100
111
445 18 return( $over );
446 }
447  
448 sub FREEZE
449 0 ▶{
Subroutine FREEZE: not called CC=9 SCAR=45.0
Pod undocumented
450 0 my $self = CORE::shift( @_ );
451 0 ▶ my $serialiser = CORE::shift( @_ ) // '';
Condition0%0/2
shift @_ // ""
l!l
00
MC/DC0%0/1
shift @_ // ""
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
452 0 my $class = CORE::ref( $self );
453  
454 # We keep a strict allow-list to avoid accidentally freezing DBI handles or other
455 # process-local state.
456 0 my @props = grep( $_ ne 'datatype', @{$self->{_fields}} );
457  
458 0 my $hash = {};
459 0 foreach my $prop ( @props )
460 {
461 0 ▶ if( CORE::exists( $self->{ $prop } ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$prop} and defined $self->{$prop} and ref $self->{$prop} ne "CODE") 0 0 0
Condition0%0/6
exists $self->{$prop} and defined $self->{$prop} and ref $self->{$prop} ne "CODE"
!ll&&!rl&&r
000
exists $self->{$prop} and defined $self->{$prop}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $self->{$prop} and defined $self->{$prop} and ref $self->{$prop} ne "CODE"
exists $self->{$prop} defined $self->{$prop} ref $self->{$prop} ne "CODE"
Truth table0%0/4
A and B and C
A= exists $self->{$prop}B= defined $self->{$prop}C= ref $self->{$prop} ne "CODE"
ABCresult
0XX0
10X0
1100
1111
462 defined( $self->{ $prop } ) &&
463 CORE::ref( $self->{ $prop } ) ne 'CODE' )
464 {
465 0 $hash->{ $prop } = $self->{ $prop };
466 }
467 }
468  
469 0 my $datatype = $self->datatype;
470 0 my $dtype_ref = {};
471 0 ▶ if( $datatype )
Branch: 1 branch
BranchTrueFalseTotal
if ($datatype) 0 0 0
472 {
473 0 $dtype_ref->{alias} = $datatype->alias;
474 0 $dtype_ref->{constant} = $datatype->constant;
475 0 $dtype_ref->{name} = $datatype->name;
476 0 $dtype_ref->{re} = $datatype->re;
477 0 $dtype_ref->{type} = $datatype->type;
478 }
479 0 $hash->{datatype} = $dtype_ref;
480  
481 # Return an array reference rather than a list so this works with Sereal and CBOR.
482 # Before Sereal version 4.023, Sereal did not support multiple values returned.
483 0 ▶ if( $serialiser eq 'Sereal' )
Branch: 1 branch
BranchTrueFalseTotal
if ($serialiser eq "Sereal") 0 0 0
484 {
485 0 require Sereal::Encoder;
486 0 require version;
487  
488 0 ▶ if( version->parse( Sereal::Encoder->VERSION ) < version->parse( '4.023' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse("Sereal::Encoder"->VERSION) < "version"->parse("4.023")) 0 0 0
489 {
490 0 CORE::return( [$class, $hash] );
491 }
492 }
493  
494 # But Storable wants a list with the first element being the serialised element
495 0 CORE::return( $class, $hash );
496 }
497  
498 0 ▶sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
Pod undocumented
499  
500 0 ▶sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
Pod undocumented
501  
502 sub THAW
503 0 ▶{
Subroutine THAW: not called CC=11 SCAR=48.8
Pod undocumented
504 # STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue
505 # 'CBOR' as the second value.
506 0 my( $self, undef, @args ) = @_;
507 0 ▶ my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
Branch: 1 branch
BranchTrueFalseTotal
scalar @args == 1 && ref $args[0] eq 'ARRAY' ? : 0 0 0
Condition0%0/3
scalar @args == 1 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @args == 1 and ref $args[0] eq "ARRAY"
scalar @args == 1 ref $args[0] eq "ARRAY"
Truth table0%0/3
A and B
A= scalar @args == 1B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
508 0 ▶ my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
Branch: 1 branch
BranchTrueFalseTotal
defined $ref && ref $ref eq 'ARRAY' && scalar @$ref > 1 ? : 0 0 0
Condition0%0/9
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
!ll&&!rl&&r
000
defined $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
000
ref $self or $self
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
defined $ref ref $ref eq "ARRAY" scalar @$ref > 1
MC/DC0%0/2
ref $self or $self
ref $self $self
Truth table0%0/4
A and B and C
A= defined $refB= ref $ref eq "ARRAY"C= scalar @$ref > 1
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
509 0 ▶ my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'ARRAY' ? : 0 0 0
510 0 my $dtype = CORE::delete( $hash->{datatype} );
511 0 my $new;
512 # Storable pattern requires to modify the object it created rather than returning a new one
513 0 ▶ if( CORE::ref( $self ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
514 {
515 0 foreach( CORE::keys( %$hash ) )
516 {
517 0 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
518 }
519 0 $new = $self;
520 }
521 else
522 {
523 0 $new = CORE::bless( $hash => $class );
524 }
525 0 ▶ $new->datatype( %$dtype ) if( $dtype );
Branch: 1 branch
BranchTrueFalseTotal
if $dtype 0 0 0
526 0 CORE::return( $new );
527 }
528  
529 4 1;
530 # NOTE: POD
531 __END__
« blib/lib/DB/Object/Mysql.pm ↑ Summary lib/DB/Object/Cache/Tables.pm »