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 (dotimes (i (length unescaped))
62 (setf (char escaped i)
63 (cond ((equal (char unescaped i) #\-)
67 (char unescaped i)))))
70 (defmacro without-interrupts (&body body)
71 #+allegro `(mp:without-scheduling ,@body)
72 #+clisp `(progn ,@body)
73 #+cmu `(system:without-interrupts ,@body)
74 #+lispworks `(mp:without-preemption ,@body)
75 #+openmcl `(ccl:without-interrupts ,@body)
76 #+sbcl `(sb-sys::without-interrupts ,@body))
78 (defun make-process-lock (name)
79 #+allegro (mp:make-process-lock :name name)
80 #+cmu (mp:make-lock name)
81 #+lispworks (mp:make-lock :name name)
82 #+openmcl (ccl:make-lock name)
83 #+sb-thread (sb-thread:make-mutex :name name)
84 #+scl (thread:make-lock name)
85 #-(or allegro cmu lispworks openmcl sb-thread scl) (declare (ignore name))
86 #-(or allegro cmu lispworks openmcl sb-thread scl) nil)
88 (defmacro with-process-lock ((lock desc) &body body)
89 #+(or cmu allegro lispworks openmcl sb-thread)
90 (declare (ignore desc))
91 #+(or allegro cmu lispworks openmcl sb-thread)
94 #+allegro (mp:with-process-lock (,l) ,@body)
95 #+cmu (mp:with-lock-held (,l) ,@body)
96 #+openmcl (ccl:with-lock-grabbed (,l) ,@body)
97 #+lispworks (mp:with-lock (,l) ,@body)
98 #+sb-thread (sb-thread:with-recursive-lock (,l) ,@body)
100 #+scl `(thread:with-lock-held (,lock ,desc) ,@body)
101 #-(or cmu allegro lispworks openmcl sb-thread scl) (declare
103 #-(or cmu allegro lispworks openmcl sb-thread scl) `(progn ,@body))
105 (defun sql-escape-quotes (s)
106 "Escape quotes for SQL string writing"
107 (substitute-string-for-char s #\' "''"))
109 (defun substitute-string-for-char (procstr match-char subst-str)
110 "Substitutes a string for a single matching character of a string"
111 (let ((pos (position match-char procstr)))
114 (subseq procstr 0 pos) subst-str
115 (substitute-string-for-char
116 (subseq procstr (1+ pos)) match-char subst-str))
120 (defun position-char (char string start max)
122 (declare (optimize (speed 3) (safety 0) (space 0))
123 (fixnum start max) (simple-string string))
124 (do* ((i start (1+ i)))
127 (when (char= char (schar string i)) (return i))))
129 (defun delimited-string-to-list (string &optional (separator #\space)
131 "Split a string with delimiter, from KMRCL."
132 (declare (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0))
134 (type character separator))
135 (do* ((len (length string))
138 (end (position-char separator string pos len)
139 (position-char separator string pos len)))
142 (push (subseq string pos) output)
143 (when (or (not skip-terminal) (zerop len))
146 (declare (type fixnum pos len)
147 (type (or null fixnum) end))
148 (push (subseq string pos end) output)
149 (setq pos (1+ end))))
151 (defun string-to-list-connection-spec (str)
152 (let ((at-pos (position-char #\@ str 0 (length str))))
154 ((and at-pos (> (length str) at-pos))
155 ;; Connection spec is SQL*NET format
156 (cons (subseq str (1+ at-pos))
157 (delimited-string-to-list (subseq str 0 at-pos) #\/)))
159 (delimited-string-to-list str #\/)))))
162 (eval-when (:compile-toplevel :load-toplevel :execute)
163 (unless (find-package '#:excl.osi)
166 (defun command-output (control-string &rest args)
167 ;; Concatenates output and error since Lispworks combines
168 ;; these, thus CLSQL can't depend upon separate results
169 (multiple-value-bind (output error status)
170 (apply #'%command-output control-string args)
172 (concatenate 'string (if output output "")
176 (defun read-stream-to-string (in)
177 (with-output-to-string (out)
178 (let ((eof (gensym)))
179 (do ((line (read-line in nil eof)
180 (read-line in nil eof)))
182 (format out "~A~%" line)))))
185 (defun %command-output (control-string &rest args)
186 "Interpolate ARGS into CONTROL-STRING as if by FORMAT, and
187 synchronously execute the result using a Bourne-compatible shell,
188 returns (VALUES string-output error-output exit-status)"
189 (let ((command (apply #'format nil control-string args)))
191 (let* ((process (sb-ext:run-program
194 :input nil :output :stream :error :stream))
195 (output (read-stream-to-string (sb-impl::process-output process)))
196 (error (read-stream-to-string (sb-impl::process-error process))))
197 (close (sb-impl::process-output process))
198 (close (sb-impl::process-error process))
202 (sb-impl::process-exit-code process)))
206 (let* ((process (ext:run-program
209 :input nil :output :stream :error :stream))
210 (output (read-stream-to-string (ext::process-output process)))
211 (error (read-stream-to-string (ext::process-error process))))
212 (close (ext::process-output process))
213 (close (ext::process-error process))
218 (ext::process-exit-code process)))
221 (multiple-value-bind (output error status)
222 (excl.osi:command-output command :whole t)
223 (values output error status))
226 ;; BUG: Lispworks combines output and error streams
227 (let ((output (make-string-output-stream)))
230 (system:call-system-showing-output
232 :shell-type "/bin/sh"
233 :output-stream output)))
234 (values (get-output-stream-string output) nil status))
238 ;; BUG: CLisp doesn't allow output to user-specified stream
242 (ext:run-shell-command command :output :terminal :wait t))
245 (let* ((process (ccl:run-program
248 :input nil :output :stream :error :stream
250 (output (read-stream-to-string (ccl::external-process-output-stream process)))
251 (error (read-stream-to-string (ccl::external-process-error-stream process))))
252 (close (ccl::external-process-output-stream process))
253 (close (ccl::external-process-error-stream process))
256 (nth-value 1 (ccl::external-process-status process))))
258 #-(or openmcl clisp lispworks allegro scl cmu sbcl)
259 (error "COMMAND-OUTPUT not implemented for this Lisp")
265 (defmacro in (obj &rest choices)
266 (let ((insym (gensym)))
267 `(let ((,insym ,obj))
268 (or ,@(mapcar #'(lambda (c) `(eql ,insym ,c))
272 (defun substitute-char-string (procstr match-char subst-str)
273 "Substitutes a string for a single matching character of a string"
274 (substitute-chars-strings procstr (list (cons match-char subst-str))))
276 (defun replaced-string-length (str repl-alist)
277 (declare (simple-string str)
278 (optimize (speed 3) (safety 0) (space 0)))
280 (orig-len (length str))
282 ((= i orig-len) new-len)
283 (declare (fixnum i orig-len new-len))
284 (let* ((c (char str i))
285 (match (assoc c repl-alist :test #'char=)))
286 (declare (character c))
288 (incf new-len (1- (length
289 (the simple-string (cdr match)))))))))
292 (defun substitute-chars-strings (str repl-alist)
293 "Replace all instances of a chars with a string. repl-alist is an assoc
294 list of characters and replacement strings."
295 (declare (simple-string str)
296 (optimize (speed 3) (safety 0) (space 0)))
297 (do* ((orig-len (length str))
298 (new-string (make-string (replaced-string-length str repl-alist)))
303 (declare (fixnum spos dpos) (simple-string new-string))
304 (let* ((c (char str spos))
305 (match (assoc c repl-alist :test #'char=)))
306 (declare (character c))
308 (let* ((subst (cdr match))
309 (len (length subst)))
310 (declare (fixnum len)
311 (simple-string subst))
314 (setf (char new-string dpos) (char subst j))
317 (setf (char new-string dpos) c)
322 "Return the value of the environment variable."
323 #+allegro (sys::getenv (string var))
324 #+clisp (ext:getenv (string var))
326 (cdr (assoc (string var) ext:*environment-list* :test #'equalp
328 #+lispworks (lw:environment-variable (string var))
329 #+mcl (ccl::getenv var)
330 #+sbcl (sb-ext:posix-getenv var))
332 (eval-when (:compile-toplevel :load-toplevel :execute)
333 (when (char= #\a (schar (symbol-name '#:a) 0))
334 (pushnew :clsql-lowercase-reader *features*)))
336 (defun symbol-name-default-case (str)
337 #-clsql-lowercase-reader
339 #+clsql-lowercase-reader
340 (string-downcase str))
342 (defun convert-to-db-default-case (str database)
344 (case (db-type-default-case (database-underlying-type database))
345 (:upper (string-upcase str))
346 (:lower (string-downcase str))
348 ;; Default CommonSQL behavior is to upcase strings
349 (string-upcase str)))
351 (defun ensure-keyword (name)
352 "Returns keyword for a name"
355 (string (nth-value 0 (intern (symbol-name-default-case name) :keyword)))
356 (symbol (nth-value 0 (intern (symbol-name name) :keyword)))))
358 (eval-when (:compile-toplevel :load-toplevel :execute)
359 (setq cl:*features* (delete :clsql-lowercase-reader cl:*features*)))