Engine.pm untested

filter: Statementstmt 0.0% 0 / 172 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 32 Podpod n/a totaltotal 0.0% 0 / 206 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;
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::Validator;
16  
17 our $VERSION = '0.01'; # VERSION
18  
19 # ATTRIBUTES
20  
21 has grammar => (
22 is => 'ro',
23 isa => 'Str',
24 opt => 1
25 );
26  
27 has operations => (
28 is => 'ro',
29 isa => 'InstanceOf["SQL::Engine::Collection"]',
30 new => 1
31 );
32  
33 fun new_operations($self) {
34  
35 SQL::Engine::Collection->new;
36 }
37  
38 has validator => (
39 is => 'ro',
40 isa => 'Maybe[InstanceOf["SQL::Validator"]]',
41 new => 1
42 );
43  
44 fun new_validator($self) {
45 SQL::Validator->new(
46 $ENV{SQL_ENGINE_SCHEMA}
47 ? (schema => $ENV{SQL_ENGINE_SCHEMA})
48 : (version => '0.0')
49 )
50 }
51  
52 # METHODS
53  
54 method column_change(@args) {
55 require SQL::Engine::Builder::ColumnChange;
56  
57 my $grammar = SQL::Engine::Builder::ColumnChange->new(@args)->grammar(
58 grammar => $self->grammar,
59 validator => $self->validator
60 );
61  
62 $self->operations->push($grammar->execute->operations->list);
63  
64 return $self;
65 }
66  
67 method column_create(@args) {
68 require SQL::Engine::Builder::ColumnCreate;
69  
70 my $grammar = SQL::Engine::Builder::ColumnCreate->new(@args)->grammar(
71 grammar => $self->grammar,
72 validator => $self->validator
73 );
74  
75 $self->operations->push($grammar->execute->operations->list);
76  
77 return $self;
78 }
79  
80 method column_drop(@args) {
81 require SQL::Engine::Builder::ColumnDrop;
82  
83 my $grammar = SQL::Engine::Builder::ColumnDrop->new(@args)->grammar(
84 grammar => $self->grammar,
85 validator => $self->validator
86 );
87  
88 $self->operations->push($grammar->execute->operations->list);
89  
90 return $self;
91 }
92  
93 method column_rename(@args) {
94 require SQL::Engine::Builder::ColumnRename;
95  
96 my $grammar = SQL::Engine::Builder::ColumnRename->new(@args)->grammar(
97 grammar => $self->grammar,
98 validator => $self->validator
99 );
100  
101 $self->operations->push($grammar->execute->operations->list);
102  
103 return $self;
104 }
105  
106 method constraint_create(@args) {
107 require SQL::Engine::Builder::ConstraintCreate;
108  
109 my $grammar = SQL::Engine::Builder::ConstraintCreate->new(@args)->grammar(
110 grammar => $self->grammar,
111 validator => $self->validator
112 );
113  
114 $self->operations->push($grammar->execute->operations->list);
115  
116 return $self;
117 }
118  
119 method constraint_drop(@args) {
120 require SQL::Engine::Builder::ConstraintDrop;
121  
122 my $grammar = SQL::Engine::Builder::ConstraintDrop->new(@args)->grammar(
123 grammar => $self->grammar,
124 validator => $self->validator
125 );
126  
127 $self->operations->push($grammar->execute->operations->list);
128  
129 return $self;
130 }
131  
132 method database_create(@args) {
133 require SQL::Engine::Builder::DatabaseCreate;
134  
135 my $grammar = SQL::Engine::Builder::DatabaseCreate->new(@args)->grammar(
136 grammar => $self->grammar,
137 validator => $self->validator
138 );
139  
140 $self->operations->push($grammar->execute->operations->list);
141  
142 return $self;
143 }
144  
145 method database_drop(@args) {
146 require SQL::Engine::Builder::DatabaseDrop;
147  
148 my $grammar = SQL::Engine::Builder::DatabaseDrop->new(@args)->grammar(
149 grammar => $self->grammar,
150 validator => $self->validator
151 );
152  
153 $self->operations->push($grammar->execute->operations->list);
154  
155 return $self;
156 }
157  
158 method delete(@args) {
159 require SQL::Engine::Builder::Delete;
160  
161 my $grammar = SQL::Engine::Builder::Delete->new(@args)->grammar(
162 grammar => $self->grammar,
163 validator => $self->validator
164 );
165  
166 $self->operations->push($grammar->execute->operations->list);
167  
168 return $self;
169 }
170  
171 method index_create(@args) {
172 require SQL::Engine::Builder::IndexCreate;
173  
174 my $grammar = SQL::Engine::Builder::IndexCreate->new(@args)->grammar(
175 grammar => $self->grammar,
176 validator => $self->validator
177 );
178  
179 $self->operations->push($grammar->execute->operations->list);
180  
181 return $self;
182 }
183  
184 method index_drop(@args) {
185 require SQL::Engine::Builder::IndexDrop;
186  
187 my $grammar = SQL::Engine::Builder::IndexDrop->new(@args)->grammar(
188 grammar => $self->grammar,
189 validator => $self->validator
190 );
191  
192 $self->operations->push($grammar->execute->operations->list);
193  
194 return $self;
195 }
196  
197 method insert(@args) {
198 require SQL::Engine::Builder::Insert;
199  
200 my $grammar = SQL::Engine::Builder::Insert->new(@args)->grammar(
201 grammar => $self->grammar,
202 validator => $self->validator
203 );
204  
205 $self->operations->push($grammar->execute->operations->list);
206  
207 return $self;
208 }
209  
210 method schema_create(@args) {
211 require SQL::Engine::Builder::SchemaCreate;
212  
213 my $grammar = SQL::Engine::Builder::SchemaCreate->new(@args)->grammar(
214 grammar => $self->grammar,
215 validator => $self->validator
216 );
217  
218 $self->operations->push($grammar->execute->operations->list);
219  
220 return $self;
221 }
222  
223 method schema_drop(@args) {
224 require SQL::Engine::Builder::SchemaDrop;
225  
226 my $grammar = SQL::Engine::Builder::SchemaDrop->new(@args)->grammar(
227 grammar => $self->grammar,
228 validator => $self->validator
229 );
230  
231 $self->operations->push($grammar->execute->operations->list);
232  
233 return $self;
234 }
235  
236 method schema_rename(@args) {
237 require SQL::Engine::Builder::SchemaRename;
238  
239 my $grammar = SQL::Engine::Builder::SchemaRename->new(@args)->grammar(
240 grammar => $self->grammar,
241 validator => $self->validator
242 );
243  
244 $self->operations->push($grammar->execute->operations->list);
245  
246 return $self;
247 }
248  
249 method select(@args) {
250 require SQL::Engine::Builder::Select;
251  
252 my $grammar = SQL::Engine::Builder::Select->new(@args)->grammar(
253 grammar => $self->grammar,
254 validator => $self->validator
255 );
256  
257 $self->operations->push($grammar->execute->operations->list);
258  
259 return $self;
260 }
261  
262 method table_create(@args) {
263 require SQL::Engine::Builder::TableCreate;
264  
265 my $grammar = SQL::Engine::Builder::TableCreate->new(@args)->grammar(
266 grammar => $self->grammar,
267 validator => $self->validator
268 );
269  
270 $self->operations->push($grammar->execute->operations->list);
271  
272 return $self;
273 }
274  
275 method table_drop(@args) {
276 require SQL::Engine::Builder::TableDrop;
277  
278 my $grammar = SQL::Engine::Builder::TableDrop->new(@args)->grammar(
279 grammar => $self->grammar,
280 validator => $self->validator
281 );
282  
283 $self->operations->push($grammar->execute->operations->list);
284  
285 return $self;
286 }
287  
288 method table_rename(@args) {
289 require SQL::Engine::Builder::TableRename;
290  
291 my $grammar = SQL::Engine::Builder::TableRename->new(@args)->grammar(
292 grammar => $self->grammar,
293 validator => $self->validator
294 );
295  
296 $self->operations->push($grammar->execute->operations->list);
297  
298 return $self;
299 }
300  
301 method transaction(@args) {
302 require SQL::Engine::Builder::Transaction;
303  
304 my $grammar = SQL::Engine::Builder::Transaction->new(@args)->grammar(
305 grammar => $self->grammar,
306 validator => $self->validator
307 );
308  
309 $self->operations->push($grammar->execute->operations->list);
310  
311 return $self;
312 }
313  
314 method update(@args) {
315 require SQL::Engine::Builder::Update;
316  
317 my $grammar = SQL::Engine::Builder::Update->new(@args)->grammar(
318 grammar => $self->grammar,
319 validator => $self->validator
320 );
321  
322 $self->operations->push($grammar->execute->operations->list);
323  
324 return $self;
325 }
326  
327 method view_create(@args) {
328 require SQL::Engine::Builder::ViewCreate;
329  
330 my $grammar = SQL::Engine::Builder::ViewCreate->new(@args)->grammar(
331 grammar => $self->grammar,
332 validator => $self->validator
333 );
334  
335 $self->operations->push($grammar->execute->operations->list);
336  
337 return $self;
338 }
339  
340 method view_drop(@args) {
341 require SQL::Engine::Builder::ViewDrop;
342  
343 my $grammar = SQL::Engine::Builder::ViewDrop->new(@args)->grammar(
344 grammar => $self->grammar,
345 validator => $self->validator
346 );
347  
348 $self->operations->push($grammar->execute->operations->list);
349  
350 return $self;
351 }
352  
353 1;
354  
355 =encoding utf8
356  
357 =head1 NAME
358  
359 SQL::Engine - SQL Generation
360  
361 =cut
362  
363 =head1 ABSTRACT
364  
365 SQL Generation for Perl 5
366  
367 =cut
368  
369 =head1 SYNOPSIS
370  
371 use SQL::Engine;
372  
373 my $sql = SQL::Engine->new;
374  
375 $sql->insert(
376 into => {
377 table => 'users'
378 },
379 columns => [
380 {
381 column => 'id'
382 },
383 {
384 column => 'name'
385 }
386 ],
387 values => [
388 {
389 value => undef
390 },
391 {
392 value => {
393 binding => 'name'
394 }
395 },
396 ]
397 );
398  
399 # then, e.g.
400 #
401 # my $dbh = DBI->connect;
402 #
403 # for my $operation ($sql->operations->list) {
404 # my $statement = $operation->statement;
405 # my @bindings = $operation->parameters({ name => 'Rob Zombie' });
406 #
407 # my $sth = $dbh->prepate($statement);
408 #
409 # $sth->execute(@bindings);
410 # }
411 #
412 # $dbh->disconnect;
413  
414 =cut
415  
416 =head1 DESCRIPTION
417  
418 This package provides an interface and builders which generate SQL statements,
419 by default using a standard SQL syntax or vendor-specific syntax if supported
420 and provided to the constructor using the I<"grammar"> property. This package
421 does not require a database connection, by design, which gives users complete
422 control over how connections and statement handles are managed.
423  
424 =cut
425  
426 =head1 LIBRARIES
427  
428 This package uses type constraints from:
429  
430 L<Types::Standard>
431  
432 =cut
433  
434 =head1 SCENARIOS
435  
436 This package supports the following scenarios:
437  
438 =cut
439  
440 =head2 validation
441  
442 use SQL::Engine;
443  
444 my $sql = SQL::Engine->new(
445 validator => undef
446 );
447  
448 # faster, no-validation
449  
450 $sql->select(
451 from => {
452 table => 'users'
453 },
454 columns => [
455 {
456 column => '*'
457 }
458 ]
459 );
460  
461 This package supports automatic validation of operations using
462 L<SQL::Validator> which can be passed to the constructor as the value of the
463 I<"validator"> property. This object will be generated if not provided.
464 Alternatively, automated validation can be disabled by passing the
465 I<"undefined"> value to the I<"validator"> property on object construction.
466 Doing so enhances the performance of SQL generation at the cost of not
467 verifying that the instructions provided are correct.
468  
469 =cut
470  
471 =head1 ATTRIBUTES
472  
473 This package has the following attributes:
474  
475 =cut
476  
477 =head2 grammar
478  
479 grammar(Str)
480  
481 This attribute is read-only, accepts C<(Str)> values, and is optional.
482  
483 =cut
484  
485 =head2 operations
486  
487 operations(InstanceOf["SQL::Engine::Collection"])
488  
489 This attribute is read-only, accepts C<(InstanceOf["SQL::Engine::Collection"])> values, and is optional.
490  
491 =cut
492  
493 =head2 validator
494  
495 validator(Maybe[InstanceOf["SQL::Validator"]])
496  
497 This attribute is read-only, accepts C<(Maybe[InstanceOf["SQL::Validator"]])> values, and is optional.
498  
499 =cut
500  
501 =head1 METHODS
502  
503 This package implements the following methods:
504  
505 =cut
506  
507 =head2 column_change
508  
509 column_change(Any %args) : Object
510  
511 The column_change method produces SQL operations which changes a table column
512 definition. The arguments expected are the constructor arguments accepted by
513 L<SQL::Engine::Builder::ColumnChange>.
514  
515 =over 4
516  
517 =item column_change example #1
518  
519 # given: synopsis
520  
521 $sql->operations->clear;
522  
523 $sql->column_change(
524 for => {
525 table => 'users'
526 },
527 column => {
528 name => 'accessed',
529 type => 'datetime',
530 nullable => 1
531 }
532 );
533  
534 =back
535  
536 =cut
537  
538 =head2 column_create
539  
540 column_create(Any %args) : Object
541  
542 The column_create method produces SQL operations which create a new table
543 column. The arguments expected are the constructor arguments accepted by
544 L<SQL::Engine::Builder::ColumnCreate>.
545  
546 =over 4
547  
548 =item column_create example #1
549  
550 # given: synopsis
551  
552 $sql->operations->clear;
553  
554 $sql->column_create(
555 for => {
556 table => 'users'
557 },
558 column => {
559 name => 'accessed',
560 type => 'datetime'
561 }
562 );
563  
564 =back
565  
566 =cut
567  
568 =head2 column_drop
569  
570 column_drop(Any %args) : Object
571  
572 The column_drop method produces SQL operations which removes an existing table
573 column. The arguments expected are the constructor arguments accepted by
574 L<SQL::Engine::Builder::ColumnDrop>.
575  
576 =over 4
577  
578 =item column_drop example #1
579  
580 # given: synopsis
581  
582 $sql->operations->clear;
583  
584 $sql->column_drop(
585 table => 'users',
586 column => 'accessed'
587 );
588  
589 =back
590  
591 =cut
592  
593 =head2 column_rename
594  
595 column_rename(Any %args) : Object
596  
597 The column_rename method produces SQL operations which renames an existing
598 table column. The arguments expected are the constructor arguments accepted by
599 L<SQL::Engine::Builder::ColumnRename>.
600  
601 =over 4
602  
603 =item column_rename example #1
604  
605 # given: synopsis
606  
607 $sql->operations->clear;
608  
609 $sql->column_rename(
610 for => {
611 table => 'users'
612 },
613 name => {
614 old => 'accessed',
615 new => 'accessed_at'
616 }
617 );
618  
619 =back
620  
621 =cut
622  
623 =head2 constraint_create
624  
625 constraint_create(Any %args) : Object
626  
627 The constraint_create method produces SQL operations which creates a new table
628 constraint. The arguments expected are the constructor arguments accepted by
629 L<SQL::Engine::Builder::ConstraintCreate>.
630  
631 =over 4
632  
633 =item constraint_create example #1
634  
635 # given: synopsis
636  
637 $sql->operations->clear;
638  
639 $sql->constraint_create(
640 source => {
641 table => 'users',
642 column => 'profile_id'
643 },
644 target => {
645 table => 'profiles',
646 column => 'id'
647 }
648 );
649  
650 =back
651  
652 =cut
653  
654 =head2 constraint_drop
655  
656 constraint_drop(Any %args) : Object
657  
658 The constraint_drop method produces SQL operations which removes an existing
659 table constraint. The arguments expected are the constructor arguments accepted
660 by L<SQL::Engine::Builder::ConstraintDrop>.
661  
662 =over 4
663  
664 =item constraint_drop example #1
665  
666 # given: synopsis
667  
668 $sql->operations->clear;
669  
670 $sql->constraint_drop(
671 source => {
672 table => 'users',
673 column => 'profile_id'
674 },
675 target => {
676 table => 'profiles',
677 column => 'id'
678 }
679 );
680  
681 =back
682  
683 =cut
684  
685 =head2 database_create
686  
687 database_create(Any %args) : Object
688  
689 The database_create method produces SQL operations which creates a new
690 database. The arguments expected are the constructor arguments accepted by
691 L<SQL::Engine::Builder::DatabaseCreate>.
692  
693 =over 4
694  
695 =item database_create example #1
696  
697 # given: synopsis
698  
699 $sql->operations->clear;
700  
701 $sql->database_create(
702 name => 'todoapp'
703 );
704  
705 =back
706  
707 =cut
708  
709 =head2 database_drop
710  
711 database_drop(Any %args) : Object
712  
713 The database_drop method produces SQL operations which removes an existing
714 database. The arguments expected are the constructor arguments accepted by
715 L<SQL::Engine::Builder::DatabaseDrop>.
716  
717 =over 4
718  
719 =item database_drop example #1
720  
721 # given: synopsis
722  
723 $sql->operations->clear;
724  
725 $sql->database_drop(
726 name => 'todoapp'
727 );
728  
729 =back
730  
731 =cut
732  
733 =head2 delete
734  
735 delete(Any %args) : Object
736  
737 The delete method produces SQL operations which deletes rows from a table. The
738 arguments expected are the constructor arguments accepted by
739 L<SQL::Engine::Builder::Delete>.
740  
741 =over 4
742  
743 =item delete example #1
744  
745 # given: synopsis
746  
747 $sql->operations->clear;
748  
749 $sql->delete(
750 from => {
751 table => 'tasklists'
752 }
753 );
754  
755 =back
756  
757 =cut
758  
759 =head2 index_create
760  
761 index_create(Any %args) : Object
762  
763 The index_create method produces SQL operations which creates a new table
764 index. The arguments expected are the constructor arguments accepted by
765 L<SQL::Engine::Builder::IndexCreate>.
766  
767 =over 4
768  
769 =item index_create example #1
770  
771 # given: synopsis
772  
773 $sql->operations->clear;
774  
775 $sql->index_create(
776 for => {
777 table => 'users'
778 },
779 columns => [
780 {
781 column => 'name'
782 }
783 ]
784 );
785  
786 =back
787  
788 =cut
789  
790 =head2 index_drop
791  
792 index_drop(Any %args) : Object
793  
794 The index_drop method produces SQL operations which removes an existing table
795 index. The arguments expected are the constructor arguments accepted by
796 L<SQL::Engine::Builder::IndexDrop>.
797  
798 =over 4
799  
800 =item index_drop example #1
801  
802 # given: synopsis
803  
804 $sql->operations->clear;
805  
806 $sql->index_drop(
807 for => {
808 table => 'users'
809 },
810 columns => [
811 {
812 column => 'name'
813 }
814 ]
815 );
816  
817 =back
818  
819 =cut
820  
821 =head2 insert
822  
823 insert(Any %args) : Object
824  
825 The insert method produces SQL operations which inserts rows into a table. The
826 arguments expected are the constructor arguments accepted by
827 L<SQL::Engine::Builder::Insert>.
828  
829 =over 4
830  
831 =item insert example #1
832  
833 # given: synopsis
834  
835 $sql->operations->clear;
836  
837 $sql->insert(
838 into => {
839 table => 'users'
840 },
841 values => [
842 {
843 value => undef
844 },
845 {
846 value => 'Rob Zombie'
847 },
848 {
849 value => {
850 function => ['now']
851 }
852 },
853 {
854 value => {
855 function => ['now']
856 }
857 },
858 {
859 value => {
860 function => ['now']
861 }
862 }
863 ]
864 );
865  
866 =back
867  
868 =cut
869  
870 =head2 schema_create
871  
872 schema_create(Any %args) : Object
873  
874 The schema_create method produces SQL operations which creates a new schema.
875 The arguments expected are the constructor arguments accepted by
876 L<SQL::Engine::Builder::SchemaCreate>.
877  
878 =over 4
879  
880 =item schema_create example #1
881  
882 # given: synopsis
883  
884 $sql->operations->clear;
885  
886 $sql->schema_create(
887 name => 'private',
888 );
889  
890 =back
891  
892 =cut
893  
894 =head2 schema_drop
895  
896 schema_drop(Any %args) : Object
897  
898 The schema_drop method produces SQL operations which removes an existing
899 schema. The arguments expected are the constructor arguments accepted by
900 L<SQL::Engine::Builder::SchemaDrop>.
901  
902 =over 4
903  
904 =item schema_drop example #1
905  
906 # given: synopsis
907  
908 $sql->operations->clear;
909  
910 $sql->schema_drop(
911 name => 'private',
912 );
913  
914 =back
915  
916 =cut
917  
918 =head2 schema_rename
919  
920 schema_rename(Any %args) : Object
921  
922 The schema_rename method produces SQL operations which renames an existing
923 schema. The arguments expected are the constructor arguments accepted by
924 L<SQL::Engine::Builder::SchemaRename>.
925  
926 =over 4
927  
928 =item schema_rename example #1
929  
930 # given: synopsis
931  
932 $sql->operations->clear;
933  
934 $sql->schema_rename(
935 name => {
936 old => 'private',
937 new => 'restricted'
938 }
939 );
940  
941 =back
942  
943 =cut
944  
945 =head2 select
946  
947 select(Any %args) : Object
948  
949 The select method produces SQL operations which select rows from a table. The
950 arguments expected are the constructor arguments accepted by
951 L<SQL::Engine::Builder::Select>.
952  
953 =over 4
954  
955 =item select example #1
956  
957 # given: synopsis
958  
959 $sql->operations->clear;
960  
961 $sql->select(
962 from => {
963 table => 'people'
964 },
965 columns => [
966 { column => 'name' }
967 ]
968 );
969  
970 =back
971  
972 =cut
973  
974 =head2 table_create
975  
976 table_create(Any %args) : Object
977  
978 The table_create method produces SQL operations which creates a new table. The
979 arguments expected are the constructor arguments accepted by
980 L<SQL::Engine::Builder::TableCreate>.
981  
982 =over 4
983  
984 =item table_create example #1
985  
986 # given: synopsis
987  
988 $sql->operations->clear;
989  
990 $sql->table_create(
991 name => 'users',
992 columns => [
993 {
994 name => 'id',
995 type => 'integer',
996 primary => 1
997 }
998 ]
999 );
1000  
1001 =back
1002  
1003 =cut
1004  
1005 =head2 table_drop
1006  
1007 table_drop(Any %args) : Object
1008  
1009 The table_drop method produces SQL operations which removes an existing table.
1010 The arguments expected are the constructor arguments accepted by
1011 L<SQL::Engine::Builder::TableDrop>.
1012  
1013 =over 4
1014  
1015 =item table_drop example #1
1016  
1017 # given: synopsis
1018  
1019 $sql->operations->clear;
1020  
1021 $sql->table_drop(
1022 name => 'people'
1023 );
1024  
1025 =back
1026  
1027 =cut
1028  
1029 =head2 table_rename
1030  
1031 table_rename(Any %args) : Object
1032  
1033 The table_rename method produces SQL operations which renames an existing
1034 table. The arguments expected are the constructor arguments accepted by
1035 L<SQL::Engine::Builder::TableRename>.
1036  
1037 =over 4
1038  
1039 =item table_rename example #1
1040  
1041 # given: synopsis
1042  
1043 $sql->operations->clear;
1044  
1045 $sql->table_rename(
1046 name => {
1047 old => 'peoples',
1048 new => 'people'
1049 }
1050 );
1051  
1052 =back
1053  
1054 =cut
1055  
1056 =head2 transaction
1057  
1058 transaction(Any %args) : Object
1059  
1060 The transaction method produces SQL operations which represents an atomic
1061 database operation. The arguments expected are the constructor arguments
1062 accepted by L<SQL::Engine::Builder::Transaction>.
1063  
1064 =over 4
1065  
1066 =item transaction example #1
1067  
1068 # given: synopsis
1069  
1070 $sql->operations->clear;
1071  
1072 $sql->transaction(
1073 queries => [
1074 {
1075 'table-create' => {
1076 name => 'users',
1077 columns => [
1078 {
1079 name => 'id',
1080 type => 'integer',
1081 primary => 1
1082 }
1083 ]
1084 }
1085 }
1086 ]
1087 );
1088  
1089 =back
1090  
1091 =cut
1092  
1093 =head2 update
1094  
1095 update(Any %args) : Object
1096  
1097 The update method produces SQL operations which update rows in a table. The
1098 arguments expected are the constructor arguments accepted by
1099 L<SQL::Engine::Builder::Update>.
1100  
1101 =over 4
1102  
1103 =item update example #1
1104  
1105 # given: synopsis
1106  
1107 $sql->operations->clear;
1108  
1109 $sql->update(
1110 for => {
1111 table => 'users'
1112 },
1113 columns => [
1114 {
1115 column => 'updated',
1116 value => { function => ['now'] }
1117 }
1118 ]
1119 );
1120  
1121 =back
1122  
1123 =cut
1124  
1125 =head2 view_create
1126  
1127 view_create(Any %args) : Object
1128  
1129 The view_create method produces SQL operations which creates a new table view.
1130 The arguments expected are the constructor arguments accepted by
1131 L<SQL::Engine::Builder::ViewCreate>.
1132  
1133 =over 4
1134  
1135 =item view_create example #1
1136  
1137 # given: synopsis
1138  
1139 $sql->operations->clear;
1140  
1141 $sql->view_create(
1142 name => 'active_users',
1143 query => {
1144 select => {
1145 from => {
1146 table => 'users'
1147 },
1148 columns => [
1149 {
1150 column => '*'
1151 }
1152 ],
1153 where => [
1154 {
1155 'not-null' => {
1156 column => 'deleted'
1157 }
1158 }
1159 ]
1160 }
1161 }
1162 );
1163  
1164 =back
1165  
1166 =cut
1167  
1168 =head2 view_drop
1169  
1170 view_drop(Any %args) : Object
1171  
1172 The view_drop method produces SQL operations which removes an existing table
1173 view. The arguments expected are the constructor arguments accepted by
1174 L<SQL::Engine::Builder::ViewDrop>.
1175  
1176 =over 4
1177  
1178 =item view_drop example #1
1179  
1180 # given: synopsis
1181  
1182 $sql->operations->clear;
1183  
1184 $sql->view_drop(
1185 name => 'active_users'
1186 );
1187  
1188 =back
1189  
1190 =cut
1191  
1192 =head1 EXAMPLES
1193  
1194 This distribution supports generating SQL statements using standard syntax or
1195 using database-specific syntax if a I<grammar> is specified. The following is a
1196 collection of examples covering the most common operations (using PostgreSQL
1197 syntax):
1198  
1199 =cut
1200  
1201 =head2 setup
1202  
1203 use SQL::Engine;
1204  
1205 my $sql = SQL::Engine->new(
1206 grammar => 'postgres'
1207 );
1208  
1209 $sql->select(
1210 from => {
1211 table => 'people'
1212 },
1213 columns => [
1214 { column => 'name' }
1215 ]
1216 );
1217  
1218 $sql->operations->first->statement;
1219  
1220 # SELECT "name" FROM "people"
1221  
1222 =cut
1223  
1224 =head2 select
1225  
1226 =over 4
1227  
1228 =item select example #1
1229  
1230 $sql->select(
1231 from => {
1232 table => 'users'
1233 },
1234 columns => [
1235 {
1236 column => '*'
1237 }
1238 ]
1239 );
1240  
1241 =item select example #1 output
1242  
1243 # SELECT * FROM "users"
1244  
1245 =item select example #2
1246  
1247 $sql->select(
1248 from => {
1249 table => 'users'
1250 },
1251 columns => [
1252 {
1253 column => 'id'
1254 },
1255 {
1256 column => 'name'
1257 }
1258 ]
1259 );
1260  
1261 =item select example #2 output
1262  
1263 # SELECT "id", "name" FROM "users"
1264  
1265 =item select example #3
1266  
1267 $sql->select(
1268 from => {
1269 table => 'users'
1270 },
1271 columns => [
1272 {
1273 column => '*'
1274 }
1275 ],
1276 where => [
1277 {
1278 eq => [{column => 'id'}, {binding => 'id'}]
1279 }
1280 ]
1281 );
1282  
1283 =item select example #3 output
1284  
1285 # SELECT * FROM "users" WHERE "id" = ?
1286  
1287 =item select example #4
1288  
1289 $sql->select(
1290 from => {
1291 table => 'users',
1292 alias => 'u'
1293 },
1294 columns => [
1295 {
1296 column => '*',
1297 alias => 'u'
1298 }
1299 ],
1300 joins => [
1301 {
1302 with => {
1303 table => 'tasklists',
1304 alias => 't'
1305 },
1306 having => [
1307 {
1308 eq => [
1309 {
1310 column => 'id',
1311 alias => 'u'
1312 },
1313 {
1314 column => 'user_id',
1315 alias => 't'
1316 }
1317 ]
1318 }
1319 ]
1320 }
1321 ],
1322 where => [
1323 {
1324 eq => [
1325 {
1326 column => 'id',
1327 alias => 'u'
1328 },
1329 {
1330 binding => 'id'
1331 }
1332 ]
1333 }
1334 ]
1335 );
1336  
1337 =item select example #4 output
1338  
1339 # SELECT "u".* FROM "users" "u"
1340 # JOIN "tasklists" "t" ON "u"."id" = "t"."user_id" WHERE "u"."id" = ?
1341  
1342 =item select example #5
1343  
1344 $sql->select(
1345 from => {
1346 table => 'tasklists'
1347 },
1348 columns => [
1349 {
1350 function => ['count', { column => 'user_id' }]
1351 }
1352 ],
1353 group_by => [
1354 {
1355 column => 'user_id'
1356 }
1357 ]
1358 );
1359  
1360 =item select example #5 output
1361  
1362 # SELECT count("user_id") FROM "tasklists" GROUP BY "user_id"
1363  
1364 =item select example #6
1365  
1366 $sql->select(
1367 from => {
1368 table => 'tasklists'
1369 },
1370 columns => [
1371 {
1372 function => ['count', { column => 'user_id' }]
1373 }
1374 ],
1375 group_by => [
1376 {
1377 column => 'user_id'
1378 }
1379 ],
1380 having => [
1381 {
1382 gt => [
1383 {
1384 function => ['count', { column => 'user_id' }]
1385 },
1386 1
1387 ]
1388 }
1389 ]
1390 );
1391  
1392 =item select example #6 output
1393  
1394 # SELECT count("user_id") FROM "tasklists" GROUP BY "user_id" HAVING
1395 # count("user_id") > 1
1396  
1397 =item select example #7
1398  
1399 $sql->select(
1400 from => {
1401 table => 'tasklists'
1402 },
1403 columns => [
1404 {
1405 column => '*'
1406 }
1407 ],
1408 order_by => [
1409 {
1410 column => 'user_id'
1411 }
1412 ]
1413 );
1414  
1415 =item select example #7 output
1416  
1417 # SELECT * FROM "tasklists" ORDER BY "user_id"
1418  
1419 =item select example #8
1420  
1421 $sql->select(
1422 from => {
1423 table => 'tasklists'
1424 },
1425 columns => [
1426 {
1427 column => '*'
1428 }
1429 ],
1430 order_by => [
1431 {
1432 column => 'user_id'
1433 },
1434 {
1435 column => 'id',
1436 sort => 'desc'
1437 }
1438 ]
1439 );
1440  
1441 =item select example #8 output
1442  
1443 # SELECT * FROM "tasklists" ORDER BY "user_id", "id" DESC
1444  
1445 =item select example #9
1446  
1447 $sql->select(
1448 from => {
1449 table => 'tasks'
1450 },
1451 columns => [
1452 {
1453 column => '*'
1454 }
1455 ],
1456 rows => {
1457 limit => 5
1458 }
1459 );
1460  
1461 =item select example #9 output
1462  
1463 # SELECT * FROM "tasks" LIMIT 5
1464  
1465 =item select example #10
1466  
1467 $sql->select(
1468 from => {
1469 table => 'tasks'
1470 },
1471 columns => [
1472 {
1473 column => '*'
1474 }
1475 ],
1476 rows => {
1477 limit => 5,
1478 offset => 1
1479 }
1480 );
1481  
1482 =item select example #10 output
1483  
1484 # SELECT * FROM "tasks" LIMIT 5, OFFSET 1
1485  
1486 =item select example #11
1487  
1488 $sql->select(
1489 from => [
1490 {
1491 table => 'tasklists',
1492 alias => 't1'
1493 },
1494 {
1495 table => 'tasks',
1496 alias => 't2'
1497 }
1498 ],
1499 columns => [
1500 {
1501 column => '*',
1502 alias => 't1'
1503 },
1504 {
1505 column => '*',
1506 alias => 't1'
1507 }
1508 ],
1509 where => [
1510 {
1511 eq => [
1512 {
1513 column => 'tasklist_id',
1514 alias => 't2'
1515 },
1516 {
1517 column => 'id',
1518 alias => 't1'
1519 }
1520 ]
1521 }
1522 ]
1523 );
1524  
1525 =item select example #11 output
1526  
1527 # SELECT "t1".*, "t1".* FROM "tasklists" "t1", "tasks" "t2"
1528 # WHERE "t2"."tasklist_id" = "t1"."id"
1529  
1530 =back
1531  
1532 =cut
1533  
1534 =head2 insert
1535  
1536 =over 4
1537  
1538 =item insert example #1
1539  
1540 $sql->insert(
1541 into => {
1542 table => 'users'
1543 },
1544 values => [
1545 {
1546 value => undef
1547 },
1548 {
1549 value => 'Rob Zombie'
1550 },
1551 {
1552 value => {
1553 function => ['now']
1554 }
1555 },
1556 {
1557 value => {
1558 function => ['now']
1559 }
1560 },
1561 {
1562 value => {
1563 function => ['now']
1564 }
1565 }
1566 ]
1567 );
1568  
1569 =item insert example #1 output
1570  
1571 # INSERT INTO "users" VALUES (NULL, 'Rob Zombie', now(), now(), now())
1572  
1573 =item insert example #2
1574  
1575 $sql->insert(
1576 into => {
1577 table => 'users'
1578 },
1579 columns => [
1580 {
1581 column => 'id'
1582 },
1583 {
1584 column => 'name'
1585 },
1586 {
1587 column => 'created'
1588 },
1589 {
1590 column => 'updated'
1591 },
1592 {
1593 column => 'deleted'
1594 }
1595 ],
1596 values => [
1597 {
1598 value => undef
1599 },
1600 {
1601 value => 'Rob Zombie'
1602 },
1603 {
1604 value => {
1605 function => ['now']
1606 }
1607 },
1608 {
1609 value => {
1610 function => ['now']
1611 }
1612 },
1613 {
1614 value => {
1615 function => ['now']
1616 }
1617 }
1618 ]
1619 );
1620  
1621 =item insert example #2 output
1622  
1623 # INSERT INTO "users" ("id", "name", "created", "updated", "deleted")
1624 # VALUES (NULL, 'Rob Zombie', now(), now(), now())
1625  
1626 =item insert example #3
1627  
1628 $sql->insert(
1629 into => {
1630 table => 'users'
1631 },
1632 default => 1
1633 );
1634  
1635 =item insert example #3 output
1636  
1637 # INSERT INTO "users" DEFAULT VALUES
1638  
1639 =item insert example #4
1640  
1641 $sql->insert(
1642 into => {
1643 table => 'users'
1644 },
1645 columns => [
1646 {
1647 column => 'name'
1648 },
1649 {
1650 column => 'user_id'
1651 }
1652 ],
1653 query => {
1654 select => {
1655 from => {
1656 table => 'users'
1657 },
1658 columns => [
1659 {
1660 column => 'name'
1661 },
1662 {
1663 column => 'id'
1664 }
1665 ]
1666 }
1667 }
1668 );
1669  
1670 =item insert example #4 output
1671  
1672 # INSERT INTO "users" ("name", "user_id") SELECT "name", "id" FROM "users"
1673  
1674 =item insert example #5
1675  
1676 $sql->insert(
1677 into => {
1678 table => 'users'
1679 },
1680 columns => [
1681 {
1682 column => 'name'
1683 },
1684 {
1685 column => 'user_id'
1686 }
1687 ],
1688 query => {
1689 select => {
1690 from => {
1691 table => 'users'
1692 },
1693 columns => [
1694 {
1695 column => 'name'
1696 },
1697 {
1698 column => 'id'
1699 }
1700 ],
1701 where => [
1702 {
1703 'not-null' => {
1704 column => 'deleted'
1705 }
1706 }
1707 ]
1708 }
1709 }
1710 );
1711  
1712 =item insert example #5 output
1713  
1714 # INSERT INTO "users" ("name", "user_id") SELECT "name", "id" FROM "users"
1715 # WHERE "deleted" IS NOT NULL
1716  
1717 =back
1718  
1719 =cut
1720  
1721 =head2 update
1722  
1723 =over 4
1724  
1725 =item update example #1
1726  
1727 $sql->update(
1728 for => {
1729 table => 'users'
1730 },
1731 columns => [
1732 {
1733 column => 'updated',
1734 value => { function => ['now'] }
1735 }
1736 ]
1737 );
1738  
1739 =item update example #1 output
1740  
1741 # UPDATE "users" SET "updated" = now()
1742  
1743 =item update example #2
1744  
1745 $sql->update(
1746 for => {
1747 table => 'users'
1748 },
1749 columns => [
1750 {
1751 column => 'name',
1752 value => { function => ['concat', '[deleted]', ' ', { column => 'name' }] }
1753 }
1754 ],
1755 where => [
1756 {
1757 'not-null' => {
1758 column => 'deleted'
1759 }
1760 }
1761 ]
1762 );
1763  
1764 =item update example #2 output
1765  
1766 # UPDATE "users" SET "name" = concat('[deleted]', ' ', "name") WHERE
1767 # "deleted" IS NOT NULL
1768  
1769 =item update example #3
1770  
1771 $sql->update(
1772 for => {
1773 table => 'users',
1774 alias => 'u1'
1775 },
1776 columns => [
1777 {
1778 column => 'updated',
1779 alias => 'u1',
1780 value => { function => ['now'] }
1781 }
1782 ],
1783 where => [
1784 {
1785 in => [
1786 {
1787 column => 'id',
1788 alias => 'u1'
1789 },
1790 {
1791 subquery => {
1792 select => {
1793 from => {
1794 table => 'users',
1795 alias => 'u2'
1796 },
1797 columns => [
1798 {
1799 column => 'id',
1800 alias => 'u2'
1801 }
1802 ],
1803 joins => [
1804 {
1805 with => {
1806 table => 'tasklists',
1807 alias => 't1'
1808 },
1809 having => [
1810 {
1811 eq => [
1812 {
1813 column => 'id',
1814 alias => 'u2'
1815 },
1816 {
1817 column => 'user_id',
1818 alias => 't1'
1819 }
1820 ]
1821 }
1822 ]
1823 }
1824 ],
1825 where => [
1826 {
1827 eq => [
1828 {
1829 column => 'id',
1830 alias => 'u2'
1831 },
1832 {
1833 binding => 'user_id'
1834 }
1835 ]
1836 }
1837 ]
1838 }
1839 }
1840 }
1841 ]
1842 }
1843 ]
1844 );
1845  
1846 =item update example #3 output
1847  
1848 # UPDATE "users" "u1" SET "u1"."updated" = now() WHERE "u1"."id" IN (SELECT
1849 # "u2"."id" FROM "users" "u2" JOIN "tasklists" "t1" ON "u2"."id" =
1850 # "t1"."user_id" WHERE "u2"."id" = ?)
1851  
1852 =back
1853  
1854 =cut
1855  
1856 =head2 delete
1857  
1858 =over 4
1859  
1860 =item delete example #1
1861  
1862 $sql->delete(
1863 from => {
1864 table => 'tasklists'
1865 }
1866 );
1867  
1868 =item delete example #1 output
1869  
1870 # DELETE FROM "tasklists"
1871  
1872 =item delete example #2
1873  
1874 $sql->delete(
1875 from => {
1876 table => 'tasklists'
1877 },
1878 where => [
1879 {
1880 'not-null' => {
1881 column => 'deleted'
1882 }
1883 }
1884 ]
1885 );
1886  
1887 =item delete example #2 output
1888  
1889 # DELETE FROM "tasklists" WHERE "deleted" IS NOT NULL
1890  
1891 =back
1892  
1893 =cut
1894  
1895 =head2 table-create
1896  
1897 =over 4
1898  
1899 =item table-create example #1
1900  
1901 $sql->table_create(
1902 name => 'users',
1903 columns => [
1904 {
1905 name => 'id',
1906 type => 'integer',
1907 primary => 1
1908 }
1909 ]
1910 );
1911  
1912 =item table-create example #1 output
1913  
1914 # CREATE TABLE "users" ("id" integer PRIMARY KEY)
1915  
1916 =item table-create example #2
1917  
1918 $sql->table_create(
1919 name => 'users',
1920 columns => [
1921 {
1922 name => 'id',
1923 type => 'integer',
1924 primary => 1
1925 },
1926 {
1927 name => 'name',
1928 type => 'text',
1929 },
1930 {
1931 name => 'created',
1932 type => 'datetime',
1933 },
1934 {
1935 name => 'updated',
1936 type => 'datetime',
1937 },
1938 {
1939 name => 'deleted',
1940 type => 'datetime',
1941 },
1942 ]
1943 );
1944  
1945 =item table-create example #2 output
1946  
1947 # CREATE TABLE "users" ("id" integer PRIMARY KEY, "name" text, "created"
1948 # timestamp(0) without time zone, "updated" timestamp(0) without time zone,
1949 # "deleted" timestamp(0) without time zone)
1950  
1951 =item table-create example #3
1952  
1953 $sql->table_create(
1954 name => 'users',
1955 columns => [
1956 {
1957 name => 'id',
1958 type => 'integer',
1959 primary => 1
1960 },
1961 {
1962 name => 'name',
1963 type => 'text',
1964 },
1965 {
1966 name => 'created',
1967 type => 'datetime',
1968 },
1969 {
1970 name => 'updated',
1971 type => 'datetime',
1972 },
1973 {
1974 name => 'deleted',
1975 type => 'datetime',
1976 },
1977 ],
1978 temp => 1
1979 );
1980  
1981 =item table-create example #3 output
1982  
1983 # CREATE TEMPORARY TABLE "users" ("id" integer PRIMARY KEY, "name" text,
1984 # "created" timestamp(0) without time zone, "updated" timestamp(0) without
1985 # time zone, "deleted" timestamp(0) without time zone)
1986  
1987 =item table-create example #4
1988  
1989 $sql->table_create(
1990 name => 'people',
1991 query => {
1992 select => {
1993 from => {
1994 table => 'users'
1995 },
1996 columns => [
1997 {
1998 column => '*'
1999 }
2000 ]
2001 }
2002 }
2003 );
2004  
2005 =item table-create example #4 output
2006  
2007 # CREATE TABLE "people" AS SELECT * FROM "users"
2008  
2009 =back
2010  
2011 =cut
2012  
2013 =head2 table-drop
2014  
2015 =over 4
2016  
2017 =item table-drop example #1
2018  
2019 $sql->table_drop(
2020 name => 'people'
2021 );
2022  
2023 =item table-drop example #1 output
2024  
2025 # DROP TABLE "people"
2026  
2027 =item table-drop example #2
2028  
2029 $sql->table_drop(
2030 name => 'people',
2031 condition => 'cascade'
2032 );
2033  
2034 =item table-drop example #2 output
2035  
2036 # DROP TABLE "people" CASCADE
2037  
2038 =back
2039  
2040 =cut
2041  
2042 =head2 table-rename
2043  
2044 =over 4
2045  
2046 =item table-rename example #1
2047  
2048 $sql->table_rename(
2049 name => {
2050 old => 'peoples',
2051 new => 'people'
2052 }
2053 );
2054  
2055 =item table-rename example #1 output
2056  
2057 # ALTER TABLE "peoples" RENAME TO "people"
2058  
2059 =back
2060  
2061 =cut
2062  
2063 =head2 index-create
2064  
2065 =over 4
2066  
2067 =item index-create example #1
2068  
2069 $sql->index_create(
2070 for => {
2071 table => 'users'
2072 },
2073 columns => [
2074 {
2075 column => 'name'
2076 }
2077 ]
2078 );
2079  
2080 =item index-create example #1 output
2081  
2082 # CREATE INDEX "index_users_name" ON "users" ("name")
2083  
2084 =item index-create example #2
2085  
2086 $sql->index_create(
2087 for => {
2088 table => 'users'
2089 },
2090 columns => [
2091 {
2092 column => 'email'
2093 }
2094 ],
2095 unique => 1
2096 );
2097  
2098 =item index-create example #2 output
2099  
2100 # CREATE UNIQUE INDEX "unique_users_email" ON "users" ("email")
2101  
2102 =item index-create example #3
2103  
2104 $sql->index_create(
2105 for => {
2106 table => 'users'
2107 },
2108 columns => [
2109 {
2110 column => 'name'
2111 }
2112 ],
2113 name => 'user_name_index'
2114 );
2115  
2116 =item index-create example #3 output
2117  
2118 # CREATE INDEX "user_name_index" ON "users" ("name")
2119  
2120 =item index-create example #4
2121  
2122 $sql->index_create(
2123 for => {
2124 table => 'users'
2125 },
2126 columns => [
2127 {
2128 column => 'email'
2129 }
2130 ],
2131 name => 'user_email_unique',
2132 unique => 1
2133 );
2134  
2135 =item index-create example #4 output
2136  
2137 # CREATE UNIQUE INDEX "user_email_unique" ON "users" ("email")
2138  
2139 =item index-create example #5
2140  
2141 $sql->index_create(
2142 for => {
2143 table => 'users'
2144 },
2145 columns => [
2146 {
2147 column => 'login'
2148 },
2149 {
2150 column => 'email'
2151 }
2152 ]
2153 );
2154  
2155 =item index-create example #5 output
2156  
2157 # CREATE INDEX "index_users_login_email" ON "users" ("login", "email")
2158  
2159 =back
2160  
2161 =cut
2162  
2163 =head2 index-drop
2164  
2165 =over 4
2166  
2167 =item index-drop example #1
2168  
2169 $sql->index_drop(
2170 for => {
2171 table => 'users'
2172 },
2173 columns => [
2174 {
2175 column => 'name'
2176 }
2177 ]
2178 );
2179  
2180 =item index-drop example #1 output
2181  
2182 # DROP INDEX "index_users_name"
2183  
2184 =item index-drop example #2
2185  
2186 $sql->index_drop(
2187 for => {
2188 table => 'users'
2189 },
2190 columns => [
2191 {
2192 column => 'email'
2193 }
2194 ],
2195 unique => 1
2196 );
2197  
2198 =item index-drop example #2 output
2199  
2200 # DROP INDEX "unique_users_email"
2201  
2202 =item index-drop example #3
2203  
2204 $sql->index_drop(
2205 for => {
2206 table => 'users'
2207 },
2208 columns => [
2209 {
2210 column => 'name'
2211 }
2212 ],
2213 name => 'user_email_unique'
2214 );
2215  
2216 =item index-drop example #3 output
2217  
2218 # DROP INDEX "user_email_unique"
2219  
2220 =back
2221  
2222 =cut
2223  
2224 =head2 column-change
2225  
2226 =over 4
2227  
2228 =item column-change example #1
2229  
2230 $sql->column_change(
2231 for => {
2232 table => 'users'
2233 },
2234 column => {
2235 name => 'accessed',
2236 type => 'datetime',
2237 nullable => 1
2238 }
2239 );
2240  
2241 =item column-change example #1 output
2242  
2243 # BEGIN TRANSACTION
2244 # ALTER TABLE "users" ALTER "accessed" TYPE timestamp(0) without time zone
2245 # ALTER TABLE "users" ALTER "accessed" DROP NOT NULL
2246 # ALTER TABLE "users" ALTER "accessed" DROP DEFAULT
2247 # COMMIT
2248  
2249 =item column-change example #2
2250  
2251 $sql->column_change(
2252 for => {
2253 table => 'users'
2254 },
2255 column => {
2256 name => 'accessed',
2257 type => 'datetime',
2258 default => { function => ['now'] }
2259 }
2260 );
2261  
2262 =item column-change example #2 output
2263  
2264 # BEGIN TRANSACTION
2265 # ALTER TABLE "users" ALTER "accessed" TYPE timestamp(0) without time zone
2266 # ALTER TABLE "users" ALTER "accessed" SET DEFAULT now()
2267 # COMMIT
2268  
2269 =item column-change example #3
2270  
2271 $sql->column_change(
2272 for => {
2273 table => 'users'
2274 },
2275 column => {
2276 name => 'accessed',
2277 type => 'datetime',
2278 default => { function => ['now'] },
2279 nullable => 1,
2280 }
2281 );
2282  
2283 =item column-change example #3 output
2284  
2285 # BEGIN TRANSACTION
2286 # ALTER TABLE "users" ALTER "accessed" TYPE timestamp(0) without time zone
2287 # ALTER TABLE "users" ALTER "accessed" DROP NOT NULL
2288 # ALTER TABLE "users" ALTER "accessed" SET DEFAULT now()
2289 # COMMIT
2290  
2291 =back
2292  
2293 =cut
2294  
2295 =head2 column-create
2296  
2297 =over 4
2298  
2299 =item column-create example #1
2300  
2301 $sql->column_create(
2302 for => {
2303 table => 'users'
2304 },
2305 column => {
2306 name => 'accessed',
2307 type => 'datetime'
2308 }
2309 );
2310  
2311 =item column-create example #1 output
2312  
2313 # ALTER TABLE "users" ADD COLUMN "accessed" timestamp(0) without time zone
2314  
2315 =item column-create example #2
2316  
2317 $sql->column_create(
2318 for => {
2319 table => 'users'
2320 },
2321 column => {
2322 name => 'accessed',
2323 type => 'datetime',
2324 nullable => 1
2325 }
2326 );
2327  
2328 =item column-create example #2 output
2329  
2330 # ALTER TABLE "users" ADD COLUMN "accessed" timestamp(0) without time zone
2331 # NULL
2332  
2333 =item column-create example #3
2334  
2335 $sql->column_create(
2336 for => {
2337 table => 'users'
2338 },
2339 column => {
2340 name => 'accessed',
2341 type => 'datetime',
2342 nullable => 1,
2343 default => {
2344 function => ['now']
2345 }
2346 }
2347 );
2348  
2349 =item column-create example #3 output
2350  
2351 # ALTER TABLE "users" ADD COLUMN "accessed" timestamp(0) without time zone
2352 # NULL DEFAULT now()
2353  
2354 =item column-create example #4
2355  
2356 $sql->column_create(
2357 for => {
2358 table => 'users'
2359 },
2360 column => {
2361 name => 'ref',
2362 type => 'uuid',
2363 primary => 1
2364 }
2365 );
2366  
2367 =item column-create example #4 output
2368  
2369 # ALTER TABLE "users" ADD COLUMN "ref" uuid PRIMARY KEY
2370  
2371 =back
2372  
2373 =cut
2374  
2375 =head2 column-drop
2376  
2377 =over 4
2378  
2379 =item column-drop example #1
2380  
2381 $sql->column_drop(
2382 table => 'users',
2383 column => 'accessed'
2384 );
2385  
2386 =item column-drop example #1 output
2387  
2388 # ALTER TABLE "users" DROP COLUMN "accessed"
2389  
2390 =back
2391  
2392 =cut
2393  
2394 =head2 column-rename
2395  
2396 =over 4
2397  
2398 =item column-rename example #1
2399  
2400 $sql->column_rename(
2401 for => {
2402 table => 'users'
2403 },
2404 name => {
2405 old => 'accessed',
2406 new => 'accessed_at'
2407 }
2408 );
2409  
2410 =item column-rename example #1 output
2411  
2412 # ALTER TABLE "users" RENAME COLUMN "accessed" TO "accessed_at"
2413  
2414 =back
2415  
2416 =cut
2417  
2418 =head2 constraint-create
2419  
2420 =over 4
2421  
2422 =item constraint-create example #1
2423  
2424 $sql->constraint_create(
2425 source => {
2426 table => 'users',
2427 column => 'profile_id'
2428 },
2429 target => {
2430 table => 'profiles',
2431 column => 'id'
2432 }
2433 );
2434  
2435 =item constraint-create example #1 output
2436  
2437 # ALTER TABLE "users" ADD CONSTRAINT "foreign_users_profile_id_profiles_id"
2438 # FOREIGN KEY ("profile_id") REFERENCES "profiles" ("id")
2439  
2440 =item constraint-create example #2
2441  
2442 $sql->constraint_create(
2443 source => {
2444 table => 'users',
2445 column => 'profile_id'
2446 },
2447 target => {
2448 table => 'profiles',
2449 column => 'id'
2450 },
2451 name => 'user_profile_id'
2452 );
2453  
2454 =item constraint-create example #2 output
2455  
2456 # ALTER TABLE "users" ADD CONSTRAINT "user_profile_id" FOREIGN KEY
2457 # ("profile_id") REFERENCES "profiles" ("id")
2458  
2459 =item constraint-create example #3
2460  
2461 $sql->constraint_create(
2462 on => {
2463 update => 'cascade',
2464 delete => 'cascade'
2465 },
2466 source => {
2467 table => 'users',
2468 column => 'profile_id'
2469 },
2470 target => {
2471 table => 'profiles',
2472 column => 'id'
2473 },
2474 name => 'user_profile_id'
2475 );
2476  
2477 =item constraint-create example #3 output
2478  
2479 # ALTER TABLE "users" ADD CONSTRAINT "user_profile_id" FOREIGN KEY
2480 # ("profile_id") REFERENCES "profiles" ("id") ON DELETE CASCADE ON UPDATE
2481 # CASCADE
2482  
2483 =back
2484  
2485 =cut
2486  
2487 =head2 constraint-drop
2488  
2489 =over 4
2490  
2491 =item constraint-drop example #1
2492  
2493 $sql->constraint_drop(
2494 source => {
2495 table => 'users',
2496 column => 'profile_id'
2497 },
2498 target => {
2499 table => 'profiles',
2500 column => 'id'
2501 }
2502 );
2503  
2504 =item constraint-drop example #1 output
2505  
2506 # ALTER TABLE "users" DROP CONSTRAINT "foreign_users_profile_id_profiles_id"
2507  
2508 =item constraint-drop example #2
2509  
2510 $sql->constraint_drop(
2511 source => {
2512 table => 'users',
2513 column => 'profile_id'
2514 },
2515 target => {
2516 table => 'profiles',
2517 column => 'id'
2518 },
2519 name => 'user_profile_id'
2520 );
2521  
2522 =item constraint-drop example #2 output
2523  
2524 # ALTER TABLE "users" DROP CONSTRAINT "user_profile_id"
2525  
2526 =back
2527  
2528 =cut
2529  
2530 =head2 database-create
2531  
2532 =over 4
2533  
2534 =item database-create example #1
2535  
2536 $sql->database_create(
2537 name => 'todoapp'
2538 );
2539  
2540 =item database-create example #1 output
2541  
2542 # CREATE DATABASE "todoapp"
2543  
2544 =back
2545  
2546 =cut
2547  
2548 =head2 database-drop
2549  
2550 =over 4
2551  
2552 =item database-drop example #1
2553  
2554 $sql->database_drop(
2555 name => 'todoapp'
2556 );
2557  
2558 =item database-drop example #1 output
2559  
2560 # DROP DATABASE "todoapp"
2561  
2562 =back
2563  
2564 =cut
2565  
2566 =head2 schema-create
2567  
2568 =over 4
2569  
2570 =item schema-create example #1
2571  
2572 $sql->schema_create(
2573 name => 'private',
2574 );
2575  
2576 =item schema-create example #1 output
2577  
2578 # CREATE SCHEMA "private"
2579  
2580 =back
2581  
2582 =cut
2583  
2584 =head2 schema-drop
2585  
2586 =over 4
2587  
2588 =item schema-drop example #1
2589  
2590 $sql->schema_drop(
2591 name => 'private',
2592 );
2593  
2594 =item schema-drop example #1 output
2595  
2596 # DROP SCHEMA "private"
2597  
2598 =back
2599  
2600 =cut
2601  
2602 =head2 schema-rename
2603  
2604 =over 4
2605  
2606 =item schema-rename example #1
2607  
2608 $sql->schema_rename(
2609 name => {
2610 old => 'private',
2611 new => 'restricted'
2612 }
2613 );
2614  
2615 =item schema-rename example #1 output
2616  
2617 # ALTER SCHEMA "private" RENAME TO "restricted"
2618  
2619 =back
2620  
2621 =cut
2622  
2623 =head2 transaction
2624  
2625 =over 4
2626  
2627 =item transaction example #1
2628  
2629 $sql->transaction(
2630 queries => [
2631 {
2632 'table-create' => {
2633 name => 'users',
2634 columns => [
2635 {
2636 name => 'id',
2637 type => 'integer',
2638 primary => 1
2639 }
2640 ]
2641 }
2642 }
2643 ]
2644 );
2645  
2646 =item transaction example #1 output
2647  
2648 # BEGIN TRANSACTION
2649 # CREATE TABLE "users" ("id" integer PRIMARY KEY)
2650 # COMMIT
2651  
2652 =item transaction example #2
2653  
2654 $sql->transaction(
2655 mode => [
2656 'exclusive'
2657 ],
2658 queries => [
2659 {
2660 'table-create' => {
2661 name => 'users',
2662 columns => [
2663 {
2664 name => 'id',
2665 type => 'integer',
2666 primary => 1
2667 }
2668 ]
2669 }
2670 }
2671 ]
2672 );
2673  
2674 =item transaction example #2 output
2675  
2676 # BEGIN TRANSACTION EXCLUSIVE
2677 # CREATE TABLE "users" ("id" integer PRIMARY KEY)
2678 # COMMIT
2679  
2680 =back
2681  
2682 =cut
2683  
2684 =head2 view-create
2685  
2686 =over 4
2687  
2688 =item view-create example #1
2689  
2690 $sql->view_create(
2691 name => 'active_users',
2692 query => {
2693 select => {
2694 from => {
2695 table => 'users'
2696 },
2697 columns => [
2698 {
2699 column => '*'
2700 }
2701 ],
2702 where => [
2703 {
2704 'not-null' => {
2705 column => 'deleted'
2706 }
2707 }
2708 ]
2709 }
2710 }
2711 );
2712  
2713 =item view-create example #1 output
2714  
2715 # CREATE VIEW "active_users" AS SELECT * FROM "users" WHERE "deleted" IS NOT
2716 # NULL
2717  
2718 =item view-create example #2
2719  
2720 $sql->view_create(
2721 name => 'active_users',
2722 query => {
2723 select => {
2724 from => {
2725 table => 'users'
2726 },
2727 columns => [
2728 {
2729 column => '*'
2730 }
2731 ],
2732 where => [
2733 {
2734 'not-null' => {
2735 column => 'deleted'
2736 }
2737 }
2738 ]
2739 }
2740 },
2741 temp => 1
2742 );
2743  
2744 =item view-create example #2 output
2745  
2746 # CREATE TEMPORARY VIEW "active_users" AS SELECT * FROM "users" WHERE
2747 # "deleted" IS NOT NULL
2748  
2749 =back
2750  
2751 =cut
2752  
2753 =head2 view-drop
2754  
2755 =over 4
2756  
2757 =item view-drop example #1
2758  
2759 $sql->view_drop(
2760 name => 'active_users'
2761 );
2762  
2763 =item view-drop example #1 output
2764  
2765 # DROP VIEW "active_users"
2766  
2767 =back
2768  
2769 =cut
2770  
2771 =head1 AUTHOR
2772  
2773 Al Newkirk, C<awncorp@cpan.org>
2774  
2775 =head1 LICENSE
2776  
2777 Copyright (C) 2011-2019, Al Newkirk, et al.
2778  
2779 This is free software; you can redistribute it and/or modify it under the terms
2780 of the The Apache License, Version 2.0, as elucidated in the L<"license
2781 file"|https://github.com/iamalnewkirk/sql-engine/blob/master/LICENSE>.
2782  
2783 =head1 PROJECT
2784  
2785 L<Wiki|https://github.com/iamalnewkirk/sql-engine/wiki>
2786  
2787 L<Project|https://github.com/iamalnewkirk/sql-engine>
2788  
2789 L<Initiatives|https://github.com/iamalnewkirk/sql-engine/projects>
2790  
2791 L<Milestones|https://github.com/iamalnewkirk/sql-engine/milestones>
2792  
2793 L<Contributing|https://github.com/iamalnewkirk/sql-engine/blob/master/CONTRIBUTE.md>
2794  
2795 L<Issues|https://github.com/iamalnewkirk/sql-engine/issues>
2796  
2797 =cut