r8848: more usql to clsql renaming
[clsql.git] / sql / operations.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; ======================================================================
3 ;;;; File:    operations.lisp
4 ;;;; Updated: <04/04/2004 12:07:26 marcusp>
5 ;;;; ======================================================================
6 ;;;;
7 ;;;; Description ==========================================================
8 ;;;; ======================================================================
9 ;;;;
10 ;;;; Definition of SQL operations used with the symbolic SQL syntax. 
11 ;;;;
12 ;;;; ======================================================================
13
14 (in-package :clsql-usql-sys)
15
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 (string-upcase 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-value-exp
41                  :modifier 'any :components rest))
42
43 (defsql sql-all (:symbol "all") (&rest rest)
44   (make-instance 'sql-value-exp
45                  :modifier 'all :components rest))
46
47 (defsql sql-not (:symbol "not") (&rest rest)
48   (make-instance 'sql-value-exp
49                  :modifier 'not :components rest))
50
51 (defsql sql-union (:symbol "union") (&rest rest)
52   (make-instance 'sql-value-exp
53                  :modifier 'union :components rest))
54
55 (defsql sql-intersect (:symbol "intersect") (&rest rest)
56   (make-instance 'sql-value-exp
57                  :modifier 'intersect :components rest))
58
59 (defsql sql-minus (:symbol "minus") (&rest rest)
60   (make-instance 'sql-value-exp
61                  :modifier 'minus :components rest))
62
63 (defsql sql-group-by (:symbol "group-by") (&rest rest)
64   (make-instance 'sql-value-exp
65                  :modifier 'group-by :components rest))
66
67 (defsql sql-limit (:symbol "limit") (&rest rest)
68   (make-instance 'sql-value-exp
69                  :modifier 'limit :components rest))
70
71 (defsql sql-having (:symbol "having") (&rest rest)
72   (make-instance 'sql-value-exp
73                  :modifier 'having :components rest))
74
75 (defsql sql-null (:symbol "null") (&rest rest)
76   (if rest
77       (make-instance 'sql-relational-exp :operator '|IS NULL| 
78                      :sub-expressions (list (car rest)))
79       (make-instance 'sql-value-exp :components 'null)))
80
81 (defsql sql-not-null (:symbol "not-null") ()
82   (make-instance 'sql-value-exp
83                  :components '|NOT NULL|))
84
85 (defsql sql-exists (:symbol "exists") (&rest rest)
86   (make-instance 'sql-value-exp
87                  :modifier 'exists :components rest))
88
89 (defsql sql-* (:symbol "*") (&rest rest)
90   (if (zerop (length rest))
91       (make-instance 'sql-ident :name '*)
92       ;(error 'clsql-sql-syntax-error :reason "'*' with arguments")))
93       (make-instance 'sql-relational-exp :operator '* :sub-expressions rest)))
94
95 (defsql sql-+ (:symbol "+") (&rest rest)
96   (if (cdr rest)
97       (make-instance 'sql-relational-exp
98                      :operator '+ :sub-expressions rest)
99       (make-instance 'sql-value-exp :modifier '+ :components rest)))
100
101 (defsql sql-/ (:symbol "/") (&rest rest)
102   (make-instance 'sql-relational-exp
103                  :operator '/ :sub-expressions rest))
104
105 (defsql sql-- (:symbol "-") (&rest rest)
106         (if (cdr rest)
107             (make-instance 'sql-relational-exp
108                            :operator '- :sub-expressions rest)
109             (make-instance 'sql-value-exp :modifier '- :components rest)))
110
111 (defsql sql-like (:symbol "like") (&rest rest)
112   (make-instance 'sql-relational-exp
113                  :operator 'like :sub-expressions rest))
114
115 (defsql sql-uplike (:symbol "uplike") (&rest rest)
116   (make-instance 'sql-upcase-like
117                  :sub-expressions rest))
118
119 (defsql sql-and (:symbol "and") (&rest rest)
120   (make-instance 'sql-relational-exp
121                  :operator 'and :sub-expressions rest))
122
123 (defsql sql-or (:symbol "or") (&rest rest)
124   (make-instance 'sql-relational-exp
125                  :operator 'or :sub-expressions rest))
126
127 (defsql sql-in (:symbol "in") (&rest rest)
128   (make-instance 'sql-relational-exp
129                  :operator 'in :sub-expressions rest))
130
131 (defsql sql-|| (:symbol "||") (&rest rest)
132     (make-instance 'sql-relational-exp
133                  :operator '|| :sub-expressions rest))
134
135 (defsql sql-is (:symbol "is") (&rest rest)
136   (make-instance 'sql-relational-exp
137                  :operator 'is :sub-expressions rest))
138
139 (defsql sql-= (:symbol "=") (&rest rest)
140   (make-instance 'sql-relational-exp
141                  :operator '= :sub-expressions rest))
142
143 (defsql sql-== (:symbol "==") (&rest rest)
144   (make-instance 'sql-assignment-exp
145                  :operator '= :sub-expressions rest))
146
147 (defsql sql-< (:symbol "<") (&rest rest)
148   (make-instance 'sql-relational-exp
149                  :operator '< :sub-expressions rest))
150
151
152 (defsql sql-> (:symbol ">") (&rest rest)
153   (make-instance 'sql-relational-exp
154                  :operator '> :sub-expressions rest))
155
156 (defsql sql-<> (:symbol "<>") (&rest rest)
157         (make-instance 'sql-relational-exp
158                        :operator '<> :sub-expressions rest))
159
160 (defsql sql->= (:symbol ">=") (&rest rest)
161   (make-instance 'sql-relational-exp
162                  :operator '>= :sub-expressions rest))
163
164 (defsql sql-<= (:symbol "<=") (&rest rest)
165   (make-instance 'sql-relational-exp
166                  :operator '<= :sub-expressions rest))
167
168 (defsql sql-count (:symbol "count") (&rest rest)
169   (make-instance 'sql-function-exp
170                  :name 'count :args rest))
171
172 (defsql sql-max (:symbol "max") (&rest rest)
173   (make-instance 'sql-function-exp
174                  :name 'max :args rest))
175
176 (defsql sql-min (:symbol "min") (&rest rest)
177   (make-instance 'sql-function-exp
178                  :name 'min :args rest))
179
180 (defsql sql-avg (:symbol "avg") (&rest rest)
181   (make-instance 'sql-function-exp
182                  :name 'avg :args rest))
183
184 (defsql sql-sum (:symbol "sum") (&rest rest)
185   (make-instance 'sql-function-exp
186                  :name 'sum :args rest))
187
188 (defsql sql-the (:symbol "the") (&rest rest)
189   (make-instance 'sql-typecast-exp
190                  :modifier (first rest) :components (second rest)))
191
192 (defsql sql-function (:symbol "function") (&rest args)
193         (make-instance 'sql-function-exp
194                        :name (make-symbol (car args)) :args (cdr args)))
195
196 ;;(defsql sql-distinct (:symbol "distinct") (&rest rest)
197 ;;  nil)
198
199 ;;(defsql sql-between (:symbol "between") (&rest rest)
200 ;;  nil)
201