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

filter: Statementstmt 0.0% 0 / 208 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 78 MC/DCmcdc 0.0% 0 / 43 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 452 CC 33 SCAR 25.5
CC
33
Cov
0%
push
30.0
THAW
30.0
merge
20.0
CRAP 12.8
1 ##----------------------------------------------------------------------------
2 ## Database Object Interface - ~/lib/DB/Object/Query/Elements.pm
3 ## Version v0.2.1
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2023/07/08
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::Query::Elements;
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 our $VERSION = 'v0.2.1';
23 };
24  
25 use strict;
26 use warnings;
27  
28 sub init
29 {
30 my $self = shift( @_ );
31 $self->{elements} = [];
32 $self->{query_object} = undef;
33 $self->{_init_strict_use_sub} = 1;
34 $self->{_exception_class} = $EXCEPTION_CLASS;
35 $self->SUPER::init( @_ ) || return( $self->pass_error );
36 $self->{_fields} = [qw( elements )];
37 $self->{_cache_fields} = '';
38 $self->{_cache_formats} = '';
39 $self->{_cache_types} = '';
40 $self->{_cache_values} = '';
41 return( $self );
42 }
43  
44 # We attach this elements collection to its query object
45 sub attach
46 {
47 my $self = shift( @_ );
48 # Query object
49 my $qo = shift( @_ ) ||
50 return( $self->error( "No DB::Object::Query object was provided." ) );
51 unless( $self->_is_a( $qo => 'DB::Object::Query' ) )
52 {
53 return( $self->error( "Value provided is not a DB::Object::Query object." ) );
54 }
55 $self->query_object( $qo ) || return( $self->pass_error );
56 $self->debug( $qo->debug );
57 # We propagate ourself to all the elements in the collection.
58 if( my $elems = $self->elements )
59 {
60 foreach my $elem ( @$elems )
61 {
62 $elem->attach( $self );
63 }
64 }
65 return( $self );
66 }
67  
68 # This is used to forward unknown method to Module::Generic::Array
69 sub autoload
70 {
71 my $self = shift( @_ );
72 my $meth = shift( @_ ) || return;
73 my $elems = $self->elements || return;
74 my $code;
75 if( $code = $elems->can( $meth ) )
76 {
77 no strict 'refs';
78 my $sub = sub
79 {
80 my $this = shift( @_ );
81 return( $code->( $this->elements, @_ ) );
82 };
83 *$meth = $sub;
84 return( $sub );
85 }
86 warn( "No method '$meth' supported by this class ", ref( $self ), " or by ", ref( $elems ) ) if( $self->_is_warnings_enabled( 'DB::Object' ) );
87 return;
88 }
89  
90 # NOTE: sub clone is inherited from Module::Generic
91  
92 sub elements { return( shift->reset(@_)->_set_get_object_array_object( 'elements', 'DB::Object::Query::Element', @_ ) ); }
93  
94 sub fields
95 {
96 my $self = shift( @_ );
97 return( $self->{_cache_fields} ) if( $self->{_cache_fields} && !CORE::length( $self->{_reset} // '' ) );
98 my $arr = $self->new_array;
99 my $e = $self->elements;
100 $e->foreach(sub
101 {
102 my $o = shift( @_ );
103 $arr->push( defined( $o ) ? $o->field : undef );
104 });
105 $self->{_cache_fields} = $arr;
106 CORE::delete( $self->{_reset} );
107 return( $arr );
108 }
109  
110 sub for { return( shift->elements->for( @_ ) ); }
111  
112 sub foreach { return( shift->elements->foreach( @_ ) ); }
113  
114 sub formats
115 {
116 my $self = shift( @_ );
117 return( $self->{_cache_formats} ) if( $self->{_cache_formats} && !CORE::length( $self->{_reset} // '' ) );
118 my $arr = $self->new_array;
119 my $e = $self->elements;
120 $e->foreach(sub
121 {
122 my $o = shift( @_ );
123 $arr->push( defined( $o ) ? $o->format : undef );
124 });
125 $self->{_cache_formats} = $arr;
126 CORE::delete( $self->{_reset} );
127 return( $arr );
128 }
129  
130 sub generics
131 {
132 my $self = shift( @_ );
133 return( $self->{_cache_generics} ) if( $self->{_cache_generics} && !CORE::length( $self->{_reset} // '' ) );
134 my $arr = $self->new_array;
135 my $e = $self->elements;
136 $e->foreach(sub
137 {
138 my $o = shift( @_ );
139 $arr->push( defined( $o ) ? $o->generic : '?' );
140 });
141 $self->{_cache_generics} = $arr;
142 CORE::delete( $self->{_reset} );
143 return( $arr );
144 }
145  
146 sub is_empty { return( shift->elements->is_empty ); }
147  
148 sub length { return( shift->elements->length ); }
149  
150 # This merge one or more objects of the same class into this one
151 sub merge
152 {
153 my $self = shift( @_ );
154 my $elems = $self->elements;
155 CORE::foreach my $this ( @_ )
156 {
157 if( !$self->_is_a( $this => 'DB::Object::Query::Elements' ) )
158 {
159 warn( "Element provided '", overload::StrVal( $this ), "' is not an DB::Object::Query::Elements or DB::Object::Query::Clause object" ) if( $self->_is_warnings_enabled( 'DB::Object' ) );
160 next;
161 }
162  
163 $elems->push( $this->elements->list );
164 }
165 # Make sure our elements are in the right order
166 $self->_sort;
167 $self->reset(1) if( scalar( @_ ) );
168 return( $self );
169 }
170  
171 sub new_element
172 {
173 my $self = shift( @_ );
174 my $el = $self->query_object->new_element( @_ ) ||
175 return( $self->pass_error( $self->query_object->error ) );
176 return( $el );
177 }
178  
179 sub push
180 {
181 my $self = shift( @_ );
182 my $e = $self->elements;
183 CORE::foreach my $elem ( @_ )
184 {
185 my( $o );
186 # This works also for DB::Object::Query::Clause that inherits from DB::Object::Query::Elements
187 if( $self->_is_a( $elem => 'DB::Object::Query::Elements' ) )
188 {
189 $self->push( $elem->elements->list );
190 next;
191 }
192 elsif( $self->_is_a( $elem => 'DB::Object::Query::Element' ) )
193 {
194 $o = $elem;
195 }
196 elsif( ref( $elem ) eq 'HASH' )
197 {
198 $o = $self->new_element( %$elem ) || return( $self->pass_error );
199 }
200 else
201 {
202 return( $self->error( "Unknown value '", ( $elem // 'undef' ), "' to add as element to our clause elements stack." ) );
203 }
204  
205 # Order does not really matter, we just stack them, and after we will sort them by their object property 'index' value, if necessary
206 $e->push( $o );
207 }
208 # Make sure our elements are in the right order
209 $self->_sort;
210 $self->reset(1) if( scalar( @_ ) );
211 return( $self );
212 }
213  
214 sub query_object { return( shift->_set_get_object( 'query_object', 'DB::Object::Query', @_ ) ); }
215  
216 sub reset
217 {
218 my $self = shift( @_ );
219 if( !CORE::length( $self->{_reset} // '' ) && scalar( @_ ) )
220 {
221 $self->{_reset} = scalar( @_ );
222 }
223 return( $self );
224 }
225  
226 sub sort
227 {
228 my $self = shift( @_ );
229 my $elems = $self->elements->sort(sub
230 {
231 my( $a, $b ) = @_;
232 return( $a->index <=> $b->index );
233 });
234 my $clone = $self->clone;
235 $clone->elements( $elems );
236 return( $clone );
237 }
238  
239 sub types
240 {
241 my $self = shift( @_ );
242 return( $self->{_cache_types} ) if( $self->{_cache_types} && !CORE::length( $self->{_reset} // '' ) );
243 my $arr = $self->new_array;
244 my $e = $self->elements;
245 $e->foreach(sub
246 {
247 my $o = shift( @_ );
248 $arr->push( defined( $o->type ) ? $o : undef );
249 });
250 $self->{_cache_types} = $arr;
251 CORE::delete( $self->{_reset} );
252 return( $arr );
253 }
254  
255 sub values
256 {
257 my $self = shift( @_ );
258 return( $self->{_cache_values} ) if( $self->{_cache_values} && !CORE::length( $self->{_reset} // '' ) );
259 my $arr = $self->new_array;
260 my $e = $self->elements;
261 $e->foreach(sub
262 {
263 my $o = shift( @_ );
264 $arr->push( defined( $o ) ? $o->value : undef );
265 });
266 $self->{_cache_values} = $arr;
267 CORE::delete( $self->{_reset} );
268 return( $arr );
269 }
270  
271 sub _sort
272 {
273 my $self = shift( @_ );
274 my $e = $self->elements;
275 my $el;
276 # We check the first element to decide whether the placeholders, if any, are numbered or not.
277 $e->foreach(sub
278 {
279 my $this = shift( @_ );
280 if( $self->_is_object( $this ) &&
281 $self->_can( $this => 'is_numbered' ) &&
282 $this->is_numbered )
283 {
284 $el = $this;
285 return(0);
286 }
287 });
288 if( $el && $el->placeholder && $el->is_numbered )
289 {
290 my $sorted = $self->sort->elements;
291 $self->elements( $sorted ) if( $sorted );
292 }
293 return( $self );
294 }
295  
296 sub FREEZE
297 {
298 my $self = CORE::shift( @_ );
299 my $serialiser = CORE::shift( @_ ) // '';
300 my $class = CORE::ref( $self );
301  
302 # We keep a strict allow-list to avoid accidentally freezing DBI handles or other
303 # process-local state.
304 my @props = @{$self->{_fields}};
305  
306 my $hash = {};
307 foreach my $prop ( @props )
308 {
309 if( CORE::exists( $self->{ $prop } ) &&
310 defined( $self->{ $prop } ) &&
311 CORE::ref( $self->{ $prop } ) ne 'CODE' )
312 {
313 $hash->{ $prop } = $self->{ $prop };
314 }
315 }
316  
317 # Return an array reference rather than a list so this works with Sereal and CBOR.
318 # Before Sereal version 4.023, Sereal did not support multiple values returned.
319 if( $serialiser eq 'Sereal' )
320 {
321 require Sereal::Encoder;
322 require version;
323  
324 if( version->parse( Sereal::Encoder->VERSION ) < version->parse( '4.023' ) )
325 {
326 CORE::return( [$class, $hash] );
327 }
328 }
329  
330 # But Storable wants a list with the first element being the serialised element
331 CORE::return( $class, $hash );
332 }
333  
334 sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
335  
336 sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
337  
338 sub THAW
339 {
340 # STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue
341 # 'CBOR' as the second value.
342 my( $self, undef, @args ) = @_;
343 my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
344 my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
345 my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
346 my $new;
347 # Storable pattern requires to modify the object it created rather than returning a new one
348 if( CORE::ref( $self ) )
349 {
350 foreach( CORE::keys( %$hash ) )
351 {
352 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
353 }
354 $new = $self;
355 }
356 else
357 {
358 $new = CORE::bless( $hash => $class );
359 }
360 CORE::return( $new );
361 }
362  
363 1;
364 # NOTE: POD
365 __END__