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"
131 (declare (type string procstr))
132 (let ((pos (position match-char procstr)))
135 (subseq procstr 0 pos) subst-str
136 (substitute-string-for-char
137 (subseq procstr (1+ pos)) match-char subst-str))
141 (defun position-char (char string start max)
143 (declare (optimize (speed 3) (safety 0) (space 0))
144 (fixnum start max) (simple-string string))
145 (do* ((i start (1+ i)))
148 (when (char= char (schar string i)) (return i))))
150 (defun delimited-string-to-list (string &optional (separator #\space)
152 "Split a string with delimiter, from KMRCL."
153 (declare (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0))
155 (type character separator))
156 (do* ((len (length string))
159 (end (position-char separator string pos len)
160 (position-char separator string pos len)))
163 (push (subseq string pos) output)
164 (when (or (not skip-terminal) (zerop len))
167 (declare (type fixnum pos len)
168 (type (or null fixnum) end))
169 (push (subseq string pos end) output)
170 (setq pos (1+ end))))
172 (defun string-to-list-connection-spec (str)
173 (declare (type string str))
174 (let ((at-pos (position-char #\@ str 0 (length str))))
176 ((and at-pos (> (length str) at-pos))
177 ;; Connection spec is SQL*NET format
178 (cons (subseq str (1+ at-pos))
179 (delimited-string-to-list (subseq str 0 at-pos) #\/)))
181 (delimited-string-to-list str #\/)))))
184 (eval-when (:compile-toplevel :load-toplevel :execute)
185 (unless (find-package '#:excl.osi)
188 (defun command-output (control-string &rest args)
189 ;; Concatenates output and error since Lispworks combines
190 ;; these, thus CLSQL can't depend upon separate results
191 (multiple-value-bind (output error status)
192 (apply #'%command-output control-string args)
194 (concatenate 'string (if output output "")
198 (defun read-stream-to-string (in)
199 (with-output-to-string (out)
200 (let ((eof (gensym)))
201 (do ((line (read-line in nil eof)
202 (read-line in nil eof)))
204 (format out "~A~%" line)))))
207 (defun %command-output (control-string &rest args)
208 "Interpolate ARGS into CONTROL-STRING as if by FORMAT, and
209 synchronously execute the result using a Bourne-compatible shell,
210 returns (VALUES string-output error-output exit-status)"
211 (let ((command (apply #'format nil control-string args)))
213 (let* ((process (sb-ext:run-program
216 :input nil :output :stream :error :stream))
217 (output (read-stream-to-string (sb-impl::process-output process)))
218 (error (read-stream-to-string (sb-impl::process-error process))))
219 (close (sb-impl::process-output process))
220 (close (sb-impl::process-error process))
224 (sb-impl::process-exit-code process)))
228 (let* ((process (ext:run-program
231 :input nil :output :stream :error :stream))
232 (output (read-stream-to-string (ext::process-output process)))
233 (error (read-stream-to-string (ext::process-error process))))
234 (close (ext::process-output process))
235 (close (ext::process-error process))
240 (ext::process-exit-code process)))
243 (multiple-value-bind (output error status)
244 (excl.osi:command-output command :whole t)
245 (values output error status))
248 ;; BUG: Lispworks combines output and error streams
249 (let ((output (make-string-output-stream)))
252 (system:call-system-showing-output
254 :shell-type "/bin/sh"
255 :output-stream output)))
256 (values (get-output-stream-string output) nil status))
260 ;; BUG: CLisp doesn't allow output to user-specified stream
264 (ext:run-shell-command command :output :terminal :wait t))
267 (let* ((process (ccl:run-program
270 :input nil :output :stream :error :stream
272 (output (read-stream-to-string (ccl::external-process-output-stream process)))
273 (error (read-stream-to-string (ccl::external-process-error-stream process))))
274 (close (ccl::external-process-output-stream process))
275 (close (ccl::external-process-error-stream process))
278 (nth-value 1 (ccl::external-process-status process))))
280 #-(or openmcl clisp lispworks allegro scl cmu sbcl)
281 (error "COMMAND-OUTPUT not implemented for this Lisp")
287 (defmacro in (obj &rest choices)
288 (let ((insym (gensym)))
289 `(let ((,insym ,obj))
290 (or ,@(mapcar #'(lambda (c) `(eql ,insym ,c))
294 (defun substitute-char-string (procstr match-char subst-str)
295 "Substitutes a string for a single matching character of a string"
296 (substitute-chars-strings procstr (list (cons match-char subst-str))))
298 (defun replaced-string-length (str repl-alist)
299 (declare (simple-string str)
300 (optimize (speed 3) (safety 0) (space 0)))
302 (orig-len (length str))
304 ((= i orig-len) new-len)
305 (declare (fixnum i orig-len new-len))
306 (let* ((c (char str i))
307 (match (assoc c repl-alist :test #'char=)))
308 (declare (character c))
310 (incf new-len (1- (length
311 (the simple-string (cdr match)))))))))
314 (defun substitute-chars-strings (str repl-alist)
315 "Replace all instances of a chars with a string. repl-alist is an assoc
316 list of characters and replacement strings."
317 (declare (simple-string str)
318 (optimize (speed 3) (safety 0) (space 0)))
319 (do* ((orig-len (length str))
320 (new-string (make-string (replaced-string-length str repl-alist)))
325 (declare (fixnum spos dpos) (simple-string new-string))
326 (let* ((c (char str spos))
327 (match (assoc c repl-alist :test #'char=)))
328 (declare (character c))
330 (let* ((subst (cdr match))
331 (len (length subst)))
332 (declare (fixnum len)
333 (simple-string subst))
336 (setf (char new-string dpos) (char subst j))
339 (setf (char new-string dpos) c)
344 "Return the value of the environment variable."
345 #+allegro (sys::getenv (string var))
346 #+clisp (ext:getenv (string var))
348 (cdr (assoc (string var) ext:*environment-list* :test #'equalp
350 #+lispworks (lw:environment-variable (string var))
351 #+mcl (ccl::getenv var)
352 #+sbcl (sb-ext:posix-getenv var))
354 (eval-when (:compile-toplevel :load-toplevel :execute)
355 (when (char= #\a (schar (symbol-name '#:a) 0))
356 (pushnew :clsql-lowercase-reader *features*)))
358 (defun symbol-name-default-case (str)
359 #-clsql-lowercase-reader
361 #+clsql-lowercase-reader
362 (string-downcase str))
364 (defun convert-to-db-default-case (str database)
366 (case (db-type-default-case (database-underlying-type database))
367 (:upper (string-upcase str))
368 (:lower (string-downcase str))
370 ;; Default CommonSQL behavior is to upcase strings
371 (string-upcase str)))
373 (defun ensure-keyword (name)
374 "Returns keyword for a name."
377 (string (nth-value 0 (intern (symbol-name-default-case name) :keyword)))
378 (symbol (nth-value 0 (intern (symbol-name name) :keyword)))))
380 (eval-when (:compile-toplevel :load-toplevel :execute)
381 (setq cl:*features* (delete :clsql-lowercase-reader cl:*features*)))
383 (defun replace-all (string part replacement &key (test #'char=) stream)
384 "Returns a new string in which all the occurences of the part
385 is replaced with replacement. [FROM http://cl-cookbook.sourceforge.net/strings.html#manip]"
386 (let ((out (or stream (make-string-output-stream))))
387 (loop with part-length = (length part)
388 for old-pos = 0 then (+ pos part-length)
389 for pos = (search part string
392 do (write-string string out
394 :end (or pos (length string)))
395 when pos do (write-string replacement out)
398 (get-output-stream-string out))))
401 (defun filter-plist (plist &rest keys-to-remove)
402 "Returns a copy of the given plist with indicated key-value pairs
403 removed. keys are searched with #'MEMBER"
404 (declare (dynamic-extent keys-to-remove))
406 (loop for (k v . rest) = plist then rest
407 unless (member k keys-to-remove)
408 collect k and collect v
411 (defmacro make-weak-hash-table (&rest args)
412 "Creates a weak hash table for use in a cache."
415 ;;NB: These are generally used for caches that may not have an alternate
416 ;;clearing mechanism. If you are on an implementation that doesn't support
417 ;;weak hash tables then you're memory may accumulate.
419 #-(or sbcl allegro clisp lispworks)
420 (warn "UNSAFE! use of weak hash on implementation without support. (see clsql/sql/utils.lisp to add)")
423 #+allegro :values #+allegro :weak
424 #+clisp :weak #+clisp :value
425 #+lispworks :weak-kind #+lispworks :value
426 #+sbcl :weakness #+sbcl :value