Engine/Grammar/Mssql.pm untested

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