NAME
SimpleDB::Class::SQL - SQL generation tools for SimpleDB.
VERSION
version 1.0503
DESCRIPTION
This class is used to generate the SQL needed for the Select operation on SimpleDB's web service.
METHODS
The following methods are available from this class.
new ( params )
Constructor.
params
A hash of options you can pass in to the constructor.
item_class
A SimpleDB::Class::Item subclass name. This is required.
simpledb
A reference to the SimpleDB::Class object. This is required.
output
Defaults to '*'. Alternatively you can pass a string of 'count(*)' or an attribute. Or you can pass an array ref of attributes.
where
A hash reference containing a series of clauses. Here are some examples and what the resulting queries would be. You can of course combine all these options to create your own queries.
NOTE: If you want to search on an item's id (or ItemName) then you should use the itemName()
function as the id doesn't actually exist in the item's data.
Direct comparison.
{ foo => 1 }
select * from domain where foo=1
{ foo => 1, bar => 2 }
select * from domain where foo=1 and bar=2
{ foo => [ '>', 5 ] } # '=', '!=', '>', '<', '<=', '>='
select * from domain where foo > 5
Direct comparison with an or clause.
{ -or => { foo => 1, bar => 2 } }
select * from domain where (foo=1 or bar=2)
Find all items where these attributes intersect.
{ -intersection => { foo => 1, bar => 2 } }
select * from domain where (foo=1 intersection bar=2)
Combining OR and AND.
{ -or => { foo => 1, -and => { this => 'that', bar => 2 } }
select * from domain where (foo=1 or ( this='that' and bar=2 ))
Finding within a range.
{ foo=>['between', 5, 10] }
select * from domain where foo between 5 and 10
Finding within a set.
{ foo => ['in', 1, 3, 5, 7 ] }
select * from domain where foo in (1, 3, 5, 7)
Finding in a set where every item returned matches all members of the set.
{ foo => ['every', 1, 3, 5, 7 ] }
select * from domain where every(foo) in (1, 3, 5, 7)
String comparisons. You can match on either side of the string ('%this', 'that%') or both ('%this%'). Note that matching at the beginning or both sides of the string is a slow operation.
{ foo => [ 'like', 'this%' ] } # 'not like'
select * from domain where foo like 'this%'
Null comparisons. These are very slow. Try inserting 'Null' or 'None' into a field and do string comparisons rather than null comparisons.
{ foo => 'is null' } # 'is not null'
select * from domain where foo is null
order_by
An attribute to order the result set by, defaults to ascending order. Can also pass in an array ref containing an attribute and 'desc' or 'asc'. If an array ref is passed in containing only an attribute name it is an implied descending order.
"foo"
["foo","desc"]
["foo"]
limit
An integer of a number of items to limit the result set to.
output ()
Returns what was passed into the constructor for the output field.
simpledb ()
Returns the reference passed into the constructor.
item_class ()
Returns what was passed into the constructor for the item_class field.
where ()
Returns what was passed into the constructor for the where field.
has_where()
Returns a boolean indicating whether a where clause has been set.
order_by ()
Returns what was passed into the constructor for the output field.
has_order_by ()
Returns a boolean indicating whether an order by clause has been set.
limit ()
Returns what was passed into the constructor for the output field.
has_limit ()
quote_value ( string )
Escapes ' and " in values. If itemName()
is what's passed in, it will not be quoted.
string
The value to escape.
quote_attribute ( string )
Escapes an attribute with so that it can contain spaces and other special characters by wrapping it in backticks `. If itemName()
is what's passed in, it will not be quoted.
string
The attribute name to escape.
recurese_where ( constraints, [ op ] )
Traverses a where() hierarchy and returns a stringified SQL version of the where clause.
constraints
A portion of a where hierarchy, perhaps broken off from the main for detailed analysis.
op
If it's a chunk broken off, -and, -or, -intersection then the operator will be passed through here.
to_sql ( )
Returns the entire query as a stringified SQL version.
LEGAL
SimpleDB::Class is Copyright 2009-2010 Plain Black Corporation (http://www.plainblack.com/) and is licensed under the same terms as Perl itself.