890798138df0ce5aa5b0583d12655b184f01ace8
[dbsrgits/SQL-Abstract-2.0-ish.git] / lib / SQL / Abstract / Manual / Examples.pod
1 =head1 NAME
2
3 SQL::Abstract::Manual::Specification
4
5 =head1 DESCRIPTION
6
7 These are the examples for the AST
8
9 =head1 EXAMPLES
10
11 The following are example SQL statements and the AST that would represent each
12 one. The SQL used is from the MySQL dialect.
13
14 =over 4
15
16 =item * SELECT 1
17
18   {
19       type => 'select',
20       ast_version => 0.0001,
21       select => [
22           {
23               type    => 'Value',
24               subtype => 'Number',
25               value   => 1,
26           },
27       ],
28   }
29
30 =item * SELECT NOW() AS time FROM dual AS duality
31
32   {
33       type => 'select',
34       ast_version => 0.0001,
35       select => [
36           {
37               type  => 'Alias',
38               value => {
39                   type     => 'Function',
40                   function => 'NOW',
41               },
42               as => {
43                   type => 'Identifier',
44                   args => [ 'time' ],
45               },
46           },
47       ],
48       tables => {
49           type => 'Alias',
50           value => {
51               type => 'Identifier',
52               args => [ 'dual' ],
53           },
54           as => {
55               type => 'Identifier',
56               args => [ 'duality' ],
57           },
58       },
59   }
60
61 =item * SELECT 1 FROM foo LEFT OUTER JOIN bar ON ( foo.col1 = bar.col2 )
62
63   {
64       type => 'select',
65       ast_version => 0.0001,
66       select => [
67           {
68               type     => 'Value',
69               subtype => 'Number',
70               value   => 1,
71           },
72       ],
73       tables => {
74           type => 'Operator',
75           op   => 'LEFT OUTER',
76           args => [
77               {
78                   type => 'Identifier',
79                   args => [ 'foo' ],
80               },
81               {
82                   type => 'Identifier',
83                   args => [ 'bar' ],
84               },
85           ],
86           on => {
87               type => 'Operator',
88               op   => '=',
89               args => [
90                   {
91                       type => 'Identifier',
92                       args => [ 'foo', 'col1' ],
93                   },
94                   {
95                       type => 'Identifier',
96                       args => [ 'bar', 'col2' ],
97                   },
98               ],
99           },
100       },
101   }
102
103 =item SELECT * FROM foo WHERE name = 'John'
104
105   {
106       type => 'select',
107       ast_version => 0.0001,
108       select => [
109           {
110               type => 'Identifier',
111               args => [ '*' ],
112           },
113       ],
114       tables => {
115           type => 'Identifier',
116           args => [ 'foo' ],
117       },
118       where => {
119           type => 'Operator',
120           op   => '=',
121           args => [
122               {
123                   type => 'Identifier',
124                   args => [ 'name' ],
125               },
126               {
127                   type    => 'Value',
128                   subtype => 'String',
129                   value   => 'John',
130               },
131           ],
132       },
133   }
134
135 =item SELECT COUNT(*) FROM foo WHERE name = 'John' AND ( title = 'Mr' OR abbrev = 'Dr' )
136
137   {
138       type => 'select',
139       ast_version => 0.0001,
140       select => [
141           {
142               type => 'Operator',
143               op   => 'COUNT',
144               args => [
145                   {
146                       type => 'Identifier',
147                       args => [ '*' ],
148                   },
149               ],
150           },
151       ],
152       tables => {
153           type => 'Identifier',
154           args => [ 'foo' ],
155       },
156       where => {
157           type => 'Operator',
158           op   => 'AND',
159           args => [
160               {
161                   type => 'Operator',
162                   op   => '=',
163                   args => [
164                       {
165                           type => 'Identifier',
166                           args => [ 'name' ],
167                       },
168                       {
169                           type    => 'Value',
170                           subtype => 'String',
171                           value   => 'John',
172                       },
173                   ],
174               },
175               {
176                   type => 'Operator',
177                   op   => 'OR',
178                   args => [
179                       {
180                           type => 'Operator',
181                           op   => '=',
182                           args => [
183                               {
184                                   type => 'Identifier',
185                                   args => [ 'title' ],
186                               },
187                               {
188                                   type    => 'Value',
189                                   subtype => 'String',
190                                   value   => 'Mr',
191                               },
192                           ],
193                       },
194                       {
195                           type => 'Operator',
196                           op   => '=',
197                           args => [
198                               {
199                                   type => 'Identifier',
200                                   args => [ 'abbrev' ],
201                               },
202                               {
203                                   type    => 'Value',
204                                   subtype => 'String',
205                                   vaue    => 'Dr',
206                               },
207                           ],
208                       },
209                   ],
210               },
211           ],
212       },
213   }
214  
215 =item SELECT COUNT(DISTINCT(*)) FROM foo WHERE ( name = 'John' AND title = 'Mr' ) OR abbrev = 'Dr'
216
217   {
218       type => 'select',
219       ast_version => 0.0001,
220       select => [
221         {
222               type => 'Operator',
223               op   => 'COUNT',
224               args => [
225                   {
226                       type => 'Operator',
227                       op   => 'DISTINCT',
228                       args => [
229                           {
230                               type => 'Identifier',
231                               args => [ '*' ],
232                           },
233                       ],
234                   },
235               ],
236           },
237       ],
238       tables => {
239           type => 'Identifier',
240           args => [ 'foo' ],
241       },
242       where => {
243           type => 'Operator',
244           op   => 'OR',
245           args => [
246               {
247                   type => 'Operator',
248                   op   => 'AND',
249                   args => [
250                       {
251                           type => 'Operator',
252                           op   => '=',
253                           args => [
254                               {
255                                   type => 'Identifier',
256                                   args => [ 'name' ],
257                               },
258                               {
259                                   type    => 'Value',
260                                   subtype => 'String',
261                                   value   => 'John',
262                               },
263                           ],
264                       },
265                       {
266                           type => 'Operator',
267                           op   => '=',
268                           args => [
269                               {
270                                   type => 'Identifier',
271                                   args => [ 'title' ],
272                               },
273                               {
274                                   type    => 'Value',
275                                   subtype => 'String',
276                                   value   => 'Mr',
277                               },
278                           ],
279                       },
280                   ],
281               },
282               {
283                   type => 'Operator',
284                   op   => '=',
285                   args => [
286                       {
287                           type => 'Identifier',
288                           args => [ 'abbrev' ],
289                       },
290                       {
291                           type     => 'Value',
292                           subtype  => 'String',
293                           value => 'Dr',
294                       },
295                   ],
296               },
297           ],
298       },
299   }
300
301 =item * SELECT foo, bar baz FROM foo ORDER BY bar, baz DESC GROUP BY 1,3,2
302
303   {
304       type => 'select',
305       ast_version => 0.0001,
306       select => [
307           {
308               type => 'Identifier',
309               args => [ 'foo' ],
310           },
311           {
312               type => 'Identifier',
313               elements => 'bar',
314           },
315           {
316               type => 'Identifier',
317               args => [ 'baz' ],
318           },
319       ],
320       tables => {
321           type => 'Identifier',
322           args => [ 'foo' ],
323       },
324       orderby => [
325           {
326               type  => 'OrderbyComponent',
327               value => {
328                   type => 'Identifier',
329                   args => [ 'bar' ],
330               },
331               dir => 'ASC',
332           },
333           {
334               type  => 'OrderbyComponent',
335               value => {
336                   type => 'Identifier',
337                   args => [ 'baz' ],
338               },
339               dir => 'DESC',
340           },
341       ],
342       groupby => [
343           {
344               type  => 'GroupbyComponent',
345               value => {
346                   type => 'Value',
347                   subtype => 'Number',
348                   value => 1,
349               },
350           },
351           {
352               type  => 'GroupbyComponent',
353               value => {
354                   type => 'Value',
355                   subtype => 'Number',
356                   value => 3,
357               },
358           },
359           {
360               type  => 'GroupbyComponent',
361               value => {
362                   type => 'Value',
363                   subtype => 'Number',
364                   value => 2,
365               },
366           },
367       ],
368   }
369
370 =item * SELECT * FROM ( SELECT 1 ) AS foo
371
372   {
373       type => 'select',
374       ast_version => 0.0001,
375       select => [
376           {
377               type => 'Identifier',
378               args => [ '*' ],
379           },
380       ],
381       tables => {
382           type => 'Identifier',
383           args => [ 'foo' ],
384           value => {
385               type => 'select',
386               ast_version => 0.0001,
387               select => [
388                   {
389                       type    => 'Value',
390                       subtype => 'Number',
391                       value   => 1,
392                   },
393               ],
394           },
395           as => {
396               type => 'Identifier',
397               args => [ 'foo' ],
398           },
399       },
400   }
401
402 =item * INSERT INTO foo ( col1, col2 ) VALUES ( 1, 3 )
403
404   {
405       type => 'insert',
406       ast_version => 0.0001,
407       tables => {
408           type => 'Identifier',
409           args => [ 'foo' ],
410       },
411       set => [
412           [
413               {
414                   type => 'Identifier,
415                   args => [ 'col1' ],
416               },
417               {
418                   type => 'Identifier,
419                   args => [ 'col2' ],
420               },
421           ],
422           [
423               {
424                   type => 'Value',
425                   subtype => 'Number',
426                   value => '1',
427               },
428               {
429                   type => 'Value',
430                   subtype => 'Number',
431                   value => '3',
432               },
433           ],
434       ],
435   }
436
437 =item * INSERT INTO foo ( col1, col2 ) VALUES ( 1, 3 ), ( 2, 4 )
438
439   {
440       type => 'insert',
441       ast_version => 0.0001,
442       tables => {
443           type => 'Identifier',
444           args => [ 'foo' ],
445       },
446       set => [
447           [
448               {
449                   type => 'Identifier,
450                   args => [ 'col1' ],
451               },
452               {
453                   type => 'Identifier,
454                   args => [ 'col2' ],
455               },
456           ],
457           [
458               {
459                   type => 'Value',
460                   subtype => 'Number',
461                   value => '1',
462               },
463               {
464                   type => 'Value',
465                   subtype => 'Number',
466                   value => '3',
467               },
468           ],
469           [
470               {
471                   type => 'Value',
472                   subtype => 'Number',
473                   value => '2',
474               },
475               {
476                   type => 'Value',
477                   subtype => 'Number',
478                   value => '3',
479               },
480           ],
481       ],
482   }
483
484 =item * UPDATE foo SET col1 = 1
485
486   {
487       type => 'update',
488       ast_version => 0.0001,
489       tables => {
490           type => 'Identifier',
491           args => [ 'foo' ],
492       },
493       set => [
494           [
495               {
496                   type => 'Identifier,
497                   args => [ 'col1' ],
498               },
499           ],
500           [
501               {
502                   type => 'Value',
503                   subtype => 'Number',
504                   value => 1,
505               },
506           ],
507       ],
508   }
509
510 =item * UPDATE foo SET col1 = 1, col2 = 6
511
512   {
513       type => 'update',
514       ast_version => 0.0001,
515       tables => {
516           type => 'Identifier',
517           args => [ 'foo' ],
518       },
519       set => [
520           [
521               {
522                   type => 'Identifier,
523                   args => [ 'col1' ],
524               },
525               {
526                   type => 'Identifier,
527                   args => [ 'col2' ],
528               },
529           ],
530           [
531               {
532                   type => 'Value',
533                   subtype => 'Number',
534                   value => 1,
535               },
536               {
537                   type => 'Value',
538                   subtype => 'Number',
539                   value => 6,
540               },
541           ],
542       ],
543   }
544
545 =item * DELETE FROM foo WHERE col1 = 10
546
547   {
548       type => 'delete',
549       ast_version => 0.0001,
550       tables => {
551           type => 'Identifier',
552           args => [ 'foo' ],
553       },
554       where => {
555           type => 'Operator',
556           op   => '=',
557           args => [
558               {
559                   type => 'Identifier',
560                   args => [ 'col1' ],
561               },
562               {
563                   type    => 'Value',
564                   subtype => 'Number',
565                   value   => 10,
566               },
567           ],
568       },
569   }
570
571 =item * INSERT INTO foo ( col1, col2 ) SELECT col1, col2 FROM bar;
572
573   {
574       type => 'insert',
575       ast_version => 0.0001,
576       tables => {
577           type => 'Identifier',
578           args => [ 'foo' ],
579       },
580       set => [
581           [
582               {
583                   type => 'Identifier,
584                   args => [ 'col1' ],
585               },
586               {
587                   type => 'Identifier,
588                   args => [ 'col2' ],
589               },
590           ],
591           [
592               {
593                   type => 'select',
594                   ast_version => 0.0001,
595                   select => [
596                       {
597                           type => 'Identifier',
598                           args => [ 'col1' ],
599                       },
600                       {
601                           type => 'Identifier',
602                           args => [ 'col2' ],
603                       },
604                   ],
605                   tables => {
606                       type => 'Identifier',
607                       args => [ 'bar' ],
608                   },
609               },
610           ],
611       ],
612   }
613
614
615 =back
616
617 =head1 AUTHORS
618
619 robkinyon: Rob Kinyon C<< <rkinyon@cpan.org> >>
620
621 =head1 LICENSE
622
623 You may distribute this code under the same terms as Perl itself.
624
625 =cut