Added skeleton of visitor guidelines and finished further TODO items
[dbsrgits/SQL-Abstract-2.0-ish.git] / lib / SQL / Abstract / Manual / Specification.pod
index 4c2cb52..f6085c5 100644 (file)
@@ -189,13 +189,10 @@ The hash will be structured as follows:
 
   {
       type     => 'Identifier',
-      element1 => Scalar,
-      element2 => Scalar,
-      element3 => Scalar,
+      elements => [ Scalar ],
   }
 
-If element3 exists, then element2 must exist. element1 must always exist. If a
-given element exists, then it must be defined and of non-zero length.
+All values in elements must be defined.
 
 Visitors are expected to, by default, quote all identifiers according to the SQL
 dialect's quoting scheme.
@@ -205,8 +202,8 @@ 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
 
@@ -221,15 +218,25 @@ A Number is an unquoted number in some numeric format.
 
 =item * Null
 
-Null is SQL's NULL and corresponds to Perl's C<undef>.
+A Null is SQL's NULL and corresponds to Perl's C<undef>.
 
-=item * BindParameter
+=item * Boolean
 
-This corresponds to a value that will be passed in. This value is normally
-quoted in such a fashion so as to protect against SQL injection attacks. (q.v.
-L<DBI/quote()> for an example.)
+A Boolean is a two-value entity - true or false. Some DBMSes provide an explicit
+boolean while others do not.
 
-BindParameters are normally represented by a '?'.
+=item * Date
+
+A Date represents (generally) a year, month, and day.
+
+=item * Time
+
+A Time represents (generally) an hour, minute, and second.
+
+=item * DateTime
+
+A DateTime represents the complete description necessary to determine a specific point in
+time. This could correspond to a L<DateTime/> object in Perl.
 
 =back
 
@@ -237,14 +244,18 @@ The hash will be structured as follows:
 
   {
       type    => 'Value'
-      subtype => [ 'String' | 'Number' | 'Null' | 'BindParameter' ]
+      subtype => [ 'String' | 'Number' | 'Null' | 'Boolean' | 'Date' | 'Time' | 'DateTime' ]
       value   => Scalar
+      is_bind => Boolean
   }
 
 The provided subtypes are the ones that all Visitors are expected to support.
 Visitors may choose to support additional subtypes. Visitors are expected to
 throw an exception upon encountering an unknown subtype.
 
+C<is_bind> defaults to true. It determines whether or not the Visitor should
+attempt to treat this value as a BindParameter or not.
+
 =head3 Operator
 
 An Operator would be, in SQL dialect terms, a unary operator, a binary operator,
@@ -258,7 +269,9 @@ The hash will be structured as follows:
   {
       type => 'Operator',
       op   => String,
-      args => ExpressionList,
+      args => [
+          Expression,
+      ],
   }
 
 Operators have a cardinality, or expected number of arguments. Some operators,
@@ -290,6 +303,18 @@ impossible.
 
 Subqueries, when expressed in SQL, must be bounded by parentheses.
 
+=head3 Alias
+
+An Alias is any place where the construct "X as Y" appears; it is the "AS Y" part.
+
+The hash will be structured as follows:
+
+  {
+      type  => 'Alias',
+      value => Expression,
+      as    => Identifier,
+  }
+
 =head3 Expression
 
 An Expression can be any one of the following:
@@ -304,27 +329,13 @@ An Expression can be any one of the following:
 
 =item * Subquery
 
+=item * Alias
+
 =back
 
 An Expression is a meta-syntactic unit. An "Expression" unit will never appear
 within the AST. It acts as a junction.
 
-=head3 ExpressionList
-
-An ExpressionList is a list of Expressions, generally separated by commas
-(though other separators may be appropriate at times or for different SQL
-dialects). An null separator may also be used.
-
-The hash for an ExpressionList is as follows:
-
-  {
-      type      => 'ExpressionList',
-      separator => ',',
-      elements  =>  Array of Expressions,
-  }
-
-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
@@ -338,52 +349,70 @@ given RDBMS engine's SQL dialect and some clauses may be required in one and
 optional in another. Detecting and enforcing those engine-specific restrictions
 is the responsibility of the Visitor object.
 
-The clauses are defined with a yacc-like syntax. The various parts are:
+The following clauses are expected to be handled by Visitors for each statement:
 
 =over 4
 
-=item * :=
+=item * select
 
-This means "defined" and is used to create a new term to be used below.
+=over 4
 
-=item * []
+=item * select
 
-This means optional and indicates that the items within it are optional.
+=item * tables
 
-=item * []*
+=item * where
 
-This means optional and repeating as many times as desired.
+=item * orderby
 
-=item * |
+=item * groupby
 
-This means alternation. It is a binary operator and indicates that either the
-left or right hand sides may be used, but not both.
+=back
 
-=item * C<< <> >>
+=item * insert
 
-This is a grouping construct. It means that all elements within this construct
-are treated together for the purposes of optional, repeating, alternation, etc.
+=over 4
+
+=item * tables
+
+=item * set
 
 =back
 
