NAME
SQL::Abstract::Query::Select - An object that represents a SQL SELECT.
SYNOPSIS
You'll need to create an SQL::Abstract::Query object first:
use SQL::Abstract::Query;
my $query = SQL::Absract::Query->new();
Now you can create a select object and use it:
my $select = $query->select(
[qw( name email )],
'users',
{ is_admin => 'is_admin', age => {'>=', 'min_age'} },
\%attributes,
);
my $sql = $select->sql();
my $sth = $dbh->prepare( $sql );
my @bind_values = $select->values({ is_admin => 1, min_age => 18 });
$sth->execute( @bind_values );
while (my $row = $sth->fetchrow_array()) {
print "$row->{name}: $row->{email}\n";
}
Or you can bypass object creation and use an interface very similar to SQL::Abstract's:
my ($sql, @bind_values) = $query->select(
[qw( name email )],
'users',
{ is_admin => 1, age => {'>=', 18} },
\%attributes,
);
DESCRIPTION
This module extends SQL::Abstract's select() method by wrapping it up in to an object that can be re-used and adding additional functionality.
ATTRIBUTES
fields
An array ref of field names or a scalar. This is passed unmodified to SQL::Abstract.
from
The FROM section of the SELECT query. Can be either a Scalar which will be quoted, an array ref of either scalars or arrays, or a scalar ref wich will not be quoted.
A single table, quoted:
from => 'users'
FROM "users"
An arbitrary string, not quoted:
from => \'users'
FROM users
A list of table names, some may be quoted, some not, separated by commas:
from => ['users', \'user_emails']
FROM "users", user_emails
A list of table names, the first one with an alias:
from => [ {name => users, as => 'u'}, 'user_emails' ]
FROM "users" "u", "user_emails"
A join with aliases:
from => [ {users => 'u'}, {user_emails => e, using => 'user_id'} ]
FROM "users" "u" JOIN "user_emails" "e" ON ( "e"."user_id" = "u"."user_id" )
Another join but using "on" instead of "using", and adding another non-join table:
from => [ {users => 'u'}, {user_emails => 'e', on=>{ 'e.user_id' => \'= u.user_id' }}, 'logs' ]
FROM "users" "u" JOIN "user_emails" "e" ON ( "e"."user_id" = u.user_id ), logs
Note that the FROM part of the SELECT is not handled by SQL::Abstract at all.
where
The WHERE clause which can be a hash ref, an array ref, or a scalar. This gets passed to SQL::Abstract unmodified.
group_by
The GROUP BY clause which can be a scalar or an array reference. SQL::Abstract does not natively support GROUP BY so this module generates the SQL itself. Here are some samples:
Group by a single column:
group_by => 'foo'
GROUP BY "foo"
Group by several columns:
group_by => ['foo', 'bar']
GROUP BY "foo", "bar"
order_by
The ORDER BY clause which can be a scalar or an array reference. This order_by is not processed by SQL::Abstract at all and is instead handled by this module completely. Here are some samples of valid input and what the SQL would look like:
Order by a single column:
order_by => 'foo'
ORDER BY "foo"
Order by several columns:
order_by => ['foo', 'bar']
ORDER BY "foo", "bar"
Order by several columns, setting the ordering direction:
order_by => [ [foo => 'asc'], 'bar' ]
ORDER BY "foo" ASC, "bar"
limit
The maximum number of rows that the query should return. This can be either an integer or a string for use with values().
offset
The number of rows to offset the query by. For example, if you had 20 rows and set the limit to 10 and the offset to 5 you'd get rows 5 through 14 (where row 1 is the first row). The setting of offset will be ignored if the limit is not also set.
This can be either an integer or a string for use with values().
AUTHOR
Aran Clary Deltac <bluefeet@gmail.com>
LICENSE
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.