-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME
More file actions
2221 lines (1730 loc) · 47 KB
/
README
File metadata and controls
2221 lines (1730 loc) · 47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
NAME
SQL::Engine - SQL Generation
ABSTRACT
SQL Generation for Perl 5
SYNOPSIS
use SQL::Engine;
my $sql = SQL::Engine->new;
$sql->insert(
into => {
table => 'users'
},
columns => [
{
column => 'id'
},
{
column => 'name'
}
],
values => [
{
value => undef
},
{
value => {
binding => 'name'
}
},
]
);
# then, e.g.
#
# my $dbh = DBI->connect;
#
# for my $operation ($sql->operations->list) {
# my $statement = $operation->statement;
# my @bindings = $operation->parameters({ name => 'Rob Zombie' });
#
# my $sth = $dbh->prepate($statement);
#
# $sth->execute(@bindings);
# }
#
# $dbh->disconnect;
DESCRIPTION
This package provides an interface and builders which generate SQL
statements, by default using a standard SQL syntax or vendor-specific
syntax if supported and provided to the constructor using the "grammar"
property. This package does not require a database connection, by
design, which gives users complete control over how connections and
statement handles are managed.
LIBRARIES
This package uses type constraints from:
Types::Standard
SCENARIOS
This package supports the following scenarios:
validation
use SQL::Engine;
my $sql = SQL::Engine->new(
validator => undef
);
# faster, no-validation
$sql->select(
from => {
table => 'users'
},
columns => [
{
column => '*'
}
]
);
This package supports automatic validation of operations using
SQL::Validator which can be passed to the constructor as the value of
the "validator" property. This object will be generated if not
provided. Alternatively, automated validation can be disabled by
passing the "undefined" value to the "validator" property on object
construction. Doing so enhances the performance of SQL generation at
the cost of not verifying that the instructions provided are correct.
ATTRIBUTES
This package has the following attributes:
grammar
grammar(Str)
This attribute is read-only, accepts (Str) values, and is optional.
operations
operations(InstanceOf["SQL::Engine::Collection"])
This attribute is read-only, accepts
(InstanceOf["SQL::Engine::Collection"]) values, and is optional.
validator
validator(Maybe[InstanceOf["SQL::Validator"]])
This attribute is read-only, accepts
(Maybe[InstanceOf["SQL::Validator"]]) values, and is optional.
METHODS
This package implements the following methods:
column_change
column_change(Any %args) : Object
The column_change method produces SQL operations which changes a table
column definition. The arguments expected are the constructor arguments
accepted by SQL::Engine::Builder::ColumnChange.
column_change example #1
# given: synopsis
$sql->operations->clear;
$sql->column_change(
for => {
table => 'users'
},
column => {
name => 'accessed',
type => 'datetime',
nullable => 1
}
);
column_create
column_create(Any %args) : Object
The column_create method produces SQL operations which create a new
table column. The arguments expected are the constructor arguments
accepted by SQL::Engine::Builder::ColumnCreate.
column_create example #1
# given: synopsis
$sql->operations->clear;
$sql->column_create(
for => {
table => 'users'
},
column => {
name => 'accessed',
type => 'datetime'
}
);
column_drop
column_drop(Any %args) : Object
The column_drop method produces SQL operations which removes an
existing table column. The arguments expected are the constructor
arguments accepted by SQL::Engine::Builder::ColumnDrop.
column_drop example #1
# given: synopsis
$sql->operations->clear;
$sql->column_drop(
table => 'users',
column => 'accessed'
);
column_rename
column_rename(Any %args) : Object
The column_rename method produces SQL operations which renames an
existing table column. The arguments expected are the constructor
arguments accepted by SQL::Engine::Builder::ColumnRename.
column_rename example #1
# given: synopsis
$sql->operations->clear;
$sql->column_rename(
for => {
table => 'users'
},
name => {
old => 'accessed',
new => 'accessed_at'
}
);
constraint_create
constraint_create(Any %args) : Object
The constraint_create method produces SQL operations which creates a
new table constraint. The arguments expected are the constructor
arguments accepted by SQL::Engine::Builder::ConstraintCreate.
constraint_create example #1
# given: synopsis
$sql->operations->clear;
$sql->constraint_create(
source => {
table => 'users',
column => 'profile_id'
},
target => {
table => 'profiles',
column => 'id'
}
);
constraint_drop
constraint_drop(Any %args) : Object
The constraint_drop method produces SQL operations which removes an
existing table constraint. The arguments expected are the constructor
arguments accepted by SQL::Engine::Builder::ConstraintDrop.
constraint_drop example #1
# given: synopsis
$sql->operations->clear;
$sql->constraint_drop(
source => {
table => 'users',
column => 'profile_id'
},
target => {
table => 'profiles',
column => 'id'
}
);
database_create
database_create(Any %args) : Object
The database_create method produces SQL operations which creates a new
database. The arguments expected are the constructor arguments accepted
by SQL::Engine::Builder::DatabaseCreate.
database_create example #1
# given: synopsis
$sql->operations->clear;
$sql->database_create(
name => 'todoapp'
);
database_drop
database_drop(Any %args) : Object
The database_drop method produces SQL operations which removes an
existing database. The arguments expected are the constructor arguments
accepted by SQL::Engine::Builder::DatabaseDrop.
database_drop example #1
# given: synopsis
$sql->operations->clear;
$sql->database_drop(
name => 'todoapp'
);
delete
delete(Any %args) : Object
The delete method produces SQL operations which deletes rows from a
table. The arguments expected are the constructor arguments accepted by
SQL::Engine::Builder::Delete.
delete example #1
# given: synopsis
$sql->operations->clear;
$sql->delete(
from => {
table => 'tasklists'
}
);
index_create
index_create(Any %args) : Object
The index_create method produces SQL operations which creates a new
table index. The arguments expected are the constructor arguments
accepted by SQL::Engine::Builder::IndexCreate.
index_create example #1
# given: synopsis
$sql->operations->clear;
$sql->index_create(
for => {
table => 'users'
},
columns => [
{
column => 'name'
}
]
);
index_drop
index_drop(Any %args) : Object
The index_drop method produces SQL operations which removes an existing
table index. The arguments expected are the constructor arguments
accepted by SQL::Engine::Builder::IndexDrop.
index_drop example #1
# given: synopsis
$sql->operations->clear;
$sql->index_drop(
for => {
table => 'users'
},
columns => [
{
column => 'name'
}
]
);
insert
insert(Any %args) : Object
The insert method produces SQL operations which inserts rows into a
table. The arguments expected are the constructor arguments accepted by
SQL::Engine::Builder::Insert.
insert example #1
# given: synopsis
$sql->operations->clear;
$sql->insert(
into => {
table => 'users'
},
values => [
{
value => undef
},
{
value => 'Rob Zombie'
},
{
value => {
function => ['now']
}
},
{
value => {
function => ['now']
}
},
{
value => {
function => ['now']
}
}
]
);
schema_create
schema_create(Any %args) : Object
The schema_create method produces SQL operations which creates a new
schema. The arguments expected are the constructor arguments accepted
by SQL::Engine::Builder::SchemaCreate.
schema_create example #1
# given: synopsis
$sql->operations->clear;
$sql->schema_create(
name => 'private',
);
schema_drop
schema_drop(Any %args) : Object
The schema_drop method produces SQL operations which removes an
existing schema. The arguments expected are the constructor arguments
accepted by SQL::Engine::Builder::SchemaDrop.
schema_drop example #1
# given: synopsis
$sql->operations->clear;
$sql->schema_drop(
name => 'private',
);
schema_rename
schema_rename(Any %args) : Object
The schema_rename method produces SQL operations which renames an
existing schema. The arguments expected are the constructor arguments
accepted by SQL::Engine::Builder::SchemaRename.
schema_rename example #1
# given: synopsis
$sql->operations->clear;
$sql->schema_rename(
name => {
old => 'private',
new => 'restricted'
}
);
select
select(Any %args) : Object
The select method produces SQL operations which select rows from a
table. The arguments expected are the constructor arguments accepted by
SQL::Engine::Builder::Select.
select example #1
# given: synopsis
$sql->operations->clear;
$sql->select(
from => {
table => 'people'
},
columns => [
{ column => 'name' }
]
);
table_create
table_create(Any %args) : Object
The table_create method produces SQL operations which creates a new
table. The arguments expected are the constructor arguments accepted by
SQL::Engine::Builder::TableCreate.
table_create example #1
# given: synopsis
$sql->operations->clear;
$sql->table_create(
name => 'users',
columns => [
{
name => 'id',
type => 'integer',
primary => 1
}
]
);
table_drop
table_drop(Any %args) : Object
The table_drop method produces SQL operations which removes an existing
table. The arguments expected are the constructor arguments accepted by
SQL::Engine::Builder::TableDrop.
table_drop example #1
# given: synopsis
$sql->operations->clear;
$sql->table_drop(
name => 'people'
);
table_rename
table_rename(Any %args) : Object
The table_rename method produces SQL operations which renames an
existing table. The arguments expected are the constructor arguments
accepted by SQL::Engine::Builder::TableRename.
table_rename example #1
# given: synopsis
$sql->operations->clear;
$sql->table_rename(
name => {
old => 'peoples',
new => 'people'
}
);
transaction
transaction(Any %args) : Object
The transaction method produces SQL operations which represents an
atomic database operation. The arguments expected are the constructor
arguments accepted by SQL::Engine::Builder::Transaction.
transaction example #1
# given: synopsis
$sql->operations->clear;
$sql->transaction(
queries => [
{
'table-create' => {
name => 'users',
columns => [
{
name => 'id',
type => 'integer',
primary => 1
}
]
}
}
]
);
union
union(Any %args) : Object
The union method produces SQL operations which returns a results from
two or more select queries. The arguments expected are the constructor
arguments accepted by SQL::Engine::Builder::Union.
union example #1
# given: synopsis
$sql->operations->clear;
$sql->union(
queries => [
{
select => {
from => {
table => 'customers',
},
columns => [
{
column => 'name',
}
]
}
},
{
select => {
from => {
table => 'employees',
},
columns => [
{
column => 'name',
}
]
}
}
]
);
update
update(Any %args) : Object
The update method produces SQL operations which update rows in a table.
The arguments expected are the constructor arguments accepted by
SQL::Engine::Builder::Update.
update example #1
# given: synopsis
$sql->operations->clear;
$sql->update(
for => {
table => 'users'
},
columns => [
{
column => 'updated',
value => { function => ['now'] }
}
]
);
view_create
view_create(Any %args) : Object
The view_create method produces SQL operations which creates a new
table view. The arguments expected are the constructor arguments
accepted by SQL::Engine::Builder::ViewCreate.
view_create example #1
# given: synopsis
$sql->operations->clear;
$sql->view_create(
name => 'active_users',
query => {
select => {
from => {
table => 'users'
},
columns => [
{
column => '*'
}
],
where => [
{
'not-null' => {
column => 'deleted'
}
}
]
}
}
);
view_drop
view_drop(Any %args) : Object
The view_drop method produces SQL operations which removes an existing
table view. The arguments expected are the constructor arguments
accepted by SQL::Engine::Builder::ViewDrop.
view_drop example #1
# given: synopsis
$sql->operations->clear;
$sql->view_drop(
name => 'active_users'
);
EXAMPLES
This distribution supports generating SQL statements using standard
syntax or using database-specific syntax if a grammar is specified. The
following is a collection of examples covering the most common
operations (using PostgreSQL syntax):
setup
use SQL::Engine;
my $sql = SQL::Engine->new(
grammar => 'postgres'
);
$sql->select(
from => {
table => 'people'
},
columns => [
{ column => 'name' }
]
);
$sql->operations->first->statement;
# SELECT "name" FROM "people"
select
select example #1
$sql->select(
from => {
table => 'users'
},
columns => [
{
column => '*'
}
]
);
select example #1 output
# SELECT * FROM "users"
select example #2
$sql->select(
from => {
table => 'users'
},
columns => [
{
column => 'id'
},
{
column => 'name'
}
]
);
select example #2 output
# SELECT "id", "name" FROM "users"
select example #3
$sql->select(
from => {
table => 'users'
},
columns => [
{
column => '*'
}
],
where => [
{
eq => [{column => 'id'}, {binding => 'id'}]
}
]
);
select example #3 output
# SELECT * FROM "users" WHERE "id" = ?
select example #4
$sql->select(
from => {
table => 'users',
alias => 'u'
},
columns => [
{
column => '*',
alias => 'u'
}
],
joins => [
{
with => {
table => 'tasklists',
alias => 't'
},
having => [
{
eq => [
{
column => 'id',
alias => 'u'
},
{
column => 'user_id',
alias => 't'
}
]
}
]
}
],
where => [
{
eq => [
{
column => 'id',
alias => 'u'
},
{
binding => 'id'
}
]
}
]
);
select example #4 output
# SELECT "u".* FROM "users" "u"
# JOIN "tasklists" "t" ON "u"."id" = "t"."user_id" WHERE "u"."id" = ?
select example #5
$sql->select(
from => {
table => 'tasklists'
},
columns => [
{
function => ['count', { column => 'user_id' }]
}
],
group_by => [
{
column => 'user_id'
}
]
);
select example #5 output
# SELECT count("user_id") FROM "tasklists" GROUP BY "user_id"
select example #6
$sql->select(
from => {
table => 'tasklists'
},
columns => [
{
function => ['count', { column => 'user_id' }]
}
],
group_by => [
{
column => 'user_id'
}
],
having => [
{
gt => [
{
function => ['count', { column => 'user_id' }]
},
1
]
}
]
);
select example #6 output
# SELECT count("user_id") FROM "tasklists" GROUP BY "user_id" HAVING
# count("user_id") > 1
select example #7
$sql->select(
from => {
table => 'tasklists'
},
columns => [
{
column => '*'
}
],
order_by => [
{
column => 'user_id'
}
]
);
select example #7 output
# SELECT * FROM "tasklists" ORDER BY "user_id"
select example #8
$sql->select(
from => {
table => 'tasklists'
},
columns => [
{
column => '*'
}
],
order_by => [
{
column => 'user_id'
},
{
column => 'id',
sort => 'desc'
}
]
);
select example #8 output
# SELECT * FROM "tasklists" ORDER BY "user_id", "id" DESC
select example #9
$sql->select(
from => {
table => 'tasks'
},
columns => [
{
column => '*'
}
],
rows => {
limit => 5
}
);
select example #9 output
# SELECT * FROM "tasks" LIMIT 5
select example #10
$sql->select(
from => {
table => 'tasks'
},
columns => [
{
column => '*'
}
],
rows => {
limit => 5,
offset => 1