-The expected clauses are (name and structure):
+There are RDBMS-specific variations of the INSERT statement, such the one in
+MySQL's 
 
-=head3 select
+=item * update
 
-This corresponds to the SELECT clause of a SELECT statement.
+=over 4
 
-A select clause unit is an array of one or more SelectComponent units.
+=item * tables
 
-The hash for a SelectComponent unit is composed as follows:
+=item * set
 
-  {
-      type  => 'SelectComponent',
-      value => Expression,
-      as    => String,
-  }
+=item * where
+
+=back
 
-The 'as' component is optional. Visitors may choose to make it required in
-certain situations.
+=item * delete
+
+=over 4
+
+=item * tables
+
+=item * where
+
+=back
+
+=back
+
+The expected clauses are (name and structure):
+
+=head3 select
+
+This corresponds to the SELECT clause of a SELECT statement.
+
+A select clause unit is an array of one or more Expressions.
 
 =head3 tables
 
@@ -396,39 +425,20 @@ The tables clause has several RDBMS-specific variations. The AST will support
 all of them and it is up to the Visitor object constructing the actual SQL to
 validate and/or use what is provided as appropriate.
 
-A TableJoin is a junction of the following elements:
-
-=over 4
-
-=item * TableIdentifier
-
-=item * Operator
-
-=back
-
-The hash for a TableIdentifier will be composed as follows:
-
-  # TableIdentifier
-  {
-      type  => 'TableIdentifier',
-      value => Expression,
-      as    => String,
-  }
-
-The value should be either an Identifier or a SubQuery. 
+A tables clause is an Expression that also allows for Joins.
 
-The hash for an Operator within a tables clause will be composed as follows:
+The hash for an Join within a tables clause will be composed as follows:
 
-  # Operator
   {
-      type => 'Operator',
+      type => 'Join',
       op   => '< LEFT|RIGHT|FULL [ OUTER ] > | INNER | CROSS',
       on   => Expression,
+      args => [ Expression ],
   }
 
 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
 
@@ -440,85 +450,87 @@ A where clause is composed of an Expression.
 
 This corresponds to the SET clause in an INSERT or UPDATE statement.
 
-A set clause is composed as follows:
-
-  SetComponent := Identifier = Expression
-
-  SetComponent [ , SetComponent ]*
-
-=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, ]*
-  ],
-
-=head3 values
-
-This corresponds to the VALUES clause in an INSERT statement.
+The hash for an set clause will be composed as follows:
 
-A values clause is an ExpressionList and the unit is composed as follows.
-
-  values => [
-      Expression,
-      [ Expression, ]*
-  ],
+  {
+      type => 'Set',
+      args => [
+          [ Identifier ],
+          [ Expresion ],
+      ],
+  }
 
-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.
+The args is an array that is organized as follows: The first element is an array of
+Identifiers for the columns being set. The following arrays are Expressions describing
+the values. The various arrays should be the same length. The array of Identifiers can
+be omitted.
 
 =head3 orderby
 
 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.
+
+The hash for a OrderbyComponent unit is composed as follows:
 
-  OrderByComponent := XXX-TODO-XXX
-  OrderByDirection := ASC | DESC
+  {
+      type  => 'OrderbyComponent',
+      value => Expression,
+      dir   => '< ASC | DESC >',
+  }
 
-  OrderByComponent [ OrderByDirection ]
-  [ , OrderByComponent [ OrderByDirection ] ]*
+The value should either be an Identifier or a Number. 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:
+
+  {
+      type  => 'GroupbyComponent',
+      value => Expression,
+  }
+
+The value should either be an Identifier or a Number. The number corresponds to
+a column in the select clause.
 
-  GroupByComponent := XXX-TODO-XXX
+=head2 Possible RDBMS-specific clauses
 
-  GroupByComponent [ , GroupByComponent ]*
+The following clauses are provided as examples for RDBMS-specific elements. They
+are B<not> expected to be supported by all Visitors. Visitors may choose whether
+or not to throw on an unexpected clause, though it is strongly recommended.
 
 =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.
+number of rows returned by a SELECT statement. 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:
-
-  UPDATE | DELETE
-
-=head3 connectby
-
-This corresponds to the clause that is used in some RDBMS engines to provide for
-an adjacency-list query.
+The hash for a for clause is composed as follows:
 
-A connectby clause is composed as follows:
-
-  Identifier, WhereExpression
+  {
+      value => '< UPDATE | DELETE >',
+  }
 
 =head1 TODO
 
@@ -526,8 +538,14 @@ A connectby clause is composed as follows:
 
 =item * sproc unit
 
+=item * UNION, UNION ALL, and MINUS
+
+=item * start the API guidelines
+
 =back
 
+Convert INSERT and UPDATE into ->populate form.
+
 =head1 AUTHORS
 
 robkinyon: Rob Kinyon C<< <rkinyon@cpan.org> >>