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