Engine/Grammar/Sqlite.pm untested

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