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)
24 (princ-to-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"
56 (symbol (substitute #\_ #\- (symbol-name identifier)))
59 (defmacro without-interrupts (&body body)
60 #+allegro `(mp:without-scheduling ,@body)
61 #+clisp `(progn ,@body)
62 #+cmu `(system:without-interrupts ,@body)
63 #+lispworks `(mp:without-preemption ,@body)
64 #+openmcl `(ccl:without-interrupts ,@body)
65 #+sbcl `(sb-sys::without-interrupts ,@body))
67 (defun make-process-lock (name)
68 #+allegro (mp:make-process-lock :name name)
69 #+cmu (mp:make-lock name)
70 #+lispworks (mp:make-lock :name name)
71 #+openmcl (ccl:make-lock name)
72 #+sb-thread (sb-thread:make-mutex :name name)
73 #+scl (thread:make-lock name)
74 #-(or allegro cmu lispworks openmcl sb-thread scl) (declare (ignore name))
75 #-(or allegro cmu lispworks openmcl sb-thread scl) nil)
77 (defmacro with-process-lock ((lock desc) &body body)
78 #+(or cmu allegro lispworks openmcl sb-thread)
79 (declare (ignore desc))
80 #+(or allegro cmu lispworks openmcl sb-thread)
83 #+allegro (mp:with-process-lock (,l) ,@body)
84 #+cmu (mp:with-lock-held (,l) ,@body)
85 #+openmcl (ccl:with-lock-grabbed (,l) ,@body)
86 #+lispworks (mp:with-lock (,l) ,@body)
87 #+sb-thread (sb-thread:with-recursive-lock (,l) ,@body)
89 #+scl `(thread:with-lock-held (,lock ,desc) ,@body)
90 #-(or cmu allegro lispworks openmcl sb-thread scl) (declare
92 #-(or cmu allegro lispworks openmcl sb-thread scl) `(progn ,@body))
94 (defun sql-escape-quotes (s)
95 "Escape quotes for SQL string writing"
96 (substitute-string-for-char s #\' "''"))
98 (defun substitute-string-for-char (procstr match-char subst-str)
99 "Substitutes a string for a single matching character of a string"
100 (let ((pos (position match-char procstr)))
103 (subseq procstr 0 pos) subst-str
104 (substitute-string-for-char
105 (subseq procstr (1+ pos)) match-char subst-str))
109 (defun position-char (char string start max)
111 (declare (optimize (speed 3) (safety 0) (space 0))
112 (fixnum start max) (simple-string string))
113 (do* ((i start (1+ i)))
116 (when (char= char (schar string i)) (return i))))
118 (defun delimited-string-to-list (string &optional (separator #\space)
120 "Split a string with delimiter, from KMRCL."
121 (declare (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0))
123 (type character separator))
124 (do* ((len (length string))
127 (end (position-char separator string pos len)
128 (position-char separator string pos len)))
131 (push (subseq string pos) output)
132 (when (or (not skip-terminal) (zerop len))
135 (declare (type fixnum pos len)
136 (type (or null fixnum) end))
137 (push (subseq string pos end) output)
138 (setq pos (1+ end))))
140 (defun string-to-list-connection-spec (str)
141 (let ((at-pos (position-char #\@ str 0 (length str))))
143 ((and at-pos (> (length str) at-pos))
144 ;; Connection spec is SQL*NET format
145 (cons (subseq str (1+ at-pos))
146 (delimited-string-to-list (subseq str 0 at-pos) #\/)))
148 (delimited-string-to-list str #\/)))))
151 (eval-when (:compile-toplevel :load-toplevel :execute)
152 (unless (find-package '#:excl.osi)
155 (defun command-output (control-string &rest args)
156 ;; Concatenates output and error since Lispworks combines
157 ;; these, thus CLSQL can't depend upon separate results
158 (multiple-value-bind (output error status)
159 (apply #'%command-output control-string args)
161 (concatenate 'string (if output output "")
165 (defun read-stream-to-string (in)
166 (with-output-to-string (out)
167 (let ((eof (gensym)))
168 (do ((line (read-line in nil eof)
169 (read-line in nil eof)))
171 (format out "~A~%" line)))))
174 (defun %command-output (control-string &rest args)
175 "Interpolate ARGS into CONTROL-STRING as if by FORMAT, and
176 synchronously execute the result using a Bourne-compatible shell,
177 returns (VALUES string-output error-output exit-status)"
178 (let ((command (apply #'format nil control-string args)))
180 (let* ((process (sb-ext:run-program
183 :input nil :output :stream :error :stream))
184 (output (read-stream-to-string (sb-impl::process-output process)))
185 (error (read-stream-to-string (sb-impl::process-error process))))
186 (close (sb-impl::process-output process))
187 (close (sb-impl::process-error process))
191 (sb-impl::process-exit-code process)))
195 (let* ((process (ext:run-program
198 :input nil :output :stream :error :stream))
199 (output (read-stream-to-string (ext::process-output process)))
200 (error (read-stream-to-string (ext::process-error process))))
201 (close (ext::process-output process))
202 (close (ext::process-error process))
207 (ext::process-exit-code process)))
210 (multiple-value-bind (output error status)
211 (excl.osi:command-output command :whole t)
212 (values output error status))
215 ;; BUG: Lispworks combines output and error streams
216 (let ((output (make-string-output-stream)))
219 (system:call-system-showing-output
221 :shell-type "/bin/sh"
222 :output-stream output)))
223 (values (get-output-stream-string output) nil status))
227 ;; BUG: CLisp doesn't allow output to user-specified stream
231 (ext:run-shell-command command :output :terminal :wait t))
234 (let* ((process (ccl:run-program
237 :input nil :output :stream :error :stream
239 (output (read-stream-to-string (ccl::external-process-output-stream process)))
240 (error (read-stream-to-string (ccl::external-process-error-stream process))))
241 (close (ccl::external-process-output-stream process))
242 (close (ccl::external-process-error-stream process))
245 (nth-value 1 (ccl::external-process-status process))))
247 #-(or openmcl clisp lispworks allegro scl cmu sbcl)
248 (error "COMMAND-OUTPUT not implemented for this Lisp")
254 (defmacro in (obj &rest choices)
255 (let ((insym (gensym)))
256 `(let ((,insym ,obj))
257 (or ,@(mapcar #'(lambda (c) `(eql ,insym ,c))
261 (defun substitute-char-string (procstr match-char subst-str)
262 "Substitutes a string for a single matching character of a string"
263 (substitute-chars-strings procstr (list (cons match-char subst-str))))
265 (defun replaced-string-length (str repl-alist)
266 (declare (simple-string str)
267 (optimize (speed 3) (safety 0) (space 0)))
269 (orig-len (length str))
271 ((= i orig-len) new-len)
272 (declare (fixnum i orig-len new-len))
273 (let* ((c (char str i))
274 (match (assoc c repl-alist :test #'char=)))
275 (declare (character c))
277 (incf new-len (1- (length
278 (the simple-string (cdr match)))))))))
281 (defun substitute-chars-strings (str repl-alist)
282 "Replace all instances of a chars with a string. repl-alist is an assoc
283 list of characters and replacement strings."
284 (declare (simple-string str)
285 (optimize (speed 3) (safety 0) (space 0)))
286 (do* ((orig-len (length str))
287 (new-string (make-string (replaced-string-length str repl-alist)))
292 (declare (fixnum spos dpos) (simple-string new-string))
293 (let* ((c (char str spos))
294 (match (assoc c repl-alist :test #'char=)))
295 (declare (character c))
297 (let* ((subst (cdr match))
298 (len (length subst)))
299 (declare (fixnum len)
300 (simple-string subst))
303 (setf (char new-string dpos) (char subst j))
306 (setf (char new-string dpos) c)
311 "Return the value of the environment variable."
312 #+allegro (sys::getenv (string var))
313 #+clisp (ext:getenv (string var))
315 (cdr (assoc (string var) ext:*environment-list* :test #'equalp
317 #+lispworks (lw:environment-variable (string var))
318 #+mcl (ccl::getenv var)
319 #+sbcl (sb-ext:posix-getenv var))
321 (eval-when (:compile-toplevel :load-toplevel :execute)
322 (when (char= #\a (schar (symbol-name '#:a) 0))
323 (pushnew :clsql-lowercase-reader *features*)))
325 (defun symbol-name-default-case (str)
326 #-clsql-lowercase-reader
328 #+clsql-lowercase-reader
329 (string-downcase str))
331 (defun convert-to-db-default-case (str database)
333 (case (db-type-default-case (database-underlying-type database))
334 (:upper (string-upcase str))
335 (:lower (string-downcase str))
337 ;; Default CommonSQL behavior is to upcase strings
338 (string-upcase str)))
340 (defun ensure-keyword (name)
341 "Returns keyword for a name"
344 (string (nth-value 0 (intern (symbol-name-default-case name) :keyword)))
345 (symbol (nth-value 0 (intern (symbol-name name) :keyword)))))
347 (eval-when (:compile-toplevel :load-toplevel :execute)
348 (setq cl:*features* (delete :clsql-lowercase-reader cl:*features*)))