Engine/Grammar.pm untested

filter: Statementstmt 0.0% 0 / 664 Branchbran 0.0% 0 / 354 Conditioncond 0.0% 0 / 38 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod n/a totaltotal 0.0% 0 / 1067 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package SQL::Engine::Grammar;
2  
3 use 5.014;
4  
5 use strict;
6 use warnings;
7  
8 use registry;
9 use routines;
10  
11 use Data::Object::Class;
12 use Data::Object::ClassHas;
13  
14 use SQL::Engine::Collection;
15 use SQL::Engine::Operation;
16 use SQL::Validator;
17  
18 use Scalar::Util ();
19  
20 our $VERSION = '0.02'; # VERSION
21  
22 # ATTRIBUTES
23  
24 has operations => (
25 is => 'ro',
26 isa => 'InstanceOf["SQL::Engine::Collection"]',
27 new => 1
28 );
29  
30 fun new_operations($self) {
31  
32 SQL::Engine::Collection->new;
33 }
34  
35 has schema => (
36 is => 'ro',
37 isa => 'HashRef',
38 req => 1
39 );
40  
41 has validator => (
42 is => 'ro',
43 isa => 'Maybe[InstanceOf["SQL::Validator"]]',
44 new => 1
45 );
46  
47 fun new_validator($self) {
48 SQL::Validator->new(
49 $ENV{SQL_ENGINE_SCHEMA}
50 ? (schema => $ENV{SQL_ENGINE_SCHEMA})
51 : (version => '0.0')
52 )
53 }
54  
55 # METHODS
56  
57 method binding(Str $name) {
58 $self->{bindings}{int(keys(%{$self->{bindings}}))} = $name;
59  
60 return '?';
61 }
62  
63 method column_change(HashRef $data) {
64 my $sql = [];
65  
66 # alter table
67 push @$sql, $self->term(qw(alter table));
68  
69 # safe
70 push @$sql, $self->term(qw(if exists)) if $data->{safe};
71  
72 # for
73 push @$sql, $self->table($data->{for});
74  
75 # column
76 push @$sql, $self->term(qw(alter column));
77  
78 # column specification
79 push @$sql, $self->column_specification($data->{column});
80  
81 # sql statement
82 my $result = join ' ', @$sql;
83  
84 return $self->operation($result);
85 }
86  
87 method column_create(HashRef $data) {
88 my $sql = [];
89  
90 # alter table
91 push @$sql, $self->term(qw(alter table));
92  
93 # safe
94 push @$sql, $self->term(qw(if exists)) if $data->{safe};
95  
96 # for
97 push @$sql, $self->table($data->{for});
98  
99 # column
100 push @$sql, $self->term(qw(add column));
101  
102 # column specification
103 push @$sql, $self->column_specification($data->{column});
104  
105 # sql statement
106 my $result = join ' ', @$sql;
107  
108 return $self->operation($result);
109 }
110  
111 method column_definition(HashRef $data) {
112 my $def = {};
113  
114 if ($data->{name}) {
115 $def->{name} = $self->name($data->{name});
116 }
117  
118 if ($data->{type}) {
119 $def->{type} = $self->type($data);
120 }
121  
122 if (exists $data->{default}) {
123 $def->{default} = join ' ', $self->term('default'),
124 $self->expression($data->{default});
125 }
126  
127 if (exists $data->{nullable}) {
128 $def->{nullable}
129 = $data->{nullable} ? $self->term('null') : $self->term(qw(not null));
130 }
131  
132 if ($data->{primary}) {
133 $def->{primary} = $self->term(qw(primary key));
134 }
135  
136 return $def;
137 }
138  
139 method column_drop(HashRef $data) {
140 my $sql = [];
141  
142 # alter table
143 push @$sql, $self->term(qw(alter table));
144  
145 # safe
146 push @$sql, $self->term(qw(if exists)) if $data->{safe};
147  
148 # table name
149 push @$sql, $self->table($data);
150  
151 # drop column
152 push @$sql, $self->term(qw(drop column));
153  
154 # safe
155 push @$sql, $self->term(qw(if exists)) if $data->{safe};
156  
157 # column name
158 push @$sql, $self->name($data->{column});
159  
160 # sql statement
161 my $result = join ' ', @$sql;
162  
163 return $self->operation($result);
164 }
165  
166 method column_specification(HashRef $data) {
167 my $sql = [];
168  
169 my $column = $self->column_definition($data);
170  
171 # name
172 push @$sql, $column->{name};
173  
174 # type
175 push @$sql, $column->{type};
176  
177 # nullable
178 push @$sql, $column->{nullable} if $column->{nullable};
179  
180 # default
181 push @$sql, $column->{default} if $column->{default};
182  
183 # primary
184 push @$sql, $column->{primary} if $column->{primary};
185  
186 # increments
187 push @$sql, $column->{increment} if $column->{increment};
188  
189 # sql statement
190 return join ' ', @$sql;
191 }
192  
193 method column_rename(HashRef $data) {
194 my $sql = [];
195  
196 # alter table
197 push @$sql, $self->term(qw(alter table));
198  
199 # safe
200 push @$sql, $self->term(qw(if exists)) if $data->{safe};
201  
202 # table name
203 push @$sql, $self->table($data->{"for"});
204  
205 # rename column
206 push @$sql, $self->term(qw(rename column)),
207 $self->name($data->{name}{old}),
208 $self->term('to'),
209 $self->name($data->{name}{new});
210  
211 # sql statement
212 my $result = join ' ', @$sql;
213  
214 return $self->operation($result);
215 }
216  
217 method constraint_create(HashRef $data) {
218 my $sql = [];
219  
220 # alter table
221 push @$sql, $self->term(qw(alter table));
222  
223 # safe
224 push @$sql, $self->term(qw(if exists)) if $data->{safe};
225  
226 # table name
227 push @$sql, $self->table($data->{source});
228  
229 # add constraint
230 push @$sql, $self->term(qw(add constraint));
231  
232 # constraint name
233 push @$sql, $self->name($self->constraint_name($data));
234  
235 # foreign key
236 push @$sql, $self->term(qw(foreign key));
237  
238 # column name
239 push @$sql, sprintf('(%s)', $self->name($data->{source}{column}));
240  
241 # references
242 push @$sql, $self->term('references');
243  
244 # foreign table and column name
245 push @$sql, sprintf('%s (%s)', $self->table($data->{target}),
246 $self->name($data->{target}{column}));
247  
248 # reference option (on delete)
249 if ($data->{on}{delete}) {
250 push @$sql, $self->term(qw(on delete)),
251 $self->constraint_option($data->{on}{delete});
252 }
253  
254 # reference option (on update)
255 if ($data->{on}{update}) {
256 push @$sql, $self->term(qw(on update)),
257 $self->constraint_option($data->{on}{update});
258 }
259  
260 # sql statement
261 my $result = join ' ', @$sql;
262  
263 return $self->operation($result);
264 }
265  
266 method constraint_drop(HashRef $data) {
267 my $sql = [];
268  
269 # alter table
270 push @$sql, $self->term(qw(alter table));
271  
272 # safe
273 push @$sql, $self->term(qw(if exists)) if $data->{safe};
274  
275 # table name
276 push @$sql, $self->table($data->{source});
277  
278 # drop constraint
279 push @$sql, $self->term(qw(drop constraint));
280  
281 # constraint name
282 push @$sql, $self->name($self->constraint_name($data));
283  
284 # sql statement
285 my $result = join ' ', @$sql;
286  
287 return $self->operation($result);
288 }
289  
290 method constraint_name(HashRef $data) {
291  
292 return $data->{name} || join('_', 'foreign',
293 join('_', grep {defined} @{$data->{source}}{qw(schema table column)}),
294 join('_', grep {defined} @{$data->{target}}{qw(schema table column)})
295 );
296 }
297  
298 method constraint_option(Str $name) {
299 if (lc($name) eq "cascade") {
300 return $self->term('cascade');
301 }
302 elsif (lc($name) eq "no-action") {
303 return $self->term(qw(no action));
304 }
305 elsif (lc($name) eq "restrict") {
306 return $self->term('restrict');
307 }
308 elsif (lc($name) eq "set-default") {
309 return $self->term(qw(set default));
310 }
311 elsif (lc($name) eq "set-null") {
312 return $self->term(qw(set null));
313 }
314 else {
315 return $self->term(qw(no action));
316 }
317 }
318  
319 method criteria(ArrayRef $data) {
320  
321 return [map $self->criterion($_), @$data];
322 }
323  
324 method criterion(HashRef $data) {
325 if (my $cond = $data->{"and"}) {
326 return sprintf('(%s)',
327 join(sprintf(' %s ', $self->term('and')), @{$self->criteria($cond)}));
328 }
329  
330 if (my $cond = $data->{"eq"}) {
331 return sprintf '%s = %s', map $self->expression($_), @$cond;
332 }
333  
334 if (my $cond = $data->{"glob"}) {
335 return sprintf '%s %s %s', $self->expression($cond->[0]),
336 $self->term('glob'), $self->expression($cond->[1]);
337 }
338  
339 if (my $cond = $data->{"gt"}) {
340 return sprintf '%s > %s', map $self->expression($_), @$cond;
341 }
342  
343 if (my $cond = $data->{"gte"}) {
344 return sprintf '%s >= %s', map $self->expression($_), @$cond;
345 }
346  
347 if (my $cond = $data->{"in"}) {
348 return sprintf '%s %s %s', $self->expression($cond->[0]),
349 $self->term('in'), join ', ', map $self->expression($_),
350 @$cond[1 .. $#$cond];
351 }
352  
353 if (my $cond = $data->{"is"}) {
354 return sprintf '(%s)',
355 (ref($cond) eq 'HASH')
356 ? $self->expression($cond)
357 : join(sprintf(' %s ', $self->term('and')), @{$self->criteria($cond)});
358 }
359  
360 if (my $cond = $data->{"is-null"}) {
361 return sprintf '%s IS NULL', $self->expression($cond);
362 }
363  
364 if (my $cond = $data->{"like"}) {
365 return sprintf '%s %s %s', $self->expression($cond->[0]),
366 $self->term('like'), $self->expression($cond->[1]);
367 }
368  
369 if (my $cond = $data->{"lt"}) {
370 return sprintf '%s < %s', map $self->expression($_), @$cond;
371 }
372  
373 if (my $cond = $data->{"lte"}) {
374 return sprintf '%s <= %s', map $self->expression($_), @$cond;
375 }
376  
377 if (my $cond = $data->{"ne"}) {
378 return sprintf '%s != %s', map $self->expression($_), @$cond;
379 }
380  
381 if (my $cond = $data->{"not"}) {
382 return sprintf 'NOT (%s)',
383 (ref($cond) eq 'HASH')
384 ? $self->expression($cond)
385 : join(sprintf(' %s ', $self->term('and')), @{$self->criteria($cond)});
386 }
387  
388 if (my $cond = $data->{"not-null"}) {
389 return sprintf '%s IS NOT NULL', $self->expression($cond);
390 }
391  
392 if (my $cond = $data->{"or"}) {
393 return sprintf('(%s)',
394 join(sprintf(' %s ', $self->term('or')), @{$self->criteria($cond)}));
395 }
396  
397 if (my $cond = $data->{"regexp"}) {
398 return sprintf '%s %s %s', $self->expression($cond->[0]),
399 $self->term('regexp'), $self->expression($cond->[1]);
400 }
401 }
402  
403 method delete(HashRef $data) {
404 my $sql = [];
405  
406 # delete
407 push @$sql, $self->term(qw(delete from));
408  
409 # from
410 push @$sql, $self->table($data->{from});
411  
412 # where
413 if (my $where = $data->{where}) {
414 push @$sql, $self->term('where'),
415 join(sprintf(' %s ', $self->term('and')), @{$self->criteria($where)});
416 }
417  
418 # returning (postgres)
419 if (my $returning = $data->{returning}) {
420 push @$sql, $self->term('returning'),
421 sprintf('(%s)', join(', ', map $self->expression($_), @$returning));
422 }
423  
424 # sql statement
425 my $result = join ' ', @$sql;
426  
427 return $self->operation($result);
428 }
429  
430 method database_create(HashRef $data) {
431 my $sql = [];
432  
433 # create database
434 push @$sql, $self->term(qw(create database)),
435 ($data->{safe} ? $self->term(qw(if not exists)) : ()),
436 $self->name($data->{name});
437  
438 # sql statement
439 my $result = join ' ', @$sql;
440  
441 return $self->operation($result);
442 }
443  
444 method database_drop(HashRef $data) {
445 my $sql = [];
446  
447 # drop database
448 push @$sql, $self->term(qw(drop database)),
449 ($data->{safe} ? $self->term(qw(if exists)) : ()),
450 $self->name($data->{name});
451  
452 # sql statement
453 my $result = join ' ', @$sql;
454  
455 return $self->operation($result);
456 }
457  
458 method execute() {
459 if ($self->validator and not $self->validate) {
460 die $self->validator->error;
461 }
462  
463 return $self->process;
464 }
465  
466 method expression(Any $data) {
467 if (!ref $data) {
468 return $self->value($data); # literal
469 }
470  
471 if (UNIVERSAL::isa($data, 'SCALAR')) {
472 if ($$data eq '1') {
473 return $self->term('true');
474 }
475 if ($$data eq '0') {
476 return $self->term('false');
477 }
478 }
479  
480 if (my $expr = $data->{"as"}) {
481 my ($alias, $other) = @$expr;
482 return sprintf '%s %s %s',
483 $self->expression($other),
484 $self->term('as'),
485 $alias;
486 }
487  
488 if (my $expr = $data->{"binary"}) {
489 if ($expr->{plus}) {
490 return sprintf '(%s + %s)',
491 map $self->expression($_), @{$expr->{plus}};
492 }
493 if ($expr->{minus}) {
494 return sprintf '(%s - %s)',
495 map $self->expression($_), @{$expr->{minus}};
496 }
497 if ($expr->{multiply}) {
498 return sprintf '(%s * %s)',
499 map $self->expression($_), @{$expr->{multiply}};
500 }
501 if ($expr->{divide}) {
502 return sprintf '(%s / %s)',
503 map $self->expression($_), @{$expr->{divide}};
504 }
505 if ($expr->{modulo}) {
506 return sprintf '(%s % %s)',
507 map $self->expression($_), @{$expr->{modulo}};
508 }
509 }
510  
511 if ($data->{"binding"}) {
512 return $self->binding($data->{"binding"});
513 }
514  
515 if (my $expr = $data->{"case"}) {
516 return sprintf(
517 '%s %s %s %s %s',
518 $self->term('case'),
519 join(
520 ' ',
521 map {
522 sprintf '%s %s %s %s', $self->term('when'),
523 ($self->expression($$_{cond}) || $self->criterion($$_{cond})),
524 $self->term('then'), $self->expression($$_{then});
525 } @{$expr->{"when"}}
526 ),
527 $self->term('else'),
528 $self->expression($expr->{"else"}),
529 $self->term('end')
530 );
531 }
532  
533 if ($data->{"cast"}) {
534 return sprintf(
535 '%s(%s)',
536 $self->term('cast'),
537 join(
538 sprintf(' %s ', $self->term('as')),
539 map $self->expression($_),
540 @{$data->{"cast"}}
541 )
542 );
543 }
544  
545 if ($data->{"column"}) {
546 return $self->name($data->{"alias"}, $data->{"column"});
547 }
548  
549 if ($data->{"function"}) {
550 my ($name, @args) = @{$data->{"function"}};
551 return sprintf('%s(%s)',
552 $name, join(', ', @args ? (map $self->expression($_), @args) : ''));
553 }
554  
555 if (my $expr = $data->{"subquery"}) {
556 $self->select($expr->{select});
557 my $operation = $self->operations->pop;
558 $self->{bindings} = $operation->bindings;
559 return sprintf('(%s)', $operation->statement);
560 }
561  
562 if (my $expr = $data->{"unary"}) {
563 if ($expr->{"plus"}) {
564 return sprintf '+%s', $self->expression($expr->{"plus"});
565 }
566 if ($expr->{"minus"}) {
567 return sprintf '-%s', $self->expression($expr->{"minus"});
568 }
569 }
570  
571 if (my $expr = $data->{"verbatim"}) {
572 my @verbatim = @{$data->{"verbatim"}};
573 return join(' ', $verbatim[0],
574 join(', ', map $self->expression($_), @verbatim[1..$#verbatim]));
575 }
576 }
577  
578 method join_option(Maybe[Str] $name) {
579 if (lc($name) eq "left-join") {
580 return $self->term(qw(left join));
581 }
582 elsif (lc($name) eq "right-join") {
583 return $self->term(qw(right join));
584 }
585 elsif (lc($name) eq "full-join") {
586 return $self->term(qw(full join));
587 }
588 elsif (lc($name) eq "inner-join") {
589 return $self->term(qw(inner join));
590 }
591 else {
592 return $self->term(qw(join));
593 }
594 }
595  
596 method name(Any @args) {
597  
598 return join '.', map { /\W/ ? $_ : $self->wrap($_) } grep {defined} @args;
599 }
600  
601 method operation(Str $statement) {
602 $self->operations->push(
603 my $operation = SQL::Engine::Operation->new(
604 bindings => delete $self->{bindings} || {},
605 statement => $statement,
606 )
607 );
608  
609 return $operation;
610 }
611  
612 method process(HashRef $schema = $self->schema) {
613 if ($schema->{"select"}) {
614 $self->select($schema->{"select"});
615  
616 return $self;
617 }
618  
619 if ($schema->{"insert"}) {
620 $self->insert($schema->{"insert"});
621  
622 return $self;
623 }
624  
625 if ($schema->{"update"}) {
626 $self->update($schema->{"update"});
627  
628 return $self;
629 }
630  
631 if ($schema->{"delete"}) {
632 $self->delete($schema->{"delete"});
633  
634 return $self;
635 }
636  
637 if ($schema->{"column-change"}) {
638 $self->column_change($schema->{"column-change"});
639  
640 return $self;
641 }
642  
643 if ($schema->{"column-create"}) {
644 $self->column_create($schema->{"column-create"});
645  
646 return $self;
647 }
648  
649 if ($schema->{"column-drop"}) {
650 $self->column_drop($schema->{"column-drop"});
651  
652 return $self;
653 }
654  
655 if ($schema->{"column-rename"}) {
656 $self->column_rename($schema->{"column-rename"});
657  
658 return $self;
659 }
660  
661 if ($schema->{"constraint-create"}) {
662 $self->constraint_create($schema->{"constraint-create"});
663  
664 return $self;
665 }
666  
667 if ($schema->{"constraint-drop"}) {
668 $self->constraint_drop($schema->{"constraint-drop"});
669  
670 return $self;
671 }
672  
673 if ($schema->{"database-create"}) {
674 $self->database_create($schema->{"database-create"});
675  
676 return $self;
677 }
678  
679 if ($schema->{"database-drop"}) {
680 $self->database_drop($schema->{"database-drop"});
681  
682 return $self;
683 }
684  
685 if ($schema->{"index-create"}) {
686 $self->index_create($schema->{"index-create"});
687  
688 return $self;
689 }
690  
691 if ($schema->{"index-drop"}) {
692 $self->index_drop($schema->{"index-drop"});
693  
694 return $self;
695 }
696  
697 if ($schema->{"schema-create"}) {
698 $self->schema_create($schema->{"schema-create"});
699  
700 return $self;
701 }
702  
703 if ($schema->{"schema-drop"}) {
704 $self->schema_drop($schema->{"schema-drop"});
705  
706 return $self;
707 }
708  
709 if ($schema->{"schema-rename"}) {
710 $self->schema_rename($schema->{"schema-rename"});
711  
712 return $self;
713 }
714  
715 if ($schema->{"table-create"}) {
716 $self->table_create($schema->{"table-create"});
717  
718 return $self;
719 }
720  
721 if ($schema->{"table-drop"}) {
722 $self->table_drop($schema->{"table-drop"});
723  
724 return $self;
725 }
726  
727 if ($schema->{"transaction"}) {
728 $self->transaction($schema->{"transaction"});
729  
730 return $self;
731 }
732  
733 if ($schema->{"table-rename"}) {
734 $self->table_rename($schema->{"table-rename"});
735  
736 return $self;
737 }
738  
739 if ($schema->{"view-create"}) {
740 $self->view_create($schema->{"view-create"});
741  
742 return $self;
743 }
744  
745 if ($schema->{"view-drop"}) {
746 $self->view_drop($schema->{"view-drop"});
747  
748 return $self;
749 }
750  
751 return $self;
752 }
753  
754 method index_create(HashRef $data) {
755 my $sql = [];
756  
757 # create
758 push @$sql, $self->term('create');
759  
760 # unique
761 push @$sql, $self->term('unique') if $data->{unique};
762  
763 # index
764 push @$sql, $self->term('index');
765  
766 # safe
767 push @$sql, $self->term(qw(if not exists)) if $data->{safe};
768  
769 # index name
770 push @$sql, $self->wrap($self->index_name($data));
771  
772 # on table
773 push @$sql, $self->term('on'), $self->table($data->{for});
774  
775 # columns
776 push @$sql, sprintf('(%s)',
777 join(', ', map $self->name($$_{alias}, $$_{column}), @{$data->{columns}}));
778  
779 # sql statement
780 my $result = join ' ', @$sql;
781  
782 return $self->operation($result);
783 }
784  
785 method index_drop(HashRef $data) {
786 my $sql = [];
787  
788 # drop
789 push @$sql, $self->term('drop');
790  
791 # index
792 push @$sql, $self->term('index');
793  
794 # safe
795 push @$sql, $self->term(qw(if exists)) if $data->{safe};
796  
797 # index name
798 push @$sql, $self->wrap($self->index_name($data));
799  
800 # sql statement
801 my $result = join ' ', @$sql;
802  
803 return $self->operation($result);
804 }
805  
806 method index_name(HashRef $data) {
807 return $data->{name} || join('_',
808 ($data->{unique} ? 'unique' : 'index'),
809 grep {defined} $data->{for}{schema}, $data->{for}{table},
810 map $$_{column}, @{$data->{columns}}
811 );
812 }
813  
814 method insert(HashRef $data) {
815 my $sql = [];
816  
817 # insert
818 push @$sql, $self->term('insert');
819  
820 # into
821 push @$sql, $self->term('into'), $self->table($data->{into});
822  
823 # columns
824 if (my $columns = $data->{columns}) {
825 push @$sql,
826 sprintf('(%s)', join(', ', map $self->expression($_), @$columns));
827 }
828  
829 # values
830 if (my $values = $data->{values}) {
831 push @$sql,
832 sprintf('%s (%s)',
833 $self->term('values'),
834 join(', ', map $self->expression($$_{value}), @$values));
835 }
836  
837 # query
838 if (my $query = $data->{query}) {
839 $self->select($query->{select});
840 my $operation = $self->operations->pop;
841 $self->{bindings} = $operation->bindings;
842 push @$sql, $operation->statement;
843 }
844  
845 # default
846 if ($data->{default} && !$data->{values} && !$data->{values}) {
847 push @$sql, $self->term('default'), $self->term('values');
848 }
849  
850 # returning (postgres)
851 if (my $returning = $data->{returning}) {
852 push @$sql, $self->term('returning'),
853 sprintf('(%s)', join(', ', map $self->expression($_), @$returning));
854 }
855  
856 # sql statement
857 my $result = join ' ', @$sql;
858  
859 return $self->operation($result);
860 }
861  
862 method schema_create(HashRef $data) {
863 my $sql = [];
864  
865 # create schema
866 push @$sql, $self->term(qw(create schema)),
867 ($data->{safe} ? $self->term(qw(if not exists)) : ()),
868 $self->name($data->{name});
869  
870 # sql statement
871 my $result = join ' ', @$sql;
872  
873 return $self->operation($result);
874 }
875  
876 method schema_drop(HashRef $data) {
877 my $sql = [];
878  
879 # drop schema
880 push @$sql, $self->term(qw(drop schema)),
881 ($data->{safe} ? $self->term(qw(if exists)) : ()),
882 $self->name($data->{name});
883  
884 # sql statement
885 my $result = join ' ', @$sql;
886  
887 return $self->operation($result);
888 }
889  
890 method schema_rename(HashRef $data) {
891 my $sql = [];
892  
893 # rename schema
894 push @$sql, $self->term(qw(alter schema)),
895 $self->name($data->{name}{old}),
896 $self->term(qw(rename to)),
897 $self->name($data->{name}{new});
898  
899 # sql statement
900 my $result = join ' ', @$sql;
901  
902 return $self->operation($result);
903 }
904  
905 method select(HashRef $data) {
906 my $sql = [];
907  
908 # select
909 push @$sql, $self->term('select');
910  
911 # columns
912 if (my $columns = $data->{columns}) {
913 push @$sql, join(', ', map $self->expression($_), @$columns);
914 }
915  
916 # into (mssql)
917 if (my $into = $data->{into}) {
918 push @$sql, $self->term('into'), $self->name($into);
919 }
920  
921 # from
922 push @$sql, $self->term('from'),
923 ref($data->{from}) eq 'ARRAY'
924 ? join(', ', map $self->table($_), @{$data->{from}})
925 : $self->table($data->{from});
926  
927 # joins
928 if (my $joins = $data->{joins}) {
929 for my $join (@$joins) {
930 push @$sql, $self->join_option($join->{type}), $self->table($join->{with});
931 push @$sql, $self->term('on'),
932 join(
933 sprintf(' %s ', $self->term('and')),
934 @{$self->criteria($join->{having})}
935 );
936 }
937 }
938  
939 # where
940 if (my $where = $data->{where}) {
941 push @$sql, $self->term('where'),
942 join(sprintf(' %s ', $self->term('and')), @{$self->criteria($where)});
943 }
944  
945 # group-by
946 if (my $group_by = $data->{"group-by"}) {
947 push @$sql, $self->term(qw(group by));
948 push @$sql, join ', ', map $self->expression($_), @$group_by;
949  
950 # having
951 if (my $having = $data->{"having"}) {
952 push @$sql, $self->term('having'),
953 join(sprintf(' %s ', $self->term('and')), @{$self->criteria($having)});
954 }
955 }
956  
957 # order-by
958 if (my $orders = $data->{"order-by"}) {
959 my @orders;
960 push @$sql, $self->term(qw(order by));
961 for my $order (@$orders) {
962 if ($order->{sort}
963 && ($order->{sort} eq 'asc' || $order->{sort} eq 'ascending'))
964 {
965 push @orders, sprintf '%s ASC',
966 $self->name($order->{"alias"}, $order->{"column"});
967 }
968 elsif ($order->{sort}
969 && ($order->{sort} eq 'desc' || $order->{sort} eq 'descending'))
970 {
971 push @orders, sprintf '%s DESC',
972 $self->name($order->{"alias"}, $order->{"column"});
973 }
974 else {
975 push @orders, $self->name($order->{"alias"}, $order->{"column"});
976 }
977 }
978 push @$sql, join ', ', @orders;
979 }
980  
981 # rows
982 if (my $rows = $data->{rows}) {
983 if ($rows->{limit} && $rows->{offset}) {
984 push @$sql, sprintf '%s %d %s %d', $self->term('limit'), $rows->{limit},
985 $self->term('offset'), $rows->{offset};
986 }
987 if ($rows->{limit} && !$rows->{offset}) {
988 push @$sql, sprintf '%s %d', $self->term('limit'), $rows->{limit};
989 }
990 }
991  
992 # sql statement
993 my $result = join ' ', @$sql;
994  
995 return $self->operation($result);
996 }
997  
998 method table(HashRef $data) {
999 my $name;
1000  
1001 my $table = $data->{table};
1002 my $schema = $data->{schema};
1003 my $alias = $data->{alias};
1004  
1005 $name = $self->name($schema, $table);
1006 $name = join ' ', $name, $self->wrap($alias) if $alias;
1007  
1008 return $name;
1009 }
1010  
1011 method table_create(HashRef $data) {
1012 my $sql = [];
1013  
1014 # create
1015 push @$sql, $self->term('create');
1016  
1017 # temporary
1018 if ($data->{temp}) {
1019 push @$sql, $self->term('temporary');
1020 }
1021  
1022 # table
1023 push @$sql, $self->term('table'),
1024 ($data->{safe} ? $self->term(qw(if not exists)) : ()),
1025 $self->name($data->{name});
1026  
1027 # body
1028 my $body = [];
1029  
1030 # columns
1031 if (my $columns = $data->{columns}) {
1032 push @$body, map $self->column_specification($_), @$columns;
1033 }
1034  
1035 # constraints
1036 if (my $constraints = $data->{constraints}) {
1037 # unique
1038 for my $constraint (grep {$_->{unique}} @{$constraints}) {
1039 if (my $unique = $constraint->{unique}) {
1040 my $name = $self->index_name({
1041 for => $data->{for},
1042 name => $unique->{name},
1043 columns => [map +{column => $_}, @{$unique->{columns}}],
1044 unique => 1,
1045 });
1046 push @$body, join ' ', $self->term('constraint'), $name,
1047 $self->term('unique'), sprintf '(%s)', join ', ',
1048 map $self->name($_), @{$unique->{columns}};
1049 }
1050 }
1051 # foreign
1052 for my $constraint (grep {$_->{foreign}} @{$constraints}) {
1053 if (my $foreign = $constraint->{foreign}) {
1054 my $name = $self->constraint_name({
1055 source => {
1056 table => $data->{name},
1057 column => $foreign->{column}
1058 },
1059 target => $foreign->{reference},
1060 name => $foreign->{name}
1061 });
1062 push @$body, join ' ', $self->term('constraint'), $name,
1063 $self->term(qw(foreign key)),
1064 sprintf('(%s)', $self->name($foreign->{column})),
1065 $self->term(qw(references)),
1066 sprintf('%s (%s)',
1067 $self->table($foreign->{reference}),
1068 $self->name($foreign->{reference}{column})),
1069 (
1070 $foreign->{on}{delete}
1071 ? (
1072 $self->term(qw(on delete)),
1073 $self->constraint_option($foreign->{on}{delete})
1074 )
1075 : ()
1076 ),
1077 (
1078 $foreign->{on}{update}
1079 ? (
1080 $self->term(qw(on update)),
1081 $self->constraint_option($foreign->{on}{update})
1082 )
1083 : ()
1084 );
1085 }
1086 }
1087 }
1088  
1089 # definition
1090 if (@$body) {
1091 push @$sql, sprintf('(%s)', join ', ', @$body);
1092 }
1093  
1094 # query
1095 if (my $query = $data->{query}) {
1096 $self->select($query->{select});
1097 my $operation = $self->operations->pop;
1098 $self->{bindings} = $operation->bindings;
1099 push @$sql, $self->term('as');
1100 push @$sql, $operation->statement;
1101 }
1102  
1103 # sql statement
1104 my $result = join ' ', @$sql;
1105  
1106 return $self->operation($result);
1107 }
1108  
1109 method table_drop(HashRef $data) {
1110 my $sql = [];
1111  
1112 # drop table
1113 push @$sql, $self->term(qw(drop table)),
1114 ($data->{safe} ? $self->term(qw(if exists)) : ()),
1115 $self->name($data->{name});
1116  
1117 # with condition
1118 if (my $condition = $data->{condition}) {
1119 push @$sql, $self->term($data->{condition});
1120 }
1121  
1122 # sql statement
1123 my $result = join ' ', @$sql;
1124  
1125 return $self->operation($result);
1126 }
1127  
1128 method table_rename(HashRef $data) {
1129 my $sql = [];
1130  
1131 # rename table
1132 push @$sql, $self->term(qw(alter table)),
1133 $self->name($data->{name}{old}),
1134 $self->term(qw(rename to)),
1135 $self->name($data->{name}{new});
1136  
1137 # sql statement
1138 my $result = join ' ', @$sql;
1139  
1140 return $self->operation($result);
1141 }
1142  
1143 method term(Str @args) {
1144 my $method = join '_', 'term', map lc, map {split /\s/} @args;
1145  
1146 if ($self->can($method)) {
1147 return $self->$method;
1148 }
1149 else {
1150 return join ' ', map uc, @args;
1151 }
1152 }
1153  
1154 method transaction(HashRef $data) {
1155 $DB::single=1;
1156 $self->operation($self->term('begin', 'transaction'));
1157 $self->process($_) for @{$data->{queries}};
1158 $self->operation($self->term('commit'));
1159  
1160 return $self;
1161 }
1162  
1163 method type(HashRef $data) {
1164 if ($data->{type} eq 'binary') {
1165 return $self->type_binary($data);
1166 }
1167  
1168 if ($data->{type} eq 'boolean') {
1169 return $self->type_boolean($data);
1170 }
1171  
1172 if ($data->{type} eq 'char') {
1173 return $self->type_char($data);
1174 }
1175  
1176 if ($data->{type} eq 'date') {
1177 return $self->type_date($data);
1178 }
1179  
1180 if ($data->{type} eq 'datetime') {
1181 return $self->type_datetime($data);
1182 }
1183  
1184 if ($data->{type} eq 'datetime-wtz') {
1185 return $self->type_datetime_wtz($data);
1186 }
1187  
1188 if ($data->{type} eq 'decimal') {
1189 return $self->type_decimal($data);
1190 }
1191  
1192 if ($data->{type} eq 'double') {
1193 return $self->type_double($data);
1194 }
1195  
1196 if ($data->{type} eq 'enum') {
1197 return $self->type_enum($data);
1198 }
1199  
1200 if ($data->{type} eq 'float') {
1201 return $self->type_float($data);
1202 }
1203  
1204 if ($data->{type} eq 'integer') {
1205 return $self->type_integer($data);
1206 }
1207  
1208 if ($data->{type} eq 'integer-big') {
1209 return $self->type_integer_big($data);
1210 }
1211  
1212 if ($data->{type} eq 'integer-big-unsigned') {
1213 return $self->type_integer_big_unsigned($data);
1214 }
1215  
1216 if ($data->{type} eq 'integer-medium') {
1217 return $self->type_integer_medium($data);
1218 }
1219  
1220 if ($data->{type} eq 'integer-medium-unsigned') {
1221 return $self->type_integer_medium_unsigned($data);
1222 }
1223  
1224 if ($data->{type} eq 'integer-small') {
1225 return $self->type_integer_small($data);
1226 }
1227  
1228 if ($data->{type} eq 'integer-small-unsigned') {
1229 return $self->type_integer_small_unsigned($data);
1230 }
1231  
1232 if ($data->{type} eq 'integer-tiny') {
1233 return $self->type_integer_tiny($data);
1234 }
1235  
1236 if ($data->{type} eq 'integer-tiny-unsigned') {
1237 return $self->type_integer_tiny_unsigned($data);
1238 }
1239  
1240 if ($data->{type} eq 'integer-unsigned') {
1241 return $self->type_integer_unsigned($data);
1242 }
1243  
1244 if ($data->{type} eq 'json') {
1245 return $self->type_json($data);
1246 }
1247  
1248 if ($data->{type} eq 'number') {
1249 return $self->type_number($data);
1250 }
1251  
1252 if ($data->{type} eq 'string') {
1253 return $self->type_string($data);
1254 }
1255  
1256 if ($data->{type} eq 'text') {
1257 return $self->type_text($data);
1258 }
1259  
1260 if ($data->{type} eq 'text-long') {
1261 return $self->type_text_long($data);
1262 }
1263  
1264 if ($data->{type} eq 'text-medium') {
1265 return $self->type_text_medium($data);
1266 }
1267  
1268 if ($data->{type} eq 'time') {
1269 return $self->type_time($data);
1270 }
1271  
1272 if ($data->{type} eq 'time-wtz') {
1273 return $self->type_time_wtz($data);
1274 }
1275  
1276 if ($data->{type} eq 'timestamp') {
1277 return $self->type_timestamp($data);
1278 }
1279  
1280 if ($data->{type} eq 'timestamp-wtz') {
1281 return $self->type_timestamp_wtz($data);
1282 }
1283  
1284 if ($data->{type} eq 'uuid') {
1285 return $self->type_uuid($data);
1286 }
1287  
1288 return $data->{type};
1289 }
1290  
1291 method type_binary(HashRef $data) {
1292  
1293 return 'blob';
1294 }
1295  
1296 method type_boolean(HashRef $data) {
1297  
1298 return 'tinyint(1)';
1299 }
1300  
1301 method type_char(HashRef $data) {
1302  
1303 return 'varchar';
1304 }
1305  
1306 method type_date(HashRef $data) {
1307  
1308 return 'date';
1309 }
1310  
1311 method type_datetime(HashRef $data) {
1312  
1313 return 'datetime';
1314 }
1315  
1316 method type_datetime_wtz(HashRef $data) {
1317  
1318 return 'datetime';
1319 }
1320  
1321 method type_decimal(HashRef $data) {
1322  
1323 return 'numeric';
1324 }
1325  
1326 method type_double(HashRef $data) {
1327  
1328 return 'float';
1329 }
1330  
1331 method type_enum(HashRef $data) {
1332  
1333 return 'varchar';
1334 }
1335  
1336 method type_float(HashRef $data) {
1337  
1338 return 'float';
1339 }
1340  
1341 method type_integer(HashRef $data) {
1342  
1343 return 'integer';
1344 }
1345  
1346 method type_integer_big(HashRef $data) {
1347  
1348 return 'integer';
1349 }
1350  
1351 method type_integer_big_unsigned(HashRef $data) {
1352  
1353 return 'integer';
1354 }
1355  
1356 method type_integer_medium(HashRef $data) {
1357  
1358 return 'integer';
1359 }
1360  
1361 method type_integer_medium_unsigned(HashRef $data) {
1362  
1363 return 'integer';
1364 }
1365  
1366 method type_integer_small(HashRef $data) {
1367  
1368 return 'integer';
1369 }
1370  
1371 method type_integer_small_unsigned(HashRef $data) {
1372  
1373 return 'integer';
1374 }
1375  
1376 method type_integer_tiny(HashRef $data) {
1377  
1378 return 'integer';
1379 }
1380  
1381 method type_integer_tiny_unsigned(HashRef $data) {
1382  
1383 return 'integer';
1384 }
1385  
1386 method type_integer_unsigned(HashRef $data) {
1387  
1388 return 'integer';
1389 }
1390  
1391 method type_json(HashRef $data) {
1392  
1393 return 'text';
1394 }
1395  
1396 method type_number(HashRef $data) {
1397  
1398 return 'integer';
1399 }
1400  
1401 method type_string(HashRef $data) {
1402  
1403 return 'varchar';
1404 }
1405  
1406 method type_text(HashRef $data) {
1407  
1408 return 'text';
1409 }
1410  
1411 method type_text_long(HashRef $data) {
1412  
1413 return 'text';
1414 }
1415  
1416 method type_text_medium(HashRef $data) {
1417  
1418 return 'text';
1419 }
1420  
1421 method type_time(HashRef $data) {
1422  
1423 return 'time';
1424 }
1425  
1426 method type_time_wtz(HashRef $data) {
1427  
1428 return 'time';
1429 }
1430  
1431 method type_timestamp(HashRef $data) {
1432  
1433 return 'datetime';
1434 }
1435  
1436 method type_timestamp_wtz(HashRef $data) {
1437  
1438 return 'datetime';
1439 }
1440  
1441 method type_uuid(HashRef $data) {
1442  
1443 return 'varchar';
1444 }
1445  
1446 method update(HashRef $data) {
1447 my $sql = [];
1448  
1449 # update
1450 push @$sql, $self->term('update');
1451  
1452 # for
1453 push @$sql, $self->table($data->{for});
1454  
1455 # columns
1456 if (my $columns = $data->{columns}) {
1457 push @$sql, $self->term('set');
1458 push @$sql, join(
1459 ', ',
1460 map {
1461 sprintf('%s = %s',
1462 $self->name($$_{alias}, $$_{column}),
1463 $self->expression($$_{value}))
1464 } @$columns
1465 );
1466 }
1467  
1468 # where
1469 if (my $where = $data->{where}) {
1470 push @$sql, $self->term('where'),
1471 join(sprintf(' %s ', $self->term('and')), @{$self->criteria($where)});
1472 }
1473  
1474 # returning (postgres)
1475 if (my $returning = $data->{returning}) {
1476 push @$sql, $self->term('returning'),
1477 sprintf('(%s)', join(', ', map $self->expression($_), @$returning));
1478 }
1479  
1480 # sql statement
1481 my $result = join ' ', @$sql;
1482  
1483 return $self->operation($result);
1484 }
1485  
1486 method validate() {
1487  
1488 return $self->validator->validate($self->schema);
1489 }
1490  
1491 method value(Any $value) {
1492  
1493 return !defined $value ? $self->term('null') : (
1494 Scalar::Util::looks_like_number($value) ? $value : do {
1495 $value =~ s/\'/\\'/g;
1496 "'$value'"
1497 }
1498 );
1499 }
1500  
1501 method view_create(HashRef $data) {
1502 my $sql = [];
1503  
1504 # create
1505 push @$sql, $self->term('create');
1506  
1507 # temporary
1508 if ($data->{temp}) {
1509 push @$sql, $self->term('temporary');
1510 }
1511  
1512 # view
1513 push @$sql, $self->term('view');
1514  
1515 # safe
1516 if ($data->{safe}) {
1517 push @$sql, $self->term(qw(if not exists));
1518 }
1519  
1520 # view name
1521 push @$sql, $self->name($data->{name});
1522  
1523 # columns
1524 if (my $columns = $data->{columns}) {
1525 push @$sql,
1526 sprintf('(%s)', join(', ', map $self->expression($_), @$columns));
1527 }
1528  
1529 # query
1530 if (my $query = $data->{query}) {
1531 $self->select($query->{select});
1532 my $operation = $self->operations->pop;
1533 $self->{bindings} = $operation->bindings;
1534 push @$sql, $self->term('as');
1535 push @$sql, $operation->statement;
1536 }
1537  
1538 # sql statement
1539 my $result = join ' ', @$sql;
1540  
1541 return $self->operation($result);
1542 }
1543  
1544 method view_drop(HashRef $data) {
1545 my $sql = [];
1546  
1547 # drop view
1548 push @$sql, $self->term(qw(drop view)),
1549 ($data->{safe} ? $self->term(qw(if exists)) : ()),
1550 $self->name($data->{name});
1551  
1552 # sql statement
1553 my $result = join ' ', @$sql;
1554  
1555 return $self->operation($result);
1556 }
1557  
1558 method wrap(Str $name) {
1559  
1560 return qq("$name");
1561 }
1562  
1563 1;
1564  
1565 =encoding utf8
1566  
1567 =head1 NAME
1568  
1569 SQL::Engine::Grammar - Standard Grammar
1570  
1571 =cut
1572  
1573 =head1 ABSTRACT
1574  
1575 SQL::Engine Standard Grammar
1576  
1577 =cut
1578  
1579 =head1 SYNOPSIS
1580  
1581 use SQL::Engine::Grammar;
1582  
1583 my $grammar = SQL::Engine::Grammar->new(
1584 schema => {
1585 select => {
1586 from => {
1587 table => 'users'
1588 },
1589 columns => [
1590 {
1591 column => '*'
1592 }
1593 ]
1594 }
1595 }
1596 );
1597  
1598 # $grammar->execute;
1599  
1600 =cut
1601  
1602 =head1 DESCRIPTION
1603  
1604 This package provides methods for converting
1605 L<json-sql|https://github.com/iamalnewkirk/json-sql> data structures into
1606 SQL statements.
1607  
1608 =cut
1609  
1610 =head1 LIBRARIES
1611  
1612 This package uses type constraints from:
1613  
1614 L<Types::Standard>
1615  
1616 =cut
1617  
1618 =head1 ATTRIBUTES
1619  
1620 This package has the following attributes:
1621  
1622 =cut
1623  
1624 =head2 operations
1625  
1626 operations(InstanceOf["SQL::Engine::Collection"])
1627  
1628 This attribute is read-only, accepts C<(InstanceOf["SQL::Engine::Collection"])> values, and is optional.
1629  
1630 =cut
1631  
1632 =head2 schema
1633  
1634 schema(HashRef)
1635  
1636 This attribute is read-only, accepts C<(HashRef)> values, and is required.
1637  
1638 =cut
1639  
1640 =head2 validator
1641  
1642 validator(Maybe[InstanceOf["SQL::Validator"]])
1643  
1644 This attribute is read-only, accepts C<(Maybe[InstanceOf["SQL::Validator"]])> values, and is optional.
1645  
1646 =cut
1647  
1648 =head1 METHODS
1649  
1650 This package implements the following methods:
1651  
1652 =cut
1653  
1654 =head2 binding
1655  
1656 binding(Str $name) : Str
1657  
1658 The binding method registers a SQL statement binding (or placeholder).
1659  
1660 =over 4
1661  
1662 =item binding example #1
1663  
1664 # given: synopsis
1665  
1666 $grammar->binding('user_id');
1667 $grammar->binding('user_id');
1668 $grammar->binding('user_id');
1669 $grammar->binding('user_id');
1670 $grammar->binding('user_id');
1671  
1672 =back
1673  
1674 =cut
1675  
1676 =head2 column_change
1677  
1678 column_change(HashRef $data) : Object
1679  
1680 The column_change method generates SQL statements to change a column
1681 definition.
1682  
1683 =over 4
1684  
1685 =item column_change example #1
1686  
1687 my $grammar = SQL::Engine::Grammar->new({
1688 schema => {
1689 'column-change' => {
1690 for => {
1691 table => 'users'
1692 },
1693 column => {
1694 name => 'accessed',
1695 type => 'datetime',
1696 nullable => 1
1697 }
1698 }
1699 }
1700 });
1701  
1702 $grammar->column_change($grammar->schema->{'column-change'});
1703  
1704 =back
1705  
1706 =cut
1707  
1708 =head2 column_create
1709  
1710 column_create(HashRef $data) : Object
1711  
1712 The column_create method generates SQL statements to add a new table column.
1713  
1714 =over 4
1715  
1716 =item column_create example #1
1717  
1718 # given: synopsis
1719  
1720 $grammar->column_create({
1721 for => {
1722 table => 'users'
1723 },
1724 column => {
1725 name => 'accessed',
1726 type => 'datetime'
1727 }
1728 });
1729  
1730 =back
1731  
1732 =cut
1733  
1734 =head2 column_definition
1735  
1736 column_definition(HashRef $data) : HashRef
1737  
1738 The column_definition method column definition SQL statement fragments.
1739  
1740 =over 4
1741  
1742 =item column_definition example #1
1743  
1744 # given: synopsis
1745  
1746 my $column_definition = $grammar->column_definition({
1747 name => 'id',
1748 type => 'number',
1749 primary => 1
1750 });
1751  
1752 =back
1753  
1754 =cut
1755  
1756 =head2 column_drop
1757  
1758 column_drop(HashRef $data) : Object
1759  
1760 The column_drop method generates SQL statements to remove a table column.
1761  
1762 =over 4
1763  
1764 =item column_drop example #1
1765  
1766 # given: synopsis
1767  
1768 $grammar->column_drop({
1769 table => 'users',
1770 column => 'accessed'
1771 });
1772  
1773 =back
1774  
1775 =cut
1776  
1777 =head2 column_rename
1778  
1779 column_rename(HashRef $data) : Object
1780  
1781 The column_rename method generates SQL statements to rename a table column.
1782  
1783 =over 4
1784  
1785 =item column_rename example #1
1786  
1787 # given: synopsis
1788  
1789 $grammar->column_rename({
1790 for => {
1791 table => 'users'
1792 },
1793 name => {
1794 old => 'accessed',
1795 new => 'accessed_at'
1796 }
1797 });
1798  
1799 =back
1800  
1801 =cut
1802  
1803 =head2 column_specification
1804  
1805 column_specification(HashRef $data) : Str
1806  
1807 The column_specification method a column definition SQL statment partial.
1808  
1809 =over 4
1810  
1811 =item column_specification example #1
1812  
1813 # given: synopsis
1814  
1815 my $column_specification = $grammar->column_specification({
1816 name => 'id',
1817 type => 'number',
1818 primary => 1
1819 });
1820  
1821 =back
1822  
1823 =cut
1824  
1825 =head2 constraint_create
1826  
1827 constraint_create(HashRef $data) : Object
1828  
1829 The constraint_create method generates SQL statements to create a table
1830 constraint.
1831  
1832 =over 4
1833  
1834 =item constraint_create example #1
1835  
1836 # given: synopsis
1837  
1838 $grammar->constraint_create({
1839 source => {
1840 table => 'users',
1841 column => 'profile_id'
1842 },
1843 target => {
1844 table => 'profiles',
1845 column => 'id'
1846 }
1847 });
1848  
1849 =back
1850  
1851 =cut
1852  
1853 =head2 constraint_drop
1854  
1855 constraint_drop(HashRef $data) : Object
1856  
1857 The constraint_drop method generates SQL statements to remove a table
1858 constraint.
1859  
1860 =over 4
1861  
1862 =item constraint_drop example #1
1863  
1864 # given: synopsis
1865  
1866 $grammar->constraint_drop({
1867 source => {
1868 table => 'users',
1869 column => 'profile_id'
1870 },
1871 target => {
1872 table => 'profiles',
1873 column => 'id'
1874 }
1875 });
1876  
1877 =back
1878  
1879 =cut
1880  
1881 =head2 constraint_name
1882  
1883 constraint_name(HashRef $data) : Str
1884  
1885 The constraint_name method returns the generated constraint name.
1886  
1887 =over 4
1888  
1889 =item constraint_name example #1
1890  
1891 # given: synopsis
1892  
1893 my $constraint_name = $grammar->constraint_name({
1894 source => {
1895 table => 'users',
1896 column => 'profile_id'
1897 },
1898 target => {
1899 table => 'profiles',
1900 column => 'id'
1901 }
1902 });
1903  
1904 =back
1905  
1906 =cut
1907  
1908 =head2 constraint_option
1909  
1910 constraint_option(Str $name) : Str
1911  
1912 The constraint_option method returns a SQL expression for the constraint option
1913 provided.
1914  
1915 =over 4
1916  
1917 =item constraint_option example #1
1918  
1919 # given: synopsis
1920  
1921 $grammar->constraint_option('no-action');
1922  
1923 =back
1924  
1925 =cut
1926  
1927 =head2 criteria
1928  
1929 criteria(ArrayRef $data) : ArrayRef[Str]
1930  
1931 The criteria method returns a list of SQL expressions.
1932  
1933 =over 4
1934  
1935 =item criteria example #1
1936  
1937 # given: synopsis
1938  
1939 my $criteria = $grammar->criteria([
1940 {
1941 eq => [{ column => 'id' }, 123]
1942 },
1943 {
1944 'not-null' => { column => 'deleted' }
1945 }
1946 ]);
1947  
1948 =back
1949  
1950 =cut
1951  
1952 =head2 criterion
1953  
1954 criterion(HashRef $data) : Str
1955  
1956 The criterion method returns a SQL expression.
1957  
1958 =over 4
1959  
1960 =item criterion example #1
1961  
1962 # given: synopsis
1963  
1964 my $criterion = $grammar->criterion({
1965 in => [{ column => 'theme' }, 'light', 'dark']
1966 });
1967  
1968 =back
1969  
1970 =cut
1971  
1972 =head2 database_create
1973  
1974 database_create(HashRef $data) : Object
1975  
1976 The database_create method generates SQL statements to create a database.
1977  
1978 =over 4
1979  
1980 =item database_create example #1
1981  
1982 # given: synopsis
1983  
1984 $grammar->database_create({
1985 name => 'todoapp'
1986 });
1987  
1988 =back
1989  
1990 =cut
1991  
1992 =head2 database_drop
1993  
1994 database_drop(HashRef $data) : Object
1995  
1996 The database_drop method generates SQL statements to remove a database.
1997  
1998 =over 4
1999  
2000 =item database_drop example #1
2001  
2002 # given: synopsis
2003  
2004 $grammar->database_drop({
2005 name => 'todoapp'
2006 });
2007  
2008 =back
2009  
2010 =cut
2011  
2012 =head2 delete
2013  
2014 delete(HashRef $data) : Object
2015  
2016 The delete method generates SQL statements to delete table rows.
2017  
2018 =over 4
2019  
2020 =item delete example #1
2021  
2022 # given: synopsis
2023  
2024 $grammar->delete({
2025 from => {
2026 table => 'tasklists'
2027 }
2028 });
2029  
2030 =back
2031  
2032 =cut
2033  
2034 =head2 execute
2035  
2036 execute() : Object
2037  
2038 The execute method validates and processes the object instruction.
2039  
2040 =over 4
2041  
2042 =item execute example #1
2043  
2044 # given: synopsis
2045  
2046 $grammar->operations->clear;
2047  
2048 $grammar->execute;
2049  
2050 =back
2051  
2052 =cut
2053  
2054 =head2 expression
2055  
2056 expression(Any $data) : Any
2057  
2058 The expression method returns a SQL expression representing the data provided.
2059  
2060 =over 4
2061  
2062 =item expression example #1
2063  
2064 # given: synopsis
2065  
2066 $grammar->expression(undef);
2067  
2068 # NULL
2069  
2070 =back
2071  
2072 =cut
2073  
2074 =head2 index_create
2075  
2076 index_create(HashRef $data) : Object
2077  
2078 The index_create method generates SQL statements to create a table index.
2079  
2080 =over 4
2081  
2082 =item index_create example #1
2083  
2084 # given: synopsis
2085  
2086 $grammar->index_create({
2087 for => {
2088 table => 'users'
2089 },
2090 columns => [
2091 {
2092 column => 'name'
2093 }
2094 ]
2095 });
2096  
2097 =back
2098  
2099 =cut
2100  
2101 =head2 index_drop
2102  
2103 index_drop(HashRef $data) : Object
2104  
2105 The index_drop method generates SQL statements to remove a table index.
2106  
2107 =over 4
2108  
2109 =item index_drop example #1
2110  
2111 # given: synopsis
2112  
2113 $grammar->index_drop({
2114 for => {
2115 table => 'users'
2116 },
2117 columns => [
2118 {
2119 column => 'name'
2120 }
2121 ]
2122 });
2123  
2124 =back
2125  
2126 =cut
2127  
2128 =head2 index_name
2129  
2130 index_name(HashRef $data) : Str
2131  
2132 The index_name method returns the generated index name.
2133  
2134 =over 4
2135  
2136 =item index_name example #1
2137  
2138 # given: synopsis
2139  
2140 my $index_name = $grammar->index_name({
2141 for => {
2142 table => 'users'
2143 },
2144 columns => [
2145 {
2146 column => 'email'
2147 }
2148 ],
2149 unique => 1
2150 });
2151  
2152 =back
2153  
2154 =cut
2155  
2156 =head2 insert
2157  
2158 insert(HashRef $data) : Object
2159  
2160 The insert method generates SQL statements to insert table rows.
2161  
2162 =over 4
2163  
2164 =item insert example #1
2165  
2166 # given: synopsis
2167  
2168 $grammar->insert({
2169 into => {
2170 table => 'users'
2171 },
2172 values => [
2173 {
2174 value => undef
2175 },
2176 {
2177 value => 'Rob Zombie'
2178 },
2179 {
2180 value => {
2181 function => ['now']
2182 }
2183 },
2184 {
2185 value => {
2186 function => ['now']
2187 }
2188 },
2189 {
2190 value => {
2191 function => ['now']
2192 }
2193 }
2194 ]
2195 });
2196  
2197 =back
2198  
2199 =cut
2200  
2201 =head2 name
2202  
2203 name(Any @args) : Str
2204  
2205 The name method returns a qualified quoted object name.
2206  
2207 =over 4
2208  
2209 =item name example #1
2210  
2211 # given: synopsis
2212  
2213 my $name = $grammar->name(undef, 'public', 'users');
2214  
2215 # "public"."users"
2216  
2217 =back
2218  
2219 =cut
2220  
2221 =head2 operation
2222  
2223 operation(Str $statement) : InstanceOf["SQL::Engine::Operation"]
2224  
2225 The operation method creates and appends an operation to the I<"operations">
2226 collection.
2227  
2228 =over 4
2229  
2230 =item operation example #1
2231  
2232 # given: synopsis
2233  
2234 $grammar->operation('SELECT TRUE');
2235  
2236 =back
2237  
2238 =cut
2239  
2240 =head2 process
2241  
2242 process(Mayb[HashRef] $schema) : Object
2243  
2244 The process method processes the object instructions.
2245  
2246 =over 4
2247  
2248 =item process example #1
2249  
2250 # given: synopsis
2251  
2252 $grammar->process;
2253  
2254 =back
2255  
2256 =cut
2257  
2258 =head2 schema_create
2259  
2260 schema_create(HashRef $data) : Object
2261  
2262 The schema_create method generates SQL statements to create a schema.
2263  
2264 =over 4
2265  
2266 =item schema_create example #1
2267  
2268 # given: synopsis
2269  
2270 $grammar->schema_create({
2271 name => 'private',
2272 });
2273  
2274 =back
2275  
2276 =cut
2277  
2278 =head2 schema_drop
2279  
2280 schema_drop(HashRef $data) : Object
2281  
2282 The schema_drop method generates SQL statements to remove a schema.
2283  
2284 =over 4
2285  
2286 =item schema_drop example #1
2287  
2288 # given: synopsis
2289  
2290 $grammar->schema_drop({
2291 name => 'private',
2292 });
2293  
2294 =back
2295  
2296 =cut
2297  
2298 =head2 schema_rename
2299  
2300 schema_rename(HashRef $data) : Object
2301  
2302 The schema_rename method generates SQL statements to rename a schema.
2303  
2304 =over 4
2305  
2306 =item schema_rename example #1
2307  
2308 # given: synopsis
2309  
2310 $grammar->schema_rename({
2311 name => {
2312 old => 'private',
2313 new => 'restricted'
2314 }
2315 });
2316  
2317 =back
2318  
2319 =cut
2320  
2321 =head2 select
2322  
2323 select(HashRef $data) : Object
2324  
2325 The select method generates SQL statements to select table rows.
2326  
2327 =over 4
2328  
2329 =item select example #1
2330  
2331 # given: synopsis
2332  
2333 $grammar->select({
2334 from => {
2335 table => 'people'
2336 },
2337 columns => [
2338 { column => 'name' }
2339 ]
2340 });
2341  
2342 =back
2343  
2344 =cut
2345  
2346 =head2 table
2347  
2348 table(HashRef $data) : Str
2349  
2350 The table method returns a qualified quoted table name.
2351  
2352 =over 4
2353  
2354 =item table example #1
2355  
2356 # given: synopsis
2357  
2358 my $table = $grammar->table({
2359 schema => 'public',
2360 table => 'users',
2361 alias => 'u'
2362 });
2363  
2364 =back
2365  
2366 =cut
2367  
2368 =head2 table_create
2369  
2370 table_create(HashRef $data) : Object
2371  
2372 The table_create method generates SQL statements to create a table.
2373  
2374 =over 4
2375  
2376 =item table_create example #1
2377  
2378 # given: synopsis
2379  
2380 $grammar->table_create({
2381 name => 'users',
2382 columns => [
2383 {
2384 name => 'id',
2385 type => 'integer',
2386 primary => 1
2387 }
2388 ]
2389 });
2390  
2391 =back
2392  
2393 =cut
2394  
2395 =head2 table_drop
2396  
2397 table_drop(HashRef $data) : Object
2398  
2399 The table_drop method generates SQL statements to remove a table.
2400  
2401 =over 4
2402  
2403 =item table_drop example #1
2404  
2405 # given: synopsis
2406  
2407 $grammar->table_drop({
2408 name => 'people'
2409 });
2410  
2411 =back
2412  
2413 =cut
2414  
2415 =head2 table_rename
2416  
2417 table_rename(HashRef $data) : Object
2418  
2419 The table_rename method generates SQL statements to rename a table.
2420  
2421 =over 4
2422  
2423 =item table_rename example #1
2424  
2425 # given: synopsis
2426  
2427 $grammar->table_rename({
2428 name => {
2429 old => 'peoples',
2430 new => 'people'
2431 }
2432 });
2433  
2434 =back
2435  
2436 =cut
2437  
2438 =head2 term
2439  
2440 term(Str @args) : Str
2441  
2442 The term method returns a SQL keyword.
2443  
2444 =over 4
2445  
2446 =item term example #1
2447  
2448 # given: synopsis
2449  
2450 $grammar->term('end');
2451  
2452 =back
2453  
2454 =cut
2455  
2456 =head2 transaction
2457  
2458 transaction(HashRef $data) : Object
2459  
2460 The transaction method generates SQL statements to commit an atomic database
2461 transaction.
2462  
2463 =over 4
2464  
2465 =item transaction example #1
2466  
2467 my $grammar = SQL::Engine::Grammar->new({
2468 schema => {
2469 'transaction' => {
2470 queries => [
2471 {
2472 'table-create' => {
2473 name => 'users',
2474 columns => [
2475 {
2476 name => 'id',
2477 type => 'integer',
2478 primary => 1
2479 }
2480 ]
2481 }
2482 }
2483 ]
2484 }
2485 }
2486 });
2487  
2488 $grammar->transaction($grammar->schema->{'transaction'});
2489  
2490 =back
2491  
2492 =cut
2493  
2494 =head2 type
2495  
2496 type(HashRef $data) : Str
2497  
2498 The type method return the SQL representation for a data type.
2499  
2500 =over 4
2501  
2502 =item type example #1
2503  
2504 # given: synopsis
2505  
2506 $grammar->type({
2507 type => 'datetime-wtz'
2508 });
2509  
2510 # datetime
2511  
2512 =back
2513  
2514 =cut
2515  
2516 =head2 type_binary
2517  
2518 type_binary(HashRef $data) : Str
2519  
2520 The type_binary method returns the SQL expression representing a binary data
2521 type.
2522  
2523 =over 4
2524  
2525 =item type_binary example #1
2526  
2527 # given: synopsis
2528  
2529 $grammar->type_binary({});
2530  
2531 # blob
2532  
2533 =back
2534  
2535 =cut
2536  
2537 =head2 type_boolean
2538  
2539 type_boolean(HashRef $data) : Str
2540  
2541 The type_boolean method returns the SQL expression representing a boolean data
2542 type.
2543  
2544 =over 4
2545  
2546 =item type_boolean example #1
2547  
2548 # given: synopsis
2549  
2550 $grammar->type_boolean({});
2551  
2552 # tinyint(1)
2553  
2554 =back
2555  
2556 =cut
2557  
2558 =head2 type_char
2559  
2560 type_char(HashRef $data) : Str
2561  
2562 The type_char method returns the SQL expression representing a char data type.
2563  
2564 =over 4
2565  
2566 =item type_char example #1
2567  
2568 # given: synopsis
2569  
2570 $grammar->type_char({});
2571  
2572 # varchar
2573  
2574 =back
2575  
2576 =cut
2577  
2578 =head2 type_date
2579  
2580 type_date(HashRef $data) : Str
2581  
2582 The type_date method returns the SQL expression representing a date data type.
2583  
2584 =over 4
2585  
2586 =item type_date example #1
2587  
2588 # given: synopsis
2589  
2590 $grammar->type_date({});
2591  
2592 # date
2593  
2594 =back
2595  
2596 =cut
2597  
2598 =head2 type_datetime
2599  
2600 type_datetime(HashRef $data) : Str
2601  
2602 The type_datetime method returns the SQL expression representing a datetime
2603 data type.
2604  
2605 =over 4
2606  
2607 =item type_datetime example #1
2608  
2609 # given: synopsis
2610  
2611 $grammar->type_datetime({});
2612  
2613 # datetime
2614  
2615 =back
2616  
2617 =cut
2618  
2619 =head2 type_datetime_wtz
2620  
2621 type_datetime_wtz(HashRef $data) : Str
2622  
2623 The type_datetime_wtz method returns the SQL expression representing a datetime
2624 (and timezone) data type.
2625  
2626 =over 4
2627  
2628 =item type_datetime_wtz example #1
2629  
2630 # given: synopsis
2631  
2632 $grammar->type_datetime_wtz({});
2633  
2634 # datetime
2635  
2636 =back
2637  
2638 =cut
2639  
2640 =head2 type_decimal
2641  
2642 type_decimal(HashRef $data) : Str
2643  
2644 The type_decimal method returns the SQL expression representing a decimal data
2645 type.
2646  
2647 =over 4
2648  
2649 =item type_decimal example #1
2650  
2651 # given: synopsis
2652  
2653 $grammar->type_decimal({});
2654  
2655 # numeric
2656  
2657 =back
2658  
2659 =cut
2660  
2661 =head2 type_double
2662  
2663 type_double(HashRef $data) : Str
2664  
2665 The type_double method returns the SQL expression representing a double data
2666 type.
2667  
2668 =over 4
2669  
2670 =item type_double example #1
2671  
2672 # given: synopsis
2673  
2674 $grammar->type_double({});
2675  
2676 # float
2677  
2678 =back
2679  
2680 =cut
2681  
2682 =head2 type_enum
2683  
2684 type_enum(HashRef $data) : Str
2685  
2686 The type_enum method returns the SQL expression representing a enum data type.
2687  
2688 =over 4
2689  
2690 =item type_enum example #1
2691  
2692 # given: synopsis
2693  
2694 $grammar->type_enum({});
2695  
2696 # varchar
2697  
2698 =back
2699  
2700 =cut
2701  
2702 =head2 type_float
2703  
2704 type_float(HashRef $data) : Str
2705  
2706 The type_float method returns the SQL expression representing a float data
2707 type.
2708  
2709 =over 4
2710  
2711 =item type_float example #1
2712  
2713 # given: synopsis
2714  
2715 $grammar->type_float({});
2716  
2717 # float
2718  
2719 =back
2720  
2721 =cut
2722  
2723 =head2 type_integer
2724  
2725 type_integer(HashRef $data) : Str
2726  
2727 The type_integer method returns the SQL expression representing a integer data
2728 type.
2729  
2730 =over 4
2731  
2732 =item type_integer example #1
2733  
2734 # given: synopsis
2735  
2736 $grammar->type_integer({});
2737  
2738 # integer
2739  
2740 =back
2741  
2742 =cut
2743  
2744 =head2 type_integer_big
2745  
2746 type_integer_big(HashRef $data) : Str
2747  
2748 The type_integer_big method returns the SQL expression representing a
2749 big-integer data type.
2750  
2751 =over 4
2752  
2753 =item type_integer_big example #1
2754  
2755 # given: synopsis
2756  
2757 $grammar->type_integer_big({});
2758  
2759 # integer
2760  
2761 =back
2762  
2763 =cut
2764  
2765 =head2 type_integer_big_unsigned
2766  
2767 type_integer_big_unsigned(HashRef $data) : Str
2768  
2769 The type_integer_big_unsigned method returns the SQL expression representing a
2770 big unsigned integer data type.
2771  
2772 =over 4
2773  
2774 =item type_integer_big_unsigned example #1
2775  
2776 # given: synopsis
2777  
2778 $grammar->type_integer_big_unsigned({});
2779  
2780 # integer
2781  
2782 =back
2783  
2784 =cut
2785  
2786 =head2 type_integer_medium
2787  
2788 type_integer_medium(HashRef $data) : Str
2789  
2790 The type_integer_medium method returns the SQL expression representing a medium
2791 integer data type.
2792  
2793 =over 4
2794  
2795 =item type_integer_medium example #1
2796  
2797 # given: synopsis
2798  
2799 $grammar->type_integer_medium({});
2800  
2801 # integer
2802  
2803 =back
2804  
2805 =cut
2806  
2807 =head2 type_integer_medium_unsigned
2808  
2809 type_integer_medium_unsigned(HashRef $data) : Str
2810  
2811 The type_integer_medium_unsigned method returns the SQL expression representing
2812 a unsigned medium integer data type.
2813  
2814 =over 4
2815  
2816 =item type_integer_medium_unsigned example #1
2817  
2818 # given: synopsis
2819  
2820 $grammar->type_integer_medium_unsigned({});
2821  
2822 # integer
2823  
2824 =back
2825  
2826 =cut
2827  
2828 =head2 type_integer_small
2829  
2830 type_integer_small(HashRef $data) : Str
2831  
2832 The type_integer_small method returns the SQL expression representing a small
2833 integer data type.
2834  
2835 =over 4
2836  
2837 =item type_integer_small example #1
2838  
2839 # given: synopsis
2840  
2841 $grammar->type_integer_small({});
2842  
2843 # integer
2844  
2845 =back
2846  
2847 =cut
2848  
2849 =head2 type_integer_small_unsigned
2850  
2851 type_integer_small_unsigned(HashRef $data) : Str
2852  
2853 The type_integer_small_unsigned method returns the SQL expression representing
2854 a unsigned small integer data type.
2855  
2856 =over 4
2857  
2858 =item type_integer_small_unsigned example #1
2859  
2860 # given: synopsis
2861  
2862 $grammar->type_integer_small_unsigned({});
2863  
2864 # integer
2865  
2866 =back
2867  
2868 =cut
2869  
2870 =head2 type_integer_tiny
2871  
2872 type_integer_tiny(HashRef $data) : Str
2873  
2874 The type_integer_tiny method returns the SQL expression representing a tiny
2875 integer data type.
2876  
2877 =over 4
2878  
2879 =item type_integer_tiny example #1
2880  
2881 # given: synopsis
2882  
2883 $grammar->type_integer_tiny({});
2884  
2885 # integer
2886  
2887 =back
2888  
2889 =cut
2890  
2891 =head2 type_integer_tiny_unsigned
2892  
2893 type_integer_tiny_unsigned(HashRef $data) : Str
2894  
2895 The type_integer_tiny_unsigned method returns the SQL expression representing a
2896 unsigned tiny integer data type.
2897  
2898 =over 4
2899  
2900 =item type_integer_tiny_unsigned example #1
2901  
2902 # given: synopsis
2903  
2904 $grammar->type_integer_tiny_unsigned({});
2905  
2906 # integer
2907  
2908 =back
2909  
2910 =cut
2911  
2912 =head2 type_integer_unsigned
2913  
2914 type_integer_unsigned(HashRef $data) : Str
2915  
2916 The type_integer_unsigned method returns the SQL expression representing a
2917 unsigned integer data type.
2918  
2919 =over 4
2920  
2921 =item type_integer_unsigned example #1
2922  
2923 # given: synopsis
2924  
2925 $grammar->type_integer_unsigned({});
2926  
2927 # integer
2928  
2929 =back
2930  
2931 =cut
2932  
2933 =head2 type_json
2934  
2935 type_json(HashRef $data) : Str
2936  
2937 The type_json method returns the SQL expression representing a json data type.
2938  
2939 =over 4
2940  
2941 =item type_json example #1
2942  
2943 # given: synopsis
2944  
2945 $grammar->type_json({});
2946  
2947 # text
2948  
2949 =back
2950  
2951 =cut
2952  
2953 =head2 type_number
2954  
2955 type_number(HashRef $data) : Str
2956  
2957 The type_number method returns the SQL expression representing a number data
2958 type.
2959  
2960 =over 4
2961  
2962 =item type_number example #1
2963  
2964 # given: synopsis
2965  
2966 $grammar->type_number({});
2967  
2968 # integer
2969  
2970 =back
2971  
2972 =cut
2973  
2974 =head2 type_string
2975  
2976 type_string(HashRef $data) : Str
2977  
2978 The type_string method returns the SQL expression representing a string data
2979 type.
2980  
2981 =over 4
2982  
2983 =item type_string example #1
2984  
2985 # given: synopsis
2986  
2987 $grammar->type_string({});
2988  
2989 # varchar
2990  
2991 =back
2992  
2993 =cut
2994  
2995 =head2 type_text
2996  
2997 type_text(HashRef $data) : Str
2998  
2999 The type_text method returns the SQL expression representing a text data type.
3000  
3001 =over 4
3002  
3003 =item type_text example #1
3004  
3005 # given: synopsis
3006  
3007 $grammar->type_text({});
3008  
3009 # text
3010  
3011 =back
3012  
3013 =cut
3014  
3015 =head2 type_text_long
3016  
3017 type_text_long(HashRef $data) : Str
3018  
3019 The type_text_long method returns the SQL expression representing a long text
3020 data type.
3021  
3022 =over 4
3023  
3024 =item type_text_long example #1
3025  
3026 # given: synopsis
3027  
3028 $grammar->type_text_long({});
3029  
3030 # text
3031  
3032 =back
3033  
3034 =cut
3035  
3036 =head2 type_text_medium
3037  
3038 type_text_medium(HashRef $data) : Str
3039  
3040 The type_text_medium method returns the SQL expression representing a medium
3041 text data type.
3042  
3043 =over 4
3044  
3045 =item type_text_medium example #1
3046  
3047 # given: synopsis
3048  
3049 $grammar->type_text_medium({});
3050  
3051 # text
3052  
3053 =back
3054  
3055 =cut
3056  
3057 =head2 type_time
3058  
3059 type_time(HashRef $data) : Str
3060  
3061 The type_time method returns the SQL expression representing a time data type.
3062  
3063 =over 4
3064  
3065 =item type_time example #1
3066  
3067 # given: synopsis
3068  
3069 $grammar->type_time({});
3070  
3071 # time
3072  
3073 =back
3074  
3075 =cut
3076  
3077 =head2 type_time_wtz
3078  
3079 type_time_wtz(HashRef $data) : Str
3080  
3081 The type_time_wtz method returns the SQL expression representing a time (and
3082 timezone) data type.
3083  
3084 =over 4
3085  
3086 =item type_time_wtz example #1
3087  
3088 # given: synopsis
3089  
3090 $grammar->type_time_wtz({});
3091  
3092 # time
3093  
3094 =back
3095  
3096 =cut
3097  
3098 =head2 type_timestamp
3099  
3100 type_timestamp(HashRef $data) : Str
3101  
3102 The type_timestamp method returns the SQL expression representing a timestamp
3103 data type.
3104  
3105 =over 4
3106  
3107 =item type_timestamp example #1
3108  
3109 # given: synopsis
3110  
3111 $grammar->type_timestamp({});
3112  
3113 # datetime
3114  
3115 =back
3116  
3117 =cut
3118  
3119 =head2 type_timestamp_wtz
3120  
3121 type_timestamp_wtz(HashRef $data) : Str
3122  
3123 The type_timestamp_wtz method returns the SQL expression representing a
3124 timestamp (and timezone) data type.
3125  
3126 =over 4
3127  
3128 =item type_timestamp_wtz example #1
3129  
3130 # given: synopsis
3131  
3132 $grammar->type_timestamp_wtz({});
3133  
3134 # datetime
3135  
3136 =back
3137  
3138 =cut
3139  
3140 =head2 type_uuid
3141  
3142 type_uuid(HashRef $data) : Str
3143  
3144 The type_uuid method returns the SQL expression representing a uuid data type.
3145  
3146 =over 4
3147  
3148 =item type_uuid example #1
3149  
3150 # given: synopsis
3151  
3152 $grammar->type_uuid({});
3153  
3154 # varchar
3155  
3156 =back
3157  
3158 =cut
3159  
3160 =head2 update
3161  
3162 update(HashRef $data) : Object
3163  
3164 The update method generates SQL statements to update table rows.
3165  
3166 =over 4
3167  
3168 =item update example #1
3169  
3170 # given: synopsis
3171  
3172 $grammar->update({
3173 for => {
3174 table => 'users'
3175 },
3176 columns => [
3177 {
3178 column => 'updated',
3179 value => { function => ['now'] }
3180 }
3181 ]
3182 });
3183  
3184 =back
3185  
3186 =cut
3187  
3188 =head2 validate
3189  
3190 validate() : Bool
3191  
3192 The validate method validates the data structure defined in the I<"schema">
3193 property.
3194  
3195 =over 4
3196  
3197 =item validate example #1
3198  
3199 # given: synopsis
3200  
3201 my $valid = $grammar->validate;
3202  
3203 =back
3204  
3205 =cut
3206  
3207 =head2 value
3208  
3209 value(Any $value) : Str
3210  
3211 The value method returns the SQL representation of a value.
3212  
3213 =over 4
3214  
3215 =item value example #1
3216  
3217 # given: synopsis
3218  
3219 $grammar->value(undef);
3220  
3221 # NULL
3222  
3223 =back
3224  
3225 =cut
3226  
3227 =head2 view_create
3228  
3229 view_create(HashRef $data) : Object
3230  
3231 The view_create method generates SQL statements to create a table view.
3232  
3233 =over 4
3234  
3235 =item view_create example #1
3236  
3237 # given: synopsis
3238  
3239 $grammar->view_create({
3240 name => 'active_users',
3241 query => {
3242 select => {
3243 from => {
3244 table => 'users'
3245 },
3246 columns => [
3247 {
3248 column => '*'
3249 }
3250 ],
3251 where => [
3252 {
3253 'not-null' => {
3254 column => 'deleted'
3255 }
3256 }
3257 ]
3258 }
3259 }
3260 });
3261  
3262 =back
3263  
3264 =cut
3265  
3266 =head2 view_drop
3267  
3268 view_drop(HashRef $data) : Object
3269  
3270 The view_drop method generates SQL statements to remove a table view.
3271  
3272 =over 4
3273  
3274 =item view_drop example #1
3275  
3276 # given: synopsis
3277  
3278 $grammar->view_drop({
3279 name => 'active_users'
3280 });
3281  
3282 =back
3283  
3284 =cut
3285  
3286 =head2 wrap
3287  
3288 wrap(Str $name) : Str
3289  
3290 The wrap method returns a SQL-escaped string.
3291  
3292 =over 4
3293  
3294 =item wrap example #1
3295  
3296 # given: synopsis
3297  
3298 $grammar->wrap('field');
3299  
3300 # "field"
3301  
3302 =back
3303  
3304 =cut
3305  
3306 =head1 AUTHOR
3307  
3308 Al Newkirk, C<awncorp@cpan.org>
3309  
3310 =head1 LICENSE
3311  
3312 Copyright (C) 2011-2019, Al Newkirk, et al.
3313  
3314 This is free software; you can redistribute it and/or modify it under the terms
3315 of the The Apache License, Version 2.0, as elucidated in the L<"license
3316 file"|https://github.com/iamalnewkirk/sql-engine/blob/master/LICENSE>.
3317  
3318 =head1 PROJECT
3319  
3320 L<Wiki|https://github.com/iamalnewkirk/sql-engine/wiki>
3321  
3322 L<Project|https://github.com/iamalnewkirk/sql-engine>
3323  
3324 L<Initiatives|https://github.com/iamalnewkirk/sql-engine/projects>
3325  
3326 L<Milestones|https://github.com/iamalnewkirk/sql-engine/milestones>
3327  
3328 L<Contributing|https://github.com/iamalnewkirk/sql-engine/blob/master/CONTRIBUTE.md>
3329  
3330 L<Issues|https://github.com/iamalnewkirk/sql-engine/issues>
3331  
3332 =cut