lib/DB/Object/Fields.pm

filter: Statementstmt 67.6% 94 / 139 Branchbran 31.8% 21 / 66 Conditioncond 18.5% 10 / 54 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 83.3% 20 / 24 Podpod 60.0% 6 / 10 totaltotal 46.8% 151 / 322 CC 57 SCAR 37.7
CC
57
Cov
48%
THAW
110.0
FREEZE
72.0
AUTOLOAD
64.5
CRAP 43.4
« lib/DB/Object/Constraint/Foreign.pm ↑ Summary blib/lib/DB/Object/Fields.pm »
1 ##----------------------------------------------------------------------------
2 ## Database Object Interface - ~/lib/DB/Object/Fields.pm
3 ## Version v1.3.0
4 ## Copyright(c) 2024 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2020/01/01
7 ## Modified 2026/03/22
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;
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 $EXCEPTION_CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶ use DB::Object::Fields::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶ our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 our $VERSION = 'v1.3.0';
25 };
26  
27 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 sub init
31 {
32 2 ▶ my $self = shift( @_ );
Subroutine init: called CC=2 SCAR=6.9
Pod documented
33 2 $self->{prefixed} = 0;
34 # Property 'query_object' not used anymore. Instead, we use table_object->query_object
35 2 $self->{table_object} = '';
36 # $self->{fatal} = 1;
37 2 $self->{_init_strict_use_sub} = 1;
38 2 $self->{_init_params_order} = [qw( table_object query_object prefixed )];
39 2 $self->{_exception_class} = $EXCEPTION_CLASS;
40 2 $self->SUPER::init( @_ );
41 2 $self->{_fields} = [qw( prefixed )];
42 2 ▶ return( $self->error( "No table object was provided" ) ) if( !$self->{table_object} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"table_object"} 0 2 2
43 2 return( $self );
44 }
45  
46 4 ▶sub database_object { return( shift->table_object->database_object ); }
Subroutine database_object: called CC=1 SCAR=0.0
Pod documented
47  
48 # To manually create a new table field
49 sub new_field
50 {
51 4 ▶ my $self = shift( @_ );
Subroutine new_field: called CC=18 SCAR=39.9
Pod documented
52 4 my $opts = $self->_get_args_as_hash( @_ );
53 4 ▶ if( !exists( $opts->{name} ) ||
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $opts->{'name'} or $self->_is_empty($opts->{'name'})) { } 0 4 4
elsif (not exists $opts->{'type'} or $self->_is_empty($opts->{'type'})) { } 0 4 4
Condition33%1/3
not exists $opts->{"name"} or $self->_is_empty($opts->{"name"})
l!l&&r!l&&!r
004
MC/DC0%0/2
not exists $opts->{"name"} or $self->_is_empty($opts->{"name"})
not exists $opts->{"name"} $self->_is_empty($opts->{"name"})
Truth table33%1/3
not(A) or B
A= not exists $opts->{"name"}B= $self->_is_empty($opts->{"name"})
ABresult
1X1
011
000
54 $self->_is_empty( $opts->{name} ) )
55 {
56 0 return( $self->error( "No field name was provided." ) );
57 }
58 ▶ elsif( !exists( $opts->{type} ) ||
Condition33%1/3
not exists $opts->{"type"} or $self->_is_empty($opts->{"type"})
l!l&&r!l&&!r
004
MC/DC0%0/2
not exists $opts->{"type"} or $self->_is_empty($opts->{"type"})
not exists $opts->{"type"} $self->_is_empty($opts->{"type"})
Truth table33%1/3
not(A) or B
A= not exists $opts->{"type"}B= $self->_is_empty($opts->{"type"})
ABresult
1X1
011
000
59 $self->_is_empty( $opts->{type} ) )
60 {
61 0 return( $self->error( "No field type was provided." ) );
62 }
63 4 $opts->{type} = lc( $opts->{type} );
64 # But actually we ignore it; users should not provide it
65 4 ▶ if( exists( $opts->{constant} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"constant"} and defined $opts->{"constant"} and ref $opts->{"constant"} eq "HASH") 0 4 4
Condition33%2/6
exists $opts->{"constant"} and defined $opts->{"constant"} and ref $opts->{"constant"} eq "HASH"
!ll&&!rl&&r
400
exists $opts->{"constant"} and defined $opts->{"constant"}
!ll&&!rl&&r
400
MC/DC0%0/3
exists $opts->{"constant"} and defined $opts->{"constant"} and ref $opts->{"constant"} eq "HASH"
exists $opts->{"constant"} defined $opts->{"constant"} ref $opts->{"constant"} eq "HASH"
Truth table25%1/4
A and B and C
A= exists $opts->{"constant"}B= defined $opts->{"constant"}C= ref $opts->{"constant"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
66 defined( $opts->{constant} ) &&
67 ref( $opts->{constant} ) eq 'HASH' )
68 {
69 0 $opts->{datatype} = delete( $opts->{constant} );
70 }
71 4 my $datatype_dict = $self->database_object->datatype_dict;
72 4 ▶ if( !exists( $datatype_dict->{ $opts->{type} } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$datatype_dict{$opts->{"type"}}) 0 4 4
73 {
74 0 return( $self->error( "Field type provided ($opts->{type}) is not supported by driver ", $self->database_object->driver ) );
75 }
76 4 my $def = $datatype_dict->{ $opts->{type} };
77 4 ▶ $opts->{debug} = $self->debug unless( exists( $opts->{debug} ) && length( $opts->{debug} // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"debug"} and length($opts->{"debug"} // "") 0 4 4
Condition40%2/5
exists $opts->{"debug"} and length($opts->{"debug"} // "")
!ll&&!rl&&r
004
$opts->{"debug"} // ""
l!l
40
MC/DC0%0/2
exists $opts->{"debug"} and length($opts->{"debug"} // "")
exists $opts->{"debug"} $opts->{"debug"}
Truth table33%1/3
A and B
A= exists $opts->{"debug"}B= $opts->{"debug"}
ABresult
0X0
100
111
78 my $fo = DB::Object::Fields::Field->new(
79 datatype => {
80 constant => $def->{constant},
81 name => $def->{name},
82 type => $def->{type},
83 },
84 debug => $opts->{debug},
85 ( !$self->_is_empty( $opts->{default} ) ? ( default => $opts->{default} ) : () ),
86 ( !$self->_is_empty( $opts->{is_array} ) ? ( is_array => $opts->{is_array} ) : () ),
87 ( !$self->_is_empty( $opts->{is_nullable} ) ? ( is_nullable => $opts->{is_nullable} ) : () ),
88 name => $opts->{name},
89 ( !$self->_is_empty( $opts->{pos} ) ? ( pos => $opts->{pos} ) : () ),
90 query_object => $self->query_object,
91 ( !$self->_is_empty( $opts->{size} ) ? ( size => $opts->{size} ) : () ),
92 table_object => $self->table_object,
93 type => $opts->{type},
94 4 ▶ ) || return( $self->pass_error( DB::Object::Fields::Field->error ) );
Branch: 5 branches
BranchTrueFalseTotal
!$self->_is_empty($opts->{'default'}) ? : 0 4 4
!$self->_is_empty($opts->{'is_array'}) ? : 0 4 4
!$self->_is_empty($opts->{'is_nullable'}) ? : 0 4 4
!$self->_is_empty($opts->{'pos'}) ? : 4 0 4
!$self->_is_empty($opts->{'size'}) ? : 0 4 4
Condition50%1/2
'DB::Object::Fields::Field'->new('datatype', {'constant', $def->{'constant'}, 'name', $def->{'name'}, 'type', $def->{'type'}}, 'debug', $opts->{'debug'}, !$self->_is_empty($opts->{'default'}) ? ('default', $opts->{'default'}) : (), !$self->_is_empty($opts->{'is_array'}) ? ('is_array', $opts->{'is_array'}) : (), !$self->_is_empty($opts->{'is_nullable'}) ? ('is_nullable', $opts->{'is_nullable'}) : (), 'name', $opts->{'name'}, !$self->_is_empty($opts->{'pos'}) ? ('pos', $opts->{'pos'}) : (), 'query_object', $self->query_object, !$self->_is_empty($opts->{'size'}) ? ('size', $opts->{'size'}) : (), 'table_object', $self->table_object, 'type', $opts->{'type'}) || (return $self->pass_error('DB::Object::Fields::Field'->error))
l!l
40
MC/DC0%0/1
'DB::Object::Fields::Field'->new('datatype', {'constant', $def->{'constant'}, 'name', $def->{'name'}, 'type', $def->{'type'}}, 'debug', $opts->{'debug'}, !$self->_is_empty($opts->{'default'}) ? ('default', $opts->{'default'}) : (), !$self->_is_empty($opts->{'is_array'}) ? ('is_array', $opts->{'is_array'}) : (), !$self->_is_empty($opts->{'is_nullable'}) ? ('is_nullable', $opts->{'is_nullable'}) : (), 'name', $opts->{'name'}, !$self->_is_empty($opts->{'pos'}) ? ('pos', $opts->{'pos'}) : (), 'query_object', $self->query_object, !$self->_is_empty($opts->{'size'}) ? ('size', $opts->{'size'}) : (), 'table_object', $self->table_object, 'type', $opts->{'type'}) || (return $self->pass_error('DB::Object::Fields::Field'->error))
'DB::Object::Fields::Field'->new('datatype', {'constant', $def->{'constant'}, 'name', $def->{'name'}, 'type', $def->{'type'}}, 'debug', $opts->{'debug'}, !$self->_is_empty($opts->{'default'}) ? ('default', $opts->{'default'}) : (), !$self->_is_empty($opts->{'is_array'}) ? ('is_array', $opts->{'is_array'}) : (), !$self->_is_empty($opts->{'is_nullable'}) ? ('is_nullable', $opts->{'is_nullable'}) : (), 'name', $opts->{'name'}, !$self->_is_empty($opts->{'pos'}) ? ('pos', $opts->{'pos'}) : (), 'query_object', $self->query_object, !$self->_is_empty($opts->{'size'}) ? ('size', $opts->{'size'}) : (), 'table_object', $self->table_object, 'type', $opts->{'type'})
Truth table50%1/2
A
A= 'DB::Object::Fields::Field'->new('datatype', {'constant', $def->{'constant'}, 'name', $def->{'name'}, 'type', $def->{'type'}}, 'debug', $opts->{'debug'}, !$self->_is_empty($opts->{'default'}) ? ('default', $opts->{'default'}) : (), !$self->_is_empty($opts->{'is_array'}) ? ('is_array', $opts->{'is_array'}) : (), !$self->_is_empty($opts->{'is_nullable'}) ? ('is_nullable', $opts->{'is_nullable'}) : (), 'name', $opts->{'name'}, !$self->_is_empty($opts->{'pos'}) ? ('pos', $opts->{'pos'}) : (), 'query_object', $self->query_object, !$self->_is_empty($opts->{'size'}) ? ('size', $opts->{'size'}) : (), 'table_object', $self->table_object, 'type', $opts->{'type'})
Aresult
11
00
95 4 return( $fo );
96 }
97  
98 sub prefixed
99 {
100 8 ▶ my $self = shift( @_ );
Subroutine prefixed: called CC=7 SCAR=21.0
Pod documented
101 8 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 8 0 8
102 {
103 8 ▶ $self->{prefixed} = ( $_[0] =~ /^\d+$/ ? $_[0] : ( $_[0] ? 1 : 0 ) );
Branch: 2 branches
BranchTrueFalseTotal
$_[0] =~ /^\d+$/ ? : 8 0 8
$_[0] ? : 0 0 0
104 }
105 else
106 {
107 0 $self->{prefixed} = 1;
108 }
109 8 my $fields = $self->table_object->fields;
110 8 foreach my $f ( keys( %$fields ) )
111 {
112 56 ▶ next if( !CORE::length( $self->{ $f } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{$f} 51 5 56
113 5 ▶ next if( !$self->_is_object( $self->{ $f } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_object($self->{$f}) 0 5 5
114 5 my $o = $self->{ $f };
115 5 $o->prefixed( $self->{prefixed} );
116 }
117 8 return( $self );
118 }
119  
120 # sub query_object { return( shift->_set_get_object_without_init( 'query_object', 'DB::Object::Query', @_ ) ); }
121 8 ▶sub query_object { return( shift->table_object->query_object ); }
Subroutine query_object: called CC=1 SCAR=0.0
Pod documented
122  
123 34 ▶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
124  
125 sub _initiate_field_object
126 {
127 2 ▶ my $self = shift( @_ );
Subroutine _initiate_field_object: called CC=5 SCAR=16.4
128 2 ▶ my $field = shift( @_ ) || return( $self->error( "No field was provided to get its object." ) );
Condition50%1/2
shift @_ || (return $self->error('No field was provided to get its object.'))
l!l
20
MC/DC0%0/1
shift @_ || (return $self->error('No field was provided to get its object.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
129 2 ▶ my $class = ref( $self ) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
130 2 my $tbl = $self->table_object;
131 2 my $fields = $tbl->fields;
132 2 ▶ return( $self->error( "Table ", $tbl->name, " has no such field \"$field\"." ) ) if( !CORE::exists( $fields->{ $field } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $fields->{$field} 0 2 2
133 2 my $code = $self->can( $field );
134 2 ▶ unless( defined( $code ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $code) 2 0 2
135 {
136 $code = sub
137 {
138 2 ▶ my $self = shift( @_ );
Subroutine __ANON__: called CC=3 SCAR=11.1
139 2 ▶ unless( $self->{ $field } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$field}) 2 0 2
140 {
141 2 my $fo = $tbl->fields( $field );
142 2 ▶ $fo->debug( ( $self->debug // 0 ) );
Condition50%1/2
$self->debug // 0
l!l
02
MC/DC0%0/1
$self->debug // 0
$self->debug
Truth table50%1/2
A
A= $self->debug
Aresult
11
00
143 2 $fo->prefixed( $self->{prefixed} );
144 2 $fo->query_object( $self->table_object->query_object );
145 2 $fo->table_object( $self->table_object );
146 2 $self->{ $field } = $fo;
147 }
148 2 return( $self->{ $field } );
149 2 };
150 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
151 2 *{"$field"} = $code;
152 }
153 2 my $o = $code->( $self );
154 2 return( $o );
155 }
156  
157 # NOTE: AUTOLOAD
158 AUTOLOAD
159 {
160 2 ▶ my( $method ) = our $AUTOLOAD =~ /([^:]+)$/;
Subroutine AUTOLOAD: called CC=11 SCAR=41.7
161 # my( $class, $method ) = our $AUTOLOAD =~ /^(.*?)::([^\:]+)$/;
162 3 ▶ no overloading;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 2 my $self = shift( @_ );
164 2 my $fields = $self->table_object->fields;
165 2 ▶ if( my $code = $self->can( $method ) )
Branch: 2 branches
BranchTrueFalseTotal
if (my $code = $self->can($method)) { } 0 2 2
elsif (exists $fields->{$method}) { } 2 0 2
166 {
167 0 return( $code->( $self, @_ ) );
168 }
169 elsif( exists( $fields->{ $method } ) )
170 {
171 2 return( $self->_initiate_field_object( $method ) );
172 }
173 else
174 {
175 # This is an unrecoverable error. We have no choice, but to die.
176 0 my $error = "Table " . $self->table_object->name . " has no such field \"$method\"";
177 0 ▶ $self->_load_class( 'Module::Generic::Exception' ) || die( $self->error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Module::Generic::Exception") or die $self->error 0 0 0
178 0 my $exception = Module::Generic::Exception->new( $error );
179 0 my $on_unknown_field = $self->table_object->database_object->unknown_field;
180 0 ▶ if( ref( $on_unknown_field ) eq 'CODE' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $on_unknown_field eq 'CODE') { } 0 0 0
elsif (defined $on_unknown_field and $on_unknown_field eq 'die' || $on_unknown_field eq 'fatal') { } 0 0 0
181 {
182 0 return( $on_unknown_field->({
183 table => $self->table_object,
184 field => $method,
185 error => $exception,
186 }) );
187 }
188 ▶ elsif( defined( $on_unknown_field ) && ( $on_unknown_field eq 'die' || $on_unknown_field eq 'fatal' ) )
Condition0%0/6
defined $on_unknown_field and $on_unknown_field eq "die" || $on_unknown_field eq "fatal"
!ll&&!rl&&r
000
$on_unknown_field eq "die" or $on_unknown_field eq "fatal"
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $on_unknown_field and $on_unknown_field eq "die" || $on_unknown_field eq "fatal"
defined $on_unknown_field $on_unknown_field eq "die" $on_unknown_field eq "fatal"
Truth table0%0/4
A and B or C
A= defined $on_unknown_fieldB= $on_unknown_field eq "die"C= $on_unknown_field eq "fatal"
ABCresult
0XX0
1000
11X1
1011
189 {
190 0 die( $exception );
191 }
192 else
193 {
194 0 ▶ $self->_load_class( 'DB::Object::Fields::Unknown' ) ||
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("DB::Object::Fields::Unknown") or die "$error, and I could not load the module DB::Object::Fields::Unknown: ", $self->error 0 0 0
195 die( "${error}, and I could not load the module DB::Object::Fields::Unknown: ", $self->error );
196 0 ▶ my $unknown = DB::Object::Fields::Unknown->new(
Condition0%0/2
'DB::Object::Fields::Unknown'->new('table', $self->table_object->name, 'error', $exception, 'field', $method) || die("$error, and I could not instantiate a new instance of the module DB::Object::Fields::Unknown: ", 'DB::Object::Fields::Unknown'->error)
l!l
00
MC/DC0%0/1
'DB::Object::Fields::Unknown'->new('table', $self->table_object->name, 'error', $exception, 'field', $method) || die("$error, and I could not instantiate a new instance of the module DB::Object::Fields::Unknown: ", 'DB::Object::Fields::Unknown'->error)
'DB::Object::Fields::Unknown'->new('table', $self->table_object->name, 'error', $exception, 'field', $method)
Truth table0%0/2
A
A= 'DB::Object::Fields::Unknown'->new('table', $self->table_object->name, 'error', $exception, 'field', $method)
Aresult
11
00
197 table => $self->table_object->name,
198 error => $exception,
199 field => $method,
200 ) || die( "${error}, and I could not instantiate a new instance of the module DB::Object::Fields::Unknown: ", DB::Object::Fields::Unknown->error );
201 0 ▶ warn( "Table ", $self->table_object->name, " has no such field \"$method\".\n" ) if( $self->_is_warnings_enabled( 'DB::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("DB::Object") 0 0 0
202 # return( $self->error( "Table ", $self->table_object->name, " has no such field \"$method\"." ) );
203 #die( "Table ", $self->table_object->name, " has no such field \"$method\".\n" );
204 0 return( $unknown );
205 }
206 }
207 };
208  
209 sub FREEZE
210 {
211 0 ▶ my $self = CORE::shift( @_ );
Subroutine FREEZE: not called CC=8 SCAR=42.8
Pod undocumented
212 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
213 0 my $class = CORE::ref( $self );
214  
215 # We keep a strict allow-list to avoid accidentally freezing DBI handles or other
216 # process-local state.
217 0 my @props = @{$self->{_fields}};
218  
219 0 my $hash = {};
220 0 foreach my $prop ( @props )
221 {
222 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
223 defined( $self->{ $prop } ) &&
224 CORE::ref( $self->{ $prop } ) ne 'CODE' )
225 {
226 0 $hash->{ $prop } = $self->{ $prop };
227 }
228 }
229  
230 # Return an array reference rather than a list so this works with Sereal and CBOR.
231 # Before Sereal version 4.023, Sereal did not support multiple values returned.
232 0 ▶ if( $serialiser eq 'Sereal' )
Branch: 1 branch
BranchTrueFalseTotal
if ($serialiser eq "Sereal") 0 0 0
233 {
234 0 require Sereal::Encoder;
235 0 require version;
236  
237 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
238 {
239 0 CORE::return( [$class, $hash] );
240 }
241 }
242  
243 # But Storable wants a list with the first element being the serialised element
244 0 CORE::return( $class, $hash );
245 }
246  
247 0 ▶sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
Pod undocumented
248  
249 0 ▶sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
Pod undocumented
250  
251 sub THAW
252 {
253 # STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue
254 # 'CBOR' as the second value.
255 0 ▶ my( $self, undef, @args ) = @_;
Subroutine THAW: not called CC=10 SCAR=47.0
Pod undocumented
256 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
257 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
258 0 ▶ my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'ARRAY' ? : 0 0 0
259 0 my $new;
260 # Storable pattern requires to modify the object it created rather than returning a new one
261 0 ▶ if( CORE::ref( $self ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
262 {
263 0 foreach( CORE::keys( %$hash ) )
264 {
265 0 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
266 }
267 0 $new = $self;
268 }
269 else
270 {
271 0 $new = CORE::bless( $hash => $class );
272 }
273 0 CORE::return( $new );
274 }
275  
276 3 1;
277 # NOTE: POD
278 __END__
279  
280 =encoding utf8
281  
282 =head1 NAME
283  
284 DB::Object::Fields - Tables Fields Object Accessor
285  
286 =head1 SYNOPSIS
287  
288 my $dbh = DB::Object->connect({
289 driver => 'Pg',
290 conf_file => $conf,
291 database => 'my_shop',
292 host => 'localhost',
293 login => 'super_admin',
294 schema => 'auth',
295 unknown_field => 'fatal',
296 # debug => 3,
297 }) || bailout( "Unable to connect to sql server on host localhost: ", DB::Object->error );
298  
299 my $tbl = $dbh->some_table || die( "No table \"some_table\" could be found: ", $dbh->error, "\n" );
300 my $fo = $tbl->fields_object || die( $tbl->error );
301 my $expr = $fo->id == 2;
302 print "Expression is: $expr\n"; # Expression is: id = 2
303  
304 my $tbl_object = $dbh->customers || die( "Unable to get the customers table object: ", $dbh->error, "\n" );
305 my $fields = $tbl_object->fields;
306 print( "Fields for table \"", $tbl_object->name, "\": ", Dumper( $fields ), "\n" );
307 my $c = $tbl_object->fo->currency;
308 print( "Got field object for currency: \"", ref( $c ), "\": '$c'\n" );
309 printf( "Name: %s\n", $c->name );
310 printf( "Type: %s\n", $c->type );
311 printf( "Default: %s\n", $c->default );
312 printf( "Position: %s\n", $c->pos );
313 printf( "Table: %s\n", $c->table );
314 printf( "Database: %s\n", $c->database );
315 printf( "Schema: %s\n", $c->schema );
316 printf( "Next field: %s (%s)\n", $c->next, ref( $c->next ) );
317 print( "Showing name fully qualified: ", $c->prefixed( 3 )->name, "\n" );
318 ## would print: my_shop.public.customers.currency
319 print( "Trying again (should keep prefix): ", $c->name, "\n" );
320 ## would print again: my_shop.public.customers.currency
321 print( "Now cancel prefixing at the table fields level.\n" );
322 $tbl_object->fo->prefixed( 0 );
323 print( "Showing name fully qualified again (should not be prefixed): ", $c->name, "\n" );
324 ## would print currency
325 print( "First element is: ", $c->first, "\n" );
326 print( "Last element is: ", $c->last, "\n" );
327 # Works also with the operators +, -, *, /, %, <, <=, >, >=, !=, <<, >>, &, |, ^, ==
328 my $table = $dbh->dummy;
329 $table->select( $c + 10 ); # SELECT currency + 10 FROM dummy;
330 $c == 'NULL' # currency IS NULL
331  
332 # if DB::Object unknown_field option is set to fatal, this will die. By default, it will simply be ignored
333 my $unknown_field = $tbl->unknown;
334  
335 =head1 VERSION
336  
337 v1.3.0
338  
339 =head1 DESCRIPTION
340  
341 The purpose of this module is to enable access to the table fields as L<DB::Object::Fields::Field> objects.
342  
343 The way this works is by having L<DB::Object::Tables/fields_object> or L<DB::Object::Tables/fo> for short, dynamically create a class based on the database name and table name. For example if the database driver were C<PostgreSQL>, the database were C<my_shop> and the table C<customers>, the dynamically created package would become C<DB::Object::Postgres::Tables::MyShop::Customers>. This class would inherit from this package L<DB::Object::Fields>.
344  
345 Field objects can than be dynamically instantiated by accessing them, such as (assuming the table object C<$tbl_object> here represent the table C<customers>) C<$tbl_object->fo->last_name>. This will return a L<DB::Object::Fields::Field> object.
346  
347 A note on the design: there had to be a separate this separate package L<DB::Object::Fields>, because access to table fields is done through the C<AUTOLOAD> and the methods within the package L<DB::Object::Tables> and its inheriting packages would clash with the tables fields. This package has very few methods, so the risk of a sql table field clashing with a method name is very limited. In any case, if you have in your table a field with the same name as one of those methods here (see below for the list), then you can instantiate a field object with:
348  
349 $tbl_object->_initiate_field_object( 'last_name' );
350  
351 If you call an unknown field, its behaviour will change depending on the option value C<unknown_field> of L<DB::Object> upon instantiation:
352  
353 =over 4
354  
355 =item * C<ignore> (default)
356  
357 The unknown field will be ignored and a warning will be emitted that this field does not exist in the given database table.
358  
359 =item * C<fatal> or C<die>
360  
361 This will trigger a L</die> using a L<Module::Generic::Exception> object. So you could catch it like this:
362  
363 use Nice::Try;
364  
365 try
366 {
367 # $opts contains the property 'unknown_field' set to 'die'
368 my $dbh = DB::Object::Postgres->connect( $opts ) || die( "Unable to connect" );
369 my $tbl = $dbh->some_table || die( "Unable to get the database table \"some_table\": ", $dbh->error );
370 $tbl->where( $dbh->AND(
371 $tbl->fo->faulty_field == '?',
372 $tbl->fo->status == 'live',
373 ) );
374 my $ref = $tbl->select->fetchrow_hashref;
375 }
376 catch( $e isa( 'Module::Generic::Exception' ) )
377 {
378 die( "Caught error preparing SQL: $e" );
379 }
380 else
381 {
382 die( "Caught some other error." );
383 }
384  
385 =item * C<code reference>
386  
387 When the option C<unknown_field> is set to a code reference, this will be executed and passed an hash reference that will contain 3 properties:
388  
389 =over 8
390  
391 =item 1. C<table>
392  
393 The L<table object|DB::Object::Tables>
394  
395 =item 2. C<field>
396  
397 A regular string containing the unknown field name
398  
399 =item 3. C<error>
400  
401 The L<error object|Module::Generic::Exception>, which includes the error string and a stack trace
402  
403 =back
404  
405 =back
406  
407 By default, the unknown field will be ignored.
408  
409 =head1 CONSTRUCTOR
410  
411 =head2 new
412  
413 Creates a new L<DB::Object::Fields> objects. It may also take an hash like arguments, that also are method of the same name.
414  
415 =over 4
416  
417 =item I<debug>
418  
419 Toggles debug mode on/off
420  
421 =back
422  
423 =head1 METHODS
424  
425 =head2 database_object
426  
427 The database object, which is a L<DB::Object> object or one of its descendant.
428  
429 =head2 new_field
430  
431 This takes an hash or hash reference of parameters and instantiate a new L<DB::Object::Fields::Field> object and return it.
432  
433 If an error occurred, it sets an L<error object|Module::Generic::Exception> and return an empty list in list context or C<undef> in scalar context.
434  
435 =head2 prefixed
436  
437 This si the prefix level, from 0 to 2.
438  
439 2 or higher including the database, higher than 1 includes the schema name and above 0 includes the table name. 0 includes nothing.
440  
441 When this value is changed, it is propagated to all the fields objects.
442  
443 =head2 query_object
444  
445 The query object, which is a L<DB::Object::Query> object or one of its descendant.
446  
447 =head2 table_object
448  
449 The query object, which is a L<DB::Object::Tables> object or one of its descendant.
450  
451 =head2 _initiate_field_object
452  
453 This method is called from C<AUTOLOAD>
454  
455 Provided with a table column name and this will create a new L<DB::Object::Fields::Field> object and add dynamically the associated method for this column in the current package so that next time, it returns the cached object without using C<AUTOLOAD>
456  
457 =head1 AUTOLOAD
458  
459 Called with a column name and this will check if the given column name actually exists in this table. If it does, it will call L</_initiate_field_object> to instantiate a new field object and returns it.
460  
461 If the column does not exist, it returns an error.
462  
463 =head1 SEE ALSO
464  
465 L<perl>
466  
467 =head1 AUTHOR
468  
469 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
470  
471 =head1 COPYRIGHT & LICENSE
472  
473 Copyright (c) 2020-2021 DEGUEST Pte. Ltd.
474  
475 You can use, copy, modify and redistribute this package and associated
476 files under the same terms as Perl itself.
477  
478 =cut
« lib/DB/Object/Constraint/Foreign.pm ↑ Summary blib/lib/DB/Object/Fields.pm »