37d751e0f90abc09189de627df483e1aab454119
[clsql.git] / sql / operations.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;;
4 ;;;; Definition of SQL operations used with the symbolic SQL syntax.
5 ;;;;
6 ;;;; This file is part of CLSQL.
7 ;;;;
8 ;;;; CLSQL users are granted the rights to distribute and use this software
9 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
10 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
11 ;;;; *************************************************************************
12
13 (in-package #:clsql-sys)
14
15 ;; Keep a hashtable for mapping symbols to sql generator functions,
16 ;; for use by the bracketed reader syntax.
17
18 (defvar *sql-op-table* (make-hash-table :test #'equal))
19
20
21 ;; Define an SQL operation type.
22
23 (defmacro defsql (function definition-keys &body body)
24   `(progn
25      (defun ,function ,@body)
26      (let ((symbol (cadr (member :symbol ',definition-keys))))
27        (setf (gethash (if symbol (symbol-name-default-case symbol) ',function)
28                       *sql-op-table*)
29              ',function))))
30
31
32 ;; SQL operations
33
34 (defsql sql-query (:symbol "select") (&rest args)
35   (apply #'make-query args))
36
37 (defsql sql-any (:symbol "any") (&rest rest)
38   (make-instance 'sql-function-exp
39                  :name 'any :args rest))
40
41 (defsql sql-some (:symbol "some") (&rest rest)
42   (make-instance 'sql-function-exp
43                  :name 'some :args rest))
44
45 (defsql sql-all (:symbol "all") (&rest rest)
46   (make-instance 'sql-function-exp
47                  :name 'all :args rest))
48
49 (defsql sql-not (:symbol "not") (&rest rest)
50   (make-instance 'sql-value-exp
51                  :modifier 'not :components rest))
52
53 (defsql sql-union (:symbol "union") (&rest rest)
54   (make-instance 'sql-set-exp
55                  :operator 'union :sub-expressions rest))
56
57 (defsql sql-intersect (:symbol "intersect") (&rest rest)
58   (make-instance 'sql-set-exp
59                  :operator 'intersect :sub-expressions rest))
60
61 (defsql sql-except (:symbol "except") (&rest rest)
62   (make-instance 'sql-set-exp
63                  :operator 'except :sub-expressions rest))
64
65 (defsql sql-minus (:symbol "minus") (&rest rest)
66   (make-instance 'sql-set-exp
67                  :operator 'minus :sub-expressions rest))
68
69 (defsql sql-limit (:symbol "limit") (&rest rest)
70   (make-instance 'sql-query-modifier-exp
71                  :modifier 'limit :components rest))
72
73 (defsql sql-group-by (:symbol "group-by") (&rest rest)
74   (make-instance 'sql-query-modifier-exp
75                  :modifier '|GROUP BY| :components rest))
76
77 (defsql sql-order-by (:symbol "order-by") (&rest rest)
78   (make-instance 'sql-query-modifier-exp
79                  :modifier '|ORDER BY| :components rest))
80
81 (defsql sql-having (:symbol "having") (&rest rest)
82   (make-instance 'sql-query-modifier-exp
83                  :modifier 'having :components rest))
84
85 (defsql sql-null (:symbol "null") (&optional not-null-thing)
86   (if not-null-thing
87       (make-instance 'sql-relational-exp :operator 'is
88                      :sub-expressions (list not-null-thing nil))
89       (make-instance 'sql-value-exp :components 'null)))
90
91 (defsql sql-not-null (:symbol "not-null") (&optional not-null-thing)
92   (if not-null-thing
93       (make-instance
94        'sql-relational-exp
95        :operator 'IS
96        :sub-expressions (list not-null-thing
97                               (sql-expression :string "NOT NULL")))
98       (sql-expression :string "NOT NULL")))
99
100 (defsql sql-exists (:symbol "exists") (&rest rest)
101   (make-instance 'sql-function-exp
102                  :name 'exists :args rest))
103
104 (defsql sql-* (:symbol "*") (&rest rest)
105   (if (zerop (length rest))
106       (make-instance 'sql-ident :name '*)
107       (make-instance 'sql-relational-exp :operator '* :sub-expressions rest)))
108
109 (defsql sql-+ (:symbol "+") (&rest rest)
110   (if (cdr rest)
111       (make-instance 'sql-relational-exp
112                      :operator '+ :sub-expressions rest)
113       (make-instance 'sql-value-exp :modifier '+ :components rest)))
114
115 (defsql sql-/ (:symbol "/") (&rest rest)
116   (make-instance 'sql-relational-exp
117                  :operator '/ :sub-expressions rest))
118
119 (defsql sql-- (:symbol "-") (&rest rest)
120         (if (cdr rest)
121             (make-instance 'sql-relational-exp
122                            :operator '- :sub-expressions rest)
123             (make-instance 'sql-value-exp :modifier '- :components rest)))
124
125 (defsql sql-like (:symbol "like") (&rest rest)
126   (make-instance 'sql-relational-exp
127                  :operator 'like :sub-expressions rest))
128
129 (defsql sql-uplike (:symbol "uplike") (&rest rest)
130   (make-instance 'sql-upcase-like
131                  :sub-expressions rest))
132
133 (defsql sql-and (:symbol "and") (&rest rest)
134   (make-instance 'sql-relational-exp
135                  :operator 'and :sub-expressions rest))
136
137 (defsql sql-or (:symbol "or") (&rest rest)
138   (make-instance 'sql-relational-exp
139                  :operator 'or :sub-expressions rest))
140
141 (defsql sql-in (:symbol "in") (&rest rest)
142   (make-instance 'sql-relational-exp
143                  :operator 'in :sub-expressions rest))
144
145 (defsql sql-concat-op (:symbol "concat-op") (&rest rest)
146   (make-instance 'sql-relational-exp
147                  :operator '\|\| :sub-expressions rest))
148
149 (defsql sql-concat (:symbol "concat") (&rest rest)
150   (make-instance 'sql-function-exp
151                  :name 'concat :args rest))
152
153 (defsql sql-substr (:symbol "substr") (&rest rest)
154   (if (= (length rest) 3)
155       (make-instance 'sql-function-exp
156                      :name 'substr :args rest)
157       (error 'sql-user-error :message "SUBSTR must have 3 arguments.")))
158
159 (defsql sql-substring (:symbol "substring") (&rest rest)
160   (if (= (length rest) 3)
161       (make-instance 'sql-function-exp
162                      :name 'substring :args rest)
163       (error 'sql-user-error :message "SUBSTRING must have 3 arguments.")))
164
165 (defsql sql-is (:symbol "is") (&rest rest)
166   (make-instance 'sql-relational-exp
167                  :operator 'is :sub-expressions rest))
168
169 (defsql sql-= (:symbol "=") (&rest rest)
170   (make-instance 'sql-relational-exp
171                  :operator '= :sub-expressions rest))
172
173 (defsql sql-== (:symbol "==") (&rest rest)
174   (make-instance 'sql-assignment-exp
175                  :operator '= :sub-expressions rest))
176
177 (defsql sql-< (:symbol "<") (&rest rest)
178   (make-instance 'sql-relational-exp
179                  :operator '< :sub-expressions rest))
180
181
182 (defsql sql-> (:symbol ">") (&rest rest)
183   (make-instance 'sql-relational-exp
184                  :operator '> :sub-expressions rest))
185
186 (defsql sql-<> (:symbol "<>") (&rest rest)
187         (make-instance 'sql-relational-exp
188                        :operator '<> :sub-expressions rest))
189
190 (defsql sql->= (:symbol ">=") (&rest rest)
191   (make-instance 'sql-relational-exp
192                  :operator '>= :sub-expressions rest))
193
194 (defsql sql-<= (:symbol "<=") (&rest rest)
195   (make-instance 'sql-relational-exp
196                  :operator '<= :sub-expressions rest))
197
198 (defsql sql-count (:symbol "count") (&rest rest)
199   (make-instance 'sql-function-exp
200                  :name 'count :args rest))
201
202 (defsql sql-max (:symbol "max") (&rest rest)
203   (make-instance 'sql-function-exp
204                  :name 'max :args rest))
205
206 (defsql sql-min (:symbol "min") (&rest rest)
207   (make-instance 'sql-function-exp
208                  :name 'min :args rest))
209
210 (defsql sql-avg (:symbol "avg") (&rest rest)
211   (make-instance 'sql-function-exp
212                  :name 'avg :args rest))
213
214 (defsql sql-sum (:symbol "sum") (&rest rest)
215   (make-instance 'sql-function-exp
216                  :name 'sum :args rest))
217
218 (defsql sql-the (:symbol "the") (&rest rest)
219   (make-instance 'sql-typecast-exp
220                  :modifier (first rest) :components (second rest)))
221
222 (defsql sql-function (:symbol "function") (&rest args)
223         (make-instance 'sql-function-exp
224                        :name (make-symbol (car args)) :args (cdr args)))
225
226 (defsql sql-between (:symbol "between") (&rest rest)
227   (if (= (length rest) 3)
228       (make-instance 'sql-between-exp :name 'between :args rest)
229       (error 'sql-user-error :message "BETWEEN must have 3 arguments.")))
230
231 (defsql sql-distinct (:symbol "distinct") (&rest rest)
232   (make-instance 'sql-query-modifier-exp :modifier 'distinct
233                  :components rest))
234
235 (defsql sql-coalesce (:symbol "coalesce") (&rest rest)
236   (make-instance 'sql-function-exp
237                  :name 'coalesce :args rest))
238
239 (defsql sql-nvl (:symbol "nvl") (&rest rest)
240   (if (= (length rest) 2)
241       (make-instance 'sql-function-exp
242                      :name 'coalesce :args rest)
243       (error 'sql-user-error :message "NVL accepts exactly 2 arguments.")))
244
245 (defsql sql-userenv (:symbol "userenv") (&rest rest)
246   (make-instance 'sql-function-exp
247                  :name 'userenv :args rest))
248
249 (defsql sql-lower  (:symbol "lower") (&rest rest)
250   (if (= (length rest) 1)
251       (make-instance 'sql-function-exp
252                      :name 'lower :args rest)
253     (error 'sql-user-error :message "LOWER must have 1 argument.")))
254
255 (defsql sql-upper  (:symbol "upper") (&rest rest)
256   (if (= (length rest) 1)
257       (make-instance 'sql-function-exp
258                      :name 'upper :args rest)
259     (error 'sql-user-error :message "UPPER must have 1 argument.")))