r4783: Auto commit for Debian build
[umlisp.git] / sql-classes.lisp
index 16f6c9265ba2c4b8a8b3b730fb600314a350d90a..ad61636689ae9e92aab8ace1960a88356575930b 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; Author:        Kevin M. Rosenberg
 ;;;; Date Started:  Apr 2000
 ;;;;
-;;;; $Id: sql-classes.lisp,v 1.50 2003/05/03 22:05:50 kevin Exp $
+;;;; $Id: sql-classes.lisp,v 1.57 2003/05/03 22:48:55 kevin Exp $
 ;;;;
 ;;;; This file, part of UMLisp, is
 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
                                    &key (lrl "KCUILRL") distinct single
                                    order like)
                             &body body)
-  (if single
-    `(unless (and ,where-name (not ,where-value)) 
-      (let ((tuple (car (umlisp-query ,table ,fields ,srl ,where-name ,where-value
-                                     :lrl ,lrl :single ,single
-                                     :distinct ,distinct :order ,order :like ,like))))
-       (when tuple
-         (destructuring-bind ,fields tuple
-           ,@body))))
-    `(unless (and ,where-name (not ,where-value)) 
-      (loop for tuple in
-       (umlisp-query ,table ,fields ,srl ,where-name ,where-value
-       :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)
-       collect (destructuring-bind ,fields tuple
-                ,@body)))))
+  (let ((value (gensym)))
+    (if single
+       `(let ((,value ,where-value))
+         (when ,value 
+           (let ((tuple (car (umlisp-query ,table ,fields ,srl ,where-name ,value
+                                           :lrl ,lrl :single ,single
+                                           :distinct ,distinct :order ,order
+                                           :like ,like))))
+             (when tuple
+               (destructuring-bind ,fields tuple
+                 ,@body)))))
+       `(let ((,value ,where-value))
+         (when ,value 
+           (loop for tuple in
+                 (umlisp-query ,table ,fields ,srl ,where-name ,value
+                               :lrl ,lrl :single ,single :distinct ,distinct
+                               :order ,order :like ,like)
+                 collect (destructuring-bind ,fields tuple
+                           ,@body)))))))
 
 (defmacro umlisp-query (table fields srl where-name where-value
                     &key (lrl "KCUILRL") single distinct order like)
   "Query the UMLisp database. Return a list of umlisp objects whose name
 is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
-  `(when (or (not ,where-name) ,where-value)
-    (mutex-sql-query
-     (query-string-macro ,table ,fields ,srl ,where-name ,where-value 
-      :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like))))
+  `(mutex-sql-query
+    (query-string-macro ,table ,fields ,srl ,where-name ,where-value 
+     :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
 
   
 (defmacro query-string-macro (table fields &optional srl where-name where-value
@@ -62,15 +66,16 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
                           (if distinct "distinct " "") fields table))
         (%%order (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) ""))
         (%%lrl (format nil " and ~:@(~A~)<=" lrl))
-        (%%where (format nil " where ~:@(~A~)~A" where-name
-                         (if like " like " "="))))
+        (%%where (when where-name
+                   (format nil " where ~:@(~A~)~A" where-name
+                         (if like " like " "=")))))
     `(concatenate
       'string
       ,%%fields
       ,@(when %%where (list %%where))
       ,@(when %%where
              `((if (numberp ,where-value)
-                   (write-to-string where-value)
+                   (write-to-string ,where-value)
                    (format nil ,(if like "'%~A%'" "'~A'") ,where-value))))
       (if ,srl (concatenate 'string ,%%lrl (write-to-string ,srl)) "")
       ,@(when %%order (list %%order))
@@ -165,7 +170,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
         (make-instance 'ucon :cui (ensure-integer cui)
                        :pfstr pfstr
                        :lrl (ensure-integer cuilrl)))
-     (query-string 'mrcon '(cui kpfstr kcuilrl) srl nil nil
+     (query-string-macro mrcon (cui kpfstr kcuilrl) srl nil nil
                   :order (cui asc) :distinct t)
      :database db)))
 
@@ -179,7 +184,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
                  (make-instance 'ucon :cui (ensure-integer cui)
                                 :pfstr pfstr
                                 :lrl (ensure-integer cuilrl))))
-     (query-string 'mrcon '(cui kpfstr kcuilrl) srl nil nil :order (cui asc) :distinct t)
+     (query-string-macro mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc) :distinct t)
      :database db)))
 
 
@@ -323,7 +328,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
                          :stt stt
                          :lrl lrl
                          :str pfstr))
-       (query-string 'mrcon '(cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl :distinct t
+       (query-string-macro mrcon (cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl :distinct t
                     :order (sui asc))
        :database db)))
 
@@ -355,7 +360,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
       (string-append ls (format nil " and KSRL <= ~d" srl)))
     (loop for tuple in (mutex-sql-query ls) collect 
          (destructuring-bind (code atn sab atv) tuple
-           (make-instance 'usat :code code :atn atn :sab sab :atv atv) usats))))
+           (make-instance 'usat :code code :atn atn :sab sab :atv atv)))))
 
 (defun find-usty-tui (tui)
   "Find usty for tui"