SQL::DB::Expr - Build SQL expressions with Perl
0.971.0. Development release.
use SQL::DB::Expr; my $col1 = SQL::DB::Expr->new( _txt => 'col1', _type => 'integer', ); my $col2 = SQL::DB::Expr->new( _txt => 'col2', ); my $expr = ($col1->is_not_null .AND. $col2->between(1,2))->as('alias');
SQL::DB::Expr provides an object class that supports mapping Perl syntax to SQL text, values and types suitable for passing to DBI methods. This class is the basis for SQL::DB and is not generally used directly.
new( _txt => $_txt, _type => $column_type, _multi => $_multi, );
The SQL representation of the expression suitable for passing to DBI methods such as do(), selectall_arrayref(), prepare(), etc.
Whether the expression requires surrounding brackets.
A subroutine called during an object's instantiation phase.
Makes a deep copy of the object.
Returns a string representation of the expression, providing surrounding brackets for '_multi' expressions.
Returns the expression as an SQL string with values interpolated.
Returns an SQL string with placeholder values, an array of values and an array of DBI bind types.
Maps '!' to 'NOT'.
Maps '==' and 'eq' to "=".
Maps '!=' and 'ne' to "!=".
Maps '&' to "&";
Maps '|' to "|";
Maps '<' to "<";
Maps '>' to ">";
Maps '<=' to "<=";
Maps '>=' to ">=";
Maps '+' to "+";
Maps '-' to "-";
Maps '*' to "*";
Maps '/' to "/";
Maps to "IS NULL".
Maps to "IS NOT NULL".
Maps to "IN (@VALUES)"
Maps to "NOT IN ( @VALUES )".
Maps to "BETWEEN $a AND $b".
Maps to "NOT BETWEEN $a AND $b".
Maps to "AS $as".
Maps to "LIKE $like".
Maps to "ASC".
Maps to "DESC".
The following functions are exported on demand.
Returns an object which is recognized by SQL::DB as something which should be quoted before being prepared.
Returns an object which is recognized by SQL::DB as something which should be bound (via a placeholder) before being prepared.
A method for building binary operator expressions.
Does the same as Perl's 'join' built-in. but for SQL::DB::Expr objects. See BUGS below for why this is needed.
These subroutines let you write SQL logical expressions in Perl using string concatenation:
( $e1 .AND. $e2 ) .OR. ( $e3 .AND. $e4 )
Note that due to operator precedence, expressions either side of .AND. or .OR. should be bracketed if they are not already single expression objects.
Things are implemented this way due to Perl not allowing the overloading of the 'and' and 'or' built-ins.
Using SQL::DB::Expr objects with the Perl "join" command does not work as expected, apparently because join does not trigger either the '.' or '.=' overload methods. The work around is to use the _expr_join subroutine.
Mark Lawrence <firstname.lastname@example.org>
Copyright (C) 2007-2011 Mark Lawrence <email@example.com>
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.