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