NAME

DB::Object::Query::Clause - SQL Query Clause Object

SYNOPSIS

my $clause = DB::Object::Query::Clause->new({
    value => "$field != '$user'",
    generic => "$field != ?",
    type => 'where',
    # or possibly:
    # bind => 
    # {
    #     values => $values_array_ref,
    #     types => $types_array_ref
    # }
})
# A DB::Object::Query object
$clause->query_object( $q );
$clause->bind->values( $res );
$clause->bind->types( '' );
$clause->fields( $field ) if( Scalar::Util::blessed( $field ) && $field->isa( 'DB::Object::Fields::Field' ) );

Merging multiple clauses

$clause = DB::Object::Query::Clause->new->merge( $dbh->AND( @clauses ) );
$clause->bind->values( @values ) if( $bind );
$clause->bind->types( @types ) if( $bind );

Get the clause stringified

my $sql = "SELECT * FROM my_table WHERE $clause";

This could become something like:

SELECT * FROM my_table WHERE username != 'joe' AND username != 'bob'

However if binding values is activated, this would rather become:

SELECT * FROM my_table WHERE username != ? AND username != ?

And the associated values would be automatically bound to the query upon execution

VERSION

v1.0.1

DESCRIPTION

The purpose of this module is to contain various attributes of a SQL clause so that it can be accessed and manipulated flexibly.

It will not create SQL query. This is done by the calling module and the query is stored in the value parameter which is also the "value" method

This is used to contain clauses such as group, having, limit, order, where

METHODS

new

It can take the following properties that can also be accessed as method:

value

The sql query fragment string

generic

The sql fragment with placeholders, for example:

username = ?
bind

Provided a hash with the following properties:

values

The values to bind as an array reference

types

The SQL types of the values to bind as an array reference

as_string

This returns the clause as a string.

This is also called when the object is used as a string

print( "SQL query is: SELECT * FROM my_table WHERE $clause\n")

If field objects were used such as:

$dbh->NOT( $user_tbl->fo->username => 'Bob' );

Then if needed, as_string would prefix the field name with its associated table name

fields

An array reference of field objects (DB::Object::Fields::Field)

The array itself is an object from Module::Generic::Array

generic

Returns a string representing the SQL fragment with placeholder.

The string returned is an object of Module::Generic::Scalar

length

Returns the length of the string in "value"

metadata

Set or get an hash reference accessible as a dynamic class object from DB::Object::Query::Clause::Metadata

merge

Given an array of clauses, this will merge them into one new clause object.

If the first value of the array is a DB::Object::Operator such as DB::Object::Operator::AND or DB::Object::Operator::OR, the list will be taken from this object and the resulting sql statement will the operator value, ie AND or OR for example

operator

Sets or get the operator used in this clause, if any.

push

This is inherited from the DB::Object::Query::Elements.

This is used to add elements.

query_object

Set or get the DB::Object::Query object, which normally would have created this clause object.

type

Set or get the type of clause this is, such as group, having, limit, order, where

The return value is a string that can be accessed as an object of Module::Generic::Scalar

types

This is inherited from the DB::Object::Query::Elements.

This is used to get the type of all elements, as an array object

value

The SQL fragment as a string. The return value is a string that can be accessed as an object of Module::Generic::Scalar

values

This is inherited from the DB::Object::Query::Elements.

This is used to get the value of all elements, as an array object

SEE ALSO

DB::Object::Query, DB::Object::Mysql::Query, DB::Object::Postgres::Query, DB::Object::SQLite::Query

AUTHOR

Jacques Deguest <jack@deguest.jp>

COPYRIGHT & LICENSE

Copyright(c) 2019-2023 DEGUEST Pte. Ltd.

All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.