blib/lib/DB/Object/Query/Element.pm untested

filter: Statementstmt 0.0% 0 / 137 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 326 CC 23 SCAR 30.1
CC
23
Cov
0%
fo
72.0
THAW
30.0
attach
20.0
CRAP 20.2
1 ##----------------------------------------------------------------------------
2 ## Database Object Interface - ~/lib/DB/Object/Query/Element.pm
3 ## Version v0.3.1
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2023/07/08
7 ## Modified 2026/03/22
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::Query::Element;
14 BEGIN
15 {
16 use strict;
17 use common::sense;
18 warnings::register_categories( 'DB::Object' );
19 use parent qw( Module::Generic );
20 use vars qw( $VERSION $EXCEPTION_CLASS );
21 our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
22 use Wanted;
23 our $VERSION = 'v0.3.1';
24 };
25  
26 use strict;
27 use warnings;
28  
29 sub init
30 {
31 my $self = shift( @_ );
32 $self->{as_is} = undef;
33 $self->{field} = undef;
34 $self->{format} = undef;
35 $self->{index} = undef;
36 $self->{is_numbered} = 0;
37 $self->{placeholder} = undef;
38 $self->{query_object} = undef;
39 $self->{type} = undef;
40 $self->{value} = undef;
41 $self->{_init_strict_use_sub} = 1;
42 $self->{_init_params_order} = [qw( query_object field placeholder format type value index )];
43 $self->{_exception_class} = $EXCEPTION_CLASS;
44 $self->SUPER::init( @_ ) || return( $self->pass_error );
45 $self->{_fields} = [qw(
46 as_is field format index is_numbered placeholder type value
47 )];
48 return( $self );
49 }
50  
51 sub as_is { return( shift->_set_get_boolean( 'as_is', @_ ) ); }
52  
53 sub attach
54 {
55 my $self = shift( @_ );
56 my $elems = shift( @_ ) ||
57 return( $self->error( "No DB::Object::Query::Elements object was provided." ) );
58 unless( $self->_is_a( $elems => 'DB::Object::Query::Elements' ) )
59 {
60 return( $self->error( "Value provided is not a DB::Object::Query::Elements object." ) );
61 }
62 $self->elements( $elems ) || return( $self->pass_error );
63 $self->debug( $elems->debug );
64 if( my $qo = $elems->query_object )
65 {
66 $self->query_object( $qo ) || return( $self->pass_error );
67 my $tbl = $qo->table_object;
68 my $field = $self->fo;
69 if( $field && $tbl )
70 {
71 $field->attach( $tbl ) || return( $self->pass_error( $field->error ) );
72 }
73 }
74 return( $self );
75 }
76  
77 sub elements { return( shift->_set_get_object_without_init( 'elements', 'DB::Object::Query::Elements', @_ ) ); }
78  
79 # The field name
80 sub field { return( shift->_set_get_scalar_or_object( 'field', 'DB::Object::Fields::Field', @_ ) ); }
81  
82 sub fo
83 {
84 my $self = shift( @_ );
85 if( @_ )
86 {
87 return( $self->error( "Value provided for method fo() in class ", ref( $self ), " is not a DB::Object::Fields::Field object." ) ) if( !$self->_is_a( $_[0] => 'DB::Object::Fields::Field' ) );
88 $self->{fo} = shift( @_ );
89 }
90 return( $self->{fo} ) if( $self->{fo} );
91 my $f = $self->field;
92 my $fo;
93 if( $self->_is_a( $f => 'DB::Object::Fields::Field' ) )
94 {
95 $fo = $f;
96 }
97 elsif( defined( $f ) && CORE::length( $f // '' ) )
98 {
99 $fo = $self->query_object->table_object->fields( $f );
100 }
101 $self->{fo} = $fo if( defined( $fo ) );
102 return( $self->new_null ) if( !defined( $fo ) && Wanted::want( 'OBJECT' ) );
103 return( $fo );
104 }
105  
106 # The formatting for insert, e.g.: field = value, or possibly field = ?, or even field = $1
107 sub format { return( shift->_set_get_scalar_as_object( 'format', @_ ) ); }
108  
109 # The generic representation of this element if we want to bind data to it
110 sub generic { return( shift->_set_get_scalar_as_object( 'generic', @_ ) ); }
111  
112 # If this is a numbered placeholder
113 sub index { return( shift->_set_get_number( { field => 'index', undef_ok => 1 }, @_ ) ); }
114  
115 # sub is_numbered { return( shift->_set_get_boolean( 'is_numbered', @_ ) ); }
116 sub is_numbered
117 {
118 my $self = shift( @_ );
119 my $placeholder = $self->placeholder;
120 return(0) if( !defined( $placeholder ) );
121 my $placeholder_re = $self->query_object->database_object->_placeholder_regexp;
122 if( $placeholder =~ /^$placeholder_re$/ && defined( $+{index} ) )
123 {
124 return(1);
125 }
126 return(0);
127 }
128  
129 # The placeholder, such as ?, $2, ?2, or others supported by the driver
130 sub placeholder { return( shift->_set_get_scalar_as_object({
131 field => 'placeholder',
132 callbacks =>
133 {
134 set => sub
135 {
136 # $val is a scalar object (Module::Generic::Scalar)
137 my( $self, $val ) = @_;
138 my $placeholder_re = $self->query_object->database_object->_placeholder_regexp;
139 return( $val ) if( $self->_is_scalar( $val ) && $self->_can( $val => 'defined' ) && !$val->defined );
140 if( defined( $val ) && "$val" =~ /^(?:$placeholder_re)$/ )
141 {
142 # Could be undef
143 $self->index( $+{index} );
144 }
145 else
146 {
147 $self->index( undef );
148 }
149 return( $val );
150 }
151 }
152 }, @_ ) ); }
153  
154 sub query_object { return( shift->_set_get_object( 'query_object', 'DB::Object::Query', @_ ) ); }
155  
156 # The field data type to be used when binding parameters
157 # sub type { return( shift->_set_get_scalar_as_object( 'type', @_ ) ); }
158 sub type { return( shift->_set_get_scalar_as_object( { field => 'type', callbacks =>
159 {
160 get => sub
161 {
162 my( $self, $val ) = @_;
163 my $field = $self->field;
164 if( ( !defined( $val ) || !$val->defined || !CORE::length( $val // '' ) ) &&
165 $self->_is_a( $field => 'DB::Object::Fields::Field' ) )
166 {
167 $val = $field->datatype->constant;
168 }
169 return( $val );
170 }
171 }}, @_ ) ); }
172  
173 # The value to bind, if any at all. So could be undef
174 sub value { return( shift->_set_get_scalar_as_object( 'value', @_ ) ); }
175  
176 sub FREEZE
177 {
178 my $self = CORE::shift( @_ );
179 my $serialiser = CORE::shift( @_ ) // '';
180 my $class = CORE::ref( $self );
181  
182 # We keep a strict allow-list to avoid accidentally freezing DBI handles or other
183 # process-local state.
184 my @props = @{$self->{_fields}};
185  
186 my $hash = {};
187 foreach my $prop ( @props )
188 {
189 if( CORE::exists( $self->{ $prop } ) &&
190 defined( $self->{ $prop } ) &&
191 CORE::ref( $self->{ $prop } ) ne 'CODE' )
192 {
193 $hash->{ $prop } = $self->{ $prop };
194 }
195 }
196  
197 # Return an array reference rather than a list so this works with Sereal and CBOR.
198 # Before Sereal version 4.023, Sereal did not support multiple values returned.
199 if( $serialiser eq 'Sereal' )
200 {
201 require Sereal::Encoder;
202 require version;
203  
204 if( version->parse( Sereal::Encoder->VERSION ) < version->parse( '4.023' ) )
205 {
206 CORE::return( [$class, $hash] );
207 }
208 }
209  
210 # But Storable wants a list with the first element being the serialised element
211 CORE::return( $class, $hash );
212 }
213  
214 sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
215  
216 sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
217  
218 sub THAW
219 {
220 # STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue
221 # 'CBOR' as the second value.
222 my( $self, undef, @args ) = @_;
223 my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
224 my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
225 my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
226 my $new;
227 # Storable pattern requires to modify the object it created rather than returning a new one
228 if( CORE::ref( $self ) )
229 {
230 foreach( CORE::keys( %$hash ) )
231 {
232 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
233 }
234 $new = $self;
235 }
236 else
237 {
238 $new = CORE::bless( $hash => $class );
239 }
240 CORE::return( $new );
241 }
242  
243 1;
244 # NOTE: POD
245 __END__