r9538: Cleaned up symbolic SQL syntax.
[clsql.git] / tests / test-syntax.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; ======================================================================
3 ;;;; File:    test-syntax.lisp
4 ;;;; Author:  Marcus Pearce <m.t.pearce@city.ac.uk>
5 ;;;; Created: 30/03/2004
6 ;;;; Updated: $Id$
7 ;;;; ======================================================================
8 ;;;;
9 ;;;; Description ==========================================================
10 ;;;; ======================================================================
11 ;;;;
12 ;;;; Tests for the CLSQL Symbolic SQL syntax. 
13 ;;;;
14 ;;;; ======================================================================
15
16 (in-package #:clsql-tests)
17
18 #.(clsql:locally-enable-sql-reader-syntax)
19
20 (setq *rt-syntax*
21       '(
22         
23 (deftest :syntax/generic/1
24     (clsql:sql "foo")
25   "'foo'")
26
27 (deftest :syntax/generic/2
28     (clsql:sql 23)
29   "23")
30
31 (deftest :syntax/generic/3
32     (clsql:sql 'bar)
33   "BAR")
34
35 (deftest :syntax/generic/4
36     (clsql:sql '("ten" 10 ten))
37   "('ten',10,TEN)")
38
39 (deftest :syntax/generic/5
40     (clsql:sql ["SELECT FOO,BAR FROM BAZ"])
41   "SELECT FOO,BAR FROM BAZ")
42
43
44 (deftest :syntax/ident/1
45     (clsql:sql [foo])
46   "FOO")
47
48 (deftest :syntax/ident/2
49     (clsql:sql [foo bar])
50   "FOO.BAR")
51
52 (deftest :syntax/ident/3
53     (clsql:sql [foo :integer])
54   "FOO")
55
56 (deftest :syntax/ident/4
57     (clsql:sql [foo bar :integer])
58   "FOO.BAR")
59
60 (deftest :syntax/ident/5
61     (clsql:sql [foo "bar"])
62   "FOO \"bar\"")
63
64 (deftest :syntax/ident/6
65     (clsql:sql ["foo" bar])
66  "\"foo\".BAR")
67
68 (deftest :syntax/ident/7
69     (clsql:sql ["foo" bar :integer])
70  "\"foo\".BAR")
71
72
73
74 (deftest :syntax/subquery/1
75     (clsql:sql [any '(3 4)])
76  "ANY((3,4))")
77
78 (deftest :syntax/subquery/2
79     (clsql:sql [in [foo] '(foo bar baz)])
80   "(FOO IN (FOO,BAR,BAZ))")
81
82 (deftest :syntax/subquery/3
83     (clsql:sql [all '(foo bar baz)])
84   "ALL((FOO,BAR,BAZ))")
85
86 (deftest :syntax/subquery/4
87     (clsql:sql [exists '(foo bar baz)])
88   "EXISTS((FOO,BAR,BAZ))")
89
90 (deftest :syntax/subquery/5
91     (clsql:sql [some '(foo bar baz)])
92   "SOME((FOO,BAR,BAZ))")
93
94
95 (deftest :syntax/aggregate/1 
96     (clsql:sql [max [+ [foo] [* 1000 [bar]]]])
97  "MAX((FOO + (1000 * BAR)))")
98
99 (deftest :syntax/aggregate/2
100     (clsql:sql [avg [+ [foo] [* 1000 [bar]]]])
101  "AVG((FOO + (1000 * BAR)))")
102
103 (deftest :syntax/aggregate/3
104     (clsql:sql [min [+ [foo] [* 1000 [bar]]]])
105  "MIN((FOO + (1000 * BAR)))")
106
107 (deftest :syntax/aggregate/4
108     (clsql:sql [sum [foo] [bar]])
109  "SUM(FOO,BAR)")
110
111 (deftest :syntax/aggregate/5
112     (clsql:sql [count [foo]])
113  "COUNT(FOO)")
114
115
116 (deftest :syntax/logical/1 
117     (clsql:sql [and [foo] [bar]])
118   "(FOO AND BAR)")
119
120 (deftest :syntax/logical/2
121     (clsql:sql [or [foo] [bar]])
122   "(FOO OR BAR)")
123
124 (deftest :syntax/logical/3 
125     (clsql:sql [not [foo]])
126   "(NOT (FOO))")
127
128
129 (deftest :syntax/null/1 
130     (clsql:sql [null [foo]])
131   "(FOO IS NULL)")
132
133 (deftest :syntax/null/2
134     (clsql:sql [not [null [foo]]])
135   "(NOT ((FOO IS NULL)))")
136  
137 (deftest :syntax/null/3
138     (clsql:sql [null])
139   "NULL")
140
141 (deftest :syntax/null/4
142     (clsql:sql [not [null]])
143   "(NOT (NULL))")
144
145
146
147 (deftest :syntax/relational/1
148     (clsql:sql [> [baz] [beep]])
149   "(BAZ > BEEP)")
150
151 (deftest :syntax/relational/2
152     (let ((x 10))
153       (clsql:sql [> [foo] x]))
154   "(FOO > 10)")
155
156 (deftest :syntax/relational/3
157     (clsql:sql [>= [baz] [beep]])
158   "(BAZ >= BEEP)")
159
160 (deftest :syntax/relational/4
161     (clsql:sql [< [baz] [beep]])
162   "(BAZ < BEEP)")
163
164 (deftest :syntax/relational/5
165     (clsql:sql [= [baz] [beep]])
166   "(BAZ = BEEP)")
167
168 (deftest :syntax/relational/6
169     (clsql:sql [<> [baz] [beep]])
170   "(BAZ <> BEEP)")
171
172
173 (deftest :syntax/between/1 
174     (clsql:sql [between [- [foo] 1] [* [bar] 5] [/ [baz] 9]])
175   "(FOO - 1) BETWEEN (BAR * 5) AND (BAZ / 9)")
176
177 (deftest :syntax/between/2 
178     (clsql:sql [not [between [- [foo] 1] [* [bar] 5] [/ [baz] 9]]])
179   "(NOT ((FOO - 1) BETWEEN (BAR * 5) AND (BAZ / 9)))")
180
181
182 (deftest :syntax/arithmetic/1 
183     (clsql:sql [+ [foo bar] [baz]])
184  "(FOO.BAR + BAZ)")
185
186 (deftest :syntax/arithmetic/2
187     (clsql:sql [- [foo bar] [baz]])
188  "(FOO.BAR - BAZ)")
189
190 (deftest :syntax/arithmetic/3
191     (clsql:sql [/ [foo bar] [baz]])
192  "(FOO.BAR / BAZ)")
193
194 (deftest :syntax/arithmetic/4
195     (clsql:sql [* [foo bar] [baz]])
196  "(FOO.BAR * BAZ)")
197
198 (deftest :syntax/arithmetic/5
199     (clsql:sql [- [foo bar]])
200  "(- (FOO.BAR))")
201
202 (deftest :syntax/arithmetic/6
203     (clsql:sql [* 2 3])
204   "(2 * 3)")
205
206
207 (deftest :syntax/substr/1 
208     (clsql:sql [substr [hello] 1 4])
209  "SUBSTR(HELLO,1,4)")
210
211 (deftest :syntax/substring/1 
212     (clsql:sql [substring [hello] 1 4])
213  "SUBSTRING(HELLO,1,4)")
214
215
216 (deftest :syntax/concat/1 
217     (clsql:sql [|| [foo] [bar] [baz]])
218  "(FOO || BAR || BAZ)")
219
220 (deftest :syntax/concat/2
221     (clsql:sql [concat [foo] [bar]])
222  "CONCAT(FOO,BAR)")
223
224
225 (deftest :syntax/pattern/1 
226     (clsql:sql [like [foo] "%v"])
227   "(FOO LIKE '%v')")
228
229 (deftest :syntax/pattern/2
230     (clsql:sql [not [like [foo] "%v"]])
231   "(NOT ((FOO LIKE '%v')))")
232
233
234 (deftest :syntax/distinct/1 
235     (clsql:sql [distinct [foo bar :string]])
236  "DISTINCT FOO.BAR")
237
238 (deftest :syntax/distinct/2
239     (clsql:sql [distinct [foo :string] [bar :integer]])
240  "DISTINCT FOO, BAR")
241
242
243 (deftest :syntax/order-by/1 
244     (clsql:sql [order-by [foo]])
245  "ORDER BY FOO")
246
247 (deftest :syntax/group-by/1 
248     (clsql:sql [group-by [foo]])
249  "GROUP BY FOO")
250
251
252 (deftest :syntax/coalesce/1 
253     (clsql:sql [coalesce [foo] [bar] "not specified"])
254  "COALESCE(FOO,BAR,'not specified')")
255
256 (deftest :syntax/coalesce/2
257     (clsql:sql [nvl [foo] "not specified"])
258  "COALESCE(FOO,'not specified')")
259
260 (deftest :syntax/nvl/1 
261     (clsql:sql [nvl [foo] "not specified"])
262  "COALESCE(FOO,'not specified')")
263
264
265
266 (deftest :syntax/sets/1 
267     (clsql:sql [union [select [foo] :from [bar]] [select [baz] :from [bar]]])
268  "SELECT FOO FROM BAR UNION SELECT BAZ FROM BAR")
269
270 (deftest :syntax/sets/2 
271     (clsql:sql [intersect [select [foo] :from [bar]] [select [baz] :from [bar]]])
272  "SELECT FOO FROM BAR INTERSECT SELECT BAZ FROM BAR")
273
274 (deftest :syntax/sets/3
275     (clsql:sql [except [select [foo] :from [bar]] [select [baz] :from [bar]]])
276  "SELECT FOO FROM BAR EXCEPT SELECT BAZ FROM BAR")
277
278 (deftest :syntax/sets/4
279     (clsql:sql [minus [select [foo] :from [bar]] [select [baz] :from [bar]]])
280  "SELECT FOO FROM BAR MINUS SELECT BAZ FROM BAR")
281
282
283 (deftest :syntax/function/1
284     (clsql:sql [function "COS" [age]])
285   "COS(AGE)")
286
287 (deftest :syntax/function/2
288     (clsql:sql [function "TO_DATE" "02/06/99" "mm/DD/RR"])
289   "TO_DATE('02/06/99','mm/DD/RR')")
290
291
292 (deftest :syntax/query/1
293     (clsql:sql [select [person_id] [surname] :from [person]])
294   "SELECT PERSON_ID,SURNAME FROM PERSON")
295
296 (deftest :syntax/query/2 
297     (clsql:sql [select [foo] [bar *]
298                       :from '([baz] [bar])
299                       :where [or [= [foo] 3]
300                                  [> [baz.quux] 10]]])
301   "SELECT FOO,BAR.* FROM BAZ,BAR WHERE ((FOO = 3) OR (BAZ.QUUX > 10))")
302
303 (deftest :syntax/query/3
304     (clsql:sql [select [foo bar] [baz]
305                       :from '([foo] [quux])
306                       :where [or [> [baz] 3]
307                                  [like [foo bar] "SU%"]]])
308   "SELECT FOO.BAR,BAZ FROM FOO,QUUX WHERE ((BAZ > 3) OR (FOO.BAR LIKE 'SU%'))")
309
310 (deftest :syntax/query/4
311     (clsql:sql [select [count [*]] :from [emp]])
312   "SELECT COUNT(*) FROM EMP")
313   
314
315 (deftest :syntax/expression1
316     (clsql:sql
317      (clsql:sql-operation
318       'select
319       (clsql:sql-expression :table 'foo :attribute 'bar)
320       (clsql:sql-expression :attribute 'baz)
321       :from (list 
322              (clsql:sql-expression :table 'foo)
323              (clsql:sql-expression :table 'quux))
324       :where
325       (clsql:sql-operation 'or 
326                           (clsql:sql-operation
327                            '>
328                            (clsql:sql-expression :attribute 'baz)
329                            3)
330                           (clsql:sql-operation
331                            'like
332                            (clsql:sql-expression :table 'foo
333                                                 :attribute 'bar)
334                            "SU%"))))
335   "SELECT FOO.BAR,BAZ FROM FOO,QUUX WHERE ((BAZ > 3) OR (FOO.BAR LIKE 'SU%'))")
336   
337 (deftest :syntax/expression/2
338     (clsql:sql
339      (apply (clsql:sql-operator 'and)
340             (loop for table in '(thistime nexttime sometime never)
341                   for count from 42
342                   collect
343                   [function "BETWEEN"
344                             (clsql:sql-expression :table table
345                                                  :attribute 'bar)
346                             (clsql:sql-operation '* [hip] [hop])
347                             count]
348                   collect
349                   [like (clsql:sql-expression :table table
350                                              :attribute 'baz)
351                         (clsql:sql table)])))
352   "(BETWEEN(THISTIME.BAR,(HIP * HOP),42) AND (THISTIME.BAZ LIKE 'THISTIME') AND BETWEEN(NEXTTIME.BAR,(HIP * HOP),43) AND (NEXTTIME.BAZ LIKE 'NEXTTIME') AND BETWEEN(SOMETIME.BAR,(HIP * HOP),44) AND (SOMETIME.BAZ LIKE 'SOMETIME') AND BETWEEN(NEVER.BAR,(HIP * HOP),45) AND (NEVER.BAZ LIKE 'NEVER'))")
353
354 ))
355
356 #.(clsql:restore-sql-reader-syntax-state)