GX::SQL::Builder - Base class for SQL builders
None.
This module provides the GX::SQL::Builder class which extends the GX::Class::Object class.
new
Returns a new SQL builder instance.
$builder = $builder_class->new( %attributes );
quote_char ( string )
quote_char
Defaults to a double quote.
$builder ( GX::SQL::Builder object )
$builder
GX::Exception
count
Creates a SELECT COUNT(*) statement.
SELECT COUNT(*)
( $sql, $bind ) = $builder->count( %arguments );
bind ( HASH reference )
bind
HASH
table ( string ) [ required ]
table
where ( ARRAY reference )
where
ARRAY
$sql ( string )
$sql
$bind ( ARRAY reference )
$bind
In scalar context, only the generated SQL is returned.
delete
Creates a DELETE statement.
DELETE
( $sql, $bind ) = $builder->delete( %arguments );
insert
Creates an INSERT statement.
INSERT
( $sql, $bind ) = $builder->insert( %arguments );
columns ( ARRAY reference ) [ required ]
columns
values ( ARRAY reference )
values
select
Creates a SELECT statement.
SELECT
( $sql, $bind ) = $builder->select( %arguments );
columns ( ARRAY reference )
distinct ( bool )
distinct
Adds the "DISTINCT" keyword to the query if set to true.
limit ( integer )
limit
offset ( integer )
offset
order ( string | SCALAR reference | ARRAY reference )
order
SCALAR
Examples:
order => 'column_1' order => [ [ 'column_1', 'DESC' ] ] order => [ 'column_1', [ 'column_2', 'DESC' ] ] order => \$sql
update
Creates an UPDATE statement.
UPDATE
( $sql, $bind ) = $builder->update( %arguments );
Creates a WHERE clause.
WHERE
( $sql, $bind ) = $builder->where( $where ); ( $sql, $bind ) = $builder->where( $where, bind => $bind );
$bind ( HASH reference ) [ optional ]
$where ( ARRAY reference ) [ required ]
$where
Example 1 - Generate a SQL INSERT statement and the neccessary bind parameters to execute it:
my ( $sql, $bind ) = $builder->insert( table => 'countries', columns => [ 'id', 'name', 'code' ], values => [ 1, 'Germany', 'DE' ] );
Returned SQL:
'INSERT INTO "countries" ( "id", "name", "code" ) VALUES ( ?, ?, ? )'
Returned bind parameters:
[ 1, 'Germany', 'DE' ]
Prepare the statement:
my $sth = $dbh->prepare( $sql );
Execute the prepared statement:
$sth->execute( @$bind );
Example 2 - Generate a SQL INSERT statement and execute it with varying data:
my $sql = $builder->insert( table => 'countries', columns => [ 'id', 'name', 'code' ] ); my $sth = $dbh->prepare( $sql ); my @data = ( [ 1, 'Germany', 'DE' ], [ 2, 'Austria', 'AT' ], # ... ); for my $row ( @data ) { $sth->execute( @$row ); }
Example:
use GX::SQL::Types qw( :all ); my ( $sql, $bind ) = $builder->insert( table => 'countries', columns => [ 'id', 'name', 'code' ], values => [ 1, 'Germany', 'DE' ], bind => { 'id' => INTEGER, 'name' => VARCHAR, 'code' => VARCHAR } );
[ [ 1, 1, DBI::SQL_INTEGER ], [ 2, 'Germany', DBI::SQL_VARCHAR ], [ 3, 'DE', DBI::SQL_VARCHAR ] ]
Usage:
my $sth = $dbh->prepare( $sql ); for my $parameters ( @$bind ) { $sth->bind_param( @$parameters ); } $sth->execute;
my ( $sql, $bind ) = $builder->insert( table => 'countries', columns => [ 'id', 'name', 'code' ], values => [ 1, 'Germany', 'DE' ], bind => { 'id' => { TYPE => DBI::SQL_INTEGER }, 'name' => { TYPE => DBI::SQL_VARCHAR }, 'code' => { TYPE => DBI::SQL_VARCHAR } } );
[ [ 1, 1, { TYPE => DBI::SQL_INTEGER } ], [ 2, 'Germany', { TYPE => DBI::SQL_VARCHAR } ], [ 3, 'DE', { TYPE => DBI::SQL_VARCHAR } ] ]
@where = ( 'name' => 'Germany' );
Result:
$sql = 'WHERE "name" = ?'; $bind = [ 'Germany' ];
"AND" logic by default:
@where = ( 'currency' => 'EUR', 'time_zone' => 'CET' );
$sql = 'WHERE "currency" = ? AND "time_zone" = ?'; $bind = [ 'Euro', 'CET' ];
Supported logic: "AND", "OR", "AND NOT", "OR NOT".
@where = ( 'currency' => 'EUR', 'OR', 'currency' => 'GBP' );
$sql = 'WHERE "currency" = ? OR "currency" = ?'; $bind = [ 'EUR', 'GBP' ];
Multiple values:
@where = ( 'currency' => [ 'EUR', 'GBP', 'USD' ] );
$sql = 'WHERE "currency" IN ( ?, ?, ? )'; $bind = [ 'EUR', 'GBP', 'USD' ];
Simple operators: "=", "!=", "<", ">", "<=", "=>", 'IS', 'IS NOT'.
@where = ( 'population' => { '>' => 50000000 } );
$sql = 'WHERE "population" > ?'; $bind = [ 50000000 ];
"IN" operator:
@where = ( 'currency' => { 'IN' => [ 'EUR', 'GBP', 'USD' ] } );
"BETWEEN" operator:
@where = ( 'population' => { 'BETWEEN' => [ 10000000, 50000000 ] } );
$sql = 'WHERE "population" BETWEEN ? AND ?'; $bind = [ 10000000, 50000000 ];
@where = ( 'id' => undef );
$sql = 'WHERE "id" IS NULL'; $bind = [];
Also NOT NULL conversion:
@where = ( 'id' => { '!=' => undef } );
$sql = 'WHERE "id" IS NOT NULL'; $bind = [];
@where = ( 'id' => 1, 'OR', [ 'name' => 'Germany', 'code' => 'DE' ] );
$sql = 'WHERE "id" = ? OR ( "name" = ? AND "code" = ? )'; $bind = [ 1, 'Germany', 'DE' ];
@where = ( 'id' => \'IS NOT NULL' );
The following classes inherit directly from GX::SQL::Builder:
GX::SQL::Builder::MySQL
GX::SQL::Builder::Pg
GX::SQL::Builder::SQLite
Jörg A. Uzarek <uzarek@runlevelnull.de>
Copyright (c) 2009-2011 Jörg A. Uzarek.
This module is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License Version 3 as published by the Free Software Foundation.
To install GX, copy and paste the appropriate command in to your terminal.
cpanm
cpanm GX
CPAN shell
perl -MCPAN -e shell install GX
For more information on module installation, please visit the detailed CPAN module installation guide.