blib/lib/DB/Object/Fields.pm untested

filter: Statementstmt 0.0% 0 / 151 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 58 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 342 CC 32 SCAR 37.2
CC
32
Cov
0%
new_field
132.0
prefixed
42.0
AUTOLOAD
42.0
CRAP 41.3
1 ##----------------------------------------------------------------------------
2 ## Database Object Interface - ~/lib/DB/Object/Fields.pm
3 ## Version v1.3.1
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2020/01/01
7 ## Modified 2026/08/05
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;
14 BEGIN
15 {
16 use strict;
17 use warnings;
18 warnings::register_categories( 'DB::Object' );
19 use common::sense;
20 use parent qw( Module::Generic );
21 use vars qw( $VERSION $EXCEPTION_CLASS );
22 use DB::Object::Fields::Field;
23 our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
24 our $VERSION = 'v1.3.1';
25 };
26  
27 use strict;
28 use warnings;
29  
30 sub init
31 {
32 my $self = shift( @_ );
33 $self->{prefixed} = 0;
34 # Property 'query_object' not used anymore. Instead, we use table_object->query_object
35 $self->{table_object} = '';
36 # $self->{fatal} = 1;
37 $self->{_init_strict_use_sub} = 1;
38 $self->{_init_params_order} = [qw( table_object query_object prefixed )];
39 $self->{_exception_class} = $EXCEPTION_CLASS;
40 $self->SUPER::init( @_ );
41 $self->{_fields} = [qw( prefixed )];
42 return( $self->error( "No table object was provided" ) ) if( !$self->{table_object} );
43 return( $self );
44 }
45  
46 sub database_object { return( shift->table_object->database_object ); }
47  
48 # To manually create a new table field
49 sub new_field
50 {
51 my $self = shift( @_ );
52 my $opts = $self->_get_args_as_hash( @_ );
53 if( !exists( $opts->{name} ) ||
54 $self->_is_empty( $opts->{name} ) )
55 {
56 return( $self->error( "No field name was provided." ) );
57 }
58 elsif( !exists( $opts->{type} ) ||
59 $self->_is_empty( $opts->{type} ) )
60 {
61 return( $self->error( "No field type was provided." ) );
62 }
63 $opts->{type} = lc( $opts->{type} );
64 # But actually we ignore it; users should not provide it
65 if( exists( $opts->{constant} ) &&
66 defined( $opts->{constant} ) &&
67 ref( $opts->{constant} ) eq 'HASH' )
68 {
69 $opts->{datatype} = delete( $opts->{constant} );
70 }
71 my $datatype_dict = $self->database_object->datatype_dict;
72 if( !exists( $datatype_dict->{ $opts->{type} } ) )
73 {
74 return( $self->error( "Field type provided ($opts->{type}) is not supported by driver ", $self->database_object->driver ) );
75 }
76 my $def = $datatype_dict->{ $opts->{type} };
77 $opts->{debug} = $self->debug unless( exists( $opts->{debug} ) && length( $opts->{debug} // '' ) );
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 ) || return( $self->pass_error( DB::Object::Fields::Field->error ) );
95 return( $fo );
96 }
97  
98 sub prefixed
99 {
100 my $self = shift( @_ );
101 if( @_ )
102 {
103 $self->{prefixed} = ( $_[0] =~ /^\d+$/ ? $_[0] : ( $_[0] ? 1 : 0 ) );
104 }
105 else
106 {
107 $self->{prefixed} = 1;
108 }
109 my $fields = $self->table_object->fields;
110 foreach my $f ( keys( %$fields ) )
111 {
112 next if( !CORE::length( $self->{ $f } ) );
113 next if( !$self->_is_object( $self->{ $f } ) );
114 my $o = $self->{ $f };
115 $o->prefixed( $self->{prefixed} );
116 }
117 return( $self );
118 }
119  
120 # sub query_object { return( shift->_set_get_object_without_init( 'query_object', 'DB::Object::Query', @_ ) ); }
121 sub query_object { return( shift->table_object->query_object ); }
122  
123 sub table_object { return( shift->_set_get_object_without_init( 'table_object', 'DB::Object::Tables', @_ ) ); }
124  
125 sub _initiate_field_object
126 {
127 my $self = shift( @_ );
128 my $field = shift( @_ ) || return( $self->error( "No field was provided to get its object." ) );
129 my $class = ref( $self ) || $self;
130 my $tbl = $self->table_object;
131 my $fields = $tbl->fields;
132 return( $self->error( "Table ", $tbl->name, " has no such field \"$field\"." ) ) if( !CORE::exists( $fields->{ $field } ) );
133 my $code = $self->can( $field );
134 unless( defined( $code ) )
135 {
136 $code = sub
137 {
138 my $self = shift( @_ );
139 unless( $self->{ $field } )
140 {
141 my $fo = $tbl->fields( $field );
142 $fo->debug( ( $self->debug // 0 ) );
143 $fo->prefixed( $self->{prefixed} );
144 $fo->query_object( $self->table_object->query_object );
145 $fo->table_object( $self->table_object );
146 $self->{ $field } = $fo;
147 }
148 return( $self->{ $field } );
149 };
150 no strict 'refs';
151 *{"$field"} = $code;
152 }
153 my $o = $code->( $self );
154 return( $o );
155 }
156  
157 # NOTE: AUTOLOAD
158 AUTOLOAD
159 {
160 my( $method ) = our $AUTOLOAD =~ /([^:]+)$/;
161 # my( $class, $method ) = our $AUTOLOAD =~ /^(.*?)::([^\:]+)$/;
162 no overloading;
163 my $self = shift( @_ );
164 my $fields = $self->table_object->fields;
165 if( my $code = $self->can( $method ) )
166 {
167 return( $code->( $self, @_ ) );
168 }
169 elsif( exists( $fields->{ $method } ) )
170 {
171 return( $self->_initiate_field_object( $method ) );
172 }
173 else
174 {
175 # This is an unrecoverable error. We have no choice, but to die.
176 my $error = "Table " . $self->table_object->name . " has no such field \"$method\"";
177 $self->_load_class( 'Module::Generic::Exception' ) || die( $self->error );
178 my $exception = Module::Generic::Exception->new( $error );
179 my $on_unknown_field = $self->table_object->database_object->unknown_field;
180 if( ref( $on_unknown_field ) eq 'CODE' )
181 {
182 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' ) )
189 {
190 die( $exception );
191 }
192 else
193 {
194 $self->_load_class( 'DB::Object::Fields::Unknown' ) ||
195 die( "${error}, and I could not load the module DB::Object::Fields::Unknown: ", $self->error );
196 my $unknown = DB::Object::Fields::Unknown->new(
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 warn( "Table ", $self->table_object->name, " has no such field \"$method\".\n" ) if( $self->_is_warnings_enabled( 'DB::Object' ) );
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 return( $unknown );
205 }
206 }
207 };
208  
209 sub FREEZE
210 {
211 my $self = CORE::shift( @_ );
212 my $serialiser = CORE::shift( @_ ) // '';
213 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 my @props = @{$self->{_fields}};
218  
219 my $hash = {};
220 foreach my $prop ( @props )
221 {
222 if( CORE::exists( $self->{ $prop } ) &&
223 defined( $self->{ $prop } ) &&
224 CORE::ref( $self->{ $prop } ) ne 'CODE' )
225 {
226 $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 if( $serialiser eq 'Sereal' )
233 {
234 require Sereal::Encoder;
235 require version;
236  
237 if( version->parse( Sereal::Encoder->VERSION ) < version->parse( '4.023' ) )
238 {
239 CORE::return( [$class, $hash] );
240 }
241 }
242  
243 # But Storable wants a list with the first element being the serialised element
244 CORE::return( $class, $hash );
245 }
246  
247 sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
248  
249 sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
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 my( $self, undef, @args ) = @_;
256 my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
257 my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
258 my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
259 my $new;
260 # Storable pattern requires to modify the object it created rather than returning a new one
261 if( CORE::ref( $self ) )
262 {
263 foreach( CORE::keys( %$hash ) )
264 {
265 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
266 }
267 $new = $self;
268 }
269 else
270 {
271 $new = CORE::bless( $hash => $class );
272 }
273 CORE::return( $new );
274 }
275  
276 1;
277 # NOTE: POD
278 __END__