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