Changed Function and the various FooOperator clauses into just Operator.
[dbsrgits/SQL-Abstract-2.0-ish.git] / lib / SQL / Abstract / Manual / Specification.pod
index 0d2c1ea..840cb55 100644 (file)
@@ -52,6 +52,40 @@ common features. The AST will provide ways of expressing common functionality in
 a common language. The emitters (objects that follow the Visitor pattern) will
 be responsible for converting that common language into RDBMS-specific SQL.
 
+=head1 RESTRICTIONS
+
+The following are the restrictions upon the AST:
+
+=head2 DML-only
+
+The AST will only support DML (Data Modelling Language). It will not (currently)
+support DDL (Data Definition Language). Practically, this means that the only
+statements supported will be:
+
+=over 4
+
+=item * SELECT
+
+=item * INSERT INTO
+
+=item * UPDATE
+
+=item * DELETE
+
+=back
+
+Additional DML statements may be supported by specific Visitors (such as a
+MySQL visitor supporting REPLACE INTO). q.v. the relevant sections of this
+specification for details.
+
+=head2 Dialect-agnostic construction
+
+The AST will not attempt to be immediately readable to a human as SQL. In fact,
+due to the dialect differences, particularly in terms of which use operators and
+which use functions for a given action, the AST will ...
+
+XXX FILL ME IN LATER XXX
+
 =head1 COMPONENTS
 
 There are two major components to SQL::Abstract v2.
@@ -93,8 +127,8 @@ These are the additional metadata keys that the AST provides for.
 
 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'. If a _query value is unrecognized by the
-Visitor, the Visitor is expected to throw an error.
+may choose to accept 'replace' for REPLACE INTO. If a _query 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:
 
@@ -131,7 +165,7 @@ following keys:
 
 =over 4
 
-=item * _name
+=item * type
 
 This indicates the structural unit that this hash is representing. While this
 specification provides for standard structural units, different Visitors may
@@ -153,30 +187,37 @@ responsible for validating that the elements are non-empty Strings.
 The hash will be structured as follows:
 
   {
-      _name   => 'identifier',
-      items   => [String],
+      type     => 'Identifier',
+      element1 => Scalar,
+      element2 => Scalar,
+      element3 => Scalar,
   }
 
-The items will always be quoted per the SQL dialect's quoting scheme. It is the
-responsibility of the Visitor to do this.
+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.
+
+Visitors are expected to, by default, quote all identifiers according to the SQL
+dialect's quoting scheme.
 
 =head3 Value
 
-A Value is a Perl scalar. It may either be a:
+A Value is a Perl scalar. Depending on the type, a Visitor may be able to make
+certain decisions.
 
 =over 4
 
 =item * String
 
-A String is a quoted series of characters
+A String is a quoted series of characters. The Visitor is expected to ensure
+that embedded quotes are properly handled per the SQL dialect's quoting scheme.
 
 =item * Number
 
-A Number is an unquoted number in some numeric format
+A Number is an unquoted number in some numeric format.
 
-=item * NULL
+=item * Null
 
-NULL is SQL's NULL and corresponds to Perl's C<undef>.
+Null is SQL's NULL and corresponds to Perl's C<undef>.
 
 =item * BindParameter
 
@@ -184,29 +225,54 @@ 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.)
 
+BindParameters are normally represented by a '?'.
+
 =back
 
-=head3 
+The hash will be structured as follows:
 
-=head3 Function
+  {
+      type    => 'Value'
+      subtype => [ 'String' | 'Number' | 'Null' | 'BindParameter' ]
+      value   => Scalar
+  }
 
-A Function is anything of the form C< name( arglist ) > where C<name> is a
-string and C<arglist> is a comma-separated list of Expressions.
+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.
 
-Yes, a Subquery is legal as an argument for many functions. Some example
-functions are:
+=head3 Operator
 
