a898dfb47baf7aeef3a4038ed49309244c5ef05a
[clsql.git] / sql / syntax.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;;
4 ;;;; $Id$
5 ;;;;
6 ;;;; CLSQL square bracket symbolic query syntax. Functions for
7 ;;;; enabling and disabling the syntax and for building SQL
8 ;;;; expressions using the syntax.
9 ;;;;
10 ;;;; This file is part of CLSQL.
11 ;;;;
12 ;;;; CLSQL users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
14 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
15 ;;;; *************************************************************************
16
17 (in-package #:clsql-sys)
18
19 (defvar *original-reader-enter* nil)
20
21 (defvar *original-reader-exit* nil)
22
23 (defvar *sql-macro-open-char* #\[)
24
25 (defvar *sql-macro-close-char* #\])
26
27 (defvar *restore-sql-reader-syntax* nil)
28
29
30 ;; Exported functions for disabling SQL syntax.
31
32 (defmacro disable-sql-reader-syntax ()
33   "Turns off the SQL reader syntax setting the syntax state such
34 that if the syntax is subsequently enabled,
35 RESTORE-SQL-READER-SYNTAX-STATE will disable it again."
36   '(eval-when (:compile-toplevel :load-toplevel :execute)
37     (setf *restore-sql-reader-syntax* nil)
38     (%disable-sql-reader-syntax)))
39
40 (defmacro locally-disable-sql-reader-syntax ()
41   "Turns off the SQL reader syntax without changing the syntax
42 state such that RESTORE-SQL-READER-SYNTAX-STATE will re-establish
43 the current syntax state."
44   '(eval-when (:compile-toplevel :load-toplevel :execute)
45     (%disable-sql-reader-syntax)))
46
47 (defun %disable-sql-reader-syntax ()
48   (when *original-reader-enter*
49     (set-macro-character *sql-macro-open-char* *original-reader-enter*))
50   (setf *original-reader-enter* nil)
51   (values))
52
53
54 ;; Exported functions for enabling SQL syntax.
55
56 (defmacro enable-sql-reader-syntax ()
57   "Turns on the SQL reader syntax setting the syntax state such
58 that if the syntax is subsequently disabled,
59 RESTORE-SQL-READER-SYNTAX-STATE will enable it again."
60   '(eval-when (:compile-toplevel :load-toplevel :execute)
61     (setf *restore-sql-reader-syntax* t)
62     (%enable-sql-reader-syntax)))
63
64 (defmacro locally-enable-sql-reader-syntax ()
65   "Turns on the SQL reader syntax without changing the syntax
66 state such that RESTORE-SQL-READER-SYNTAX-STATE will re-establish
67 the current syntax state."
68   '(eval-when (:compile-toplevel :load-toplevel :execute)
69     (%enable-sql-reader-syntax)))
70
71 (defun %enable-sql-reader-syntax ()
72   (unless *original-reader-enter*
73     (setf *original-reader-enter* (get-macro-character *sql-macro-open-char*)))
74   (set-macro-character *sql-macro-open-char* #'sql-reader-open)
75   (enable-sql-close-syntax)
76   (values))
77
78 (defmacro restore-sql-reader-syntax-state ()
79   "Enables the SQL reader syntax if ENABLE-SQL-READER-SYNTAX has
80 been called more recently than DISABLE-SQL-READER-SYNTAX and
81 otherwise disables the SQL reader syntax. By default, the SQL
82 reader syntax is disabled."
83   '(eval-when (:compile-toplevel :load-toplevel :execute)
84     (if *restore-sql-reader-syntax*
85         (%enable-sql-reader-syntax)
86         (%disable-sql-reader-syntax))))
87
88 (defun sql-reader-open (stream char)
89   (declare (ignore char))
90   (let ((sqllist (read-delimited-list #\] stream t)))
91     (handler-case
92         (cond ((string= (write-to-string (car sqllist)) "||")
93                (cons (sql-operator 'concat-op) (cdr sqllist)))
94               ((and (= (length sqllist) 1) (eql (car sqllist) '*))
95                (apply #'generate-sql-reference sqllist))
96               ((sql-operator (car sqllist))
97                (cons (sql-operator (car sqllist)) (cdr sqllist)))
98               (t (apply #'generate-sql-reference sqllist)))
99       (sql-user-error (c)
100         (error 'sql-user-error
101                :message (format nil "Error ~A occured while attempting to parse '~A' at file position ~A"
102                                 (sql-user-error-message c) sqllist (file-position stream)))))))
103
104 (defun disable-sql-close-syntax ()
105   "Internal function that disables the close syntax when leaving
106   sql context."
107   (set-macro-character *sql-macro-close-char* *original-reader-exit*)
108   (setf *original-reader-exit* nil))
109
110 (defun enable-sql-close-syntax ()
111   "Internal function that enables close syntax when entering SQL
112   context."
113   (setf *original-reader-exit* (get-macro-character *sql-macro-close-char*))
114   (set-macro-character *sql-macro-close-char* (get-macro-character #\))))
115
116 (defun generate-sql-reference (&rest arglist)
117   (cond ((= (length arglist) 1) ; string, table or attribute
118          (if (stringp (car arglist))
119              (sql-expression :string (car arglist))
120              (sql-expression :attribute (car arglist))))
121         ((<= 2 (length arglist))
122          (let ((sqltype (when (keywordp (caddr arglist)) (caddr arglist) nil)))
123            (cond
124              ((stringp (cadr arglist))
125              (sql-expression :table (car arglist)
126                              :alias (cadr arglist)
127                              :type sqltype))
128             ((keywordp (cadr arglist))
129              (sql-expression :attribute (car arglist)
130                              :type (cadr arglist)))
131             (t
132              (sql-expression :attribute (cadr arglist)
133                              :table (car arglist)
134                              :type sqltype)))))
135         (t
136          (error 'sql-user-error :message "bad expression syntax"))))
137
138
139 ;; Exported functions for dealing with SQL syntax 
140
141 (defun sql (&rest args)
142   "Returns an SQL string generated from the expressions ARGS. The
143 expressions are translated into SQL strings and then concatenated
144 with a single space delimiting each expression. An error of type
145 SQL-USER-ERROR is signalled if any element in ARGS is not of the
146 supported types (a symbol, string, number or symbolic SQL
147 expression) or a list or vector containing only these supported
148 types."
149   (format nil "~{~A~^ ~}" (mapcar #'sql-output args)))
150
151 (defun sql-expression (&key string table alias attribute type)
152   "Returns an SQL expression constructed from the supplied
153 arguments which may be combined as follows: ATTRIBUTE and TYPE;
154 ATTRIBUTE; ALIAS or TABLE and ATTRIBUTE and TYPE; ALIAS or TABLE
155 and ATTRIBUTE; TABLE, ATTRIBUTE and TYPE; TABLE and ATTRIBUTE;
156 TABLE and ALIAS; TABLE; and STRING. An error of type
157 SQL-USER-ERROR is signalled if an unsupported combination of
158 keyword arguments is specified."
159   (cond
160     (string
161      (make-instance 'sql :string string))
162     (attribute
163      (make-instance 'sql-ident-attribute  :name attribute
164                     :qualifier (or table alias)
165                     :type type))
166     ((and table (not attribute))
167      (make-instance 'sql-ident-table :name table
168                     :table-alias alias))))
169
170 (defun sql-operator (operator)
171   "Returns the Lisp symbol corresponding to the SQL operator
172   represented by the symbol OPERATOR. If OPERATOR does not
173   represent a supported SQL operator or is not a symbol, nil is
174   returned."
175   (typecase operator
176     (string nil)
177     (symbol (values (gethash (symbol-name-default-case (symbol-name operator))
178                              *sql-op-table*)))))
179
180 (defun sql-operation (operator &rest args)
181   "Returns an SQL expression constructed from the supplied symbol
182 OPERATOR representing an SQL operator or function and its
183 arguments ARGS. An error of type SQL-USER-ERROR is signalled if
184 OPERATOR is not a symbol representing a supported SQL
185 operator. If OPERATOR is passed the symbol FUNCTION then the
186 first value in ARGS must be a string representing a valid SQL
187 function and the remaining values in ARGS its arguments as
188 strings."
189   (if (sql-operator operator)
190       (apply (symbol-function (sql-operator operator)) args)
191       (error 'sql-user-error 
192              :message 
193              (format nil "~A is not a recognized SQL operator." operator))))
194
195