DB::Object::Query::Clause - SQL Query Clause Object
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
v0.1
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
It can take the following properties that can also be accessed as method:
The sql query fragment string
The sql fragment with placeholders, for example:
username = ?
Provided a hash with the following properties:
The values to bind as an array reference
The SQL types of the values to bind as an array reference
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
An array reference of field objects (DB::Object::Fields::Field)
The array itself is an object from Module::Generic::Array
Returns a string representing the sql fragment with placeholder.
The string returned is an object of Module::Generic::Scalar
Returns the length of the string in "value"
Set or get an hash reference accessible as a dynamic class object from DB::Object::Query::Clause::Metadata
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
AND
OR
Set or get the DB::Object::Query object, which normally would have created this clause object.
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
The SQL fragment as a string. The return value is a string that can be accessed as an object of Module::Generic::Scalar
Jacques Deguest <jack@deguest.jp>
DB::Object::Query
Copyright (c) 2019-2020 DEGUEST Pte. Ltd.
You can use, copy, modify and redistribute this package and associated files under the same terms as Perl itself.
To install DB::Object, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DB::Object
CPAN shell
perl -MCPAN -e shell install DB::Object
For more information on module installation, please visit the detailed CPAN module installation guide.