-=over 4
+An Operator would be, in SQL dialect terms, a unary operator, a binary operator,
+a trinary operator, or a function. Since different dialects may have a given
+functionality as an operator or a function (such as CONCAT in MySQl vs. || in
+Oracle for string concatenation), they will be represented in the AST as generic
+operators.
 
-=item * C<< IN >>
+The hash will be structured as follows:
 
-=item * C<< MAX >>
+  {
+      type => 'Operator',
+      op   => String,
+      args => ExpressionList,
+  }
 
-=item * C<< MIN >>
+Operators have a cardinality, or expected number of arguments. Some operators,
+such as MAX(), have a cardinality of 1. Others, such as IF(), have a cardinality
+of N, meaning they can have any number of arguments greater than 0. Others, such
+as NOW(), have a cardinality of 0. Several operators with the same meaning may
+have a different cardinality in different SQL dialects as different engines may
+allow different behaviors. As cardinality may differ between dialects, enforcing
+cardinality is necessarily left to the Visitor.
 
-=item * C<< SUM >>
+Operators also have restrictions on the types of arguments they will accept. The
+first argument may or may not restricted in the same fashion as the other
+arguments. As with cardinality, this restriction will need to be managed by the
+Visitor.
 
-=back
+The operator name needs to take into account the possibility that the RDBMS may
+allow UDFs (User-Defined Functions) that have the same name as an operator, such
+as 'AND'. This will have to be managed by the Visitor.
 
 =head3 Subquery
 
@@ -214,72 +280,46 @@ A Subquery is another AST whose _query 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
-easily enforce. The single-column restriction can possibly be enforced, but the
+easily enforce. The single-column restriction may possibly be enforced, but the
 single-row restriction is much more difficult and, in most cases, probably
 impossible.
 
-Subqueries, when expressed in SQL, must bounded by parentheses.
-
-=head3 Unary Operator
-
-A UnaryOperator takes a single argument on the RHS and is one of the following:
-
-=over 4
-
-=item * C<< NOT >>
+Subqueries, when expressed in SQL, must be bounded by parentheses.
 
-=back
-
-=head3 BinaryOperator
+=head3 Expression
 
-A BinaryOperator takes two arguments (one on the LHS and one on the RHS) and is
-one of the following:
+An Expression can be any one of the following:
 
 =over 4
 
-=item * C<< = >>
-
-=item * C<< != >>
-
-=item * C<< > >>
+=item * Identifier
 
-=item * C<< < >>
-
-=item * C<< >= >>
-
-=item * C<< <= >>
+=item * Value
 
-=item * C<< IS >>
+=item * Operator
 
-=item * C<< IS NOT >>
+=item * Subquery
 
 =back
 
-Note that an operator can comprise of what would be multiple tokens in a normal
-parsing effort.
-
-=head3 Expression
-
-An expression can be any one of the following:
-
-=over 4
+An Expression is a meta-syntactic unit. An "Expression" unit will never appear
+within the AST. It acts as a junction.
 
-=item * Value
+=head3 ExpressionList
 
-=item * Function
+An ExpressionList is a list of Expressions, generally separated by commas
+(though other separators may be appropriate at times or for different SQL
+dialects).
 
-=item * Subquery
+The hash for an ExpressionList is as follows:
 
-=item * UnaryOperator Expression
-
-=item * Expression BinaryOperator Expression
-
-=item * ( Expression )
-
-=back
+  {
+      type      => 'ExpressionList',
+      separator => ',',
+      elements  =>  Array of Expressions,
+  }
 
-Parentheses indicate precedence and, in some situations, are necessary for
-certain operators.
+An ExpressionList is always rendered in SQL with parentheses around it.
 
 =head2 SQL clauses
 
@@ -323,12 +363,18 @@ The expected clauses are (name and structure):
 
 This corresponds to the SELECT clause of a SELECT statement.
 
-A select clause is composed as follows:
+A select clause unit is an array of one or more SelectComponent units.
 
