4484cd95c30e61d63da0a6dcf2c5d16be345462d
[dbsrgits/SQL-Abstract-2.0-ish.git] / lib / SQL / Abstract / Manual / Specification.pod
1 =head1 NAME
2
3 SQL::Abstract::Manual::Specification
4
5 =head1 SYNOPSIS
6
7 This discusses the specification for the AST provided by L<SQL::Abstract>. It is
8 meant to describe how the AST is structured, various components provided by
9 L<SQL::Abstract> for use with this AST, how to manipulate the AST, and various
10 uses for the AST once it is generated.
11
12 =head1 MOTIVATIONS
13
14 L<SQL::Abstract> has been in use for many years. Originally created to handle
15 the where-clause formation found in L<DBIx::Abstract>, it was generalized to
16 manage the creation of any SQL statement through the use of Perl structures.
17 Through the beating it received as the SQL generation syntax for L<DBIx::Class>,
18 various deficiencies were found and a generalized SQL AST was designed. This
19 document describes that AST.
20
21 =head1 GOALS
22
23 The goals for this AST are as follows:
24
25 =head2 SQL-specific semantics
26
27 Instead of attempting to be an AST to handle any form of query, this will
28 instead be specialized to manage SQL queries (and queries that map to SQL
29 queries). This means that there will be support for SQL-specific features, such
30 as placeholders.
31
32 =head2 Perl-specific semantics
33
34 This AST is meant to be used from within Perl5 only. So, it will take advantage
35 of as many Perl-specific features that make sense to use. No attempt whatosever
36 will be made to make this AST work within any other language, including Perl6.
37
38 =head2 Whole-lifecycle management
39
40 Whether a query is built out of whole cloth in one shot or cobbled together from
41 several snippets over the lifetime of a process, this AST will support any way
42 to construct the query. Queries can also be built from other queries, so an
43 UPDATE statement could be used as the basis for a SELECT statement, DELETE
44 statement, or even a DDL statement of some kind.
45
46 =head2 Dialect-agnostic usage
47
48 Even though SQL itself has several ANSI specifications (SQL-92 and SQL-99 among
49 them), this only serves as a basis for what a given RDBMS will expect. However,
50 every engine has its own specific extensions and specific ways of handling
51 common features. The AST will provide ways of expressing common functionality in
52 a common language. The emitters (objects that follow the Visitor pattern) will
53 be responsible for converting that common language into RDBMS-specific SQL.
54
55 =head1 RESTRICTIONS
56
57 The following are the restrictions upon the AST:
58
59 =head2 DML-only
60
61 The AST will only support DML (Data Modelling Language). It will not (currently)
62 support DDL (Data Definition Language). Practically, this means that the only
63 statements supported will be:
64
65 =over 4
66
67 =item * SELECT
68
69 =item * INSERT INTO
70
71 =item * UPDATE
72
73 =item * DELETE
74
75 =back
76
77 Additional DML statements may be supported by specific Visitors (such as a
78 MySQL visitor supporting REPLACE INTO). q.v. the relevant sections of this
79 specification for details.
80
81 =head2 Dialect-agnostic construction
82
83 The AST will not attempt to be immediately readable to a human as SQL. In fact,
84 due to the dialect differences, particularly in terms of which use operators and
85 which use functions for a given action, the AST will provide simple units. It is
86 the responsibility of the Visitor to provide the appropriate SQL. Furthermore,
87 the AST will be very generic and only provide hints for a subset of SQL. If a
88 Visitor is sufficiently intelligent, pretty SQL may be emitted, but that is not
89 the goal of this AST.
90
91 =head1 COMPONENTS
92
93 There are two major components to SQL::Abstract v2.
94
95 =over 4
96
97 =item * AST
98
99 This is the Abstract Syntax Tree. It is a data structure that represents
100 everything necessary to construct the SQL statement in whatever dialect the
101 user requires.
102
103 =item * Visitor
104
105 This object conforms to the Visitor pattern and is used to generate the SQL
106 represented by the AST. Each dialect will have a different Visitor object. In
107 addition, there will be visitors for at least one of the ANSI specifications.
108
109 =back
110
111 The division of duties between the two components will focus on what the AST
112 can and cannot assume. For example, identifiers do not have 20 components in
113 any dialect, so the AST can validate that. However, determining what
114 constitutes a legal identifier can only be determined by the Visitor object
115 enforcing that dialect's rules.
116
117 =head1 AST STRUCTURE
118
119 The AST will be a HoHo..oH (hash of hash of ... of  hashes). The keys to the
120 outermost hash will be the various clauses of a SQL statement, plus some
121 metadata keys. All metadata keys will be identifiable as such by being prefixed
122 with an underscore. All keys will be in lowercase.
123
124 =head2 Metadata keys
125
126 These are the additional metadata keys that the AST provides for.
127
128 =head3 _query
129
130 This denotes what kind of query this AST should be interpreted as. Different
131 Visitors may accept additional values for _query. For example, a MySQL Visitor
132 may choose to accept 'replace' for REPLACE INTO. If a _query value is
133 unrecognized by the Visitor, the Visitor is expected to throw an error.
134
135 All Visitors are expected to handle the following values for _query:
136
137 =over 4
138
139 =item * select
140
141 This is a SELECT statement.
142
143 =item * insert
144
145 This is an INSERT statement.
146
147 =item * update
148
149 This is an UPDATE statement.
150
151 =item * delete
152
153 This is a DELETE statement.
154
155 =back
156
157 =head3 _version
158
159 This denotes the version of the AST. Different versions will indicate different
160 capabilities provided. Visitors will choose to respect the _version as needed
161 and desired.
162
163 =head2 Structural units
164
165 All structural units will be hashes. These hashes will have, at minimum, the
166 following keys:
167
168 =over 4
169
170 =item * type
171
172 This indicates the structural unit that this hash is representing. While this
173 specification provides for standard structural units, different Visitors may
174 choose to accept additional units as desired. If a Visitor encounters a unit it
175 doesn't know how to handle, it is expected to throw an exception. 
176
177 =back
178
179 Structural units in the AST are supported by loaded components. L<SQL::Abstract>
180 provides for the following structural units by default:
181
182 =head3 Identifier
183
184 This is a (potentially) fully canonicalized identifier for a elemnt in the
185 query. This element could be a schema, table, or column. The Visitor will
186 determine validity within the context of that SQL dialect. The AST is only
187 responsible for validating that the elements are non-empty Strings.
188
189 The hash will be structured as follows:
190
191   {
192       type     => 'Identifier',
193       element1 => Scalar,
194       element2 => Scalar,
195       element3 => Scalar,
196   }
197
198 If element3 exists, then element2 must exist. element1 must always exist. If a
199 given element exists, then it must be defined and of non-zero length.
200
201 Visitors are expected to, by default, quote all identifiers according to the SQL
202 dialect's quoting scheme.
203
204 =head3 Value
205
206 A Value is a Perl scalar. Depending on the type, a Visitor may be able to make
207 certain decisions.
208
209 =over 4
210
211 =item * String
212
213 A String is a quoted series of characters. The Visitor is expected to ensure
214 that embedded quotes are properly handled per the SQL dialect's quoting scheme.
215
216 =item * Number
217
218 A Number is an unquoted number in some numeric format.
219
220 =item * Null
221
222 Null is SQL's NULL and corresponds to Perl's C<undef>.
223
224 =item * BindParameter
225
226 This corresponds to a value that will be passed in. This value is normally
227 quoted in such a fashion so as to protect against SQL injection attacks. (q.v.
228 L<DBI/quote()> for an example.)
229
230 BindParameters are normally represented by a '?'.
231
232 =back
233
234 The hash will be structured as follows:
235
236   {
237       type    => 'Value'
238       subtype => [ 'String' | 'Number' | 'Null' | 'BindParameter' ]
239       value   => Scalar
240   }
241
242 The provided subtypes are the ones that all Visitors are expected to support.
243 Visitors may choose to support additional subtypes. Visitors are expected to
244 throw an exception upon encountering an unknown subtype.
245
246 =head3 Operator
247
248 An Operator would be, in SQL dialect terms, a unary operator, a binary operator,
249 a trinary operator, or a function. Since different dialects may have a given
250 functionality as an operator or a function (such as CONCAT in MySQl vs. || in
251 Oracle for string concatenation), they will be represented in the AST as generic
252 operators.
253
254 The hash will be structured as follows:
255
256   {
257       type => 'Operator',
258       op   => String,
259       args => ExpressionList,
260   }
261
262 Operators have a cardinality, or expected number of arguments. Some operators,
263 such as MAX(), have a cardinality of 1. Others, such as IF(), have a cardinality
264 of N, meaning they can have any number of arguments greater than 0. Others, such
265 as NOW(), have a cardinality of 0. Several operators with the same meaning may
266 have a different cardinality in different SQL dialects as different engines may
267 allow different behaviors. As cardinality may differ between dialects, enforcing
268 cardinality is necessarily left to the Visitor.
269
270 Operators also have restrictions on the types of arguments they will accept. The
271 first argument may or may not restricted in the same fashion as the other
272 arguments. As with cardinality, this restriction will need to be managed by the
273 Visitor.
274
275 The operator name needs to take into account the possibility that the RDBMS may
276 allow UDFs (User-Defined Functions) that have the same name as an operator, such
277 as 'AND'. This will have to be managed by the Visitor.
278
279 =head3 Subquery
280
281 A Subquery is another AST whose _query metadata parameter is set to "SELECT".
282
283 Most places that a Subquery can be used would require a single value to be
284 returned (single column, single row), but that is not something that the AST can
285 easily enforce. The single-column restriction may possibly be enforced, but the
286 single-row restriction is much more difficult and, in most cases, probably
287 impossible.
288
289 Subqueries, when expressed in SQL, must be bounded by parentheses.
290
291 =head3 Expression
292
293 An Expression can be any one of the following:
294
295 =over 4
296
297 =item * Identifier
298
299 =item * Value
300
301 =item * Operator
302
303 =item * Subquery
304
305 =back
306
307 An Expression is a meta-syntactic unit. An "Expression" unit will never appear
308 within the AST. It acts as a junction.
309
310 =head3 ExpressionList
311
312 An ExpressionList is a list of Expressions, generally separated by commas
313 (though other separators may be appropriate at times or for different SQL
314 dialects). An null separator may also be used.
315
316 The hash for an ExpressionList is as follows:
317
318   {
319       type      => 'ExpressionList',
320       separator => ',',
321       elements  =>  Array of Expressions,
322   }
323
324 An ExpressionList is always rendered in SQL with parentheses around it.
325
326 =head2 SQL clauses
327
328 These are all the legal and acceptable clauses within the AST that would
329 correpsond to clauses in a SQL statement. Not all clauses are legal within a
330 given RDBMS engine's SQL dialect and some clauses may be required in one and
331 optional in another. Detecting and enforcing those engine-specific restrictions
332 is the responsibility of the Visitor object.
333
334 The clauses are defined with a yacc-like syntax. The various parts are:
335
336 =over 4
337
338 =item * :=
339
340 This means "defined" and is used to create a new term to be used below.
341
342 =item * []
343
344 This means optional and indicates that the items within it are optional.
345
346 =item * []*
347
348 This means optional and repeating as many times as desired.
349
350 =item * |
351
352 This means alternation. It is a binary operator and indicates that either the
353 left or right hand sides may be used, but not both.
354
355 =item * C<< <> >>
356
357 This is a grouping construct. It means that all elements within this construct
358 are treated together for the purposes of optional, repeating, alternation, etc.
359
360 =back
361
362 The expected clauses are (name and structure):
363
364 =head3 select
365
366 This corresponds to the SELECT clause of a SELECT statement.
367
368 A select clause unit is an array of one or more SelectComponent units.
369
370 The hash for a SelectComponent unit is composed as follows:
371
372   {
373       type  => 'SelectComponent',
374       value => Expression,
375       as    => String,
376   }
377
378 The 'as' component is optional. Visitors may choose to make it required in
379 certain situations.
380
381 =head3 tables
382
383 This is a list of tables that this clause is affecting. It corresponds to the
384 FROM clause in a SELECT statement and the INSERT INTO/UPDATE/DELETE clauses in
385 those respective statements. Depending on the _query metadata entry, the
386 appropriate clause name will be used.
387
388 The tables clause has several RDBMS-specific variations. The AST will support
389 all of them and it is up to the Visitor object constructing the actual SQL to
390 validate and/or use what is provided as appropriate.
391
392 A TableJoin is a junction of the following elements:
393
394 =over 4
395
396 =item * TableIdentifier
397
398 =item * Operator
399
400 =back
401
402 The hash for a TableIdentifier will be composed as follows:
403
404   # TableIdentifier
405   {
406       type  => 'TableIdentifier',
407       value => Expression,
408       as    => String,
409   }
410
411 The value should be either an Identifier or a SubQuery. 
412
413 The hash for an Operator within a tables clause will be composed as follows:
414
415   # Operator
416   {
417       type => 'Operator',
418       op   => '< LEFT|RIGHT|FULL [ OUTER ] > | INNER | CROSS',
419       on   => Expression,
420   }
421
422 A USING clause is syntactic sugar for an ON clause and, as such, is not provided
423 for by the AST. A join of a comma is identical to a CROSS JOIN. The on clause is
424 optional.
425
426 =head3 where
427
428 This corresponds to the WHERE clause in a SELECT, UPDATE, or DELETE statement.
429
430 A where clause is composed as follows:
431
432   WhereOperator := AND | OR
433   WhereExpression := Expression | Expression WhereOperator Expression
434
435   WhereExpression
436
437 =head3 set
438
439 This corresponds to the SET clause in an INSERT or UPDATE statement.
440
441 A set clause is composed as follows:
442
443   SetComponent := Identifier = Expression
444
445   SetComponent [ , SetComponent ]*
446
447 =head3 columns
448
449 This corresponds to the optional list of columns in an INSERT statement.
450
451 A columns clause is an IdentifierList and the unit is composed as follows:
452
453   columns => [
454       Identifier,
455       [ Identifier, ]*
456   ],
457
458 =head3 values
459
460 This corresponds to the VALUES clause in an INSERT statement.
461
462 A values clause is an ExpressionList and the unit is composed as follows.
463
464   values => [
465       Expression,
466       [ Expression, ]*
467   ],
468
469 If there is a columns clause, the number of entries in the values clause must be
470 equal to the number of entries in the columns clause.
471
472 =head3 orderby
473
474 This corresponds to the ORDER BY clause in a SELECT statement.
475
476 An orderby clause is composed as follows:
477
478   OrderByComponent := XXX-TODO-XXX
479   OrderByDirection := ASC | DESC
480
481   OrderByComponent [ OrderByDirection ]
482   [ , OrderByComponent [ OrderByDirection ] ]*
483
484 =head3 groupby
485
486 This corresponds to the GROUP BY clause in a SELECT statement.
487
488 An groupby clause is composed as follows:
489
490   GroupByComponent := XXX-TODO-XXX
491
492   GroupByComponent [ , GroupByComponent ]*
493
494 =head3 rows
495
496 This corresponds to the clause that is used in some RDBMS engines to limit the
497 number of rows returned by a query. In MySQL, this would be the LIMIT clause.
498
499 A rows clause is composed as follows:
500
501   Number [, Number ]
502
503 =head3 for
504
505 This corresponds to the clause that is used in some RDBMS engines to indicate
506 what locks are to be taken by this SELECT statement.
507
508 A for clause is composed as follows:
509
510   UPDATE | DELETE
511
512 =head3 connectby
513
514 This corresponds to the clause that is used in some RDBMS engines to provide for
515 an adjacency-list query.
516
517 A connectby clause is composed as follows:
518
519   Identifier, WhereExpression
520
521 =head1 EXAMPLES
522
523 The following are example SQL statements and a possible AST for each one.
524
525 =over 4
526
527 =item * SELECT 1
528
529   {
530       _query => 'select',
531       _ast_version => 0.0001,
532       select => [
533           {
534               type  => 'SelectComponent',
535               value => {
536                   type    => 'Value',
537                   subtype => 'number',
538                   value   => 1,
539               },
540           },
541       ],
542   }
543
544 =item * SELECT NOW() AS time FROM dual AS duality
545
546   {
547       _query => 'select',
548       _ast_version => 0.0001,
549       select => [
550           {
551               type  => 'SelectComponent',
552               value => {
553                   type     => 'Function',
554                   function => 'NOW',
555               },
556               as => {
557                   type     => 'Identifier',
558                   element1 => 'time',
559               },
560           },
561       ],
562       tables => {
563           type => 'TableIdentifier',
564           value => {
565               type => 'Identifier',
566               element1 => 'dual',
567           },
568           as => 'duality',
569       },
570   }
571
572 =item * SELECT 1 FROM foo LEFT OUTER JOIN bar ON ( foo.col1 = bar.col2 )
573
574   {
575       _query => 'select',
576       _ast_version => 0.0001,
577       select => [
578           {
579               type  => 'SelectComponent',
580               value => {
581                   type     => 'Value',
582                   subtype => 'number',
583                   value   => 1,
584               },
585           },
586       ],
587       tables => {
588           type => 'Operator',
589           op   => 'LEFT OUTER',
590           args => [
591               {
592                   type => 'TableIdentifier',
593                   value => {
594                       type => 'Identifier',
595                       element1 => 'foo',
596                   },
597               },
598               {
599                   type => 'TableIdentifier',
600                   value => {
601                       type => 'Identifier',
602                       element1 => 'bar',
603                   },
604               },
605           ],
606           on => {
607               type => 'Operator',
608               op   => '=',
609               args => [
610                   {
611                       type     => 'Identifier',
612                       element1 => 'foo',
613                       element2 => 'col1',
614                   },
615                   {
616                       type     => 'Identifier',
617                       element1 => 'bar',
618                       element2 => 'col2',
619                   },
620               ],
621           },
622       },
623   }
624
625 =back
626
627 =head1 TODO
628
629 =over 4
630
631 =item * sproc unit
632
633 =back
634
635 =head1 AUTHORS
636
637 robkinyon: Rob Kinyon C<< <rkinyon@cpan.org> >>
638
639 =head1 LICENSE
640
641 You may distribute this code under the same terms as Perl itself.
642
643 =cut