r11077: fix perm
[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 (deftest :syntax/group-by/2
255     (clsql:sql
256      (clsql-sys::make-query [foo] [bar] [count [foo]]
257                             :from [table]
258                             :group-by '([foo] [bar])
259                             :order-by '([foo] [bar])))
260   "SELECT FOO,BAR,COUNT(FOO) FROM TABLE GROUP BY FOO,BAR ORDER BY FOO,BAR")
261
262
263 (deftest :syntax/coalesce/1
264     (clsql:sql [coalesce [foo] [bar] "not specified"])
265  "COALESCE(FOO,BAR,'not specified')")
266
267 (deftest :syntax/coalesce/2
268     (clsql:sql [nvl [foo] "not specified"])
269  "COALESCE(FOO,'not specified')")
270
271 (deftest :syntax/nvl/1
272     (clsql:sql [nvl [foo] "not specified"])
273  "COALESCE(FOO,'not specified')")
274
275
276
277 (deftest :syntax/sets/1
278     (clsql:sql [union [select [foo] :from [bar]] [select [baz] :from [bar]]])
279  "SELECT FOO FROM BAR UNION SELECT BAZ FROM BAR")
280
281 (deftest :syntax/sets/2
282     (clsql:sql [intersect [select [foo] :from [bar]] [select [baz] :from [bar]]])
283  "SELECT FOO FROM BAR INTERSECT SELECT BAZ FROM BAR")
284
285 (deftest :syntax/sets/3
286     (clsql:sql [except [select [foo] :from [bar]] [select [baz] :from [bar]]])
287  "SELECT FOO FROM BAR EXCEPT SELECT BAZ FROM BAR")
288
289 (deftest :syntax/sets/4
290     (clsql:sql [minus [select [foo] :from [bar]] [select [baz] :from [bar]]])
291  "SELECT FOO FROM BAR MINUS SELECT BAZ FROM BAR")
292
293
294 (deftest :syntax/function/1
295     (clsql:sql [function "COS" [age]])
296   "COS(AGE)")
297
298 (deftest :syntax/function/2
299     (clsql:sql [function "TO_DATE" "02/06/99" "mm/DD/RR"])
300   "TO_DATE('02/06/99','mm/DD/RR')")
301
302
303 (deftest :syntax/query/1
304     (clsql:sql [select [person_id] [surname] :from [person]])
305   "SELECT PERSON_ID,SURNAME FROM PERSON")
306
307 (deftest :syntax/query/2
308     (clsql:sql [select [foo] [bar *]
309                       :from '([baz] [bar])
310                       :where [or [= [foo] 3]
311                                  [> [baz.quux] 10]]])
312   "SELECT FOO,BAR.* FROM BAZ,BAR WHERE ((FOO = 3) OR (BAZ.QUUX > 10))")
313
314 (deftest :syntax/query/3
315     (clsql:sql [select [foo bar] [baz]
316                       :from '([foo] [quux])
317                       :where [or [> [baz] 3]
318                                  [like [foo bar] "SU%"]]])
319   "SELECT FOO.BAR,BAZ FROM FOO,QUUX WHERE ((BAZ > 3) OR (FOO.BAR LIKE 'SU%'))")
320
321 (deftest :syntax/query/4
322     (clsql:sql [select [count [*]] :from [emp]])
323   "SELECT COUNT(*) FROM EMP")
324
325
326 (deftest :syntax/expression1
327     (clsql:sql
328      (clsql:sql-operation
329       'select
330       (clsql:sql-expression :table 'foo :attribute 'bar)
331       (clsql:sql-expression :attribute 'baz)
332       :from (list
333              (clsql:sql-expression :table 'foo)
334              (clsql:sql-expression :table 'quux))
335       :where
336       (clsql:sql-operation 'or
337                           (clsql:sql-operation
338                            '>
339                            (clsql:sql-expression :attribute 'baz)
340                            3)
341                           (clsql:sql-operation
342                            'like
343                            (clsql:sql-expression :table 'foo
344                                                 :attribute 'bar)
345                            "SU%"))))
346   "SELECT FOO.BAR,BAZ FROM FOO,QUUX WHERE ((BAZ > 3) OR (FOO.BAR LIKE 'SU%'))")
347
348 (deftest :syntax/expression/2
349     (clsql:sql
350      (apply (clsql:sql-operator 'and)
351             (loop for table in '(thistime nexttime sometime never)
352                   for count from 42
353                   collect
354                   [function "BETWEEN"
355                             (clsql:sql-expression :table table
356                                                  :attribute 'bar)
357                             (clsql:sql-operation '* [hip] [hop])
358                             count]
359                   collect
360                   [like (clsql:sql-expression :table table
361                                              :attribute 'baz)
362                         (clsql:sql table)])))
363   "(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'))")
364
365 ))
366
367 #.(clsql:restore-sql-reader-syntax-state)