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

filter: Statementstmt 0.0% 0 / 139 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 64 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 337 CC 30 SCAR 46.8
CC
30
Cov
0%
merge
306.0
as_string
42.0
THAW
30.0
CRAP 108.2
1 ##----------------------------------------------------------------------------
2 ## Database Object Interface - ~/lib/DB/Object/Query/Clause.pm
3 ## Version v1.1.2
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::Clause;
14 BEGIN
15 {
16 use strict;
17 use common::sense;
18 warnings::register_categories( 'DB::Object' );
19 use parent qw( DB::Object::Query::Elements );
20 use vars qw( $VERSION $EXCEPTION_CLASS );
21 use overload (
22 '""' => 'as_string',
23 'bool' => sub{1},
24 fallback => 1,
25 );
26 our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
27 our $VERSION = 'v1.1.2';
28 };
29  
30 use strict;
31 use warnings;
32  
33 sub init
34 {
35 my $self = shift( @_ );
36 $self->{generic} = '';
37 $self->{operator} = '';
38 $self->{type} = '';
39 $self->{value} = '';
40 $self->{_init_strict_use_sub} = 1;
41 # $self->{fields} = [];
42 $self->{_exception_class} = $EXCEPTION_CLASS;
43 $self->SUPER::init( @_ ) || return( $self->pass_error );
44 $self->{_clause_reset} = '';
45 $self->{_fields} = [qw( generic operator type value )];
46 return( $self );
47 }
48  
49 sub as_string
50 {
51 # no overloading;
52 my $self = shift( @_ );
53 my $fields = $self->fields;
54 if( $self->generic->length && $self->query_object->table_object->use_bind )
55 {
56 return( $self->{_cache_clause_generic} ) if( $self->{_cache_clause_generic} && !CORE::length( $self->{_reset} // '' ) );
57 CORE::delete( $self->{_reset} );
58 return( $self->{_cache_clause_generic} = $self->generic ) if( !$fields->length );
59 return( $self->{_cache_clause_generic} = Module::Generic::Scalar->new( CORE::sprintf( $self->generic, @$fields ) ) );
60 }
61 return( $self->{_cache_clause} ) if( $self->{_cache_clause} && !CORE::length( $self->{_reset} // '' ) );
62 CORE::delete( $self->{_reset} );
63 my $str = $self->value;
64 return( $self->{_cache_clause} = $str ) if( !length( $str // '' ) || $str !~ /(?<!\%)\%\s\d/ );
65 # Stringification of the fields will automatically format them properly, ie with a table prefix, schema prefix, database prefix as necessary
66 return( $self->{_cache_clause} = Module::Generic::Scalar->new( CORE::sprintf( $str, @$fields ) ) );
67 }
68  
69 # sub bind
70 # {
71 # return( shift->_set_get_class( 'bind',
72 # {
73 # # The sql types of the value bound to the placeholders
74 # types => { type => 'array_as_object' },
75 # # The values bound to the placeholders in the sql clause
76 # values => { type => 'array_as_object' },
77 # }, @_ ) );
78 # }
79 sub bind { warn( "Call to ", ref( $_[0] ), "->bind is now deprecated." ) if( warnings::enabled( 'DB::Object' ) ); }
80  
81 # sub fields { return( shift->_set_get_array_as_object( 'fields', @_ ) ); }
82 # NOTE: sub fields is inherited from DB::Object::Query::Elements
83  
84 sub generic { return( shift->_set_get_scalar_as_object( 'generic', @_ ) ); }
85  
86 sub length { return( shift->value->length ); }
87  
88 sub metadata { return( shift->_set_get_hash_as_object( 'metadata', @_ ) ); }
89 # sub metadata { return( shift->_set_get_hash( 'metadata', @_ ) ); }
90  
91 # NOTE: sub merge supersedes the one inherited from DB::Object::Query::Elements
92 # This takes or or more values and merge its clauses and the binded parameters
93 sub merge
94 {
95 my $self = shift( @_ );
96 if( @_ )
97 {
98 # By default
99 my $op = 'AND';
100 my @params = ();
101 # $clause->merge( $dbh->OR( $clause1, $clause2, $clause3 ) );
102 # or just
103 # $clause->merge( $clause1, $clause2, $clause3 );
104 if( $self->_is_object( $_[0] ) && $_[0]->isa( 'DB::Object::Operator' ) )
105 {
106 my $op_obj = shift( @_ );
107 return( $self->error( "Database Object operator provided is invalid. It should be either an AND or OR." ) ) if( $op_obj->operator ne 'AND' and $op_obj->operator ne 'OR' and $op_obj->operator ne 'NOT' );
108 $op = $op_obj->operator;
109 @params = grep( !$self->_is_a( 'DB::Object::Fields::Unknown' ), $op_obj->value );
110 }
111 else
112 {
113 @params = @_;
114 }
115  
116 my @clause = ();
117 @clause = ( $self->value ) if( $self->value->length > 0 );
118 my @generic = ();
119 @generic = ( $self->generic ) if( $self->generic->length > 0 );
120 my $elems = $self->elements;
121 foreach my $this ( @params )
122 {
123 # Safeguard against garbage
124 # Special treatment for DB::Object::Fields::Overloaded who are already formatted
125 if( $self->_is_a( $this => [qw( DB::Object::Fields::Overloaded DB::Object::Expression )] ) )
126 {
127 push( @clause, $this );
128 # Also transfer any bind element so execute() can find the placeholder types
129 $elems->push( $this ) if( $self->_is_a( $this => 'DB::Object::Fields::Overloaded' ) && $this->placeholder );
130 next;
131 }
132  
133 next if( !$self->_is_a( $this => 'DB::Object::Query::Clause' ) );
134 # First check we even have a clause, otherwise skip
135 if( !$this->value->length )
136 {
137 CORE::next;
138 }
139 # where, order, group, limit etc...
140 if( $self->type->length && $this->type->length && $this->type ne $self->type )
141 {
142 return( $self->error( "This clause provided for merge is not of the same type \"", $this->type, "\" as ours \"", $self->type, "\"." ) );
143 }
144 # Possibly our type is empty and if so, we initiate it by using the type of the first object we find
145 # This makes it convenient to merge without having to set the type beforehand like so:
146 # $clause->type( 'where' );
147 # $clause->merge( $w1, $w2, $e3 );
148 # We can do instead
149 # $clause->merge( $w1, $w2, $e3 );
150 # And it will take the type from $w1
151 $self->type( $this->type ) if( !$self->type->length );
152 if( !$this->operator->is_empty && $this->operator ne $op )
153 {
154 CORE::push( @clause, '( ' . $this->value . ' )' );
155 }
156 else
157 {
158 CORE::push( @clause, $this->value );
159 }
160  
161 if( !$this->generic->is_empty )
162 {
163 if( !$this->operator->is_empty && $this->operator ne $op )
164 {
165 CORE::push( @generic, '( ' . $this->generic . ' )' );
166 }
167 else
168 {
169 CORE::push( @generic, $this->generic );
170 }
171 }
172  
173 # we just stack them, and later we will sort them by their object property 'index' value.
174 $this->elements->for(sub
175 {
176 my( $i, $v ) = @_;
177 # This is perfectly ok. It just means there is nothing at this offset
178 if( !defined( $v ) )
179 {
180 return;
181 }
182 # If an entry with same index is provided, it overwrite the previous one.
183 # elsif( $v->is_numbered )
184 # {
185 # $elems->[$i] = $v;
186 # }
187 # else
188 # {
189 # $elems->push( $v );
190 # }
191 $elems->push( $v );
192 });
193  
194 # $self->fields->push( @{$this->fields} ) if( $this->fields->length );
195 # $self->bind->types->push( @{$this->bind->types} ) if( $this->bind->types->length );
196 # $self->bind->values->push( @{$this->bind->values} ) if( $this->bind->values->length );
197 # $ref returned here is a DB::Object::Query::Clause::Metadata object made from Module::Generic::Hash
198 my $ref = $this->metadata;
199 my $hash = $self->metadata;
200 foreach my $k ( keys( %$ref ) )
201 {
202 $hash->{ $k } = $ref->{ $k } if( !CORE::exists( $hash->{ $k } ) );
203 }
204 $self->metadata( $hash );
205 }
206 $self->value( CORE::join( " $op ", @clause ) );
207 $self->generic( CORE::join( " $op ", @generic ) );
208 $self->operator( $op );
209 }
210 return( $self );
211 }
212  
213 sub operator { return( shift->_set_get_scalar_as_object( 'operator', @_ ) ); }
214  
215 # NOTE: sub push is inherited from DB::Object::Query::Elements
216  
217 # NOTE: sub query_object is inherited from DB::Object::Query::Elements
218  
219 # NOTE: sub reset is inherited from DB::Object::Query::Elements
220  
221 # The clause type e.g. where, order, group, having, limit, etc
222 sub type { return( shift->_set_get_scalar_as_object( 'type', @_ ) ); }
223  
224 # NOTE: sub types is inherited from DB::Object::Query::Elements
225  
226 # The string value of the clause
227 sub value { return( shift->_set_get_scalar_as_object( 'value', @_ ) ); }
228  
229 # NOTE: sub values is inherited from DB::Object::Query::Elements
230  
231 sub FREEZE
232 {
233 my $self = CORE::shift( @_ );
234 my $serialiser = CORE::shift( @_ ) // '';
235 my $class = CORE::ref( $self );
236  
237 # We keep a strict allow-list to avoid accidentally freezing DBI handles or other
238 # process-local state.
239 my @props = @{$self->{_fields}};
240  
241 my $hash = {};
242 foreach my $prop ( @props )
243 {
244 if( CORE::exists( $self->{ $prop } ) &&
245 defined( $self->{ $prop } ) &&
246 CORE::ref( $self->{ $prop } ) ne 'CODE' )
247 {
248 $hash->{ $prop } = $self->{ $prop };
249 }
250 }
251  
252 # Return an array reference rather than a list so this works with Sereal and CBOR.
253 # Before Sereal version 4.023, Sereal did not support multiple values returned.
254 if( $serialiser eq 'Sereal' )
255 {
256 require Sereal::Encoder;
257 require version;
258  
259 if( version->parse( Sereal::Encoder->VERSION ) < version->parse( '4.023' ) )
260 {
261 CORE::return( [$class, $hash] );
262 }
263 }
264  
265 # But Storable wants a list with the first element being the serialised element
266 CORE::return( $class, $hash );
267 }
268  
269 sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
270  
271 sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
272  
273 sub THAW
274 {
275 # STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue
276 # 'CBOR' as the second value.
277 my( $self, undef, @args ) = @_;
278 my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
279 my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
280 my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
281 my $new;
282 # Storable pattern requires to modify the object it created rather than returning a new one
283 if( CORE::ref( $self ) )
284 {
285 foreach( CORE::keys( %$hash ) )
286 {
287 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
288 }
289 $new = $self;
290 }
291 else
292 {
293 $new = CORE::bless( $hash => $class );
294 }
295 CORE::return( $new );
296 }
297  
298 1;
299 # NOTE: POD
300 __END__