r4796: Auto commit for Debian build
authorKevin M. Rosenberg <kevin@rosenberg.net>
Sun, 4 May 2003 03:45:05 +0000 (03:45 +0000)
committerKevin M. Rosenberg <kevin@rosenberg.net>
Sun, 4 May 2003 03:45:05 +0000 (03:45 +0000)
sql-classes.lisp

index 086b5cdfe749f3c5ae6ad2de98328c3be7ced700..6a3ccd0fc79e3c551bc3e7c9cb4facbe4a456c96 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; Author:        Kevin M. Rosenberg
 ;;;; Date Started:  Apr 2000
 ;;;;
-;;;; $Id: sql-classes.lisp,v 1.67 2003/05/04 01:42:20 kevin Exp $
+;;;; $Id: sql-classes.lisp,v 1.68 2003/05/04 03:45:05 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))
 
-;; only WHERE-VALUE and SRL are evaluated
-(defmacro with-umlisp-query ((table fields srl where-name where-value
-                                   &key (lrl "KCUILRL") distinct single
-                                   order like)
-                            &body 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 with-umlisp-query-eval ((table fields srl where-name where-value
-                                        &key (lrl "KCUILRL") distinct single
-                                        order like)
-                                 &body body)
-  (let ((value (gensym))
-       (eval-fields (cadr fields)))
-    (if single
-       `(let ((,value ,where-value))
-         (when ,value 
-           (let ((tuple (car (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
-                                           :lrl ,lrl :single ,single
-                                           :distinct ,distinct :order ,order
-                                           :like ,like))))
-             (when tuple
-               (destructuring-bind ,eval-fields tuple
-                 ,@body)))))
-       `(let ((,value ,where-value))
-         (when ,value 
-           (loop for tuple in
-                 (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
-                                    :lrl ,lrl :single ,single :distinct ,distinct
-                                    :order ,order :like ,like)
-                 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 ,table ,fields ,srl ,where-name ,where-value 
-     :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
-
-(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-eval ,table ,fields ,srl ,where-name ,where-value 
-     :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
-
-  
 (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~)"
@@ -108,17 +40,13 @@ 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)))
              `((typecase ,where-value
                  (number
                   (concatenate 'string "=" (write-to-string ,where-value)))
                  (null
                   " is null")
                  (t
-                  (format nil ,(if like "'%~A%'" "'=~A'") ,where-value)))))
+                  (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")))))
@@ -131,42 +59,97 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
           (if distinct "distinct " "") fields table)
    (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)
+              (typecase where-value
+                (number " where ~A=~D")
+                (null " where ~A is null")
+                (t
+                 (if like
+                     " where ~A like '%~A%'"
+                     " where ~A='~A'")))
+              where-name where-value)
        "")
    (if srl (format nil " and ~:@(~A~) <= ~D" lrl srl) "")
    (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) "")
    (if single " limit 1" "")))
 
+
+(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 ,table ,fields ,srl ,where-name ,where-value 
+     :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
+
+(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-eval ,table ,fields ,srl ,where-name ,where-value 
+     :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
+
+;; only WHERE-VALUE and SRL are evaluated
+(defmacro with-umlisp-query ((table fields srl where-name where-value
+                                   &key (lrl "KCUILRL") distinct single
+                                   order like)
+                            &body body)
+  (let ((value (gensym)))
+    (if single
+       `(let* ((,value ,where-value)
+               (tuple (car (umlisp-query ,table ,fields ,srl ,where-name ,value
+                                         :lrl ,lrl :single ,single
+                                         :distinct ,distinct :order ,order
+                                         :like ,like))))
+         ,@(unless where-name `((declare (ignore ,value))))
+         (when tuple
+               (destructuring-bind ,fields tuple
+                 ,@body)))
+       `(let ((,value ,where-value))
+         ,@(unless where-name `((declare (ignore ,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 with-umlisp-query-eval ((table fields srl where-name where-value
+                                        &key (lrl "KCUILRL") distinct single
+                                        order like)
+                                 &body body)
+  (let ((value (gensym))
+       (eval-fields (cadr fields)))
+    (if single
+       `(let* ((,value ,where-value)
+               (tuple (car (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
+                                              :lrl ,lrl :single ,single
+                                              :distinct ,distinct :order ,order
+                                              :like ,like))))
+         (when tuple
+           (destructuring-bind ,eval-fields tuple
+             ,@body)))
+       `(let ((,value ,where-value))
+         (loop for tuple in
+          (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
+           :lrl ,lrl :single ,single :distinct ,distinct
+           :order ,order :like ,like)
+          collect (destructuring-bind ,eval-fields tuple
+                    ,@body))))))
+
+;;;
+;;; Read from SQL database
+
 (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)
-    (make-instance 'ucon :cui (parse-cui cui)
-                  :pfstr kpfstr
+    (make-instance 'ucon :cui (parse-cui cui) :pfstr kpfstr
                   :lrl (ensure-integer kcuilrl))))
 
-(defun find-ucon-cui-old (cui &key (srl *current-srl*))
-  "Find ucon for a cui"
-  (when (stringp cui) (setq cui (parse-cui cui)))
-  (when cui
-    (let ((ls (format nil "select KPFSTR,KCUILRL from MRCON where CUI=~d" cui)))
-      (when srl
-       (string-append ls (format nil " and KCUILRL <= ~d" srl)))
-      (string-append ls " limit 1")
-      (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"
   (with-umlisp-query (mrcon (kcuilrl) srl cui (parse-cui cui) :single t)
-    (make-instance 'ucon :cui (parse-cui cui)
-                  :lrl (ensure-integer kcuilrl)
+    (make-instance 'ucon :cui (parse-cui cui) :lrl (ensure-integer kcuilrl)
                   :pfstr nil)))
 
 (defun find-pfstr-cui (cui &key (srl *current-srl*))
@@ -177,8 +160,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 (defun find-ucon-lui (lui &key (srl *current-srl*))
   "Find list of ucon for lui"
   (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl lui (parse-lui lui) :distinct t)
-    (make-instance 'ucon :cui (ensure-integer cui)
-                  :pfstr kpfstr
+    (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
                   :lrl (ensure-integer kcuilrl))))
 
 (defun find-ucon-sui (sui &key (srl *current-srl*))
@@ -248,14 +230,14 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 (defun find-urel-cui (cui &key (srl *current-srl*))
   "Return a list of urel for cui"
   (with-umlisp-query (mrrel (rel cui2 rela sab sl mg kpfstr2) srl cui1 (parse-cui cui) :lrl "KSRL")
-    (make-instance 'urel :cui1 (parse-cui cui) :rel rel :cui2 (ensure-integer cui2) :rela rela
-                  :sab sab :sl sl :mg mg :pfstr2 kpfstr2)))
+    (make-instance 'urel :cui1 (parse-cui cui) :rel rel :cui2 (ensure-integer cui2)
+                  :rela rela :sab sab :sl sl :mg mg :pfstr2 kpfstr2)))
 
 (defun find-urel-cui2 (cui2 &key (srl *current-srl*))
   "Return a list of urel for cui2"
   (with-umlisp-query (mrrel (rel cui1 rela sab sl mg kpfstr2) srl cui2 (parse-cui cui2) :lrl "KSRL")
-    (make-instance 'urel :cui2 (parse-cui cui2) :rel rel :cui1 (ensure-integer cui1) :rela rela
-                  :sab sab :sl sl :mg mg :pfstr2 kpfstr2)))
+    (make-instance 'urel :cui2 (parse-cui cui2) :rel rel :cui1 (ensure-integer cui1)
+                  :rela rela :sab sab :sl sl :mg mg :pfstr2 kpfstr2)))
 
 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
   (mapcar 
@@ -268,8 +250,8 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
                             :lrl "KSRL" :order (cof asc))
     (setq cui2 (ensure-integer cui2))
     (when (zerop cui2) (setq cui2 nil))
-    (make-instance 'ucoc :cui1 (parse-cui cui) :cui2 (ensure-integer cui2) :soc soc :cot cot
-                  :cof (ensure-integer cof) :coa coa :pfstr2 kpfstr2)))
+    (make-instance 'ucoc :cui1 (parse-cui cui) :cui2 (ensure-integer cui2) :soc soc
+                  :cot cot :cof (ensure-integer cof) :coa coa :pfstr2 kpfstr2)))
 
 (defun find-ucoc-cui2 (cui2 &key (srl *current-srl*))
   "Return a list of ucoc for cui2"
@@ -277,8 +259,8 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
                             :lrl "KSRL" :order (cof asc))
     (setq cui2 (ensure-integer cui2))
     (when (zerop cui2) (setq cui2 nil))
-    (make-instance 'ucoc :cui1 (ensure-integer cui1) :cui2 (parse-cui cui2) :soc soc :cot cot
-                  :cof (ensure-integer cof) :coa coa :pfstr2 kpfstr2)))
+    (make-instance 'ucoc :cui1 (ensure-integer cui1) :cui2 (parse-cui cui2) :soc soc
+                  :cot cot :cof (ensure-integer cof) :coa coa :pfstr2 kpfstr2)))
 
 (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
   "List of ucon with co-occurance cui2"
@@ -289,8 +271,8 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 (defun find-ulo-cui (cui &key (srl *current-srl*))
   "Return a list of ulo for cui"
   (with-umlisp-query (mrlo (isn fr un sui sna soui) srl cui (parse-cui cui) :lrl "KLRL")
-    (make-instance 'ulo :isn isn :fr (ensure-integer fr) :un un :sui (ensure-integer sui) :sna sna
-                  :soui soui)))
+    (make-instance 'ulo :isn isn :fr (ensure-integer fr) :un un :sui (ensure-integer sui)
+                  :sna sna :soui soui)))
 
 (defgeneric suistr (lo))
 (defmethod suistr ((lo ulo))
@@ -322,7 +304,8 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
   (with-umlisp-query (mrcon (lat ts kluilrl) srl kcuilui
                             (make-cuilui (parse-cui cui) (parse-lui lui))
                             :lrl kluilrl :single t)
-    (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts :lrl (ensure-integer kluilrl))))
+    (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts
+                  :lrl (ensure-integer kluilrl))))
 
 (defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
   "Return a list of ustr for cui/lui"
@@ -342,8 +325,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
   (with-umlisp-query (mrcon (cui lui stt str lrl) srl sui (parse-sui sui) :lrl lrl)
     (make-instance 'ustr :sui sui :cui cui :stt stt :str str
                   :cuisui (make-cuisui (ensure-integer cui) (parse-sui sui))
-                  :lui (ensure-integer lui)
-                  :lrl (ensure-integer lrl))))
+                  :lui (ensure-integer lui) :lrl (ensure-integer lrl))))
       
 (defun find-ustr-sab (sab &key (srl *current-srl*))
   "Return the list of ustr for sab"
@@ -359,17 +341,10 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
       (clsql:map-query 
        'list
        #'(lambda (cui lui sui stt lrl pfstr)
-          (setq cui (ensure-integer cui))
-          (setq lui (ensure-integer lui))
-          (setq sui (ensure-integer sui))      
-          (setq lrl (ensure-integer lrl))
-          (make-instance 'ustr :cui cui
-                         :lui lui
-                         :sui sui
-                         :cuisui (make-cuisui cui sui)
-                         :stt stt
-                         :lrl lrl
-                         :str pfstr))
+          (make-instance 'ustr :cui (ensure-integer cui) :lui (ensure-integer lui)
+                         :sui (ensure-integer sui) :stt stt :str pfstr
+                         :cuisui (make-cuisui (ensure-integer cui) (ensure-integer sui))
+                         :lrl (ensure-integer lrl)))
        (query-string mrcon (cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl :distinct t
                     :order (sui asc))
        :database db)))
@@ -387,10 +362,8 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
   (with-umlisp-query (mrcxt (sab code cxn cxl rnk cxs cui2 hcd rela xc) srl kcuisui
                             (make-cuisui cui sui) :lrl ksrl)
     (make-instance 'ucxt :sab sab :code code
-                  :cxn (ensure-integer cxn)
-                  :cxl cxl :cxs cxs :hcd hcd :rela rela :xc xc
-                  :rnk (ensure-integer rnk)
-                  :cui2 (ensure-integer cui2))))
+                  :cxn (ensure-integer cxn) :cxl cxl :cxs cxs :hcd hcd :rela rela :xc xc
+                  :rnk (ensure-integer rnk) :cui2 (ensure-integer cui2))))
 
 (defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*))
   (let ((ls (format nil "select CODE,ATN,SAB,ATV from MRSAT where ")))
@@ -421,27 +394,27 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 
 (defun find-usab-all ()
   "Find usab for a key"
-  (with-umlisp-query (mrsab (vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) nil nil nil)
+  (with-umlisp-query (mrsab (vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc
+                                 scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin)
+                           nil nil nil)
     (make-instance 'usab :vcui (ensure-integer vcui) 
-                  :rcui (ensure-integer rcui)
-                  :vsab vsab :rsab rsab :son son :sf sf :sver sver :mstart mstart
-                  :mend mend :imeta imeta :rmeta rmeta :slc slc :scc scc
-                  :srl (ensure-integer srl) 
-                  :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
-                  :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
-                  :curver curver :sabin sabin)))
+                  :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son :sf sf
+                  :sver sver :mstart mstart :mend mend :imeta imeta :rmeta rmeta
+                  :slc slc :scc scc  :srl (ensure-integer srl) :tfr (ensure-integer tfr)
+                  :cfr (ensure-integer cfr) :cxty cxty :ttyl ttyl :atnl atnl :lat lat
+                  :cenc cenc :curver curver :sabin sabin)))
 
 (defun find-usab-by-key (key-name key)
   "Find usab for a key"
-  (with-umlisp-query-eval ('mrsab '(vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) nil key-name key :single t)
+  (with-umlisp-query-eval ('mrsab '(vcui rcui vsab rsab son sf sver mstart mend imeta
+                                   rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc
+                                   curver sabin) nil key-name key :single t)
     (make-instance 'usab :vcui (ensure-integer vcui) 
-                  :rcui (ensure-integer rcui)
-                  :vsab vsab :rsab rsab :son son :sf sf :sver sver :mstart mstart
-                  :mend mend :imeta imeta :rmeta rmeta :slc slc :scc scc
-                  :srl (ensure-integer srl) 
-                  :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
-                  :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
-                  :curver curver :sabin sabin)))
+                  :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son :sf sf
+                  :sver sver :mstart mstart :mend mend :imeta imeta :rmeta rmeta
+                  :slc slc :scc scc :srl (ensure-integer srl) :tfr (ensure-integer tfr)
+                  :cfr (ensure-integer cfr) :cxty cxty :ttyl ttyl :atnl atnl :lat lat
+                  :cenc cenc :curver curver :sabin sabin)))
 
 (defun find-usab-rsab (rsab)
   "Find usab for rsab"
@@ -458,8 +431,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 
 (defun find-ucon-tui (tui &key (srl *current-srl*))
   "Find list of ucon for tui"
-  (with-umlisp-query (mrsty (cui) srl tui (parse-tui tui) :lrl klrl
-                            :order (cui asc))
+  (with-umlisp-query (mrsty (cui) srl tui (parse-tui tui) :lrl klrl :order (cui asc))
     (find-ucon-cui (ensure-integer cui) :srl srl)))
   
 (defun find-ucon-word (word &key (srl *current-srl*) (like nil))
@@ -476,9 +448,8 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 
 (defun find-ustr-word (word &key (srl *current-srl*))
   "Return list of ustrs that match word"
-  (with-umlisp-query (mrxw_eng (cui sui) srl wd word
-                               :lrl klrl
-                               :order (cui asc sui asc))
+  (with-umlisp-query (mrxw_eng (cui sui) srl wd word :lrl klrl
+                              :order (cui asc sui asc))
     (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
 
 (defun find-ustr-normalized-word (word &key (srl *current-srl*))
@@ -523,9 +494,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
   "Sort a list of objects based on scoring to a string"
   (let ((scored '()))
     (dolist (obj objs)
-      (push 
-       (list obj 
-            (score-multiword-match str (funcall lookup-func obj))) 
+      (push (list obj (score-multiword-match str (funcall lookup-func obj))) 
        scored))
     (mapcar #'car (sort scored #'> :key #'cadr))))