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

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 63 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 290 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 ##
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::Query::Element;
15 BEGIN
16 {
17 use strict;
18 use common::sense;
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__
246  
247 =encoding utf-8
248  
249 =head1 NAME
250  
251 DB::Object::Query::Element - Database Object Interface
252  
253 =head1 SYNOPSIS
254  
255 use DB::Object::Query::Element;
256 my $this = DB::Object::Query::Element->new(
257 # a scalar, or an DB::Object::Fields::Field object
258 field => $some_sql_field,
259 # designed to be used for insert statements
260 format => $some_format,
261 # The position, if any, of this new object
262 # This is used for numbered placeholders only,
263 # such as $1, $2, or ?1, ?2 depending on the driver
264 index => $integer,
265 # Could also be $1, $2, ?1, ?2 depending on the driver
266 placeholder => '?',
267 # a DB::Object::Query object
268 query_object => $object,
269 type => $sql_type,
270 value => $some_value,
271 ) || die( DB::Object::Query::Element->error );
272  
273 =head1 VERSION
274  
275 v0.3.1
276  
277 =head1 DESCRIPTION
278  
279 This class represent a L<query|DB::Object::Query> element as used throughout this API. It can represent the formatting of some part of an insert query, or some placeholder and its type and field, or just a field and its value, or a combination of those.
280  
281 It makes it more efficient to build query with their associated binded values and types in the proper order, possibly using numbered placeholder, if the SQL driver (such as L<PostgreSQL|DBD::Pg> or L<SQLite|DBD::sqlite>) support them.
282  
283 =head1 CONSTRUCTOR
284  
285 =head2 new
286  
287 Takes an hash or hash reference of key-value pairs matching any of the methods below.
288  
289 Returns a newly instantiated object upon success, or sets an L<error|Module::Generic/error> and return C<undef> or an empty list, depending on the caller's context.
290  
291 =head1 METHODS
292  
293 =head2 as_is
294  
295 Sets or gets the boolean value. If true, then the format specified will be used as-is during execution. This is aimed for value representing SQL functions or other values passed through that the user wants no optimisation.
296  
297 For example:
298  
299 my $tbl = $dbh->some_table || die( "No table found" );
300 $tbl->where( user_id => '?' );
301 my $sth = $tbl->update( updated => \'NOW()' ) || die( $tbl->error );
302 my $rv = $sth->exec( $user_id ) || die( $sth->error );
303  
304 In the example above, when C<NOW()> was specified as a scalar reference, it indicates we want the value to be used I<as-is>. This would translate to something like:
305  
306 UPDATE some_table SET updated = NOW() WHERE user_id = 'b9c01f91-8094-462c-9f49-a0340dc9fcec'
307  
308 =head2 attach
309  
310 This takes a L<DB::Object::Query::Elements> object, and attach it to our current object.
311  
312 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.
313  
314 =head2 elements
315  
316 Sets or gets an L<DB::Object::Query::Elements> object. By default this is C<undef> and is used when this element represent a sub-query.
317  
318 =head2 field
319  
320 Sets or gets the element SQL field (or column) name. It can also be set to a L<DB::Object::Fields::Field> object.
321  
322 =head2 fo
323  
324 my $field_object = $el->fo;
325 $el->fo( $field_object );
326  
327 Sets or gets a L<table field object|DB::Object::Fields::Field>.
328  
329 If no field is set, then in accessor mode, this will retrieve the L<table field object|DB::Object::Fields::Field> for the L<field|/field> value currently set by calling L<fields|DB::Object::Tables> and passing it the field name set in L</field>, if any.
330  
331 If the value set in L</field> is already a L<table field object|DB::Object::Fields::Field>, then this is used instead of course.
332  
333 If nothing was found, it returns C<undef>, but if this method is called in object context, such as chaining, then a L<null object|Module::Generic::Null> will be returned instead to prevent a hard perl error.
334  
335 For example, assuming no L<field object|DB::Object::Fields::Field> could be found, the call below would normally return an error that C<name> cannot be called on an undefined value, but here it detects the call is in an object context and returns L<null object|Module::Generic::Null> allowing a fake C<name> method to be called and that will return C<undef>
336  
337 $el->fo->name;
338  
339 Once found the value is cached, so if called many times, there is no performance penalty.
340  
341 =head2 format
342  
343 Sets or gets the element formatting. This is used for insert statements.
344  
345 It returns a L<scalar object|Module::Generic::Scalar>
346  
347 =head2 generic
348  
349 Returns a string representing the element with placeholder. This does not mean this element is using a placeholder, but rather provides a generic representation to be used when binding data to it.
350  
351 The string returned is an object of L<Module::Generic::Scalar>
352  
353 =head2 index
354  
355 Sets or gets the placeholder index position.
356  
357 This is used if this element represents a placeholder and it is a numbered one, such as C<$1>, C<$2>, or C<?1>, C<?2> depending on what the driver supports.
358  
359 Returns the current value, which is by default C<undef>, or a L<Module::Generic::Number> object.
360  
361 =head2 is_numbered
362  
363 Read-only. Returns true (C<1>) if the element represent a placeholder and it is a numbered one, such as C<$1>, C<$2>, or C<?1>, C<?2> depending on what the driver supports, or false (C<0>) otherwise.
364  
365 =head2 placeholder
366  
367 Sets or gets the element placeholder, such as C<?>, or C<$1>, C<$2>, or C<?1>, C<?2> depending on what the driver supports.
368  
369 When a value is set, it will check if this is a numbered placeholder and set the value for L</index> accordingly.
370  
371 =head2 query_object
372  
373 Sets or gets the L<DB::Object::Query> object set for this object.
374  
375 =head2 type
376  
377 Sets or gets the field SQL type.
378  
379 Returns a L<scalar object|Module::Generic::Scalar> object.
380  
381 =head2 value
382  
383 Sets or gets the element value.
384  
385 Returns a L<scalar object|Module::Generic::Scalar> object.
386  
387 =head1 AUTHOR
388  
389 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
390  
391 =head1 SEE ALSO
392  
393 L<perl>
394  
395 =head1 COPYRIGHT & LICENSE
396  
397 Copyright(c) 2023 DEGUEST Pte. Ltd.
398  
399 All rights reserved
400 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
401  
402 =cut