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
10 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
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 ;;;; *************************************************************************
17 (in-package #:clsql-sys)
19 (defvar +whitespace-chars+
20 '(#\space #\tab #\newline #\return
21 ;; Tested: sbcl unicode, allegrocl, openmcl,clisp use #\no-break_space
22 ;; lispworks uses #\no-break-space
23 ;; sbcl non-unicode doesn't support no break space
24 ;; AllegroCL 8-bit strings don't fail on reading #\no-break_space,
25 ;; but can't represent such a character
26 ;; CMUCL errors when trying to read #\no-break_space
27 #+(and lispworks unicode) #\no-break-space
28 #+(or (and sbcl sb-unicode) (and allegro ics) (and clisp i18n)
29 (and openmcl openmcl-unicode-strings))
32 "List of whitespace characters for this lisp implementation.")
34 (defun number-to-sql-string (num)
37 (princ-to-string num))
39 (float-to-sql-string (coerce num 'double-float)))
41 (float-to-sql-string num))))
43 (defun float-to-sql-string (num)
44 "Convert exponent character for SQL"
45 (let ((str (write-to-string num :readably t)))
46 (declare (type string str))
49 (substitute #\e #\f str))
51 (substitute #\e #\d str))
53 (substitute #\e #\l str))
55 (substitute #\e #\S str))
57 (substitute #\e #\F str))
59 (substitute #\e #\D str))
61 (substitute #\e #\L str))
63 (substitute #\e #\S str))
67 (defun sql-escape (identifier)
68 "Change hyphens to underscores, ensure string"
69 (let ((unescaped (etypecase identifier
70 (symbol (symbol-name identifier))
71 (string identifier))))
72 (substitute #\_ #\- unescaped)))
75 (defvar +lw-has-without-preemption+
79 (defvar +lw-global-lock+
80 (unless +lw-has-without-preemption+
81 (mp:make-lock :name "CLSQL" :important-p nil :safep t :recursivep nil
84 (defmacro without-interrupts (&body body)
85 #+allegro `(mp:without-scheduling ,@body)
86 #+clisp `(progn ,@body)
87 #+cmu `(system:without-interrupts ,@body)
89 (if +lw-has-without-preemption+
90 `(mp:without-preemption ,@body)
91 `(mp:with-exclusive-lock (+lw-global-lock+)
93 #+openmcl `(ccl:without-interrupts ,@body)
94 #+sbcl `(sb-sys::without-interrupts ,@body))
96 (defun make-process-lock (name)
97 #+allegro (mp:make-process-lock :name name)
98 #+cmu (mp:make-lock name)
99 #+lispworks (mp:make-lock :name name)
100 #+openmcl (ccl:make-lock name)
101 #+sb-thread (sb-thread:make-mutex :name name)
102 #+scl (thread:make-lock name)
103 #-(or allegro cmu lispworks openmcl sb-thread scl) (declare (ignore name))
104 #-(or allegro cmu lispworks openmcl sb-thread scl) nil)
106 (defmacro with-process-lock ((lock desc) &body body)
107 #+(or cmu allegro lispworks openmcl sb-thread)
108 (declare (ignore desc))
109 #+(or allegro cmu lispworks openmcl sb-thread)
112 #+allegro (mp:with-process-lock (,l) ,@body)
113 #+cmu (mp:with-lock-held (,l) ,@body)
114 #+openmcl (ccl:with-lock-grabbed (,l) ,@body)
115 #+lispworks (mp:with-lock (,l) ,@body)
116 #+sb-thread (sb-thread:with-recursive-lock (,l) ,@body)
118 #+scl `(thread:with-lock-held (,lock ,desc) ,@body)
119 #-(or cmu allegro lispworks openmcl sb-thread scl) (declare
121 #-(or cmu allegro lispworks openmcl sb-thread scl) `(progn ,@body))
123 (defun sql-escape-quotes (s)
124 "Escape quotes for SQL string writing"
125 (substitute-string-for-char s #\' "''"))
127 (defun substitute-string-for-char (procstr match-char subst-str)
128 "Substitutes a string for a single matching character of a string"
129 (declare (type string procstr))
130 (let ((pos (position match-char procstr)))
133 (subseq procstr 0 pos) subst-str
134 (substitute-string-for-char
135 (subseq procstr (1+ pos)) match-char subst-str))
139 (defun position-char (char string start max)
141 (declare (optimize (speed 3) (safety 0) (space 0))
142 (fixnum start max) (simple-string string))
143 (do* ((i start (1+ i)))
146 (when (char= char (schar string i)) (return i))))
148 (defun delimited-string-to-list (string &optional (separator #\space)
150 "Split a string with delimiter, from KMRCL."
151 (declare (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0))
153 (type character separator))
154 (do* ((len (length string))
157 (end (position-char separator string pos len)
158 (position-char separator string pos len)))
161 (push (subseq string pos) output)
162 (when (or (not skip-terminal) (zerop len))
165 (declare (type fixnum pos len)
166 (type (or null fixnum) end))
167 (push (subseq string pos end) output)
168 (setq pos (1+ end))))
170 (defun string-to-list-connection-spec (str)
171 (declare (type string str))
172 (let ((at-pos (position-char #\@ str 0 (length str))))
174 ((and at-pos (> (length str) at-pos))
175 ;; Connection spec is SQL*NET format
176 (cons (subseq str (1+ at-pos))
177 (delimited-string-to-list (subseq str 0 at-pos) #\/)))
179 (delimited-string-to-list str #\/)))))
182 (eval-when (:compile-toplevel :load-toplevel :execute)
183 (unless (find-package '#:excl.osi)
186 (defun command-output (control-string &rest args)
187 ;; Concatenates output and error since Lispworks combines
188 ;; these, thus CLSQL can't depend upon separate results
189 (multiple-value-bind (output error status)
190 (apply #'%command-output control-string args)
192 (concatenate 'string (if output output "")
196 (defun read-stream-to-string (in)
197 (with-output-to-string (out)
198 (let ((eof (gensym)))
199 (do ((line (read-line in nil eof)
200 (read-line in nil eof)))
202 (format out "~A~%" line)))))
205 (defun %command-output (control-string &rest args)
206 "Interpolate ARGS into CONTROL-STRING as if by FORMAT, and
207 synchronously execute the result using a Bourne-compatible shell,
208 returns (VALUES string-output error-output exit-status)"
209 (let ((command (apply #'format nil control-string args)))
211 (let* ((process (sb-ext:run-program
214 :input nil :output :stream :error :stream))
215 (output (read-stream-to-string (sb-impl::process-output process)))
216 (error (read-stream-to-string (sb-impl::process-error process))))
217 (close (sb-impl::process-output process))
218 (close (sb-impl::process-error process))
222 (sb-impl::process-exit-code process)))
226 (let* ((process (ext:run-program
229 :input nil :output :stream :error :stream))
230 (output (read-stream-to-string (ext::process-output process)))
231 (error (read-stream-to-string (ext::process-error process))))
232 (close (ext::process-output process))
233 (close (ext::process-error process))
238 (ext::process-exit-code process)))
241 (multiple-value-bind (output error status)
242 (excl.osi:command-output command :whole t)
243 (values output error status))
246 ;; BUG: Lispworks combines output and error streams
247 (let ((output (make-string-output-stream)))
250 (system:call-system-showing-output
252 :shell-type "/bin/sh"
253 :output-stream output)))
254 (values (get-output-stream-string output) nil status))
258 ;; BUG: CLisp doesn't allow output to user-specified stream
262 (ext:run-shell-command command :output :terminal :wait t))
265 (let* ((process (ccl:run-program
268 :input nil :output :stream :error :stream
270 (output (read-stream-to-string (ccl::external-process-output-stream process)))
271 (error (read-stream-to-string (ccl::external-process-error-stream process))))
272 (close (ccl::external-process-output-stream process))
273 (close (ccl::external-process-error-stream process))
276 (nth-value 1 (ccl::external-process-status process))))
278 #-(or openmcl clisp lispworks allegro scl cmu sbcl)
279 (error "COMMAND-OUTPUT not implemented for this Lisp")
285 (defmacro in (obj &rest choices)
286 (let ((insym (gensym)))
287 `(let ((,insym ,obj))
288 (or ,@(mapcar #'(lambda (c) `(eql ,insym ,c))
292 (defun substitute-char-string (procstr match-char subst-str)
293 "Substitutes a string for a single matching character of a string"
294 (substitute-chars-strings procstr (list (cons match-char subst-str))))
296 (defun replaced-string-length (str repl-alist)
297 (declare (simple-string str)
298 (optimize (speed 3) (safety 0) (space 0)))
300 (orig-len (length str))
302 ((= i orig-len) new-len)
303 (declare (fixnum i orig-len new-len))
304 (let* ((c (char str i))
305 (match (assoc c repl-alist :test #'char=)))
306 (declare (character c))
308 (incf new-len (1- (length
309 (the simple-string (cdr match)))))))))
312 (defun substitute-chars-strings (str repl-alist)
313 "Replace all instances of a chars with a string. repl-alist is an assoc
314 list of characters and replacement strings."
315 (declare (simple-string str)
316 (optimize (speed 3) (safety 0) (space 0)))
317 (do* ((orig-len (length str))
318 (new-string (make-string (replaced-string-length str repl-alist)))
323 (declare (fixnum spos dpos) (simple-string new-string))
324 (let* ((c (char str spos))
325 (match (assoc c repl-alist :test #'char=)))
326 (declare (character c))
328 (let* ((subst (cdr match))
329 (len (length subst)))
330 (declare (fixnum len)
331 (simple-string subst))
334 (setf (char new-string dpos) (char subst j))
337 (setf (char new-string dpos) c)
342 "Return the value of the environment variable."
343 #+allegro (sys::getenv (string var))
344 #+clisp (ext:getenv (string var))
346 (cdr (assoc (string var) ext:*environment-list* :test #'equalp
348 #+lispworks (lw:environment-variable (string var))
349 #+mcl (ccl::getenv var)
350 #+sbcl (sb-ext:posix-getenv var))
352 (eval-when (:compile-toplevel :load-toplevel :execute)
353 (when (char= #\a (schar (symbol-name '#:a) 0))
354 (pushnew :clsql-lowercase-reader *features*)))
356 (defun symbol-name-default-case (str)
357 #-clsql-lowercase-reader
359 #+clsql-lowercase-reader
360 (string-downcase str))
362 (defun convert-to-db-default-case (str database)
364 (case (db-type-default-case (database-underlying-type database))
365 (:upper (string-upcase str))
366 (:lower (string-downcase str))
368 ;; Default CommonSQL behavior is to upcase strings
369 (string-upcase str)))
371 (defun ensure-keyword (name)
372 "Returns keyword for a name."
375 (string (nth-value 0 (intern (symbol-name-default-case name) :keyword)))
376 (symbol (nth-value 0 (intern (symbol-name name) :keyword)))))
378 (eval-when (:compile-toplevel :load-toplevel :execute)
379 (setq cl:*features* (delete :clsql-lowercase-reader cl:*features*)))
381 (defun replace-all (string part replacement &key (test #'char=) stream)
382 "Returns a new string in which all the occurences of the part
383 is replaced with replacement. [FROM http://cl-cookbook.sourceforge.net/strings.html#manip]"
384 (let ((out (or stream (make-string-output-stream))))
385 (loop with part-length = (length part)
386 for old-pos = 0 then (+ pos part-length)
387 for pos = (search part string
390 do (write-string string out
392 :end (or pos (length string)))
393 when pos do (write-string replacement out)
396 (get-output-stream-string out))))
399 (defun filter-plist (plist &rest keys-to-remove)
400 "Returns a copy of the given plist with indicated key-value pairs
401 removed. keys are searched with #'MEMBER"
402 (declare (dynamic-extent keys-to-remove))
404 (loop for (k v . rest) = plist then rest
405 unless (member k keys-to-remove)
406 collect k and collect v
409 (defmacro make-weak-hash-table (&rest args)
410 "Creates a weak hash table for use in a cache."
413 ;;NB: These are generally used for caches that may not have an alternate
414 ;;clearing mechanism. If you are on an implementation that doesn't support
415 ;;weak hash tables then you're memory may accumulate.
417 #-(or sbcl allegro clisp lispworks)
418 (warn "UNSAFE! use of weak hash on implementation without support. (see clsql/sql/utils.lisp to add)")
421 #+allegro :values #+allegro :weak
422 #+clisp :weak #+clisp :value
423 #+lispworks :weak-kind #+lispworks :value
424 #+sbcl :weakness #+sbcl :value