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