-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsql.scrbl
1145 lines (872 loc) · 35 KB
/
sql.scrbl
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
#lang scribble/manual
@(require scribble/manual
scribble/basic
scribble/example
racket/runtime-path
racket/sandbox
(for-label racket
racket/contract
db/base
sql))
@title{SQL: A Structured Notation for SQL Statements}
@author[@author+email["Ryan Culpepper" "[email protected]"]]
@(define (lit str) (racketfont str))
@(begin
(define-syntax-rule (interaction e ...) (examples #:label #f e ...))
(define-runtime-path log-file "private/log-for-sql-docs.rktd")
(define log-mode 'replay)
(define (make-pg-eval log-file)
(let ([ev (make-log-based-eval log-file log-mode)])
(ev '(require racket/class db sql db/util/postgresql db/util/datetime))
ev))
(define db-eval (make-pg-eval log-file)))
@(define the-eval (make-base-eval))
@(the-eval '(require sql racket/match))
@defmodule[sql]
This library provides an S-expression notation for a subset of
SQL. It provides forms that produce statements (as opaque values
rather than strings) that can be used directly with Racket's
@racketmodname[db] library. It also provides macros and functions for
creating and manipulating SQL ASTs.
@; ============================================================
@section[#:tag "sql-intro"]{Using the SQL Library}
This library complements the @racketmodname[db] library. The database
library manages connecting to databases and executing queries; this
library helps construction of the queries to execute.
We'll start by going through the examples @secref["intro-basic" #:doc
'(lib "db/scribblings/db.scrbl")] using this library's SQL notation
instead.
@interaction[#:eval db-eval
(require sql db)
(eval:alts
(define pgc ....)
(define pgc (dsn-connect 'db-scribble-env)))
]
First we create a temporary table to play around with:
@interaction[#:eval db-eval
(query-exec pgc
(create-table #:temporary the_numbers
#:columns [n integer #:not-null] [d varchar]))
(query-exec pgc
(insert #:into the_numbers #:set [n 0] [d "nothing"]))
]
Let's take a look at the statements that just went by:
@interaction[#:eval db-eval
(create-table #:temporary the_numbers
#:columns [n integer #:not-null] [d varchar])
(insert #:into the_numbers #:set [n 0] [d "nothing"])
]
Now let's add another row, using ``computed'' values rather than
literals. We can use @racket[unquote] (or @litchar{,}) in a scalar
expression position to insert a Racket value:
@interaction[#:eval db-eval
(define n1 1)
(define d1 "the loneliest number")
(query-exec pgc
(insert #:into the_numbers #:set [n ,n1] [d ,d1]))
]
Let's look at that last statement:
@interaction[#:eval db-eval
(insert #:into the_numbers #:set [n ,n1] [d ,d1])
]
The @racket[unquote]d expressions turned into parameter placeholders,
and the statement stores their values separately. Strangely, the
placeholders appear as @tt{?}, and PostgreSQL doesn't understand
@tt{?} placeholders; they should have been @tt{$1} and @tt{$2}. But
the statement seems to have worked. What's going on?
We need to set the interactive printing dialect to PostgreSQL. This has no
effect on the query operations; they set the dialect independently
based on the database connection.
@interaction[#:eval db-eval
(parameterize ((current-sql-dialect 'postgresql))
(print (insert #:into the_numbers #:set [n ,n1] [d ,d1])))
]
And now we see @tt{$1} and @tt{$2} as expected.
We can introduce placeholders explicitly (although @racket[unquote] is
usually more convenient). An explicit placeholder is written
@racket[?], regardless of the dialect. The parameters are given in the
query call as usual:
@interaction[#:eval db-eval
(query-exec pgc
(insert #:into the_numbers #:set [n ?] [d ?])
(+ 1 1) "company")
]
It is not currently possible to mix explicit placeholders and
@racket[unquote] parameters:
@interaction[#:eval the-eval
(eval:error
(query-exec pgc
(insert #:into the_numbers #:set [n ,3] [d ?])
"a crowd"))
]
You can, of course, mix constant literals and @racket[unquote]s (or
placeholders).
@interaction[#:eval db-eval
(query-exec pgc
(insert #:into the_numbers #:set [n 3] [d ,"a crowd"]))
]
@tt{SELECT} statements are constructed similarly, and they follow the
same rules regarding parameters. The statements work the same with all
of the query operations.
@interaction[#:eval db-eval
(query pgc
(select n d #:from the_numbers #:where (= (% n 2) 0)))
(query-rows pgc
(select n d #:from the_numbers #:where (= (+ n n) (* n n))))
(query-row pgc
(select n d #:from the_numbers #:where (< n 1)))
(query-list pgc
(select d #:from the_numbers #:where (= 0 (% n 2))))
(query-value pgc
(select (string_agg d ", ") #:from the_numbers #:where (= 0 (% n 2))))
]
There are S-expression notations for many common SQL operators and
expression forms. See @secref["scalar-exprs"] for details.
The rest of this manual uses the default SQL1992 dialect for printing
results:
@interaction[#:eval db-eval
(current-sql-dialect #f)
]
@; ============================================================
@section[#:tag "statement-forms"]{Statement Forms}
The macros in this section create statement values suitable for
passing to the query functions of the @racketmodname[db]
library. These statement values satisfy the @racketmodname[db]
library's @racket[statement?] predicate. They are different from the
@svar[statement] ASTs produced by @racket[statement-qq].
The printing of a statement value is controlled by
@racket[(current-sql-dialect)], but the code it generates when passed
to a query function is determined by the dialect of the connection the
query is performed on.
@defform*[[(sql statement)
(sql ddl-statement)]]{
Produces a statement value that can be passed to a @racketmodname[db]
query function. The syntax corresponds to the syntax of the
@svar[statement] or @svar[ddl-statement] nonterminals from
@secref["sql-syntax"].
@examples[#:eval the-eval
(sql (select a b c #:from mytable #:where (> a 10)))
(sql (insert #:into mytable #:set [a 1] [b 2] [c 3]))
(sql (create-table numbers
#:columns [n integer #:not-null] [t text]
#:constraints (primary-key n)))
]}
@deftogether[[
@defform*[[(select select-item ... select-clause ...)
(select select-clause ...)]]
@defform*[[(insert #:into table-name assign-clause maybe-conflict)
(insert #:into table-name maybe-columns #:from table-expr maybe-conflict)]]
@defform[(update table-name assign-clause maybe-where)]
@defform[(delete #:from table-name maybe-where)]
]]{
Like @racket[sql], but specialized to the syntax of the
@svar[select-statement], @svar[insert-statement],
@svar[update-statement], and @svar[delete-statement] nonterminals from
@secref["sql-syntax"], respectively.
@examples[#:eval the-eval
(select a b c #:from mytable #:where (> a 10))
(insert #:into mytable #:set [a 1] [b 2] [c 3])
(insert #:into mytable
#:from (select a b c
#:from other_table
#:where (is-not-null d)))
]
Equivalent to
@racketblock[
(sql (@#,lit{select} select-item ... select-clause ...))
(sql (@#,lit{insert} #:into table-name assign-clause))
]
and so forth.
@history[#:changed "1.1" @elem{Added @racket[#:or-ignore] for @racket[insert].}]
}
@deftogether[[
@defform*[[(create-table maybe-temp maybe-ifnotexists table-name
#:columns column-def ...
maybe-constraints)
(create-table maybe-temp maybe-ifnotexists table-name
#:as statement)]]
@defform[(create-view maybe-temp view-name
statement)]
]]{
Like @racket[sql], but specialized to the syntax of the DDL
@svar[create-table-statement] and @svar[create-view-statement],
respectively.
@examples[#:eval the-eval
(create-table numbers
#:columns [n integer #:not-null] [t text]
#:constraints (primary-key n))
]
Equivalent to
@racketblock[
(sql (@#,lit{create-table} maybe-temp table-name
#:columns column-def ...
maybe-constraints))
]
and so forth.
@history[#:changed "1.1" @elem{Added @racket[#:if-not-exists] option.}]
}
@defproc[(sql-statement? [v any/c]) boolean?]{
Returns @racket[#t] if @racket[v] is a statement value returned by one
of the forms in this section such as @racket[select], @racket[#f]
otherwise.
}
@defproc[(sql-statement->string [statement sql-statement?]
[dialect (or/c symbol? #f) (current-sql-dialect)])
string?]{
Produces SQL code as a string for the given @racket[statement]
according to the rules of @racket[dialect].
}
@; ============================================================
@section[#:tag "sql-syntax"]{S-expression Syntax for SQL}
This section describes this library's S-expression syntax for a subset
of SQL. The SQL support is organized by nonterminals (eg
@svar[statement], @svar[table-ref], and @svar[scalar-expr]); the
grammar handled by this library is adapted from @emph{A Guide to the
SQL Standard, 4th ed@._} by C@._ J@._ Date and Hugh Darwen. Each
non-terminal has the following:
@itemlist[
@item{an S-expression syntax,}
@item{an AST type predicate, and}
@item{a quasiquotation macro to produce AST values from the
S-expression syntax.}
]
All literals are recognized symbolically, rather than by identifier
binding, to avoid cluttering the namespace. The AST type
representations are not considered public; they are likely to change
in future versions of this library.
@defproc[(sql-ast->string [ast (or/c name-ast? scalar-expr-ast? table-expr-ast?
table-ref-ast? statement-ast? ddl-ast?)]
[dialect (or/c symbol? #f) (current-sql-dialect)])
string?]{
Produces SQL code as a string for the given AST to a string according
to the rules of @racket[dialect]. Examples are given throughout the
following sections.
}
@; ----------------------------------------
@subsection[#:tag "names"]{SQL Names and Identifiers}
A name is either an unqualified identifier or an identifier qualified
with another name, which depending on its usage might represent a
catalog, schema, table, range variable, etc.
Concrete SQL has both unquoted and quoted identifiers. Different SQL
environments (eg, database backends) have different restrictions on
unquoted identifiers, regarding illegal characters and reserved
words. Most (but not all) systems also apply some case-folding rule to
unquoted identifiers (eg, PostgreSQL converts to lowercase, some
others convert to uppercase).
Similarly, this library has both ``tagged'' and ``untagged'' notations
for identifiers and names. Untagged identifiers are written as raw
symbols; they are short and convenient, but they run the risk of
confusion with operators and special symbols used by this
library. Examples of special symbols include @lit{select}, @lit{as},
and @lit{from}. Examples of identifiers containing operator characters
include @tt{hello-goodbye} and @tt{first/last}. These identifiers must
be written in tagged form.
@racketgrammar*[
[ident symbol
(@#,lit{Ident:} string)
(@#,lit{Ident:} symbol)]
[name symbol
ident
(@#,lit{Name:} name ...+)]
]
@specsubform[(@#,lit{Ident:} symbol)]{
Unquoted if possible; case-folded and quoted according the SQL
dialect's rules if @racket[symbol] is a reserved word or contains
illegal characters.
@racketblock[
(Ident: MyTable) (code:comment "MyTable")
(Ident: Select) (code:comment "\"SELECT\"")
(Ident: a+b.c) (code:comment "\"a+b.c\"")
]}
@specsubform[(@#,lit{Ident:} string)]{
Always quoted without case-folding.
@racketblock[
(Ident: "MyTable") (code:comment "\"MyTable\"")
(Ident: "Select") (code:comment "\"Select\"")
(Ident: "x1.$!!") (code:comment "\"x1.$!!\"")
]}
@specsubform[(@#,lit{Name:} name ...+)]{
Qualified name; each name except the last qualifies the name to its
right.
@racketblock[
(Name: x y z) (code:comment "x.y.z")
(Name: x y.z) (code:comment "x.y.z")
(Name: x (Ident: y.z)) (code:comment "x.\"y.z\"")
]}
@specsubform[symbol]{
Must not be a special symbol; otherwise an error is raised.
Equivalent to @racket[(@#,lit{Ident:} symbol)] if @racket[symbol] contains
no dot (@litchar{.}) characters and matches the pattern
@racket[#px"^(?:\\p{L}|_)(?:\\p{L}|\\p{N}|[_$])*$"]---that is, a letter
or underscore followed by zero or more letters, numbers, underscores,
and dollar signs.
If @racket[symbol] consists of dot-separated @racket[_part]s satisfying
the rule above, it is equivalent to @racket[(Name: _part ...)].
@racketblock[
MyTable (code:comment "MyTable")
x.y.z (code:comment "x.y.z")
x.select.as (code:comment "x.\"SELECT\".\"AS\"")
]}
Because case-folding behavior is system-dependent, it is wisest to
either always quote a given name or never quote it.
@deftogether[[
@defform[(ident-qq ident)]
@defproc[(ident-ast? [v any/c]) boolean?]
]]{
Quasiquotation macro and predicate, respectively, for @svar[ident].
@examples[#:eval the-eval
(sql-ast->string (ident-qq MyTable))
(sql-ast->string (ident-qq (Ident: MyTable)))
(sql-ast->string (ident-qq (Ident: "MyTable")))
(sql-ast->string (ident-qq Select))
(sql-ast->string (ident-qq (Ident: Select)))
(sql-ast->string (ident-qq (Ident: "Select")))
(sql-ast->string (ident-qq (Ident: a+b.c)))
(eval:error (sql-ast->string (ident-qq select)))
(eval:error (sql-ast->string (ident-qq a+b.c)))
]}
@deftogether[[
@defform[(name-qq name)]
@defproc[(name-ast? [v any/c]) boolean?]
]]{
Quasiquotation macro and predicate, respectively, for @svar[name].
@examples[#:eval the-eval
(sql-ast->string (name-qq (Name: x y z)))
(sql-ast->string (name-qq (Name: x.y z)))
(sql-ast->string (name-qq x.y.z))
(sql-ast->string (name-qq x.select.as))
]}
@; ----------------------------------------
@subsection[#:tag "scalar-exprs"]{SQL Scalar Expressions}
A scalar expression is either a name, a literal integer or string value,
or an application of some function or operator. Note: not every kind of
expression is supported in every SQL dialect.
@racketgrammar*[
[scalar-expr name
exact-integer
string
(@#,lit{exists} table-expr)
(@#,lit{in} scalar-expr #:from table-expr)
(@#,lit{in} scalar-expr #:values scalar-expr ...)
(@#,lit{case} [scalar-expr scalar-expr] ... maybe-else)
(@#,lit{case} #:of scalar-expr [scalar-expr scalar-expr] ... maybe-else)
(compare-operator scalar-expr #:some table-expr)
(compare-operator scalar-expr #:all table-expr)
(name scalar-expr ...)
table-expr
(operator/special scalar-expr ...)
?
(@#,lit{unquote} racket-expr)]
]
@specsubform[(@#,lit{exists} table-expr)]{
Produces an @tt{EXISTS} expression:
@racketblock[
(exists (select 1 #:from t)) (code:comment "EXISTS (SELECT 1 FROM t)")
]}
@specsubform[(code:line
(@#,lit{in} scalar-expr #:from table-expr)
(@#,lit{in} scalar-expr #:values scalar-expr ...))]{
There are two forms of @tt{IN} expression, one for table expressions
and one for lists of scalar expressions:
@racketblock[
(in x #:from (select y #:from ys)) (code:comment "x IN (SELECT y FROM ys)")
(in x #:values 1 2 3) (code:comment "x IN (1, 2, 3)")
]}
@specsubform[(code:line
(@#,lit{case} [scalar-expr scalar-expr] ... maybe-else)
(@#,lit{case} #:of scalar-expr [scalar-expr scalar-expr] ... maybe-else))]{
There are two forms of @tt{CASE} expression, one like Racket's
@racket[cond] and the other like Racket's @racket[case]:
@racketblock[
(case [(= x 0) "zero"] [else "no"]) (code:comment "CASE WHEN x = 0 THEN 'zero' ELSE 'no' END")
(case #:of x [0 "zero"] [else "no"]) (code:comment "CASE x WHEN 0 THEN 'zero' ELSE 'no' END")
]}
@specsubform[(code:line
(compare-operator scalar-expr #:some table-expr)
(compare-operator scalar-expr #:all table-expr))]{
Produces an ``all-or-any'' comparison between a scalar (or row)
expression and a table expression.
@racketblock[
(= x #:some (select y #:from ys)) (code:comment "x = SOME (SELECT y FROM ys)")
(< x #:all (select y #:from ys)) (code:comment "x < ALL (select y FROM ys)")
]}
@specsubform[(name scalar-expr ...)]{
Represents an ordinary function call; no arity checking is done.
@racketblock[
(coalesce x y z) (code:comment "coalesce(x, y, z)")
]}
@specsubform[table-expr]{
Represents a subquery; the query must return at most one row.
@racketblock[
(select y #:from ys #:where (x = 0)) (code:comment "(SELECT y FROM ys WHERE x = 0)")
]}
@deftogether[[
@defform[(scalar-expr-qq scalar-expr)]
@defproc[(scalar-expr-ast? [v any/c]) boolean?]
]]{
Quasiquotation macro and predicate, respectively, for
@svar[scalar-expr].
@examples[#:eval the-eval
(sql-ast->string (scalar-expr-qq mytable.mycolumn))
(sql-ast->string (scalar-expr-qq 42))
(sql-ast->string (scalar-expr-qq "Salutations"))
(sql-ast->string (scalar-expr-qq "a 'tricky' string"))
(sql-ast->string (scalar-expr-qq (log (- 1 p))))
(sql-ast->string (scalar-expr-qq (and (> x 10) (< x 55))))
(sql-ast->string (scalar-expr-qq (coalesce x y z)))
(sql-ast->string (scalar-expr-qq (cast "2015-03-15" DATE)))
(sql-ast->string (scalar-expr-qq (extract YEAR dob)))
(sql-ast->string (scalar-expr-qq (is-null mytable.mycolumn)))
(sql-ast->string (scalar-expr-qq (like ph_num "555-____")))
(sql-ast->string (scalar-expr-qq (|| lname ", " fname)))
]}
@; ----------------------------------------
@subsubsection[#:tag "special-operators"]{Special Scalar Expressions}
@specsubform[(operator/special scalar-expr ...)]{
This function-like syntax is used to represent uses of SQL operators,
standard SQL functions that don't use ordinary function-call notation,
and a few other special cases, listed below.
}
@itemlist[
@item{The @tt{CAST} and @tt{EXTRACT} special functions:
@racketblock[
(cast "2015-03-15" DATE) (code:comment "CAST('2015-03-15' AS DATE)")
(cast "123" (NUMERIC 5 0)) (code:comment "CAST('123' AS NUMERIC(5, 0))")
(extract YEAR dob) (code:comment "EXTRACT(YEAR FROM dob)")
]
Note that as above, types and fields are written as ``scalar
expressions'', in a mild abuse of syntax.
}
@item{The @tt{OVERLAY}, @tt{POSITION}, and @tt{SUBSTRING} functions:
@racketblock[
(overlay "abc" "z" 2 1) (code:comment "OVERLAY('abc' PLACING 'z' FROM 2 FOR 1)")
(position "c" "abc") (code:comment "POSITION('c' IN 'abc)")
(substring "abc" 2 1) (code:comment "SUBSTRING('abc' FROM 2 FOR 1)")
]}
@item{The @tt{TRIM} function is written using one of the following variants:
@racketblock[
(trim-leading "z" "zzabc") (code:comment "TRIM(LEADING 'z' FROM 'zzabc')")
(trim-trailing "z" "abczz") (code:comment "TRIM(TRAILING 'z' FROM 'abczz')")
(trim-both "z" "zzabczz") (code:comment "TRIM(BOTH 'z' FROM 'zzabczz')")
]}
@item{The syntax @tt{COUNT(*)} can be written as follows:
@racketblock[
(count-all) (code:comment "COUNT(*)")
]}
@item{The chaining arithmetic operators @tt{+}, @tt{-}, @tt{*}, and @tt{/}:
@racketblock[
(+ 1 2 3 4) (code:comment "1 + 2 + 3 + 4")
]}
@item{The chaining infix logical operators @tt{AND} and @tt{OR}:
@racketblock[
(and x y z) (code:comment "x AND y AND z")
(or x y z) (code:comment "x OR y OR z")
]}
@item{The chaining infix binary operator @tt{||} can be written as
@racket[\|\|] or as @racket[||]; the latter reads as the empty symbol.
@racketblock[
(|| lname ", " fname) (code:comment "lname || ', ' || fname")
(\|\| lname ", " fname) (code:comment "lname || ', ' || fname")
]}
@item{Any identifier consisting of only characters in
@litchar["~!@#%^&*-_=+|<>?/"] is considered a non-chaining infix
binary operator:
@racketblock[
(< x y) (code:comment "x < y")
(%&! 1 2) (code:comment "1 %&! 2")
]}
@item{The following operators:
@racketblock[
(not x) (code:comment "NOT x")
(is-null x) (code:comment "x IS NULL")
(is-not-null x) (code:comment "x IS NOT NULL")
(is-true x) (code:comment "x IS TRUE")
(is-not-true x) (code:comment "x IS NOT TRUE")
(is-false x) (code:comment "x IS FALSE")
(is-not-false x) (code:comment "x IS NOT FALSE")
(is-unknown x) (code:comment "x IS UNKNOWN")
(is-not-unknown x) (code:comment "x IS NOT UNKNOWN")
(collate x utf8) (code:comment "x COLLATE utf8")
(between-and 5 1 10) (code:comment "5 BETWEEN 1 AND 10")
(distinct-from x y) (code:comment "x DISTINCT FROM y")
(like "abc" "a%") (code:comment "'abc' LIKE 'a%'")
(ilike "aBC" "ab_") (code:comment "'aBC' ILIKE 'ab_'")
(similar-to "abc" "(a|z)%") (code:comment "'abc' SIMILAR TO '(a|z)%'")
]}
@item{Field selection is written as a regular identifier (or @tt{*})
prefixed by a dot.
@racketblock[
(.city state) (code:comment "(state).city")
(.* table1) (code:comment "(table1).*")
(.*) (code:comment "*")
]}
@item{Row constructors (the @tt{ROW} syntax is a PostgreSQL extension):
@racketblock[
(%row 1 2 3) (code:comment "(1, 2, 3)")
(row 1 2 3) (code:comment "ROW(1, 2, 3)")
]}
@item{Arrays and array indexing (PostgreSQL extension):
@racketblock[
(%array 1 2 3) (code:comment "ARRAY[1, 2, 3]")
(%ref x 1) (code:comment "(x)[1]")
(%ref x 1 2 3) (code:comment "(x)[1,2,3]")
]}
]
@; ----------------------------------------
@subsubsection[#:tag "unquote"]{Placeholders and Unquoted Parameters}
There are two variants of @svar[scalar-expr] that enable
the construction of parameterized queries. The first is the placeholder,
written @lit{?} (regardless of the notation used by the database the
query is to be sent to). The second is the @lit{unquote} form, which
is equivalent to inserting a placeholder and also providing the
expression as a query parameter.
@racketgrammar*[
[scalar-expr ....
?
(@#,lit{unquote} racket-expr)]
]
Note: Due to limitations in the @racketmodname[db] library,
@lit{unquote} parameters and ordinary placeholders cannot be mixed in
the same statement.
@examples[#:eval the-eval
(select a #:from mytable #:where (= b ?))
]
The resulting statement can be used with parameters thus:
@racketblock[
(query-value c (select a #:from mytable #:where (= b ?)) 10)
]
Using the @lit{unquote} form eliminates the need to keep track of
positional parameters; instead, the parameter value is written as a
Racket expression within the statement. It is automatically translated
to SQL code containing placeholders.
@examples[#:eval the-eval
(define b-param 10)
(select a #:from mytable #:where (= b ,b-param))
]
The resulting statement must be called without additional parameters:
@racketblock[
(query-value c (select a #:from mytable #:where (= b ,b-param)))
]
Note that placeholder syntax varies between SQL dialects. We can see
the code a statement produces for a specific dialect by setting the
@racket[current-sql-dialect] parameter:
@interaction[#:eval the-eval
(parameterize ((current-sql-dialect 'postgresql))
(sql-statement->string (select a #:from mytable #:where (= b ,b-param))))
]
@; ----------------------------------------
@subsection[#:tag "table-exprs"]{SQL Table References and Expressions}
A table reference is either a reference to a defined table (or view)
or a computed table with a name or named components. A table
expression can be formed using join and set operations.
@racketgrammar*[
[table-ref
table-name
(@#,lit{as} table-name range-var-ident)
(@#,lit{as} table-expr range-var-ident)
table-expr]
]
Note: in the final variant of @svar[table-ref], the @svar[table-expr]
must be a join table expression, specifically.
@racketgrammar*[
[table-expr
(@#,lit{cross-join} table-ref table-ref)
(join-op table-ref table-ref join-condition)
(set-op table-ref table-ref
maybe-all correspond-clause)
(@#,lit{values} scalar-expr ...)
(@#,lit{values*} (scalar-expr ...) ...)
select-statement]
[join-op @#,lit{inner-join}
@#,lit{left-join}
@#,lit{right-join}
@#,lit{full-join}
@#,lit{union-join}]
[join-condition
(code:line #:natural)
(code:line #:using column-ident ...)
(code:line #:on scalar-expr)]
[set-op @#,lit{union}
@#,lit{except}
@#,lit{intersect}]
[maybe-all
(code:line)
(code:line #:all)]
[correspond-clause
(code:line)
(code:line #:corresponding)
(code:line #:corresponding-by column-ident ...)]
]
@deftogether[[
@defform[(table-ref-qq table-ref)]
@defproc[(table-ref-ast? [v any/c]) boolean?]
]]{
Quasiquotation macro and predicate, respectively, for
@svar[table-ref].
@examples[#:eval the-eval
(sql-ast->string (table-ref-qq supplier))
(sql-ast->string (table-ref-qq (as supplier s)))
(sql-ast->string (table-ref-qq (inner-join supplier part #:using supply_id)))
]
}
@deftogether[[
@defform[(table-expr-qq table-expr)]
@defproc[(table-expr-ast? [v any/c]) boolean?]
]]{
Quasiquotation macro and predicate, respectively, for
@svar[table-expr].
}
@; ----------------------------------------
@subsection[#:tag "statements"]{SQL Statements}
A statement is one of the four standard DML statements or a @tt{WITH}
statement that combines them with one or more common table
expressions.
@racketgrammar*[
[statement select-statement
insert-statement
update-statement
delete-statement
with-statement]
]
@bold{Select}
@racketgrammar*[
[select-statement (@#,lit{select} distinct-clause select-item ... select-clause ...)
(@#,lit{select} distinct-clause select-clause ...)]
[select-clause (code:line #:values select-item ...)
(code:line #:from table-ref ...)
(code:line #:where condition-scalar-expr ...)
(code:line #:group-by column-ident ...)
(code:line #:having condition-scalar-expr ...)
(code:line #:order-by order-item ...)
(code:line #:limit scalar-expr)
(code:line #:offset scalar-expr)]
[select-item scalar-expr
(@#,lit{as} scalar-expr ident)]
[distinct-clause (code:line)
(code:line #:all)
(code:line #:distinct)]
[order-item (code:line scalar-expr #:asc)
(code:line scalar-expr #:desc)
(code:line scalar-expr)]
]
A @svar[select-statement] can contain each kind of
@svar[select-clause] at most once. The clauses can occur in any
order. If the first form of @svar[select-statement] is used (that is,
with the initial @svar[select-item]s), then the @racket[#:values]
clause may not also be used.
@history[#:changed "1.2" @elem{Added @svar[distinct-clause]}]
@bold{Insert}
@racketgrammar*[
[insert-statement (@#,lit{insert} #:into table-name assign-clause maybe-conflict)
(@#,lit{insert} #:into table-name maybe-columns
#:from table-expr maybe-conflict)]
[assign-clause (code:line #:set [column-ident scalar-expr] ...)]
[maybe-columns (code:line)
(code:line #:columns column-ident ...)]
[maybe-conflict (code:line)
(code:line #:or-ignore)
(code:line #:or-fail)]
]
@history[#:changed "1.1" @elem{Added @racket[#:or-ignore].}]
@bold{Update}
@racketgrammar*[
[update-statement (@#,lit{update} table-name assign-clause maybe-where)]
[assign-clause (code:line #:set [column-ident scalar-expr] ...)]
[maybe-where (code:line)
(code:line #:where condition-scalar-expr ...)]
]
@bold{Delete}
@racketgrammar*[
[delete-statement (@#,lit{delete} #:from table-name maybe-where)]
[maybe-where (code:line)
(code:line #:where condition-scalar-expr ...)]
]
@bold{With}
@racketgrammar*[
[with-statement (@#,lit{with} maybe-rec ([table-ident/columns cte])
statement)]
[maybe-rec (code:line)
#:recursive]
[table-ident/columns table-ident
(table-ident column-ident ...)]
[cte statement table-expr]
]
Different database systems place different restrictions on what kinds
of statements and table expressions they allow within @tt{WITH}
statements.
@history[#:changed "1.6" @elem{Allowed @svar[table-expr] for right-hand side.}]
@deftogether[[
@defform[(statement-qq statement)]
@defproc[(statement-ast? [v any/c]) boolean?]
]]{
Quasiquotation macro and predicate, respectively, for
@svar[statement].
@examples[#:eval the-eval
(sql-ast->string
(statement-qq (select a b c #:from mytable #:where (> a 10))))
(sql-ast->string
(statement-qq (insert #:into mytable #:set [a 1] [b 2] [c 3])))
]
}
@deftogether[[
@defform[(select-item-qq select-item)]
@defproc[(select-item-ast? [v any/c]) boolean?]
]]{
Quasiquotation macro and predicate, respectively, for
@svar[select-item]. Note that a @svar[select-item] AST cannot be used
with @racket[sql-ast->string] unless it is just @svar[scalar-expr].
A @svar[select-item] AST can be a list of @svar[select-item] ASTs,
representing multiple items to be spliced into the @tt{SELECT}
statement.
@history[#:added "1.6"]}
@; ----------------------------------------
@subsection[#:tag "ddl-statements"]{SQL DDL Statements}
@racketgrammar*[
[ddl-statement create-table-statement
create-view-statement]
[create-table-statement
(@#,lit{create-table} maybe-temp maybe-ifnotexists table-name
#:columns column-def ...
maybe-constraints)
(@#,lit{create-table} maybe-temp maybe-ifnotexists table-name
#:as statement)]
[maybe-temp (code:line) (code:line #:temporary)]
[maybe-ifnotexists (code:line) (code:line #:if-not-exists)]
[column-def [column-ident type column-option ...]]
[column-option #:not-null (code:line #:default scalar-expr)]
[maybe-constraints (code:line)
(code:line #:constraints constraint-decl ...)]
[constraint-decl (@#,lit{constraint} constraint-ident constraint)
constraint]
[constraint (@#,lit{primary-key} column-ident ...)
(@#,lit{unique} column-ident ...)
(@#,lit{check} scalar-expr)
(@#,lit{foreign-key} column-ident ...
#:references table-ident/columns
action-decl ...)]
[action-decl (code:line #:on-delete action)
(code:line #:on-update action)]
[action #:set-null #:set-default #:cascade #:restrict #:no-action]
[create-view
(@#,lit{create-view} maybe-temp view-name
statement)]
]
@history[#:changed "1.1" @elem{Added @racket[#:if-not-exists] option
for @lit{create-table}.}
#:changed "1.4" @elem{Added @svar[action-decl] for @lit{foreign-key}
and the @racket[#:default] @svar[column-option] for @svar[column-def].
}]
@deftogether[[
@defform[(ddl-qq ddl-statement)]
@defproc[(ddl-ast? [v any/c]) boolean?]
]]{
Quasiquotation macro and predicate, respectively, for
@svar[ddl-statement].