r4774: Auto commit for Debian build
[umlisp.git] / sql-classes.lisp
index f11b8913e714c020572e2102b5eb08b526080139..37ed4d1e1313e132540f5d868c0cb9a799a7972f 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; Author:        Kevin M. Rosenberg
 ;;;; Date Started:  Apr 2000
 ;;;;
-;;;; $Id: sql-classes.lisp,v 1.29 2003/05/03 20:01:18 kevin Exp $
+;;;; $Id: sql-classes.lisp,v 1.48 2003/05/03 21:27:59 kevin Exp $
 ;;;;
 ;;;; This file, part of UMLisp, is
 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
 (defun current-srl! (srl)
   (setq *current-srl* srl))
 
-
 (defmacro with-umlisp-query ((table fields srl where-name where-value
                                    &key (lrlname "KCUILRL") distinct single
                                    order like)
                             &body body)
-  (let ((query (gensym)))
+  (if single
+    `(unless (and ,where-name (not ,where-value)) 
+      (let ((tuple (car (umlisp-query ,table ,fields ,srl ,where-name ,where-value
+                                     :lrlname ,lrlname :single ,single
+                                     :distinct ,distinct :order ,order :like ,like))))
+       (when tuple
+         (destructuring-bind ,fields tuple
+           ,@body))))
     `(unless (and ,where-name (not ,where-value)) 
-      (let ((,query (umlisp-query ,table (quote ,fields) ,srl ,where-name ,where-value
-                                 :lrlname ,lrlname :single ,single :distinct ,distinct
-                                 :order ,order :like ,like)))
-       (if ,single
-           (let ((tuple (car ,query)))
-             (when tuple
-               (destructuring-bind ,fields tuple
-                 ,@body)))
-           (loop
-            for tuple in ,query collect
-            (destructuring-bind ,fields tuple
-              ,@body)))))))
-
-(defun umlisp-query (table fields srl where-name where-value
+      (loop for tuple in
+       (umlisp-query ,table ,fields ,srl ,where-name ,where-value
+       :lrlname ,lrlname :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 (lrlname "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)
+  `(when (or (not ,where-name) ,where-value)
     (mutex-sql-query
-     (query-string table fields srl where-name where-value 
-                  :lrlname lrlname :single single :distinct distinct :order order :like like))))
+     (query-string-macro ,table ,fields ,srl ,where-name ,where-value 
+      :lrlname ,lrlname :single ,single :distinct ,distinct :order ,order :like ,like))))
 
   
 (defmacro query-string-macro (table fields &optional srl where-name where-value
                        &key (lrlname "KCUILRL") single distinct order like)
   (let* ((%%fields (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)"
                           (if distinct "distinct " "") fields table))
-        (%%order (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) "")))
+        (%%order (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) ""))
+        (%%lrl (format nil " and ~:@(~A~)<=" lrlname))
+        (%%where (format nil " where ~:@(~A~)~A" where-name
+                         (if like " like " "="))))
     `(concatenate
       'string
-      %%fields
-      (if ,where-name
-         (format nil
-                 (if (stringp ,where-value)
-                     (if ,like
-                         " where ~A like '%~A%'"
-                         " where ~A='~A'")
-                     " where ~A=~A")
-                 ,where-name ,where-value)
+      ,%%fields
+      ,@(when %%where (list %%where))
+      (if (quote ,where-name)
+         (if (numberp ,where-value)
+             (write-to-string ,where-value)
+             (format nil ,(if like "'%~A%'" "'~A'") ,where-value))
          "")
-      (if ,srl (format nil " and ~:@(~A~) <= ~D" ,lrlname ,srl) "")
-      ,%%order
-      (if ,single " limit 1" ""))))
+      (if ,srl (concatenate 'string ,%%lrl (write-to-string ,srl)) "")
+      ,@(when %%order (list %%order))
+      ,@(when single (list " limit 1")))))
 
 (defun query-string (table fields &optional srl where-name where-value
                     &key (lrlname "KCUILRL") single distinct order like)
@@ -98,7 +97,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 
 (defun find-ucon-cui (cui &key (srl *current-srl*))
   "Find ucon for a cui"
-  (with-umlisp-query ('mrcon (kpfstr kcuilrl) srl 'cui (parse-cui cui) :single t)
+  (with-umlisp-query (mrcon (kpfstr kcuilrl) srl cui (parse-cui cui) :single t)
     (make-instance 'ucon :cui (parse-cui cui)
                   :pfstr kpfstr
                   :lrl (ensure-integer kcuilrl))))
@@ -111,10 +110,10 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
       (when srl
        (string-append ls (format nil " and KCUILRL <= ~d" srl)))
       (string-append ls " limit 1")
-      (awhen (car (mutex-sql-query ls))
-            (destructuring-bind (kpfstr kcuilrl)
-                (make-instance 'ucon :cui cui :pfstr kpfstr
-                           :lrl (ensure-integer kcuilrl)))))))
+      (let ((tuple (car (mutex-sql-query ls))))
+       (destructuring-bind (kpfstr kcuilrl) tuple
+         (make-instance 'ucon :cui cui :pfstr kpfstr
+                        :lrl (ensure-integer kcuilrl)))))))
 
 (defun find-ucon-cui-sans-pfstr (cui &key (srl *current-srl*))
   "Find ucon for a cui"