| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## Database Object Interface - ~/lib//mnt/src/perl/DB-Object/lib/DB/Object/Query/Clause.pm | ||
| 3 | ## Version v1.1.1 | ||
| 4 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2023/07/08 | ||
| 7 | ## Modified 2026/03/26 | ||
| 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::Clause; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use common::sense; | ||
| 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.1'; | ||
| 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." ); } | ||
| 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__ | ||
| 301 | |||
| 302 | =encoding utf-8 | ||
| 303 | |||
| 304 | =head1 NAME | ||
| 305 | |||
| 306 | DB::Object::Query::Clause - SQL Query Clause Object | ||
| 307 | |||
| 308 | =head1 SYNOPSIS | ||
| 309 | |||
| 310 | my $clause = DB::Object::Query::Clause->new({ | ||
| 311 | value => "$field != '$user'", | ||
| 312 | generic => "$field != ?", | ||
| 313 | type => 'where', | ||
| 314 | # or possibly: | ||
| 315 | # bind => | ||
| 316 | # { | ||
| 317 | # values => $values_array_ref, | ||
| 318 | # types => $types_array_ref | ||
| 319 | # } | ||
| 320 | }) | ||
| 321 | # A DB::Object::Query object | ||
| 322 | $clause->query_object( $q ); | ||
| 323 | $clause->bind->values( $res ); | ||
| 324 | $clause->bind->types( '' ); | ||
| 325 | $clause->fields( $field ) if( Scalar::Util::blessed( $field ) && $field->isa( 'DB::Object::Fields::Field' ) ); | ||
| 326 | |||
| 327 | Merging multiple clauses | ||
| 328 | |||
| 329 | $clause = DB::Object::Query::Clause->new->merge( $dbh->AND( @clauses ) ); | ||
| 330 | $clause->bind->values( @values ) if( $bind ); | ||
| 331 | $clause->bind->types( @types ) if( $bind ); | ||
| 332 | |||
| 333 | Get the clause stringified | ||
| 334 | |||
| 335 | my $sql = "SELECT * FROM my_table WHERE $clause"; | ||
| 336 | |||
| 337 | This could become something like: | ||
| 338 | |||
| 339 | SELECT * FROM my_table WHERE username != 'joe' AND username != 'bob' | ||
| 340 | |||
| 341 | However if binding values is activated, this would rather become: | ||
| 342 | |||
| 343 | SELECT * FROM my_table WHERE username != ? AND username != ? | ||
| 344 | |||
| 345 | And the associated values would be automatically bound to the query upon execution | ||
| 346 | |||
| 347 | =head1 VERSION | ||
| 348 | |||
| 349 | v1.1.1 | ||
| 350 | |||
| 351 | =head1 DESCRIPTION | ||
| 352 | |||
| 353 | The purpose of this module is to contain various attributes of a SQL clause so that it can be accessed and manipulated flexibly. | ||
| 354 | |||
| 355 | It will not create SQL query. This is done by the calling module and the query is stored in the I<value> parameter which is also the L</"value"> method | ||
| 356 | |||
| 357 | This is used to contain clauses such as I<group>, I<having>, I<limit>, I<order>, I<where> | ||
| 358 | |||
| 359 | =head1 METHODS | ||
| 360 | |||
| 361 | =head2 new | ||
| 362 | |||
| 363 | It can take the following properties that can also be accessed as method: | ||
| 364 | |||
| 365 | =over 4 | ||
| 366 | |||
| 367 | =item C<value> | ||
| 368 | |||
| 369 | The sql query fragment string | ||
| 370 | |||
| 371 | =item C<generic> | ||
| 372 | |||
| 373 | The sql fragment with placeholders, for example: | ||
| 374 | |||
| 375 | username = ? | ||
| 376 | |||
| 377 | =item C<bind> | ||
| 378 | |||
| 379 | Provided a hash with the following properties: | ||
| 380 | |||
| 381 | =over 8 | ||
| 382 | |||
| 383 | =item C<values> | ||
| 384 | |||
| 385 | The values to bind as an array reference | ||
| 386 | |||
| 387 | =item C<types> | ||
| 388 | |||
| 389 | The SQL types of the values to bind as an array reference | ||
| 390 | |||
| 391 | =back | ||
| 392 | |||
| 393 | =back | ||
| 394 | |||
| 395 | =head2 as_string | ||
| 396 | |||
| 397 | This returns the clause as a string. | ||
| 398 | |||
| 399 | This is also called when the object is used as a string | ||
| 400 | |||
| 401 | print( "SQL query is: SELECT * FROM my_table WHERE $clause\n") | ||
| 402 | |||
| 403 | If field objects were used such as: | ||
| 404 | |||
| 405 | $dbh->NOT( $user_tbl->fo->username => 'Bob' ); | ||
| 406 | |||
| 407 | Then if needed, B<as_string> would prefix the field name with its associated table name | ||
| 408 | |||
| 409 | =head2 fields | ||
| 410 | |||
| 411 | An array reference of field objects (L<DB::Object::Fields::Field>) | ||
| 412 | |||
| 413 | The array itself is an object from L<Module::Generic::Array> | ||
| 414 | |||
| 415 | =head2 generic | ||
| 416 | |||
| 417 | Returns a string representing the SQL fragment with placeholder. | ||
| 418 | |||
| 419 | The string returned is an object of L<Module::Generic::Scalar> | ||
| 420 | |||
| 421 | =head2 length | ||
| 422 | |||
| 423 | Returns the length of the string in L</"value"> | ||
| 424 | |||
| 425 | =head2 metadata | ||
| 426 | |||
| 427 | Set or get an hash reference accessible as a dynamic class object from L<DB::Object::Query::Clause::Metadata> | ||
| 428 | |||
| 429 | =head2 merge | ||
| 430 | |||
| 431 | Given an array of clauses, this will merge them into one new clause object. | ||
| 432 | |||
| 433 | If the first value of the array is a L<DB::Object::Operator> such as L<DB::Object::Operator::AND> or L<DB::Object::Operator::OR>, the list will be taken from this object and the resulting sql statement will the operator value, ie C<AND> or C<OR> for example | ||
| 434 | |||
| 435 | =head2 operator | ||
| 436 | |||
| 437 | Sets or get the operator used in this clause, if any. | ||
| 438 | |||
| 439 | =head2 push | ||
| 440 | |||
| 441 | This is inherited from the L<DB::Object::Query::Elements>. | ||
| 442 | |||
| 443 | This is used to add elements. | ||
| 444 | |||
| 445 | =head2 query_object | ||
| 446 | |||
| 447 | Set or get the L<DB::Object::Query> object, which normally would have created this clause object. | ||
| 448 | |||
| 449 | =head2 type | ||
| 450 | |||
| 451 | Set or get the type of clause this is, such as I<group>, I<having>, I<limit>, I<order>, I<where> | ||
| 452 | |||
| 453 | The return value is a string that can be accessed as an object of L<Module::Generic::Scalar> | ||
| 454 | |||
| 455 | =head2 types | ||
| 456 | |||
| 457 | This is inherited from the L<DB::Object::Query::Elements>. | ||
| 458 | |||
| 459 | This is used to get the type of all elements, as an L<array object|Module::Generic::Array> | ||
| 460 | |||
| 461 | =head2 value | ||
| 462 | |||
| 463 | The SQL fragment as a string. The return value is a string that can be accessed as an object of L<Module::Generic::Scalar> | ||
| 464 | |||
| 465 | =head2 values | ||
| 466 | |||
| 467 | This is inherited from the L<DB::Object::Query::Elements>. | ||
| 468 | |||
| 469 | This is used to get the value of all elements, as an L<array object|Module::Generic::Array> | ||
| 470 | |||
| 471 | =head1 SEE ALSO | ||
| 472 | |||
| 473 | L<DB::Object::Query>, L<DB::Object::Mysql::Query>, L<DB::Object::Postgres::Query>, L<DB::Object::SQLite::Query> | ||
| 474 | |||
| 475 | =head1 AUTHOR | ||
| 476 | |||
| 477 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 478 | |||
| 479 | =head1 COPYRIGHT & LICENSE | ||
| 480 | |||
| 481 | Copyright(c) 2019-2023 DEGUEST Pte. Ltd. | ||
| 482 | |||
| 483 | All rights reserved. | ||
| 484 | |||
| 485 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 486 | |||
| 487 | =cut |