lib/DB/Object/Fields/Field.pm

filter: Statementstmt 51.8% 138 / 266 Branchbran 36.7% 36 / 98 Conditioncond 26.2% 16 / 61 MC/DCmcdc 12.9% 4 / 31 Subroutinesub 60.0% 48 / 80 Podpod 89.4% 34 / 38 totaltotal 48.0% 276 / 574 CC 75 SCAR 34.3
CC
75
Cov
45%
THAW
132.0
FREEZE
90.0
_find_siblings
56.0
CRAP 30.8
« lib/DB/Object/Fields/Unknown.pm ↑ Summary blib/lib/DB/Object/Mysql.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 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package DB::Object::Fields::Field;
15 BEGIN
16 {
17 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶ use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶ use vars qw( $VERSION $FIELD_NAMES $EXCEPTION_CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶ use DB::Object::Fields::Overloaded;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶ use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶ use Module::Generic::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 use overload (
26 '""' => 'as_string',
27 60 ▶ '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 3 fallback => 1,
54 3 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
55 3 ▶ use Wanted;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 3 ▶ our $VERSION = 'v1.4.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
57 3 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 3 our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
62 };
63  
64 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 sub init
68 {
69 12 ▶ my $self = shift( @_ );
Subroutine init: called CC=2 SCAR=6.9
Pod documented
70 12 $self->{check_name} = undef;
71 12 $self->{comment} = undef;
72 12 $self->{datatype} = undef;
73 12 $self->{default} = undef;
74 12 $self->{foreign_name} = undef;
75 12 $self->{index_name} = undef;
76 12 $self->{is_array} = undef;
77 12 $self->{is_check} = undef;
78 12 $self->{is_foreign} = undef;
79 12 $self->{is_nullable} = undef;
80 12 $self->{is_primary} = undef;
81 12 $self->{is_unique} = undef;
82 12 $self->{name} = undef;
83 12 $self->{pos} = undef;
84 12 $self->{prefixed} = 0;
85 12 $self->{query_object} = undef;
86 12 $self->{size} = undef;
87 12 $self->{table_object} = undef;
88 12 $self->{type} = undef;
89 12 $self->{_init_params_order} = [qw( table_object query_object default pos type prefixed name )];
90 12 $self->{_init_strict_use_sub} = 1;
91 12 $self->{_exception_class} = $EXCEPTION_CLASS;
92 12 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 12 12
93 12 $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 12 return( $self );
99 }
100  
101 11 ▶sub as_string { return( shift->name ); }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
102  
103 # We attach this field to its table object
104 sub attach
105 {
106 0 ▶ my $self = shift( @_ );
Subroutine attach: not called CC=4 SCAR=30.0
Pod documented
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 {
121 0 ▶ my $self = shift( @_ );
Subroutine base_class: not called CC=3 SCAR=20.6
Pod documented
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 3 ▶ 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 {
134 0 ▶ my $self = shift( @_ );
Subroutine clone: not called CC=4 SCAR=30.0
Pod documented
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 34 ▶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 43 ▶sub default { return( shift->_set_get_scalar( 'default', @_ ) ); }
Subroutine default: called CC=1 SCAR=0.0
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 10 ▶sub is_nullable { return( shift->_set_get_boolean( 'is_nullable', @_ ) ); }
Subroutine is_nullable: called CC=1 SCAR=0.0
Pod documented
200  
201 7 ▶sub is_primary { return( shift->_set_get_boolean( 'is_primary', @_ ) ); }
Subroutine is_primary: called CC=1 SCAR=0.0
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 {
207 0 ▶ my $self = shift( @_ );
Subroutine last: not called CC=1 SCAR=6.9
Pod documented
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 {
215 43 ▶ my $self = shift( @_ );
Subroutine name: called CC=10 SCAR=33.2
Pod documented
216 3 ▶ no overloading;
Subroutine BEGIN: called CC=1 SCAR=0.0
217 43 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 12 31 43
218 {
219 12 my $name = shift( @_ );
220 12 ▶ if( !defined( $name ) || !CORE::length( $name // '' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name and length($name // "")) 0 12 12
Condition40%2/5
defined $name and length($name // "")
!ll&&!rl&&r
0012
$name // ""
l!l
120
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 12 $self->{name} = $name;
225 }
226 43 my $name = $self->{name};
227 43 my $alias = $self->query_object->table_alias;
228 43 ▶ if( $self->{prefixed} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'prefixed'}) { } 0 43 43
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 43 return( $name );
249 }
250 }
251  
252 sub next
253 {
254 0 ▶ my $self = shift( @_ );
Subroutine next: not called CC=1 SCAR=6.9
Pod documented
255 0 return( $self->_find_siblings( $self->pos + 1 ) );
256 }
257  
258 227 ▶sub pos { return( shift->_set_get_scalar( 'pos', @_ ) ); }
Subroutine pos: called CC=1 SCAR=0.0
Pod documented
259  
260 sub prefixed
261 {
262 7 ▶ my $self = shift( @_ );
Subroutine prefixed: called CC=5 SCAR=20.1
Pod documented
263 7 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 7 0 7
264 {
265 7 ▶ $self->{prefixed} = ( $_[0] =~ /^\d+$/ ? $_[0] : ( $_[0] ? 1 : 0 ) );
Branch: 2 branches
BranchTrueFalseTotal
$_[0] =~ /^\d+$/ ? : 7 0 7
$_[0] ? : 0 0 0
266 }
267 else
268 {
269 0 $self->{prefixed} = 1;
270 }
271 7 ▶ return( want( 'OBJECT' ) ? $self : $self->{prefixed} );
Branch: 1 branch
BranchTrueFalseTotal
want('OBJECT') ? : 0 7 7
272 }
273  
274 sub prev
275 {
276 0 ▶ my $self = shift( @_ );
Subroutine prev: not called CC=1 SCAR=6.9
Pod documented
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 77 ▶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 7 ▶sub size { return( shift->_set_get_number( 'size', @_ ) ); }
Subroutine size: called CC=1 SCAR=0.0
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 111 ▶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 53 ▶sub type { return( shift->_set_get_scalar( 'type', @_ ) ); }
Subroutine type: called CC=1 SCAR=0.0
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 {
299 0 ▶ my $self = shift( @_ );
Subroutine _find_siblings: not called CC=7 SCAR=40.3
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 {
326 20 ▶ my( $self, $val, $swap, $op ) = @_;
Subroutine _op_overload: called CC=29 SCAR=39.4
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 {
450 0 ▶ my $self = CORE::shift( @_ );
Subroutine FREEZE: not called CC=9 SCAR=45.0
Pod undocumented
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 {
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 ) = @_;
Subroutine THAW: not called CC=11 SCAR=48.8
Pod undocumented
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 3 1;
530 # NOTE: POD
531 __END__
532  
533 =encoding utf8
534  
535 =head1 NAME
536  
537 DB::Object::Fields::Field - Table Field Object
538  
539 =head1 SYNOPSIS
540  
541 my $dbh = DB::Object->connect({
542 driver => 'Pg',
543 conf_file => $conf,
544 database => 'my_shop',
545 host => 'localhost',
546 login => 'super_admin',
547 schema => 'auth',
548 # debug => 3,
549 }) || bailout( "Unable to connect to sql server on host localhost: ", DB::Object->error );
550 my $tbl_object = $dbh->customers || die( "Unable to get the customers table object: ", $dbh->error, "\n" );
551 my $fields = $tbl_object->fields;
552 print( "Fields for table \"", $tbl_object->name, "\": ", Dumper( $fields ), "\n" );
553 my $c = $tbl_object->fo->currency;
554 print( "Got field object for currency: \"", ref( $c ), "\": '$c'\n" );
555 printf( "Name: %s\n", $c->name );
556 printf( "Type: %s\n", $c->type );
557 printf( "Default: %s\n", $c->default );
558 printf( "Is nullable: %s\n", $c->is_nullable );
559 printf( "Is primary key: %s\n", $c->is_primary );
560 printf( "Is an array: %s\n", $c->is_array );
561 printf( "Position: %s\n", $c->pos );
562 # For example for varchar, this could be 255 based on the table schema
563 printf( "Size: %s\n", $c->size );
564 printf( "Table: %s\n", $c->table );
565 printf( "Database: %s\n", $c->database );
566 printf( "Schema: %s\n", $c->schema );
567 printf( "Field comment: %s\n", $c->comment );
568 printf( "Constant value: %s\n", $c->datatype->constant ); # 12
569 printf( "Constant name: %s\n", $c->datatype->name ); # For example: SQL_VARCHAR
570 printf( "Constant type: %s\n", $c->datatype->type ); # varchar
571 printf( "Next field: %s (%s)\n", $c->next, ref( $c->next ) );
572 print( "Showing name fully qualified: ", $c->prefixed(3)->name, "\n" );
573 # would print: my_shop.public.customers.currency
574 print( "Trying again (should keep prefix): ", $c->name, "\n" );
575 # would print again: my_shop.public.customers.currency
576 print( "Now cancel prefixing at the table fields level.\n" );
577 $tbl_object->fo->prefixed( 0 );
578 print( "Showing name fully qualified again (should not be prefixed): ", $c->name, "\n" );
579 # would print currency
580 print( "First element is: ", $c->first, "\n" );
581 print( "Last element is: ", $c->last, "\n" );
582 # Works also with the operators +, -, *, /, %, <, <=, >, >=, !=, <<, >>, &, |, ^, ==
583 my $table = $dbh->dummy;
584 $table->select( $c + 10 ); # SELECT currency + 10 FROM dummy;
585 $c == 'NULL' # currency IS NULL
586  
587 You can also use a L<DB::Object::Statement> as a value in the operation:
588  
589 my $tbl = $dbh->services || die( "Unable to get the table object \"services\": ", $dbh->error );
590 my $userv_tbl = $dbh->user_services || die( "Unable to get the table object \"user_services\": ", $tbl->->error );
591 $tbl->where( $tbl->fo->name == '?' );
592 my $sub_sth = $tbl->select( 'id' ) || die( "Unable to prepare the sql query to get the service id: ", $tbl->error );
593 $userv_tbl->where(
594 $dbh->AND(
595 $tbl->fo->user_id == '?',
596 $tbl->fo->service_id == $sub_sth
597 )
598 );
599 my $query = $userv_tbl->delete->as_string || die( $tbl->error );
600  
601 This would yield:
602  
603 DELETE FROM user_services WHERE user_id = ? AND name = (SELECT id FROM services WHERE name = ?)
604  
605 =head1 VERSION
606  
607 v1.4.0
608  
609 =head1 DESCRIPTION
610  
611 This is a table field object as instantiated by L<DB::Object::Fields>
612  
613 =head1 CONSTRUCTOR
614  
615 =head2 new
616  
617 Takes an hash or hash reference of parameters and this will create a new L<DB::Object::Fields::Field> object.
618  
619 =over 4
620  
621 =item * C<check_name>
622  
623 Specifies the name of the check constraint associated wit this field.
624  
625 =item * C<comment>
626  
627 Specifies the field comment, if any.
628  
629 =item * C<datatype>
630  
631 Specifies an hash of key-value pairs, namely: C<name>, C<constant>, C<type> and C<re>
632  
633 =item * C<debug>
634  
635 Toggles debug mode on/off
636  
637 =item * C<default>
638  
639 Specifies the default field value.
640  
641 =item * C<foreign_name>
642  
643 Specifies the name of the foreign key constraint associated wit this field.
644  
645 =item * C<index_name>
646  
647 Specifies the index name to which this field is related.
648  
649 =item * C<is_array>
650  
651 Specifies a boolean value whether the field value represents an array or not.
652  
653 =item * C<is_check>
654  
655 Specifies a boolean value whether the field is associated with a check constraint or not.
656  
657 =item * C<is_foreign>
658  
659 Specifies a boolean value whether the field is associated with a foreign key constraint or not.
660  
661 =item * C<is_nullable>
662  
663 Specifies a boolean value whether the field value can be null or not.
664  
665 =item * C<is_primary>
666  
667 Specifies a boolean value whether the field is the primary key for its table or not.
668  
669 =item * C<is_unique>
670  
671 Specifies a boolean value whether the field is part of a unique index or not.
672  
673 =item * C<name>
674  
675 The table column name.
676  
677 An error will be returned if this value is not provided upon instantiation.
678  
679 =item * C<pos>
680  
681 The table column position in the table.
682  
683 =item * C<prefixed>
684  
685 Defaults to 0
686  
687 =item * C<query_object>
688  
689 The L<DB::Object::Query> object.
690  
691 =item * C<size>
692  
693 Set the field size, such as for varchar.
694  
695 Defaults to C<undef>
696  
697 =item * C<table_object>
698  
699 The L<DB::Object::Tables> object.
700  
701 An error will be returned if this value is not provided upon instantiation.
702  
703 =item * C<type>
704  
705 The column data type.
706  
707 =back
708  
709 =head1 METHODS
710  
711 =head2 as_string
712  
713 This returns the name of the field, possibly prefixed
714  
715 This is also called to stringify the object
716  
717 print( "Field is: $field\n" );
718  
719 =head2 attach
720  
721 This takes a table object, and attach it to our current object.
722  
723 This returns the current object upon success, or upon error, it sets an L<error object|DB::Object::Exception>, and returns C<undef> in scalar context, or an empty list in list context.
724  
725 =head2 base_class
726  
727 Returns the base class, such as C<DB::Object::Postgres> or C<DB::Object::SQLite>
728  
729 =head2 clone
730  
731 Makes a clone of the object and returns it.
732  
733 However, it does not makes a clone of the entire field object, but instead leaves out the L<query object|DB::Object::Query> and the L<table object|DB::Object::Tables>
734  
735 =head2 constant
736  
737 A data type constant set by L<DB::Object::Table/structure>. This helps determine how to deal with some fields.
738  
739 This is an hash object that contains 3 properties:
740  
741 =over 4
742  
743 =item * C<constant>
744  
745 An integer set by the database driver to represent the constant
746  
747 =item * C<name>
748  
749 The constant name, e.g. C<PG_JSONB>
750  
751 =item * C<type>
752  
753 The data type, e.g. C<jsonb>
754  
755 =back
756  
757 =head2 check_name
758  
759 Sets or gets the optional name of the check constraint associated with this field.
760  
761 =head2 comment
762  
763 Sets or gets the optional comment that may have been set for this table field.
764  
765 =head2 database
766  
767 Sets or gets the name of the database this field is attached to.
768  
769 =head2 database_object
770  
771 Returns the database object, ie the one used to make sql queries
772  
773 =head2 default
774  
775 Sets or gets the default value, if any, for that field.
776  
777 =head2 foreign_name
778  
779 Sets or gets the optional name of the foreign key constraint associated with this field.
780  
781 =head2 first
782  
783 Returns the first field in the table.
784  
785 =head2 index_name
786  
787 Sets or gets the index name to which this field is related. Defaults to C<undef>
788  
789 =head2 is_array
790  
791 Sets or gets true if the field is an array, or false otherwise.
792  
793 =head2 is_check
794  
795 Sets or gets true if the field is associated with a check constraint, or false otherwise.
796  
797 =head2 is_foreign
798  
799 Sets or gets true if the field is associated with a foreign key constraint, or false otherwise.
800  
801 =head2 is_nullable
802  
803 Sets or gets true if the field can be null, or false otherwise.
804  
805 =head2 is_primary
806  
807 Sets or gets true if the field is the primary key of the table, or false otherwise.
808  
809 =head2 is_unique
810  
811 Sets or gets true if the field is part of a unique index, or false otherwise.
812  
813 If it is, check out the value for L<index_name|/index_name>
814  
815 =head2 last
816  
817 Returns the last field in the table.
818  
819 =head2 name
820  
821 Sets or gets the field name. This is also what is returned when object is stringified. For example
822  
823 my $c = $tbl_object->fo->last_name;
824 print( "$c\n" );
825 # will produce "last_name"
826  
827 The output is altered by the use of B<prefixed>. See below.
828  
829 =head2 next
830  
831 Returns the next field object.
832  
833 =head2 pos
834  
835 Sets or gets the position of the field in the table. This is an integer starting from 1.
836  
837 =head2 prefixed
838  
839 Called without argument, this will instruct the field name to be returned prefixed by the table name.
840  
841 print( $tbl_object->fo->last_name->prefixed, "\n" );
842 # would produce my_shop.last_name
843  
844 B<prefixed> can also be called with an integer as argument. 1 will prefix it with the table name, 2 with the schema name and 3 with the database name.
845  
846 =head2 prev
847  
848 Returns the previous field object.
849  
850 =head2 query_object
851  
852 Sets or gets the query object (L<DB::Object::Query> or one of its descendant)
853  
854 =head2 schema
855  
856 Returns the table schema to which this field is attached.
857  
858 =head2 size
859  
860 Sets or gets the size of the field when appropriate, such as when the type is C<varchar> or C<char>
861  
862 =head2 table
863  
864 Returns the table name for this field.
865  
866 =head2 table_name
867  
868 Same as above. This returns the table name.
869  
870 =head2 table_object
871  
872 Sets or gets the table object which is a L<DB::Object::Tables> object.
873  
874 =head2 type
875  
876 Returns the field type such as C<jsonb>, C<json>, C<varchar>, C<integer>, etc.
877  
878 See also L</constant> for an even more accurate data type, and the driver associated constant that is used for binding values to placeholders.
879  
880 =head2 _find_siblings
881  
882 Given a field position from 1 to n, this will find and return the field object. It returns undef or empty list if none could be found.
883  
884 =head1 OVERLOADING
885  
886 The following operators are overloaded:
887  
888 +, -, *, /, %, <, <=, >, >=, !=, <<, >>, lt, gt, le, ge, ne, &, |, ^, ==, eq, ~~
889  
890 Thus a field named "dummy" could be used like:
891  
892 $f + 10
893  
894 which would become:
895  
896 dummy + 10
897  
898 And this works too:
899  
900 10 + $f # 10 + dummy
901  
902 Another example, which works in PostgreSQL:
903  
904 $ip_tbl->where( 'inet 192.16.1.20' << $ip_tbl->fo->ip_addr );
905 my $ref = $ip_tbl->select->fetchrow_hashref;
906  
907 The equal operator C<==> would become C<=>:
908  
909 $f == 'NULL' # dummy = NULL
910  
911 but, if you use perl's C<eq> instead of C<==>, you would get:
912  
913 $f eq 'NULL' # dummy IS NULL
914  
915 Note that you have to take care of quotes yourself, because there is no way to tell if the right hand side is a string or a function
916  
917 $f == q{'JPY'} # dummy IS 'JPY'
918  
919 or, to insert a placeholder
920  
921 $f == '?' # dummy = ?
922 # or;
923 $f eq '?' # dummy IS ?
924 my $sth = $table->select( $f eq '?' ); # SELECT dummy IS ? FROM some_table
925 my $row = $sth->exec( 'JPY' )->fetchrow;
926  
927 of course
928  
929 my $sth = $table->select( dummy => '?' );
930  
931 also works
932  
933 The C<=~> and C<!~> operators cannot be overloaded in perl, so for regular expressions, use the C<REGEXP> function if available, or provided the expression directly as a string:
934  
935 $table->select( "currency ~ '^[A-Z]{3}$'" );
936  
937 If you want to use placeholder in the value provided, you will have to provide a C<?> in the value next to the operator. This module will not parse the value used with the operation, so if you wanted to use a placeholder in:
938  
939 $f == "'JPY'"
940  
941 Simply provide:
942  
943 $f == '?'
944  
945 You can use the search operator C<~~> for SQL Full Text Search and it would be converted into C<@@>:
946  
947 Let's imagine a table C<articles> in a L<PostgreSQL database|https://www.postgresql.org/docs/current/textsearch.html>, such as:
948  
949 CREATE TABLE articles (
950 id SERIAL PRIMARY KEY,
951 title TEXT NOT NULL,
952 content TEXT NOT NULL,
953 ts TSVECTOR GENERATED ALWAYS AS
954 (setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
955 setweight(to_tsvector('english', coalesce(content, '')), 'B')) STORED
956 );
957  
958 them you coud do:
959  
960 $tbl->where(
961 \"websearch_to_tsquery(?)" ~~ $tbl->fo->ts,
962 );
963  
964 and this would create a C<WHERE> clause, such as:
965  
966 WHERE websearch_to_tsquery(?) @@ ts
967  
968 See L<PostgreSQL documentation|https://www.postgresql.org/docs/current/textsearch.html> for more details.
969  
970 but, under L<SQLite|https://www.sqlite.org/fts5.html>, this is not necessary, because the Full Text Search syntax is different:
971  
972 Create a FTS-enabled virtual table.
973  
974 CREATE VIRTUAL TABLE articles
975 USING FTS5(title, content);
976  
977 then query it:
978  
979 SELECT * FROM articles WHERE articles MATCH(?);
980  
981 See L<SQLite documentation|https://www.sqlite.org/fts5.html> for more details.
982  
983 and, in a L<MySQL database|https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html>, also unnecessary, because a bit different:
984  
985 CREATE TABLE articles (
986 id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
987 title TEXT NOT NULL,
988 content TEXT NOT NULL,
989 FULLTEXT (title,content)
990 ) ENGINE=InnoDB;
991  
992 then:
993  
994 SELECT * FROM articles WHERE MATCH(title,content) AGAINST(? IN NATURAL LANGUAGE MODE);
995  
996 See L<MySQL documentation|https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html> for more details.
997  
998 =head1 SEE ALSO
999  
1000 L<perl>
1001  
1002 =head1 AUTHOR
1003  
1004 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
1005  
1006 =head1 COPYRIGHT & LICENSE
1007  
1008 Copyright (c) 2020-2021 DEGUEST Pte. Ltd.
1009  
1010 You can use, copy, modify and redistribute this package and associated
1011 files under the same terms as Perl itself.
1012  
1013 =cut
« lib/DB/Object/Fields/Unknown.pm ↑ Summary blib/lib/DB/Object/Mysql.pm »