1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
6 ;;;; Definition of SQL operations used with the symbolic SQL syntax.
8 ;;;; This file is part of CLSQL.
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 ;;;; *************************************************************************
17 ;; Keep a hashtable for mapping symbols to sql generator functions,
18 ;; for use by the bracketed reader syntax.
20 (defvar *sql-op-table* (make-hash-table :test #'equal))
23 ;; Define an SQL operation type.
25 (defmacro defsql (function definition-keys &body body)
27 (defun ,function ,@body)
28 (let ((symbol (cadr (member :symbol ',definition-keys))))
29 (setf (gethash (if symbol (symbol-name-default-case symbol) ',function)
36 (defsql sql-query (:symbol "select") (&rest args)
37 (apply #'make-query args))
39 (defsql sql-any (:symbol "any") (&rest rest)
40 (make-instance 'sql-value-exp
41 :modifier 'any :components rest))
43 (defsql sql-all (:symbol "all") (&rest rest)
44 (make-instance 'sql-value-exp
45 :modifier 'all :components rest))
47 (defsql sql-not (:symbol "not") (&rest rest)
48 (make-instance 'sql-value-exp
49 :modifier 'not :components rest))
51 (defsql sql-union (:symbol "union") (&rest rest)
52 (make-instance 'sql-value-exp
53 :modifier 'union :components rest))
55 (defsql sql-intersect (:symbol "intersect") (&rest rest)
56 (make-instance 'sql-value-exp
57 :modifier 'intersect :components rest))
59 (defsql sql-minus (:symbol "minus") (&rest rest)
60 (make-instance 'sql-value-exp
61 :modifier 'minus :components rest))
63 (defsql sql-group-by (:symbol "group-by") (&rest rest)
64 (make-instance 'sql-value-exp
65 :modifier 'group-by :components rest))
67 (defsql sql-limit (:symbol "limit") (&rest rest)
68 (make-instance 'sql-value-exp
69 :modifier 'limit :components rest))
71 (defsql sql-having (:symbol "having") (&rest rest)
72 (make-instance 'sql-value-exp
73 :modifier 'having :components rest))
75 (defsql sql-null (:symbol "null") (&rest rest)
77 (make-instance 'sql-relational-exp :operator '|IS NULL|
78 :sub-expressions (list (car rest)))
79 (make-instance 'sql-value-exp :components 'null)))
81 (defsql sql-not-null (:symbol "not-null") ()
82 (make-instance 'sql-value-exp
83 :components '|NOT NULL|))
85 (defsql sql-exists (:symbol "exists") (&rest rest)
86 (make-instance 'sql-value-exp
87 :modifier 'exists :components rest))
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)))
95 (defsql sql-+ (:symbol "+") (&rest rest)
97 (make-instance 'sql-relational-exp
98 :operator '+ :sub-expressions rest)
99 (make-instance 'sql-value-exp :modifier '+ :components rest)))
101 (defsql sql-/ (:symbol "/") (&rest rest)
102 (make-instance 'sql-relational-exp
103 :operator '/ :sub-expressions rest))
105 (defsql sql-- (:symbol "-") (&rest rest)
107 (make-instance 'sql-relational-exp
108 :operator '- :sub-expressions rest)
109 (make-instance 'sql-value-exp :modifier '- :components rest)))
111 (defsql sql-like (:symbol "like") (&rest rest)
112 (make-instance 'sql-relational-exp
113 :operator 'like :sub-expressions rest))
115 (defsql sql-uplike (:symbol "uplike") (&rest rest)
116 (make-instance 'sql-upcase-like
117 :sub-expressions rest))
119 (defsql sql-and (:symbol "and") (&rest rest)
120 (make-instance 'sql-relational-exp
121 :operator 'and :sub-expressions rest))
123 (defsql sql-or (:symbol "or") (&rest rest)
124 (make-instance 'sql-relational-exp
125 :operator 'or :sub-expressions rest))
127 (defsql sql-in (:symbol "in") (&rest rest)
128 (make-instance 'sql-relational-exp
129 :operator 'in :sub-expressions rest))
131 (defsql sql-|| (:symbol "||") (&rest rest)
132 (make-instance 'sql-relational-exp
133 :operator '|| :sub-expressions rest))
135 (defsql sql-is (:symbol "is") (&rest rest)
136 (make-instance 'sql-relational-exp
137 :operator 'is :sub-expressions rest))
139 (defsql sql-= (:symbol "=") (&rest rest)
140 (make-instance 'sql-relational-exp
141 :operator '= :sub-expressions rest))
143 (defsql sql-== (:symbol "==") (&rest rest)
144 (make-instance 'sql-assignment-exp
145 :operator '= :sub-expressions rest))
147 (defsql sql-< (:symbol "<") (&rest rest)
148 (make-instance 'sql-relational-exp
149 :operator '< :sub-expressions rest))
152 (defsql sql-> (:symbol ">") (&rest rest)
153 (make-instance 'sql-relational-exp
154 :operator '> :sub-expressions rest))
156 (defsql sql-<> (:symbol "<>") (&rest rest)
157 (make-instance 'sql-relational-exp
158 :operator '<> :sub-expressions rest))
160 (defsql sql->= (:symbol ">=") (&rest rest)
161 (make-instance 'sql-relational-exp
162 :operator '>= :sub-expressions rest))
164 (defsql sql-<= (:symbol "<=") (&rest rest)
165 (make-instance 'sql-relational-exp
166 :operator '<= :sub-expressions rest))
168 (defsql sql-count (:symbol "count") (&rest rest)
169 (make-instance 'sql-function-exp
170 :name 'count :args rest))
172 (defsql sql-max (:symbol "max") (&rest rest)
173 (make-instance 'sql-function-exp
174 :name 'max :args rest))
176 (defsql sql-min (:symbol "min") (&rest rest)
177 (make-instance 'sql-function-exp
178 :name 'min :args rest))
180 (defsql sql-avg (:symbol "avg") (&rest rest)
181 (make-instance 'sql-function-exp
182 :name 'avg :args rest))
184 (defsql sql-sum (:symbol "sum") (&rest rest)
185 (make-instance 'sql-function-exp
186 :name 'sum :args rest))
188 (defsql sql-the (:symbol "the") (&rest rest)
189 (make-instance 'sql-typecast-exp
190 :modifier (first rest) :components (second rest)))
192 (defsql sql-function (:symbol "function") (&rest args)
193 (make-instance 'sql-function-exp
194 :name (make-symbol (car args)) :args (cdr args)))
196 ;;(defsql sql-distinct (:symbol "distinct") (&rest rest)
199 ;;(defsql sql-between (:symbol "between") (&rest rest)