r4790: Auto commit for Debian build
[umlisp.git] / sql-classes.lisp
index 088aec8fb9b07ce2c2567a6130115e0b515eb6b5..b04569f50bf376c1a77e3b1f6c425d82e9178c4f 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; Author:        Kevin M. Rosenberg
 ;;;; Date Started:  Apr 2000
 ;;;;
-;;;; $Id: sql-classes.lisp,v 1.58 2003/05/04 00:30:36 kevin Exp $
+;;;; $Id: sql-classes.lisp,v 1.64 2003/05/04 01:34:33 kevin Exp $
 ;;;;
 ;;;; This file, part of UMLisp, is
 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
@@ -55,7 +55,8 @@
                                         &key (lrl "KCUILRL") distinct single
                                         order like)
                                  &body body)
-  (let ((value (gensym)))
+  (let ((value (gensym))
+       (eval-fields (cadr fields)))
     (if single
        `(let ((,value ,where-value))
          (when ,value 
@@ -64,7 +65,7 @@
                                            :distinct ,distinct :order ,order
                                            :like ,like))))
              (when tuple
-               (destructuring-bind ,fields tuple
+               (destructuring-bind ,eval-fields tuple
                  ,@body)))))
        `(let ((,value ,where-value))
          (when ,value 
                  (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
                                     :lrl ,lrl :single ,single :distinct ,distinct
                                     :order ,order :like ,like)
-                 collect (destructuring-bind ,fields tuple
+                 collect (destructuring-bind ,eval-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"
   `(mutex-sql-query
-    (query-string-macro ,table ,fields ,srl ,where-name ,where-value 
+    (query-string ,table ,fields ,srl ,where-name ,where-value 
      :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
 
-(defun umlisp-query-eval (table fields srl where-name where-value
+(defmacro umlisp-query-eval (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"
-  (mutex-sql-query
-   (query-string-macro ,table ,fields ,srl ,where-name ,where-value 
+  `(mutex-sql-query
+    (query-string-eval ,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
+(defmacro query-string (table fields &optional srl where-name where-value
                        &key (lrl "KCUILRL") single distinct order like)
   (let* ((%%fields (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)"
                           (if distinct "distinct " "") fields table))
@@ -108,15 +108,23 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
       ,%%fields
       ,@(when %%where (list %%where))
       ,@(when %%where
+             #+ignore
              `((if (numberp ,where-value)
                    (write-to-string ,where-value)
-                   (format nil ,(if like "'%~A%'" "'~A'") ,where-value))))
+                   (format nil ,(if like "'%~A%'" "'~A'") ,where-value)))
+             `((typecase ,where-value
+                 (number
+                  (write-to-string ,where-value))
+                 ((nil)
+                  " is null")
+                 (t
+                  (format nil ,(if like "'%~A%'" "'~A'") ,where-value)))))
       (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 (lrl "KCUILRL") single distinct order like)
+(defun query-string-eval (table fields &optional srl where-name where-value
+                         &key (lrl "KCUILRL") single distinct order like)
   (concatenate
    'string
    (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)" 
@@ -204,7 +212,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-macro mrcon (cui kpfstr kcuilrl) srl nil nil
+     (query-string mrcon (cui kpfstr kcuilrl) srl nil nil
                   :order (cui asc) :distinct t)
      :database db)))
 
@@ -218,7 +226,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-macro mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc) :distinct t)
+     (query-string mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc) :distinct t)
      :database db)))
 
 
@@ -362,7 +370,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
                          :stt stt
                          :lrl lrl
                          :str pfstr))
-       (query-string-macro mrcon (cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl :distinct t
+       (query-string mrcon (cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl :distinct t
                     :order (sui asc))
        :database db)))
 
@@ -457,13 +465,13 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 (defun find-ucon-word (word &key (srl *current-srl*) (like nil))
   "Return list of ucons that match word. Optionally, use SQL's LIKE syntax"
   (with-umlisp-query-eval ('mrxw_eng '(cui) srl 'wd word :like like :distinct t
-                                    :lrl klrl :order '(cui asc))
+                                    :lrl 'klrl :order '(cui asc))
     (find-ucon-cui cui :srl srl)))
 
 (defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil))
   "Return list of ucons that match word, optionally use SQL's LIKE syntax"
   (with-umlisp-query-eval ('mrxnw_eng '(cui) srl 'nwd word :like like :distinct t
-                                     :lrl klrl :order '(cui asc))
+                                     :lrl 'klrl :order '(cui asc))
     (find-ucon-cui cui :srl srl)))
 
 (defun find-ustr-word (word &key (srl *current-srl*))