- COPYRIGHT & LICENSE
Fey::SQL::Select - Represents a SELECT query
my $sql = Fey::SQL->new_select(); # SELECT Part.part_id, Part.part_name # FROM Part JOIN MachinePart # ON Part.part_id = MachinePart.part_id # WHERE MachinePart.machine_id = $value # ORDER BY Part.name DESC # LIMIT 10 $sql->select( $part_id, $part_name ); $sql->from( $Part, $MachinePart ); $sql->where( $machine_id, '=', $value ); $sql->order_by( $part_Name, 'DESC' ); $sql->limit(10); print $sql->sql($dbh);
This class represents a
This class provides the following methods:
To construct an object of this class, call
$query->select() on a
This method accepts a list of parameters, which are the things being selected.
The list can include the following types of elements:
plain scalars, including
These will be passed to
If a table is passed, then all of its columns will be included in the
Fey::Columnobjects, and aliases
This specifies an individual column (possibly aliased) to include in the select.
$column->is_selectable()method must return true for these objects.
This method can be called multiple times with different elements each time.
Any type of literal can be included in a
If this is called, the generated SQL will start with
This method specifies the
FROM clause of the query. It can accept a variety of argument lists.
If called with a single
Fey::Tableor table alias object, that table is included in the
FROM Part FROM Part as Part0
If called with a single
Fey::SQL::Selectobject, that object's SQL will be included in the
FROMclause as a subselect.
FROM (SELECT part_id FROM Part) AS SUBSELECT0
If two tables (or aliases) are passed to this method, these two tables are included and joined together. The foreign key between these two tables will be looked up in the
Fey::Schemaobject for the tables. However, if the tables do not have a foreign key between them, or have more than one foreign key, an exception is thrown.
FROM Part, MachinePart ON Part.part_id = MachinePart.part_id
($table1, $table2, $fk)
When joining two tables, you can manually specify the foreign key which should be used to join them. This is necessary when there are multiple foreign keys between two tables.
You can also use this to "fake" a foreign key between two tables which don't really have one, but where it makes sense to join them anyway. If this paragraph doesn't make sense, don't worry about it ;)
($table1, 'left', $table2)
If you want to do an outer join between two tables, pass the two tables, separated by one of the following string:
This will generate the appropriate outer join SQL in the
FROM Part LEFT OUTER JOIN MachinePart ON Part.part_id = MachinePart.part_id
Just as with a normal join, the
<$select-from() >> will attempt to automatically find a foreign key between the two tables.
($table1, 'left', $table2, $fk)
Just as with a normal join, you can manually specify the foreign key to use for an outer join as well.
($table1, 'left', $table2, $where_clause)
If you want to specify a
WHEREclause as part of an outer join, include this as the fourth argument when calling
FROM Part LEFT OUTER JOIN MachinePart ON Part.part_id = MachinePart.part_id AND MachinePart.machine_id = ?
To create a standalone
WHEREclause suitable for passing to this method, use the
($table1, 'left', $table2, $fk, $where_clause)
You can manually specify a foreign key and include a where clause in an outer join.
$select->from() method can be called multiple times with different join options. If you call the method with arguments that it has already seen, then it will effectively ignore that call.
See the Fey::SQL section on WHERE Clauses for more details.
This method accepts a list of elements. Each element can be a
Fey::Column object, a column alias, or a literal function or term.
$select->having() method accepts exactly the same arguments as the
See the Fey::SQL section on ORDER BY Clauses for more details.
See the Fey::SQL section on LIMIT Clauses for more details.
Returns the full SQL statement which this object represents. A DBI handle must be passed so that identifiers can be properly quoted.
See the Fey::SQL section on Bind Parameters for more details.
Returns a list of objects, one for each element in the
SELECT clause. These can be
Fey::Column::Aliases, or any type of
These items are returned in the order in which they will be included in the
SELECT clause portion of the SQL statement as a string.
FROM clause portion of the SQL statement as a string.
WHERE clause portion of the SQL statement as a string.
ORDER BY clause portion of the SQL statement as a string.
GROUP BY clause portion of the SQL statement as a string.
HAVING clause portion of the SQL statement as a string.
LIMIT clause portion of the SQL statement as a string.
Returns the appropriate SQL snippet. See Fey::Role::HasAliasName.
Returns a unique identifier for the select.
This role allows a
Fey::SQL::Selectobject to be used as a subselect in
Dave Rolsky, <firstname.lastname@example.org>
See Fey for details on how to report bugs.
Copyright 2006-2009 Dave Rolsky, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.