Engine/Grammar/Postgres.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 143 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::Postgres;
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  
13 extends 'SQL::Engine::Grammar';
14  
15 our $VERSION = '0.02'; # VERSION
16  
17 # METHODS
18  
19 method column_change(HashRef $data) {
20 $self->operation($self->term(qw(begin transaction)));
21  
22 my $def = $self->column_definition($data->{column});
23  
24 # column type
25 $self->operation(do {
26 my $sql = [];
27  
28 # alter table
29 push @$sql, $self->term(qw(alter table));
30  
31 # safe
32 push @$sql, $self->term(qw(if exists)) if $data->{safe};
33  
34 # for
35 push @$sql, $self->table($data->{for});
36  
37 # alter column
38 push @$sql, $self->term('alter');
39  
40 # column name
41 push @$sql, $self->name($data->{column}{name});
42  
43 # column tyoe
44 push @$sql, $self->term('type'), $def->{type};
45  
46 # sql statement
47 join ' ', @$sql
48 });
49  
50 # column nullable
51 if (exists $data->{column}{nullable}) {
52 $self->operation(do {
53 my $sql = [];
54  
55 # alter table
56 push @$sql, $self->term(qw(alter table));
57  
58 # safe
59 push @$sql, $self->term(qw(if exists)) if $data->{safe};
60  
61 # for
62 push @$sql, $self->table($data->{for});
63  
64 # alter column
65 push @$sql, $self->term('alter');
66  
67 # column name
68 push @$sql, $self->name($data->{column}{name});
69  
70 # column (set | drop) not null
71 push @$sql,
72 $data->{column}{nullable}
73 ? ($self->term('drop'), $self->term(qw(not null)))
74 : ($self->term('set'), $self->term(qw(not null)));
75  
76 # sql statement
77 join ' ', @$sql
78 });
79 }
80  
81 # column default
82 $self->operation(do {
83 my $sql = [];
84  
85 # alter table
86 push @$sql, $self->term(qw(alter table));
87  
88 # safe
89 push @$sql, $self->term(qw(if exists)) if $data->{safe};
90  
91 # for
92 push @$sql, $self->table($data->{for});
93  
94 # alter column
95 push @$sql, $self->term('alter');
96  
97 # column name
98 push @$sql, $self->name($data->{column}{name});
99  
100 # column (set | drop) default
101 push @$sql,
102 $data->{column}{default}
103 ? ($self->term('set'), $def->{default})
104 : ($self->term('drop'), $self->term('default'));
105  
106 # sql statement
107 join ' ', @$sql
108 });
109  
110 $self->operation($self->term('commit'));
111  
112 return $self;
113 }
114  
115 method transaction(HashRef $data) {
116 my @mode;
117 if ($data->{mode}) {
118 @mode = map $self->term($_), @{$data->{mode}};
119 }
120 $self->operation($self->term('begin', 'transaction', @mode));
121 $self->process($_) for @{$data->{queries}};
122 $self->operation($self->term('commit'));
123  
124 return $self;
125 }
126  
127 method type_binary(HashRef $data) {
128  
129 return 'bytea';
130 }
131  
132 method type_boolean(HashRef $data) {
133  
134 return 'boolean';
135 }
136  
137 method type_char(HashRef $data) {
138 my $options = $data->{options} || [];
139  
140 return sprintf('char(%s)', $self->value($options->[0] || 1));
141 }
142  
143 method type_date(HashRef $data) {
144  
145 return 'date';
146 }
147  
148 method type_datetime(HashRef $data) {
149  
150 return 'timestamp(0) without time zone';
151 }
152  
153 method type_datetime_wtz(HashRef $data) {
154  
155 return 'timestamp(0) with time zone';
156 }
157  
158 method type_decimal(HashRef $data) {
159 my $options = $data->{options} || [];
160  
161 return sprintf(
162 'decimal(%s)',
163 join(', ',
164 $self->value($options->[0]) || 5,
165 $self->value($options->[1]) || 2)
166 );
167 }
168  
169 method type_double(HashRef $data) {
170  
171 return 'double precision';
172 }
173  
174 method type_enum(HashRef $data) {
175 my $column = $data->{name};
176 my $options = $data->{options};
177  
178 return sprintf('varchar(225) check (%s in (%s))',
179 $column, join(', ', map $self->value($_), @$options));
180 }
181  
182 method type_float(HashRef $data) {
183  
184 return 'double precision';
185 }
186  
187 method type_integer(HashRef $data) {
188  
189 return $data->{increment} ? 'serial' : 'integer';
190 }
191  
192 method type_integer_big(HashRef $data) {
193  
194 return $data->{increment} ? 'bigserial' : 'bigint';
195 }
196  
197 method type_integer_big_unsigned(HashRef $data) {
198  
199 return $self->type_integer_big($data);
200 }
201  
202 method type_integer_medium(HashRef $data) {
203  
204 return $data->{increment} ? 'serial' : 'integer';
205 }
206  
207 method type_integer_medium_unsigned(HashRef $data) {
208  
209 return $self->type_integer_medium($data);
210 }
211  
212 method type_integer_small(HashRef $data) {
213  
214 return $data->{increment} ? 'smallserial' : 'smallint';
215 }
216  
217 method type_integer_small_unsigned(HashRef $data) {
218  
219 return $self->type_integer_small($data);
220 }
221  
222 method type_integer_tiny(HashRef $data) {
223  
224 return $data->{increment} ? 'smallserial' : 'smallint';
225 }
226  
227 method type_integer_tiny_unsigned(HashRef $data) {
228  
229 return $self->type_integer_tiny($data);
230 }
231  
232 method type_integer_unsigned(HashRef $data) {
233  
234 return $self->type_integer($data);
235 }
236  
237 method type_json(HashRef $data) {
238  
239 return 'json';
240 }
241  
242 method type_number(HashRef $data) {
243  
244 return $self->type_integer($data);
245 }
246  
247 method type_string(HashRef $data) {
248 my $options = $data->{options} || [];
249  
250 return sprintf('varchar(%s)', $options->[0] || 255);
251 }
252  
253 method type_text(HashRef $data) {
254  
255 return 'text';
256 }
257  
258 method type_text_long(HashRef $data) {
259  
260 return 'text';
261 }
262  
263 method type_text_medium(HashRef $data) {
264  
265 return 'text';
266 }
267  
268 method type_time(HashRef $data) {
269  
270 return 'time(0) without time zone';
271 }
272  
273 method type_time_wtz(HashRef $data) {
274  
275 return 'time(0) with time zone';
276 }
277  
278 method type_timestamp(HashRef $data) {
279  
280 return 'timestamp(0) without time zone';
281 }
282  
283 method type_timestamp_wtz(HashRef $data) {
284  
285 return 'timestamp(0) with time zone';
286 }
287  
288 method type_uuid(HashRef $data) {
289  
290 return 'uuid';
291 }
292  
293 method wrap(Str $name) {
294  
295 return qq("$name");
296 }
297  
298 1;
299  
300 =encoding utf8
301  
302 =head1 NAME
303  
304 SQL::Engine::Grammar::Postgres - Grammar For PostgreSQL
305  
306 =cut
307  
308 =head1 ABSTRACT
309  
310 SQL::Engine Grammar For PostgreSQL
311  
312 =cut
313  
314 =head1 SYNOPSIS
315  
316 use SQL::Engine::Grammar::Postgres;
317  
318 my $grammar = SQL::Engine::Grammar::Postgres->new(
319 schema => {
320 select => {
321 from => {
322 table => 'users'
323 },
324 columns => [
325 {
326 column => '*'
327 }
328 ]
329 }
330 }
331 );
332  
333 # $grammar->execute;
334  
335 =cut
336  
337 =head1 DESCRIPTION
338  
339 This package provides methods for converting
340 L<json-sql|https://github.com/iamalnewkirk/json-sql> data structures into
341 PostgreSQL statements.
342  
343 =cut
344  
345 =head1 INHERITS
346  
347 This package inherits behaviors from:
348  
349 L<SQL::Engine::Grammar>
350  
351 =cut
352  
353 =head1 LIBRARIES
354  
355 This package uses type constraints from:
356  
357 L<Types::Standard>
358  
359 =cut
360  
361 =head1 METHODS
362  
363 This package implements the following methods:
364  
365 =cut
366  
367 =head2 column_change
368  
369 column_change(HashRef $data) : Object
370  
371 The column_change method generates SQL statements to change a column
372 definition.
373  
374 =over 4
375  
376 =item column_change example #1
377  
378 my $grammar = SQL::Engine::Grammar::Postgres->new(
379 schema => {
380 'column-change' => {
381 for => {
382 table => 'users'
383 },
384 column => {
385 name => 'accessed',
386 type => 'datetime',
387 nullable => 1
388 }
389 }
390 }
391 );
392  
393 $grammar->column_change($grammar->schema->{'column-change'});
394  
395 =back
396  
397 =cut
398  
399 =head2 transaction
400  
401 transaction(HashRef $data) : Object
402  
403 The transaction method generates SQL statements to commit an atomic database
404 transaction.
405  
406 =over 4
407  
408 =item transaction example #1
409  
410 my $grammar = SQL::Engine::Grammar::Postgres->new(
411 schema => {
412 'transaction' => {
413 queries => [
414 {
415 'table-create' => {
416 name => 'users',
417 columns => [
418 {
419 name => 'id',
420 type => 'integer',
421 primary => 1
422 }
423 ]
424 }
425 }
426 ]
427 }
428 }
429 );
430  
431 $grammar->transaction($grammar->schema->{'transaction'});
432  
433 =back
434  
435 =cut
436  
437 =head2 type_binary
438  
439 type_binary(HashRef $data) : Str
440  
441 The type_binary method returns the SQL expression representing a binary data
442 type.
443  
444 =over 4
445  
446 =item type_binary example #1
447  
448 # given: synopsis
449  
450 $grammar->type_binary({});
451  
452 # bytea
453  
454 =back
455  
456 =cut
457  
458 =head2 type_boolean
459  
460 type_boolean(HashRef $data) : Str
461  
462 The type_boolean method returns the SQL expression representing a boolean data type.
463  
464 =over 4
465  
466 =item type_boolean example #1
467  
468 # given: synopsis
469  
470 $grammar->type_boolean({});
471  
472 # boolean
473  
474 =back
475  
476 =cut
477  
478 =head2 type_char
479  
480 type_char(HashRef $data) : Str
481  
482 The type_char method returns the SQL expression representing a char data type.
483  
484 =over 4
485  
486 =item type_char example #1
487  
488 # given: synopsis
489  
490 $grammar->type_char({});
491  
492 # char(1)
493  
494 =back
495  
496 =cut
497  
498 =head2 type_date
499  
500 type_date(HashRef $data) : Str
501  
502 The type_date method returns the SQL expression representing a date data type.
503  
504 =over 4
505  
506 =item type_date example #1
507  
508 # given: synopsis
509  
510 $grammar->type_date({});
511  
512 # date
513  
514 =back
515  
516 =cut
517  
518 =head2 type_datetime
519  
520 type_datetime(HashRef $data) : Str
521  
522 The type_datetime method returns the SQL expression representing a datetime
523 data type.
524  
525 =over 4
526  
527 =item type_datetime example #1
528  
529 # given: synopsis
530  
531 $grammar->type_datetime({});
532  
533 # timestamp(0) without time zone
534  
535 =back
536  
537 =cut
538  
539 =head2 type_datetime_wtz
540  
541 type_datetime_wtz(HashRef $data) : Str
542  
543 The type_datetime_wtz method returns the SQL expression representing a datetime
544 (and timezone) data type.
545  
546 =over 4
547  
548 =item type_datetime_wtz example #1
549  
550 # given: synopsis
551  
552 $grammar->type_datetime_wtz({});
553  
554 # timestamp(0) with time zone
555  
556 =back
557  
558 =cut
559  
560 =head2 type_decimal
561  
562 type_decimal(HashRef $data) : Str
563  
564 The type_decimal method returns the SQL expression representing a decimal data
565 type.
566  
567 =over 4
568  
569 =item type_decimal example #1
570  
571 # given: synopsis
572  
573 $grammar->type_decimal({});
574  
575 # decimal(NULL, NULL)
576  
577 =back
578  
579 =cut
580  
581 =head2 type_double
582  
583 type_double(HashRef $data) : Str
584  
585 The type_double method returns the SQL expression representing a double data
586 type.
587  
588 =over 4
589  
590 =item type_double example #1
591  
592 # given: synopsis
593  
594 $grammar->type_double({});
595  
596 # double precision
597  
598 =back
599  
600 =cut
601  
602 =head2 type_enum
603  
604 type_enum(HashRef $data) : Str
605  
606 The type_enum method returns the SQL expression representing a enum data type.
607  
608 =over 4
609  
610 =item type_enum example #1
611  
612 # given: synopsis
613  
614 $grammar->type_enum({ name => 'theme', options => ['light', 'dark'] });
615  
616 # varchar(225) check ("theme" in ('light', 'dark'))
617  
618 =back
619  
620 =cut
621  
622 =head2 type_float
623  
624 type_float(HashRef $data) : Str
625  
626 The type_float method returns the SQL expression representing a float data
627 type.
628  
629 =over 4
630  
631 =item type_float example #1
632  
633 # given: synopsis
634  
635 $grammar->type_float({});
636  
637 # double precision
638  
639 =back
640  
641 =cut
642  
643 =head2 type_integer
644  
645 type_integer(HashRef $data) : Str
646  
647 The type_integer method returns the SQL expression representing a integer data
648 type.
649  
650 =over 4
651  
652 =item type_integer example #1
653  
654 # given: synopsis
655  
656 $grammar->type_integer({});
657  
658 # integer
659  
660 =back
661  
662 =cut
663  
664 =head2 type_integer_big
665  
666 type_integer_big(HashRef $data) : Str
667  
668 The type_integer_big method returns the SQL expression representing a
669 big-integer data type.
670  
671 =over 4
672  
673 =item type_integer_big example #1
674  
675 # given: synopsis
676  
677 $grammar->type_integer_big({});
678  
679 # bigint
680  
681 =back
682  
683 =cut
684  
685 =head2 type_integer_big_unsigned
686  
687 type_integer_big_unsigned(HashRef $data) : Str
688  
689 The type_integer_big_unsigned method returns the SQL expression representing a
690 big unsigned integer data type.
691  
692 =over 4
693  
694 =item type_integer_big_unsigned example #1
695  
696 # given: synopsis
697  
698 $grammar->type_integer_big_unsigned({});
699  
700 # bigint
701  
702 =back
703  
704 =cut
705  
706 =head2 type_integer_medium
707  
708 type_integer_medium(HashRef $data) : Str
709  
710 The type_integer_medium method returns the SQL expression representing a medium
711 integer data type.
712  
713 =over 4
714  
715 =item type_integer_medium example #1
716  
717 # given: synopsis
718  
719 $grammar->type_integer_medium({});
720  
721 # integer
722  
723 =back
724  
725 =cut
726  
727 =head2 type_integer_medium_unsigned
728  
729 type_integer_medium_unsigned(HashRef $data) : Str
730  
731 The type_integer_medium_unsigned method returns the SQL expression representing
732 a unsigned medium integer data type.
733  
734 =over 4
735  
736 =item type_integer_medium_unsigned example #1
737  
738 # given: synopsis
739  
740 $grammar->type_integer_medium_unsigned({});
741  
742 # integer
743  
744 =back
745  
746 =cut
747  
748 =head2 type_integer_small
749  
750 type_integer_small(HashRef $data) : Str
751  
752 The type_integer_small method returns the SQL expression representing a small
753 integer data type.
754  
755 =over 4
756  
757 =item type_integer_small example #1
758  
759 # given: synopsis
760  
761 $grammar->type_integer_small({});
762  
763 # smallint
764  
765 =back
766  
767 =cut
768  
769 =head2 type_integer_small_unsigned
770  
771 type_integer_small_unsigned(HashRef $data) : Str
772  
773 The type_integer_small_unsigned method returns the SQL expression representing
774 a unsigned small integer data type.
775  
776 =over 4
777  
778 =item type_integer_small_unsigned example #1
779  
780 # given: synopsis
781  
782 $grammar->type_integer_small_unsigned({});
783  
784 # smallint
785  
786 =back
787  
788 =cut
789  
790 =head2 type_integer_tiny
791  
792 type_integer_tiny(HashRef $data) : Str
793  
794 The type_integer_tiny method returns the SQL expression representing a tiny
795 integer data type.
796  
797 =over 4
798  
799 =item type_integer_tiny example #1
800  
801 # given: synopsis
802  
803 $grammar->type_integer_tiny({});
804  
805 # smallint
806  
807 =back
808  
809 =cut
810  
811 =head2 type_integer_tiny_unsigned
812  
813 type_integer_tiny_unsigned(HashRef $data) : Str
814  
815 The type_integer_tiny_unsigned method returns the SQL expression representing a
816 unsigned tiny integer data type.
817  
818 =over 4
819  
820 =item type_integer_tiny_unsigned example #1
821  
822 # given: synopsis
823  
824 $grammar->type_integer_tiny_unsigned({});
825  
826 # smallint
827  
828 =back
829  
830 =cut
831  
832 =head2 type_integer_unsigned
833  
834 type_integer_unsigned(HashRef $data) : Str
835  
836 The type_integer_unsigned method returns the SQL expression representing a
837 unsigned integer data type.
838  
839 =over 4
840  
841 =item type_integer_unsigned example #1
842  
843 # given: synopsis
844  
845 $grammar->type_integer_unsigned({});
846  
847 # integer
848  
849 =back
850  
851 =cut
852  
853 =head2 type_json
854  
855 type_json(HashRef $data) : Str
856  
857 The type_json method returns the SQL expression representing a json data type.
858  
859 =over 4
860  
861 =item type_json example #1
862  
863 # given: synopsis
864  
865 $grammar->type_json({});
866  
867 # json
868  
869 =back
870  
871 =cut
872  
873 =head2 type_number
874  
875 type_number(HashRef $data) : Str
876  
877 The type_number method returns the SQL expression representing a number data
878 type.
879  
880 =over 4
881  
882 =item type_number example #1
883  
884 # given: synopsis
885  
886 $grammar->type_number({});
887  
888 # integer
889  
890 =back
891  
892 =cut
893  
894 =head2 type_string
895  
896 type_string(HashRef $data) : Str
897  
898 The type_string method returns the SQL expression representing a string data
899 type.
900  
901 =over 4
902  
903 =item type_string example #1
904  
905 # given: synopsis
906  
907 $grammar->type_string({});
908  
909 # varchar(255)
910  
911 =back
912  
913 =cut
914  
915 =head2 type_text
916  
917 type_text(HashRef $data) : Str
918  
919 The type_text method returns the SQL expression representing a text data type.
920  
921 =over 4
922  
923 =item type_text example #1
924  
925 # given: synopsis
926  
927 $grammar->type_text({});
928  
929 # text
930  
931 =back
932  
933 =cut
934  
935 =head2 type_text_long
936  
937 type_text_long(HashRef $data) : Str
938  
939 The type_text_long method returns the SQL expression representing a long text
940 data type.
941  
942 =over 4
943  
944 =item type_text_long example #1
945  
946 # given: synopsis
947  
948 $grammar->type_text_long({});
949  
950 # text
951  
952 =back
953  
954 =cut
955  
956 =head2 type_text_medium
957  
958 type_text_medium(HashRef $data) : Str
959  
960 The type_text_medium method returns the SQL expression representing a medium
961 text data type.
962  
963 =over 4
964  
965 =item type_text_medium example #1
966  
967 # given: synopsis
968  
969 $grammar->type_text_medium({});
970  
971 # text
972  
973 =back
974  
975 =cut
976  
977 =head2 type_time
978  
979 type_time(HashRef $data) : Str
980  
981 The type_time method returns the SQL expression representing a time data type.
982  
983 =over 4
984  
985 =item type_time example #1
986  
987 # given: synopsis
988  
989 $grammar->type_time({});
990  
991 # time(0) without time zone
992  
993 =back
994  
995 =cut
996  
997 =head2 type_time_wtz
998  
999 type_time_wtz(HashRef $data) : Str
1000  
1001 The type_time_wtz method returns the SQL expression representing a time (and
1002 timezone) data type.
1003  
1004 =over 4
1005  
1006 =item type_time_wtz example #1
1007  
1008 # given: synopsis
1009  
1010 $grammar->type_time_wtz({});
1011  
1012 # time(0) with time zone
1013  
1014 =back
1015  
1016 =cut
1017  
1018 =head2 type_timestamp
1019  
1020 type_timestamp(HashRef $data) : Str
1021  
1022 The type_timestamp method returns the SQL expression representing a timestamp
1023 data type.
1024  
1025 =over 4
1026  
1027 =item type_timestamp example #1
1028  
1029 # given: synopsis
1030  
1031 $grammar->type_timestamp({});
1032  
1033 # timestamp(0) without time zone
1034  
1035 =back
1036  
1037 =cut
1038  
1039 =head2 type_timestamp_wtz
1040  
1041 type_timestamp_wtz(HashRef $data) : Str
1042  
1043 The type_timestamp_wtz method returns the SQL expression representing a
1044 timestamp (and timezone) data type.
1045  
1046 =over 4
1047  
1048 =item type_timestamp_wtz example #1
1049  
1050 # given: synopsis
1051  
1052 $grammar->type_timestamp_wtz({});
1053  
1054 # timestamp(0) with time zone
1055  
1056 =back
1057  
1058 =cut
1059  
1060 =head2 type_uuid
1061  
1062 type_uuid(HashRef $data) : Str
1063  
1064 The type_uuid method returns the SQL expression representing a uuid data type.
1065  
1066 =over 4
1067  
1068 =item type_uuid example #1
1069  
1070 # given: synopsis
1071  
1072 $grammar->type_uuid({});
1073  
1074 # uuid
1075  
1076 =back
1077  
1078 =cut
1079  
1080 =head2 wrap
1081  
1082 wrap(Str $name) : Str
1083  
1084 The wrap method returns a SQL-escaped string.
1085  
1086 =over 4
1087  
1088 =item wrap example #1
1089  
1090 # given: synopsis
1091  
1092 $grammar->wrap('field');
1093  
1094 # "field"
1095  
1096 =back
1097  
1098 =cut
1099  
1100 =head1 AUTHOR
1101  
1102 Al Newkirk, C<awncorp@cpan.org>
1103  
1104 =head1 LICENSE
1105  
1106 Copyright (C) 2011-2019, Al Newkirk, et al.
1107  
1108 This is free software; you can redistribute it and/or modify it under the terms
1109 of the The Apache License, Version 2.0, as elucidated in the L<"license
1110 file"|https://github.com/iamalnewkirk/sql-engine/blob/master/LICENSE>.
1111  
1112 =head1 PROJECT
1113  
1114 L<Wiki|https://github.com/iamalnewkirk/sql-engine/wiki>
1115  
1116 L<Project|https://github.com/iamalnewkirk/sql-engine>
1117  
1118 L<Initiatives|https://github.com/iamalnewkirk/sql-engine/projects>
1119  
1120 L<Milestones|https://github.com/iamalnewkirk/sql-engine/milestones>
1121  
1122 L<Contributing|https://github.com/iamalnewkirk/sql-engine/blob/master/CONTRIBUTE.md>
1123  
1124 L<Issues|https://github.com/iamalnewkirk/sql-engine/issues>
1125  
1126 =cut