NAME

EntityModel::Query - handle SQL queries

VERSION

version 0.102

SYNOPSIS

 use EntityModel::Query;

 # Provide a definition on instantiation:
 my $query = EntityModel::Query->new(
 	select	=> [qw(id name)]
	from	=> 'table',
	where	=> [ created => { '<' => '2010-01-01' } ],
	limit	=> 5
 );

 # or using chained methods:
 my $query = EntityModel::Query->new
 ->select( qw(id name) )
 ->from( 'table' )
 ->where(
 	created => { '<' => '2010-01-01' }
 )
 ->limit(5);

 # Extract query as SQL
 my ($sql, @bind) = $query->sqlAndParameters;
 my $sth = $dbh->prepare($sql);
 $sth->execute(@bind);

DESCRIPTION

Provides an abstraction layer for building SQL queries programmatically.

When generating the query, each of the components is called in turn to get an "inline SQL" arrayref. This is an arrayref consisting of SQL string fragments interspersed with refs for items such as EntityModel::Entity names, direct scalar values, or EntityModel::Field names.

As an example:

[ 'select * from ', EntityModel::Entity->new('table'), ' where ', EntityModel::Field->new('something'), ' = ', \3 ]

This can then be used by sqlAndParameters to generate:

'select * from "table" where "something" = ?', 3

or as a plain SQL string (perhaps for diagnostic purposes) from sqlString as:

'select * from "table" where "something" = 3'

METHODS

new

Construct a new EntityModel::Query. Most of the work is passed off to parse_spec.

type

Returns the type of the current query. The query object will be reblessed into an appropriate subclass depending on whether this is an insert, select, delete etc. A query that has not been reblessed is invalid.

parse_spec

Parse the specification we were given.

parse_base

Base method for parsing.

reclassify

Virtual method to allow subclass to perform any required updates after reblessing to an alternative class.

upgradeTo

Upgrade an existing EntityModel::Query object to a subclass.

parse_limit

Handle a 'limit' directive.

parse_group

parse_where

typeSQL

Proxy method for type, returns the SQL string representation for the current query type (such as 'select' or 'insert into').

fieldsSQL

Generate the SQL for fields.

fromSQL

SQL for the 'from' clause.

limitSQL

offsetSQL

orderSQL

groupSQL

havingSQL

whereSQL

joinSQL

keyword_order { qw{type fields from join where having group order offset limit};

inlineSQL

results

iterate

Calls the given method for each result returned from the current query.

dbh

SEE ALSO

INHERITED METHODS

EntityModel::Query::Base

asString, can_parse, decantQuotedValue, decantValue, normaliseInlineSQL, register, sqlAndParameters, sqlString

EntityModel::BaseClass

clone, dump, sap

AUTHOR

Tom Molesworth <cpan@entitymodel.com>

LICENSE

Licensed under the same terms as Perl itself.