-  SelectComponent := Expression [ [ AS ] String ]
+The hash for a SelectComponent unit is composed as follows:
 
-  SelectComponent
-  [ , SelectComponent ]*
+  {
+      type  => 'SelectComponent',
+      value => Expression,
+      [ as    => Identifier, ]
+  }
+
+The 'as' component is optional. Visitors may choose to make it required in
+certain situations.
 
 =head3 tables
 
@@ -337,30 +383,53 @@ FROM clause in a SELECT statement and the INSERT INTO/UPDATE/DELETE clauses in
 those respective statements. Depending on the _query metadata entry, the
 appropriate clause name will be used.
 
+A tables clause unit is an array of one or more TableComponent units.
+
 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 table clause is composed as follows:
+The hash for a TableJoin will be composed as follows:
 
-  TableIdentifier := Identifier [ [ AS ] String ]
-  JoinType := < LEFT|RIGHT [ OUTER ] > | INNER | CROSS
+  # TableJoin
+  {
+      type => 'TableJoin',
+      join => < LEFT|RIGHT [ OUTER ] > | INNER | CROSS | ',',
+      [ using => IdentifierList, ]
+      [ on    => ExpressionList, ]
+  }
+
+A TableJoin may not have both a 'using' element and an 'on' element. It may
+have one of them if the 'join' element is not equal to ',' but doesn't have to.
+If the 'join' element is equal to ',', then it may not have either a 'using' or
+an 'on' element.
 
-  TableIdentifier
-  [
-      < , TableIdentifier >
-    | <
-        [ JoinType ] JOIN TableIdentifier
-        [
-            < USING ( Identifier [ , Identifier ] ) >
-          | < ON [ ( ] Expression [ , Expression ] [ ) ] >
-        ]
-      >
-  ]*
+The hash for a TableIdentifier will be composed as follows:
+
+  # TableIdentifier
+  {
+      type  => 'TableIdentifier',
+      value => Identifier | SubQuery
+      [ join  => TableJoin, ]
+      [ as    => Identifier, ]
+  }
+
+The first TableComponent in a tables clause may not have a join element. All
+other TableComponent elements that do not have a join element will have a
+default join element of:
+
+  {
+      type => 'TableJoin',
+      join => ',',
+  }
+
+The 'as' component is optional. Visitors may choose to make it required in
+certain situations (such as MySQL requiring an alias for subqueries).
 
 Additionally, where aliases are provided for in the TableIdentifier, those
 aliases must be used as the tablename in subsequent Identifiers that identify a
-column of that table.
+column of that table. This may be enforceable by the AST or the Visitor. But, it
+is more likely that it will not be.
 
 =head3 where
 
@@ -387,17 +456,23 @@ A set clause is composed as follows:
 
 This corresponds to the optional list of columns in an INSERT statement.
 
-A columns clause is composed as follows:
+A columns clause is an IdentifierList and the unit is composed as follows:
 
-  ( Identifier [ , Identifier ]* )
+  columns => [
+      Identifier,
+      [ Identifier, ]*
+  ],
 
 =head3 values
 
 This corresponds to the VALUES clause in an INSERT statement.
 
-A values clause is composed as follows:
+A values clause is an ExpressionList and the unit is composed as follows.
 
-  ( Expression [ , Expression ]* )
+  values => [
+      Expression,
+      [ Expression, ]*
+  ],
 
 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.
@@ -451,6 +526,64 @@ 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
+
+  {
+      _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 => 'TablesComponent',
+              value => {
+                 type => 'Identifier',
+                 element1 => 'dual',
+             },
+              as => {
+                  type     => 'Identifier',
+                  element1 => 'duality',
+              },
+         },
+      ],
+  }
+
+=back
+
 =head1 AUTHORS
 
 robkinyon: Rob Kinyon C<< <rkinyon@cpan.org> >>