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
7 ;;;; ======================================================================
9 ;;;; Description ==========================================================
10 ;;;; ======================================================================
12 ;;;; Tests for the CLSQL Symbolic SQL syntax.
14 ;;;; ======================================================================
16 (in-package #:clsql-tests
)
18 #.
(clsql:locally-enable-sql-reader-syntax
)
23 (deftest :syntax
/generic
/1
27 (deftest :syntax
/generic
/2
31 (deftest :syntax
/generic
/3
35 (deftest :syntax
/generic
/4
36 (clsql:sql
'("ten" 10 ten
))
39 (deftest :syntax
/generic
/5
40 (clsql:sql
["SELECT FOO,BAR FROM BAZ"])
41 "SELECT FOO,BAR FROM BAZ")
43 (deftest :syntax
/generic
/6
44 (clsql:sql
"What's up Doc?")
47 (deftest :syntax
/ident
/1
51 (deftest :syntax/ident/2
55 (deftest :syntax/ident/3
56 (clsql:sql [foo :integer])
59 (deftest :syntax/ident/4
60 (clsql:sql [foo bar :integer])
63 (deftest :syntax/ident/5
64 (clsql:sql [foo "bar"])
67 (deftest :syntax/ident/6
68 (clsql:sql ["foo" bar])
71 (deftest :syntax/ident/7
72 (clsql:sql ["foo" bar :integer])
77 (deftest :syntax/subquery/1
78 (clsql:sql [any '(3 4)])
81 (deftest :syntax/subquery/2
82 (clsql:sql [in [foo] '(foo bar baz
)])
83 "(FOO IN (FOO,BAR,BAZ))")
85 (deftest :syntax
/subquery
/3
86 (clsql:sql
[all
'(foo bar baz
)])
89 (deftest :syntax
/subquery
/4
90 (clsql:sql
[exists
'(foo bar baz
)])
91 "EXISTS((FOO,BAR,BAZ))")
93 (deftest :syntax
/subquery
/5
94 (clsql:sql
[some
'(foo bar baz
)])
95 "SOME((FOO,BAR,BAZ))")
98 (deftest :syntax
/aggregate
/1
99 (clsql:sql
[max
[+ [foo] [* 1000 [bar]]]])
100 "MAX((FOO + (1000 * BAR)))")
102 (deftest :syntax/aggregate/2
103 (clsql:sql [avg [+ [foo] [* 1000 [bar]]]])
104 "AVG((FOO + (1000 * BAR)))")
106 (deftest :syntax/aggregate/3
107 (clsql:sql [min [+ [foo] [* 1000 [bar]]]])
108 "MIN((FOO + (1000 * BAR)))")
110 (deftest :syntax
/aggregate
/4
111 (clsql:sql
[sum
[foo] [bar]])
114 (deftest :syntax/aggregate/5
115 (clsql:sql [count [foo]])
119 (deftest :syntax
/logical
/1
120 (clsql:sql
[and
[foo] [bar]])
123 (deftest :syntax/logical/2
124 (clsql:sql [or [foo] [bar]])
127 (deftest :syntax/logical/3
128 (clsql:sql [not [foo]])
132 (deftest :syntax/null/1
133 (clsql:sql [null [foo]])
136 (deftest :syntax/null/2
137 (clsql:sql [not [null [foo]]])
138 "(NOT ((FOO IS NULL)))")
140 (deftest :syntax/null/3
144 (deftest :syntax/null/4
145 (clsql:sql [not [null]])
150 (deftest :syntax/relational/1
151 (clsql:sql [> [baz] [beep]])
154 (deftest :syntax/relational/2
156 (clsql:sql [> [foo] x]))
159 (deftest :syntax/relational/3
160 (clsql:sql [>= [baz] [beep]])
163 (deftest :syntax/relational/4
164 (clsql:sql [< [baz] [beep]])
167 (deftest :syntax/relational/5
168 (clsql:sql [= [baz] [beep]])
171 (deftest :syntax/relational/6
172 (clsql:sql [<> [baz] [beep]])
176 (deftest :syntax/between/1
177 (clsql:sql [between [- [foo] 1] [* [bar] 5] [/ [baz] 9]])
178 "(FOO - 1) BETWEEN (BAR * 5) AND (BAZ / 9)")
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)))")
185 (deftest :syntax
/arithmetic
/1
186 (clsql:sql
[+ [foo bar
] [baz]])
189 (deftest :syntax/arithmetic/2
190 (clsql:sql [- [foo bar] [baz]])
193 (deftest :syntax
/arithmetic
/3
194 (clsql:sql
[/ [foo bar
] [baz]])
197 (deftest :syntax/arithmetic/4
198 (clsql:sql [* [foo bar] [baz]])
201 (deftest :syntax
/arithmetic
/5
202 (clsql:sql
[-
[foo bar
]])
205 (deftest :syntax
/arithmetic
/6
210 (deftest :syntax
/substr
/1
211 (clsql:sql
[substr
[hello] 1 4])
214 (deftest :syntax/substring/1
215 (clsql:sql [substring [hello] 1 4])
216 "SUBSTRING(HELLO,1,4)")
219 (deftest :syntax
/concat
/1
220 (clsql:sql
[||
[foo] [bar] [baz]])
221 "(FOO || BAR || BAZ)")
223 (deftest :syntax/concat/2
224 (clsql:sql [concat [foo] [bar]])
228 (deftest :syntax/pattern/1
229 (clsql:sql [like [foo] "%v"])
232 (deftest :syntax/pattern/2
233 (clsql:sql [not [like [foo] "%v"]])
234 "(NOT ((FOO LIKE '%v')))")
237 (deftest :syntax/distinct/1
238 (clsql:sql [distinct [foo bar :string]])
241 (deftest :syntax/distinct/2
242 (clsql:sql [distinct [foo :string] [bar :integer]])
246 (deftest :syntax/order-by/1
247 (clsql:sql [order-by [foo]])
250 (deftest :syntax/group-by/1
251 (clsql:sql [group-by [foo]])
254 (deftest :syntax/group-by/2
256 (clsql-sys::make-query [foo] [bar] [count
[foo]]
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")
263 (deftest :syntax
/coalesce
/1
264 (clsql:sql
[coalesce
[foo] [bar] "not specified"])
265 "COALESCE(FOO,BAR,'not specified')")
267 (deftest :syntax/coalesce/2
268 (clsql:sql [nvl [foo] "not specified"])
269 "COALESCE(FOO,'not specified')")
271 (deftest :syntax
/nvl
/1
272 (clsql:sql
[nvl
[foo] "not specified"])
273 "COALESCE(FOO,'not specified')")
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")
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")
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")
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")
294 (deftest :syntax/function/1
295 (clsql:sql [function "COS" [age]])
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')")
303 (deftest :syntax/query/1
304 (clsql:sql [select [person_id] [surname] :from [person]])
305 "SELECT PERSON_ID,SURNAME FROM PERSON")
307 (deftest :syntax/query/2
308 (clsql:sql [select [foo] [bar
*]
310 :where [or [= [foo] 3]
312 "SELECT FOO,BAR.* FROM BAZ,BAR WHERE ((FOO = 3) OR (BAZ.QUUX > 10))")
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%'))")
321 (deftest :syntax/query/4
322 (clsql:sql [select [count [*]] :from [emp]])
323 "SELECT COUNT(*) FROM EMP")
326 (deftest :syntax/expression1
330 (clsql:sql-expression :table 'foo :attribute 'bar)
331 (clsql:sql-expression :attribute 'baz)
333 (clsql:sql-expression :table 'foo)
334 (clsql:sql-expression :table 'quux))
336 (clsql:sql-operation 'or
339 (clsql:sql-expression :attribute 'baz)
343 (clsql:sql-expression :table 'foo
346 "SELECT FOO.BAR,BAZ FROM FOO,QUUX WHERE ((BAZ > 3) OR (FOO.BAR LIKE 'SU%'))")
348 (deftest :syntax/expression/2
350 (apply (clsql:sql-operator 'and)
351 (loop for table in '(thistime nexttime sometime never)
355 (clsql:sql-expression :table table
357 (clsql:sql-operation '* [hip] [hop])
360 [like (clsql:sql-expression :table table
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'))")
367 #.(clsql:restore-sql-reader-syntax-state)