EntityModel::Query - handle SQL queries
version 0.102
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);
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'
Construct a new EntityModel::Query. Most of the work is passed off to parse_spec.
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 the specification we were given.
Base method for parsing.
Virtual method to allow subclass to perform any required updates after reblessing to an alternative class.
Upgrade an existing EntityModel::Query object to a subclass.
Handle a 'limit' directive.
Proxy method for type, returns the SQL string representation for the current query type (such as 'select' or 'insert into').
Generate the SQL for fields.
SQL for the 'from' clause.
Calls the given method for each result returned from the current query.
SQL::Translator
SQL::Abstract
asString, can_parse, decantQuotedValue, decantValue, normaliseInlineSQL, register, sqlAndParameters, sqlString
clone, dump, sap
Tom Molesworth <cpan@entitymodel.com>
Licensed under the same terms as Perl itself.
To install EntityModel, copy and paste the appropriate command in to your terminal.
cpanm
cpanm EntityModel
CPAN shell
perl -MCPAN -e shell install EntityModel
For more information on module installation, please visit the detailed CPAN module installation guide.