The AST will be a HoHo..oH (hash of hash of ... of hashes). The keys to the
outermost hash will be the various clauses of a SQL statement, plus some
-metadata keys. All metadata keys will be identifiable as such by being prefixed
-with an underscore. All keys will be in lowercase.
+metadata keys.
=head2 Metadata keys
These are the additional metadata keys that the AST provides for.
-=head3 _query
+=head3 type
This denotes what kind of query this AST should be interpreted as. Different
-Visitors may accept additional values for _query. For example, a MySQL Visitor
-may choose to accept 'replace' for REPLACE INTO. If a _query value is
+Visitors may accept additional values for type. For example, a MySQL Visitor
+may choose to accept 'replace' for REPLACE INTO. If a type value is
unrecognized by the Visitor, the Visitor is expected to throw an error.
-All Visitors are expected to handle the following values for _query:
+All Visitors are expected to handle the following values for type:
=over 4
=back
-=head3 _version
+=head3 ast_version
This denotes the version of the AST. Different versions will indicate different
-capabilities provided. Visitors will choose to respect the _version as needed
+capabilities provided. Visitors will choose to respect the ast_version as needed
and desired.
=head2 Structural units
Visitors are expected to, by default, quote all identifiers according to the SQL
dialect's quoting scheme.
+Any of the elements may be '*', as in SELECT * or SELECT COUNT(*). Visitors must
+be careful to I<not> quote asterisks.
+
=head3 Value
-A Value is a Perl scalar. Depending on the type, a Visitor may be able to make
-certain decisions.
+A Value is a Perl scalar. Depending on the subtype, a Visitor may be able to
+make certain decisions. The following are the minimally-valid subtypes:
=over 4
=head3 Subquery
-A Subquery is another AST whose _query metadata parameter is set to "SELECT".
+A Subquery is another AST whose type metadata parameter is set to "SELECT".
Most places that a Subquery can be used would require a single value to be
returned (single column, single row), but that is not something that the AST can
An ExpressionList is always rendered in SQL with parentheses around it.
+=head3 Nesting
+
+There is no specific operator or nodetype for nesting. Instead, nesting is
+explicitly specified by node descent in the AST.
+
=head2 SQL clauses
These are all the legal and acceptable clauses within the AST that would
This is a list of tables that this clause is affecting. It corresponds to the
FROM clause in a SELECT statement and the INSERT INTO/UPDATE/DELETE clauses in
-those respective statements. Depending on the _query metadata entry, the
+those respective statements. Depending on the type metadata entry, the
appropriate clause name will be used.
The tables clause has several RDBMS-specific variations. The AST will support
}
A USING clause is syntactic sugar for an ON clause and, as such, is not provided
-for by the AST. A join of a comma is identical to a CROSS JOIN. The on clause is
-optional.
+for by the AST. A join of a comma is identical to a CROSS JOIN and, as such, is
+not provided for by the AST. The on clause is optional.
=head3 where
This corresponds to the WHERE clause in a SELECT, UPDATE, or DELETE statement.
-A where clause is composed as follows:
-
- WhereOperator := AND | OR
- WhereExpression := Expression | Expression WhereOperator Expression
-
- WhereExpression
+A where clause is composed of an Expression.
=head3 set
This corresponds to the SET clause in an INSERT or UPDATE statement.
-A set clause is composed as follows:
+A set clause unit is an array of one or more SetComponent units.
- SetComponent := Identifier = Expression
+The hash for SetComponent unit is composed as follows:
- SetComponent [ , SetComponent ]*
+ {
+ type => 'SetComponent',
+ col => Identifier,
+ value => Expression,
+ }
=head3 columns
This corresponds to the optional list of columns in an INSERT statement.
-A columns clause is an IdentifierList and the unit is composed as follows:
-
- columns => [
- Identifier,
- [ Identifier, ]*
- ],
+A columns clause unit is an array of one or more Identifier units.
=head3 values
This corresponds to the VALUES clause in an INSERT statement.
-A values clause is an ExpressionList and the unit is composed as follows.
-
- values => [
- Expression,
- [ Expression, ]*
- ],
+A values clause unit is an array of one or more Expression units.
If there is a columns clause, the number of entries in the values clause must be
equal to the number of entries in the columns clause.
This corresponds to the ORDER BY clause in a SELECT statement.
-An orderby clause is composed as follows:
+A orderby clause unit is an array of one or more OrderbyComponent units.
- OrderByComponent := XXX-TODO-XXX
- OrderByDirection := ASC | DESC
+The hash for a OrderbyComponent unit is composed as follows:
- OrderByComponent [ OrderByDirection ]
- [ , OrderByComponent [ OrderByDirection ] ]*
+ {
+ type => 'OrderbyComponent',
+ value => < Identifier | Number >
+ dir => '< ASC | DESC >',
+ }
+
+The dir element, if omitted, will be defaulted to ASC by the AST. The number
+corresponds to a column in the select clause.
=head3 groupby
This corresponds to the GROUP BY clause in a SELECT statement.
-An groupby clause is composed as follows:
+A groupby clause unit is an array of one or more GroupbyComponent units.
+
+The hash for a GroupbyComponent unit is composed as follows:
- GroupByComponent := XXX-TODO-XXX
+ {
+ type => 'GroupbyComponent',
+ value => < Identifier | Number >
+ }
- GroupByComponent [ , GroupByComponent ]*
+The number corresponds to a column in the select clause.
=head3 rows
This corresponds to the clause that is used in some RDBMS engines to limit the
number of rows returned by a query. In MySQL, this would be the LIMIT clause.
-A rows clause is composed as follows:
+The hash for a rows clause is composed as follows:
- Number [, Number ]
+ {
+ start => Number,
+ count => Number,
+ }
+
+The start attribute, if ommitted, will default to 0. The count attribute is
+optional.
=head3 for
This corresponds to the clause that is used in some RDBMS engines to indicate
what locks are to be taken by this SELECT statement.
-A for clause is composed as follows:
+The hash for a for clause is composed as follows:
- UPDATE | DELETE
+ {
+ value => '< UPDATE | DELETE >',
+ }
=head3 connectby
This corresponds to the clause that is used in some RDBMS engines to provide for
an adjacency-list query.
-A connectby clause is composed as follows:
-
- Identifier, WhereExpression
-
-=head1 EXAMPLES
-
-The following are example SQL statements and a possible AST for each one.
-
-=over 4
-
-=item * SELECT 1
+The hash for a for clause is composed as follows:
{
- _query => 'select',
- _ast_version => 0.0001,
- select => [
- {
- type => 'SelectComponent',
- value => {
- type => 'Value',
- subtype => 'number',
- value => 1,
- },
- },
- ],
- }
-
-=item * SELECT NOW() AS time FROM dual AS duality
-
- {
- _query => 'select',
- _ast_version => 0.0001,
- select => [
- {
- type => 'SelectComponent',
- value => {
- type => 'Function',
- function => 'NOW',
- },
- as => {
- type => 'Identifier',
- element1 => 'time',
- },
- },
- ],
- tables => {
- type => 'TableIdentifier',
- value => {
- type => 'Identifier',
- element1 => 'dual',
- },
- as => 'duality',
+ start_with => ExpressionList,
+ connect_by => {
+ option => '< PRIOR | NOCYCLE >'
+ cond => ExpressionList,
},
+ order_siblings => orderby-clause,
}
-=item * SELECT 1 FROM foo LEFT OUTER JOIN bar ON ( foo.col1 = bar.col2 )
-
- {
- _query => 'select',
- _ast_version => 0.0001,
- select => [
- {
- type => 'SelectComponent',
- value => {
- type => 'Value',
- subtype => 'number',
- value => 1,
- },
- },
- ],
- tables => {
- type => 'Operator',
- op => 'LEFT OUTER',
- args => [
- {
- type => 'TableIdentifier',
- value => {
- type => 'Identifier',
- element1 => 'foo',
- },
- },
- {
- type => 'TableIdentifier',
- value => {
- type => 'Identifier',
- element1 => 'bar',
- },
- },
- ],
- on => {
- type => 'Operator',
- op => '=',
- args => [
- {
- type => 'Identifier',
- element1 => 'foo',
- element2 => 'col1',
- },
- {
- type => 'Identifier',
- element1 => 'bar',
- element2 => 'col2',
- },
- ],
- },
- },
- }
-
-=back
+Both the start_with and order_siblings clauses are optional.
=head1 TODO