1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: SQL utility functions
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Mar 2002
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2004 by Kevin M. Rosenberg
14 ;;;; CLSQL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
19 (in-package #:clsql-sys)
21 (defun number-to-sql-string (num)
26 (float-to-sql-string (coerce num 'double-float)))
28 (float-to-sql-string num))))
30 (defun float-to-sql-string (num)
31 "Convert exponent character for SQL"
32 (let ((str (write-to-string num :readably t)))
35 (substitute #\e #\f str))
37 (substitute #\e #\d str))
39 (substitute #\e #\l str))
41 (substitute #\e #\S str))
43 (substitute #\e #\F str))
45 (substitute #\e #\D str))
47 (substitute #\e #\L str))
49 (substitute #\e #\S str))
53 (defun sql-escape (identifier)
54 "Change hyphens to underscores, ensure string"
55 (let* ((unescaped (etypecase identifier
56 (symbol (symbol-name identifier))
58 (escaped (make-string (length unescaped))))
59 (substitute #\_ #\- unescaped)))
61 (defmacro without-interrupts (&body body)
62 #+allegro `(mp:without-scheduling ,@body)
63 #+clisp `(progn ,@body)
64 #+cmu `(system:without-interrupts ,@body)
65 #+lispworks `(mp:without-preemption ,@body)
66 #+openmcl `(ccl:without-interrupts ,@body)
67 #+sbcl `(sb-sys::without-interrupts ,@body))
69 (defun make-process-lock (name)
70 #+allegro (mp:make-process-lock :name name)
71 #+cmu (mp:make-lock name)
72 #+lispworks (mp:make-lock :name name)
73 #+openmcl (ccl:make-lock name)
74 #+sb-thread (sb-thread:make-mutex :name name)
75 #+scl (thread:make-lock name)
76 #-(or allegro cmu lispworks openmcl sb-thread scl) (declare (ignore name))
77 #-(or allegro cmu lispworks openmcl sb-thread scl) nil)
79 (defmacro with-process-lock ((lock desc) &body body)
80 #+(or cmu allegro lispworks openmcl sb-thread)
81 (declare (ignore desc))
82 #+(or allegro cmu lispworks openmcl sb-thread)
85 #+allegro (mp:with-process-lock (,l) ,@body)
86 #+cmu (mp:with-lock-held (,l) ,@body)
87 #+openmcl (ccl:with-lock-grabbed (,l) ,@body)
88 #+lispworks (mp:with-lock (,l) ,@body)
89 #+sb-thread (sb-thread:with-recursive-lock (,l) ,@body)
91 #+scl `(thread:with-lock-held (,lock ,desc) ,@body)
92 #-(or cmu allegro lispworks openmcl sb-thread scl) (declare
94 #-(or cmu allegro lispworks openmcl sb-thread scl) `(progn ,@body))
96 (defun sql-escape-quotes (s)
97 "Escape quotes for SQL string writing"
98 (substitute-string-for-char s #\' "''"))
100 (defun substitute-string-for-char (procstr match-char subst-str)
101 "Substitutes a string for a single matching character of a string"
102 (let ((pos (position match-char procstr)))
105 (subseq procstr 0 pos) subst-str
106 (substitute-string-for-char
107 (subseq procstr (1+ pos)) match-char subst-str))
111 (defun position-char (char string start max)
113 (declare (optimize (speed 3) (safety 0) (space 0))
114 (fixnum start max) (simple-string string))
115 (do* ((i start (1+ i)))
118 (when (char= char (schar string i)) (return i))))
120 (defun delimited-string-to-list (string &optional (separator #\space)
122 "Split a string with delimiter, from KMRCL."
123 (declare (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0))
125 (type character separator))
126 (do* ((len (length string))
129 (end (position-char separator string pos len)
130 (position-char separator string pos len)))
133 (push (subseq string pos) output)
134 (when (or (not skip-terminal) (zerop len))
137 (declare (type fixnum pos len)
138 (type (or null fixnum) end))
139 (push (subseq string pos end) output)
140 (setq pos (1+ end))))
142 (defun string-to-list-connection-spec (str)
143 (let ((at-pos (position-char #\@ str 0 (length str))))
145 ((and at-pos (> (length str) at-pos))
146 ;; Connection spec is SQL*NET format
147 (cons (subseq str (1+ at-pos))
148 (delimited-string-to-list (subseq str 0 at-pos) #\/)))
150 (delimited-string-to-list str #\/)))))
153 (eval-when (:compile-toplevel :load-toplevel :execute)
154 (unless (find-package '#:excl.osi)
157 (defun command-output (control-string &rest args)
158 ;; Concatenates output and error since Lispworks combines
159 ;; these, thus CLSQL can't depend upon separate results
160 (multiple-value-bind (output error status)
161 (apply #'%command-output control-string args)
163 (concatenate 'string (if output output "")
167 (defun read-stream-to-string (in)
168 (with-output-to-string (out)
169 (let ((eof (gensym)))
170 (do ((line (read-line in nil eof)
171 (read-line in nil eof)))
173 (format out "~A~%" line)))))
176 (defun %command-output (control-string &rest args)
177 "Interpolate ARGS into CONTROL-STRING as if by FORMAT, and
178 synchronously execute the result using a Bourne-compatible shell,
179 returns (VALUES string-output error-output exit-status)"
180 (let ((command (apply #'format nil control-string args)))
182 (let* ((process (sb-ext:run-program
185 :input nil :output :stream :error :stream))
186 (output (read-stream-to-string (sb-impl::process-output process)))
187 (error (read-stream-to-string (sb-impl::process-error process))))
188 (close (sb-impl::process-output process))
189 (close (sb-impl::process-error process))
193 (sb-impl::process-exit-code process)))
197 (let* ((process (ext:run-program
200 :input nil :output :stream :error :stream))
201 (output (read-stream-to-string (ext::process-output process)))
202 (error (read-stream-to-string (ext::process-error process))))
203 (close (ext::process-output process))
204 (close (ext::process-error process))
209 (ext::process-exit-code process)))
212 (multiple-value-bind (output error status)
213 (excl.osi:command-output command :whole t)
214 (values output error status))
217 ;; BUG: Lispworks combines output and error streams
218 (let ((output (make-string-output-stream)))
221 (system:call-system-showing-output
223 :shell-type "/bin/sh"
224 :output-stream output)))
225 (values (get-output-stream-string output) nil status))
229 ;; BUG: CLisp doesn't allow output to user-specified stream
233 (ext:run-shell-command command :output :terminal :wait t))
236 (let* ((process (ccl:run-program
239 :input nil :output :stream :error :stream
241 (output (read-stream-to-string (ccl::external-process-output-stream process)))
242 (error (read-stream-to-string (ccl::external-process-error-stream process))))
243 (close (ccl::external-process-output-stream process))
244 (close (ccl::external-process-error-stream process))
247 (nth-value 1 (ccl::external-process-status process))))
249 #-(or openmcl clisp lispworks allegro scl cmu sbcl)
250 (error "COMMAND-OUTPUT not implemented for this Lisp")
256 (defmacro in (obj &rest choices)
257 (let ((insym (gensym)))
258 `(let ((,insym ,obj))
259 (or ,@(mapcar #'(lambda (c) `(eql ,insym ,c))
263 (defun substitute-char-string (procstr match-char subst-str)
264 "Substitutes a string for a single matching character of a string"
265 (substitute-chars-strings procstr (list (cons match-char subst-str))))
267 (defun replaced-string-length (str repl-alist)
268 (declare (simple-string str)
269 (optimize (speed 3) (safety 0) (space 0)))
271 (orig-len (length str))
273 ((= i orig-len) new-len)
274 (declare (fixnum i orig-len new-len))
275 (let* ((c (char str i))
276 (match (assoc c repl-alist :test #'char=)))
277 (declare (character c))
279 (incf new-len (1- (length
280 (the simple-string (cdr match)))))))))
283 (defun substitute-chars-strings (str repl-alist)
284 "Replace all instances of a chars with a string. repl-alist is an assoc
285 list of characters and replacement strings."
286 (declare (simple-string str)
287 (optimize (speed 3) (safety 0) (space 0)))
288 (do* ((orig-len (length str))
289 (new-string (make-string (replaced-string-length str repl-alist)))
294 (declare (fixnum spos dpos) (simple-string new-string))
295 (let* ((c (char str spos))
296 (match (assoc c repl-alist :test #'char=)))
297 (declare (character c))
299 (let* ((subst (cdr match))
300 (len (length subst)))
301 (declare (fixnum len)
302 (simple-string subst))
305 (setf (char new-string dpos) (char subst j))
308 (setf (char new-string dpos) c)
313 "Return the value of the environment variable."
314 #+allegro (sys::getenv (string var))
315 #+clisp (ext:getenv (string var))
317 (cdr (assoc (string var) ext:*environment-list* :test #'equalp
319 #+lispworks (lw:environment-variable (string var))
320 #+mcl (ccl::getenv var)
321 #+sbcl (sb-ext:posix-getenv var))
323 (eval-when (:compile-toplevel :load-toplevel :execute)
324 (when (char= #\a (schar (symbol-name '#:a) 0))
325 (pushnew :clsql-lowercase-reader *features*)))
327 (defun symbol-name-default-case (str)
328 #-clsql-lowercase-reader
330 #+clsql-lowercase-reader
331 (string-downcase str))
333 (defun convert-to-db-default-case (str database)
335 (case (db-type-default-case (database-underlying-type database))
336 (:upper (string-upcase str))
337 (:lower (string-downcase str))
339 ;; Default CommonSQL behavior is to upcase strings
340 (string-upcase str)))
342 (defun ensure-keyword (name)
343 "Returns keyword for a name"
346 (string (nth-value 0 (intern (symbol-name-default-case name) :keyword)))
347 (symbol (nth-value 0 (intern (symbol-name name) :keyword)))))
349 (eval-when (:compile-toplevel :load-toplevel :execute)
350 (setq cl:*features* (delete :clsql-lowercase-reader cl:*features*)))