r4871: *** empty log message ***
[umlisp.git] / sql-classes.lisp
index 9d64345db65eccd6f50a5b673256b2dcccd21aea..9e71b0f7b09972c0dcc41a68fce99b222e31857a 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; Author:        Kevin M. Rosenberg
 ;;;; Date Started:  Apr 2000
 ;;;;
-;;;; $Id: sql-classes.lisp,v 1.40 2003/05/03 20:33:16 kevin Exp $
+;;;; $Id: sql-classes.lisp,v 1.75 2003/05/07 22:53:36 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)
-  (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)) 
-      (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)
-    (mutex-sql-query
-     (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)
+(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))
-        (%%order (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) ""))
-        (%%lrl (format nil " and ~:@(~A~) <= ~~D" lrlname)))
+        (%%order (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}"
+                                   order)
+                     ""))
+        (%%lrl (format nil " and ~:@(~A~)<=" lrl))
+        (%%where (when where-name
+                   (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)
-         "")
-      (if ,srl (format nil ,%%lrl ,srl) "")
+      ,@(when %%where (list %%where))
+      ,@(when %%where
+             `((typecase ,where-value
+                 (number
+                  (concatenate 'string "=" (write-to-string ,where-value)))
+                 (null
+                  " 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 (" limit 1")))))
+      ,@(when single (list " limit 1")))))
 
-(defun query-string (table fields &optional srl where-name where-value
-                    &key (lrlname "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~)" 
           (if distinct "distinct " "") fields table)
+   (if where-name (format nil " where ~:@(~A~)" where-name) "")
    (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 "=~D")
+                (null " is null")
+                (t
+                 (if like " like '%~A%""='~A'")))
+              where-value)
        "")
-   (if srl (format nil " and ~:@(~A~) <= ~D" lrlname srl) "")
+   (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))
+       (r (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))))
+          (let ((,r '()))
+            (dolist (tuple (umlisp-query ,table ,fields ,srl ,where-name ,value
+                                         :lrl ,lrl :single ,single :distinct ,distinct
+                                         :order ,order :like ,like))
+              (push (destructuring-bind ,fields tuple ,@body) ,r))
+            (nreverse ,r))
+          #+ignore
+          (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))
+       (r (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)
+              (,r '()))
+          (dolist (tuple (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
+                                            :lrl ,lrl :single ,single :distinct ,distinct
+                                            :order ,order :like ,like))
+            (push (destructuring-bind ,eval-fields tuple ,@body) ,r))
+          (nreverse ,r)
+          #+ignore
+          (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
+  (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))))
 
-(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)
+  (with-umlisp-query (mrcon (kcuilrl) srl cui (parse-cui cui) :single t)
+    (make-instance 'ucon :cui (parse-cui cui) :lrl (ensure-integer kcuilrl)
                   :pfstr nil)))
 
 (defun find-pfstr-cui (cui &key (srl *current-srl*))
   "Find preferred string for a cui"
-  (with-umlisp-query ('mrcon (kpfstr) srl 'cui (parse-cui cui) :single t)
+  (with-umlisp-query (mrcon (kpfstr) srl cui (parse-cui cui) :single t)
     kpfstr))
 
 (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
+  (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl lui (parse-lui lui)
+                           :distinct t)
+    (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
                   :lrl (ensure-integer kcuilrl))))
 
 (defun find-ucon-sui (sui &key (srl *current-srl*))
   "Find list of ucon for sui"
-  (with-umlisp-query ('mrcon (cui kpfstr kcuilrl) srl 'sui (parse-sui sui) :distinct t)
-    (make-instance 'ucon :cui (ensure-integer cui)
-                  :pfstr kpfstr
+  (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl sui (parse-sui sui)
+                           :distinct t)
+    (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
                   :lrl (ensure-integer kcuilrl))))
 
 (defun find-ucon-cuisui (cui sui &key (srl *current-srl*))
   "Find ucon for cui/sui"
   (when (and cui sui)
-    (with-umlisp-query ('mrcon (cui kpfstr kcuilrl) srl 'kcuisui 
+    (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl kcuisui 
                               (make-cuisui (parse-cui cui) (parse-sui sui)))
       (make-instance 'ucon :cui (ensure-integer cui)
                     :pfstr kpfstr
@@ -150,7 +197,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 
 (defun find-ucon-str (str &key (srl *current-srl*))
   "Find ucon that are exact match for str"
-  (with-umlisp-query ('mrcon (cui kpfstr kcuilrl) srl 'str str :distinct t)
+  (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl str str :distinct t)
     (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
                   :lrl (ensure-integer kcuilrl))))
 
@@ -163,10 +210,18 @@ 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 mrcon (cui kpfstr kcuilrl) srl nil nil
+                  :order (cui asc) :distinct t)
      :database db)))
 
+(defun find-ucon-all2 (&key (srl *current-srl*))
+  "Return list of all ucon's"
+  (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc)
+                           :distinct t)
+    (make-instance 'ucon :cui (ensure-integer cui)
+                  :pfstr kpfstr
+                  :lrl (ensure-integer kcuilrl))))
+
 (defun map-ucon-all (fn &key (srl *current-srl*))
   "Map a function over all ucon's"
   (with-sql-connection (db)
@@ -177,59 +232,67 @@ 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 mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc)
+                  :distinct t)
      :database db)))
 
 
 (defun find-udef-cui (cui &key (srl *current-srl*))
   "Return a list of udefs for cui"
-  (with-umlisp-query ('mrdef (sab def) srl 'cui (parse-cui cui) :lrlname "KSRL")
+  (with-umlisp-query (mrdef (sab def) srl cui (parse-cui cui) :lrl "KSRL")
     (make-instance 'udef :sab sab :def def)))
 
 (defun find-usty-cui (cui &key (srl *current-srl*))
   "Return a list of usty for cui"
-  (with-umlisp-query ('mrsty (tui sty) srl 'cui (parse-cui cui) :lrlname "KLRL")
+  (with-umlisp-query (mrsty (tui sty) srl cui (parse-cui cui) :lrl "KLRL")
     (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
 
 (defun find-usty-word (word &key (srl *current-srl*))
   "Return a list of usty that match word"
-  (with-umlisp-query ('mrsty (tui sty) srl 'sty word :lrlname 'klrl :like t :distinct t)
+  (with-umlisp-query (mrsty (tui sty) srl sty word :lrl klrl :like t
+                           :distinct t)
     (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
 
 (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) :lrlname "KSRL")
-    (make-instance 'urel :cui1 (parse-cui cui) :rel rel :cui2 (ensure-integer cui2) :rela rela
-                  :sab sab :sl sl :mg mg :pfstr2 kpfstr2)))
+  (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)))
 
 (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) :lrlname "KSRL")
-    (make-instance 'urel :cui2 (parse-cui cui2) :rel rel :cui1 (ensure-integer cui1) :rela rela
-                  :sab sab :sl sl :mg mg :pfstr2 kpfstr2)))
+  (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)))
 
 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
-  (mapcar 
-   #'(lambda (cui) (find-ucon-cui cui :srl srl))
-   (remove-duplicates (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))))
+  (loop for cui in (remove-duplicates
+                   (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))
+       collect (find-ucon-cui cui :srl srl)))
 
 (defun find-ucoc-cui (cui &key (srl *current-srl*))
   "Return a list of ucoc for cui"
-  (with-umlisp-query ('mrcoc (cui2 soc cot cof coa kpfstr2) srl 'cui1 (parse-cui cui) 
-                            :lrlname "KSRL" :order '(cof asc))
+  (with-umlisp-query (mrcoc (cui2 soc cot cof coa kpfstr2) srl cui1
+                           (parse-cui cui) :lrl klrl :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"
-  (with-umlisp-query ('mrcoc (cui1 soc cot cof coa kpfstr2) srl 'cui2 (parse-cui cui2) 
-                            :lrlname "KSRL" :order '(cof asc))
+  (with-umlisp-query (mrcoc (cui1 soc cot cof coa kpfstr2) srl cui2
+                           (parse-cui cui2) :lrl klrl :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"
@@ -239,70 +302,71 @@ 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) :lrlname "KLRL")
-    (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))
-  "Return the string for a ulo object"
-  (find-string-sui (sui lo)))
+  (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)))
 
 (defun find-uatx-cui (cui &key (srl *current-srl*))
   "Return a list of uatx for cui"
-  (with-umlisp-query ('mratx (sab rel atx) srl 'cui (parse-cui cui) :lrlname 'ksrl)
+  (with-umlisp-query (mratx (sab rel atx) srl cui (parse-cui cui) :lrl ksrl)
     (make-instance 'uatx :sab sab :rel rel :atx atx)))
 
 
 (defun find-uterm-cui (cui &key (srl *current-srl*))
   "Return a list of uterm for cui"
-  (with-umlisp-query ('mrcon (lui lat ts kluilrl) srl 'cui (parse-cui cui) :lrlname 'kluilrl
-                            :distinct t)
+  (with-umlisp-query (mrcon (lui lat ts kluilrl) srl cui (parse-cui cui)
+                           :lrl kluilrl :distinct t)
     (make-instance 'uterm :lui (ensure-integer lui) :cui (parse-cui cui)
                   :lat lat :ts ts :lrl (ensure-integer kluilrl))))
 
 (defun find-uterm-lui (lui &key (srl *current-srl*))
   "Return a list of uterm for lui"
-  (with-umlisp-query ('mrcon (cui lat ts kluilrl) srl 'lui (parse-lui lui) 
-                            :lrlname 'kluilrl :distinct t)
+  (with-umlisp-query (mrcon (cui lat ts kluilrl) srl lui (parse-lui lui) 
+                            :lrl kluilrl :distinct t)
     (make-instance 'uterm :cui (ensure-integer cui) :lui (parse-lui lui)
                   :lat lat :ts ts :lrl (ensure-integer kluilrl))))
 
 (defun find-uterm-cuilui (cui lui &key (srl *current-srl*))
   "Return single uterm for cui/lui"
-  (with-umlisp-query ('mrcon (lat ts kluilrl) srl 'kcuilui
+  (with-umlisp-query (mrcon (lat ts kluilrl) srl kcuilui
                             (make-cuilui (parse-cui cui) (parse-lui lui))
-                            :lrlname 'kluilrl :single t)
-    (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts :lrl (ensure-integer kluilrl))))
+                            :lrl kluilrl :single t)
+    (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"
-  (with-umlisp-query ('mrcon (sui stt str lrl) srl 'kcuilui (make-cuilui cui lui) :lrlname 'lrl)
+  (with-umlisp-query (mrcon (sui stt str lrl) srl kcuilui
+                           (make-cuilui cui lui) :lrl lrl)
     (make-instance 'ustr :sui (ensure-integer sui) :cui cui :lui lui
                   :cuisui (make-cuisui cui sui) :stt stt :str str
                   :lrl (ensure-integer lrl))))
 
 (defun find-ustr-cuisui (cui sui &key (srl *current-srl*))
   "Return the single ustr for cuisui"
-  (with-umlisp-query ('mrcon (lui stt str lrl) srl 'kcuisui (make-cuisui cui sui) :lrlname 'lrl :single t)
+  (with-umlisp-query (mrcon (lui stt str lrl) srl kcuisui
+                           (make-cuisui cui sui) :lrl lrl :single t)
     (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui)
-                  :lui (ensure-integer lui) :stt stt :str str :lrl (ensure-integer lrl))))
+                  :lui (ensure-integer lui) :stt stt :str str
+                  :lrl (ensure-integer lrl))))
 
 (defun find-ustr-sui (sui &key (srl *current-srl*))
   "Return the list of ustr for sui"
-  (with-umlisp-query ('mrcon (cui lui stt str lrl) srl 'sui (parse-sui sui) :lrlname 'lrl)
+  (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"
-  (with-umlisp-query ('mrso (kcuisui) srl 'sab sab :lrlname 'srl)
+  (with-umlisp-query (mrso (kcuisui) srl sab sab :lrl srl)
     (let ((cuisui (ensure-integer kcuisui)))
       (apply #'find-ustr-cuisui 
             (append
-             (multiple-value-list (decompose-cuisui cuisui)) (list :srl srl))))))
+             (multiple-value-list (decompose-cuisui cuisui))
+             (list :srl srl))))))
 
 (defun find-ustr-all (&key (srl *current-srl*))
   "Return list of all ustr's"
@@ -310,101 +374,97 @@ 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))
-       (query-string 'mrcon '(cui lui sui stt lrl kpfstr) srl nil nil :lrlname 'lrl :distinct t
-                    :order '(sui asc))
+          (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)))
 
 (defun find-string-sui (sui &key (srl *current-srl*))
   "Return the string associated with sui"
-  (with-umlisp-query ('mrcon (str) srl 'sui sui :lrlname 'lrl :single t)
+  (with-umlisp-query (mrcon (str) srl sui sui :lrl lrl :single t)
     str))
 
 (defun find-uso-cuisui (cui sui &key (srl *current-srl*))
-  (with-umlisp-query ('mrso (sab code srl tty) srl 'kcuisui (make-cuisui cui sui) :lrlname 'srl)
+  (with-umlisp-query (mrso (sab code srl tty) srl kcuisui
+                          (make-cuisui cui sui) :lrl srl)
       (make-instance 'uso :sab sab :code code :srl srl :tty tty)))
 
 (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*))
-  (with-umlisp-query ('mrcxt (sab code cxn cxl rnk cxs cui2 hcd rela xc) srl 'kcuisui
-                            (make-cuisui cui sui) :lrlname 'ksrl)
+  (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)
+                  :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 ")))
     (cond
-      (sui (string-append ls (format nil "KCUISUI=~d" (make-cuisui cui sui))))
-      (lui (string-append ls (format nil "KCUILUI=~d and sui=0" (make-cuilui cui lui))))
-      (t (string-append ls (format nil "cui=~d and lui=0 and sui=0" cui))))
+      (sui (string-append ls (format nil "KCUISUI=~D" (make-cuisui cui sui))))
+      (lui (string-append ls (format nil "KCUILUI=~D and sui=0"
+                                    (make-cuilui cui lui))))
+      (t (string-append ls (format nil "cui=~D and lui=0 and sui=0" cui))))
     (when srl
-      (string-append ls (format nil " and KSRL <= ~d" srl)))
-    (let ((usats '()))
-      (dolist (tuple (mutex-sql-query ls))
-       (destructuring-bind (code atn sab atv) tuple
-         (push (make-instance 'usat :code code :atn atn :sab sab :atv atv)
-               usats)))
-      (nreverse usats))))
-
+      (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)))))
 
 (defun find-usty-tui (tui)
   "Find usty for tui"
-  (with-umlisp-query ('mrsty (sty) nil 'tui (parse-tui tui) :single t)
+  (with-umlisp-query (mrsty (sty) nil tui (parse-tui tui) :single t)
     (make-instance 'usty :tui (parse-tui tui) :sty sty)))
 
 (defun find-usty-sty (sty)
   "Find usty for a sty"
-  (with-umlisp-query ('mrsty (tui) nil 'sty sty :single t)
+  (with-umlisp-query (mrsty (tui) nil sty sty :single t)
     (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
 
 (defun find-usty-all ()
   "Return list of usty's for all semantic types"
-  (with-umlisp-query ('mrsty (tui) nil nil nil :distinct t)
+  (with-umlisp-query (mrsty (tui) nil nil nil :distinct t)
     (find-usty-tui tui)))
 
 (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) 
+                  :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 ('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) 
+                  :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"
-  (find-usab-by-key "RSAB" rsab))
+  (find-usab-by-key 'rsab rsab))
 
 (defun find-usab-vsab (vsab)
   "Find usab for vsab"
-  (find-usab-by-key "VSAB" vsab))
+  (find-usab-by-key 'vsab vsab))
 
 (defun find-cui-max ()
   (ensure-integer (caar (mutex-sql-query "select max(CUI) from MRCON"))))
@@ -413,39 +473,49 @@ 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) :lrlname '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))
   "Return list of ucons that match word. Optionally, use SQL's LIKE syntax"
-  (with-umlisp-query ('mrxw_eng (cui) srl 'wd word :like like :distinct t
-                               :lrlname 'klrl :order '(cui asc))
+  (with-umlisp-query-eval ('mrxw_eng '(cui) srl 'wd word :like like :distinct t
+                                    :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 ('mrxnw_eng (cui) srl 'nwd word :like like :distinct t
-                               :lrlname 'klrl :order '(cui asc))
+  (with-umlisp-query-eval ('mrxnw_eng '(cui) srl 'nwd word :like like :distinct t
+                                     :lrl 'klrl :order '(cui asc))
     (find-ucon-cui cui :srl srl)))
 
 (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
-                               :lrlname '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*))
   "Return list of ustrs that match word"
-  (with-umlisp-query ('mrxnw_eng (cui sui) srl 'nwd word :lrlname 'klrl
-                                :order '(cui asc sui asc))
+  (with-umlisp-query (mrxnw_eng (cui sui) srl nwd word :lrl klrl
+                                :order (cui asc sui asc))
+    (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
+
+(defun find-ucon-noneng-word (word &key (srl *current-srl*) (like nil))
+  "Return list of ucons that match non-english word"
+  (with-umlisp-query-eval ('mrxw_noneng '(cui) srl 'wd word :like like
+                                       :distinct t :lrl 'klrl :order '(cui asc))
+    (find-ucon-cui cui :srl srl)))
+
+(defun find-ustr-noneng-word (word &key (srl *current-srl*))
+  "Return list of ustrs that match non-english word"
+  (with-umlisp-query (mrxw_noneng (cui sui) srl wd word :lrl klrl
+                                 :order (cui asc sui asc))
     (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
 
 ;; Special tables
 
 (defun find-usrl-all ()
-  (with-umlisp-query ('usrl (sab srl) nil nil nil :order '(sab asc))
+  (with-umlisp-query (usrl (sab srl) nil nil nil :order (sab asc))
     (make-instance 'usrl :sab sab :srl (ensure-integer srl))))
 
 ;;; Multiword lookup and score functions
@@ -478,9 +548,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))))
 
@@ -521,96 +589,96 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 ;;; LEX SQL functions
 
 (defun find-lexterm-eui (eui)
-  (with-umlisp-query ('lrwd (wrd) nil 'eui eui :single t)
+  (with-umlisp-query (lrwd (wrd) nil eui eui :single t)
     (make-instance 'lexterm :eui eui :wrd wrd)))
 
 (defun find-lexterm-word (wrd)
-  (with-umlisp-query ('lrwd (eui) nil 'wrd wrd)
+  (with-umlisp-query (lrwd (eui) nil wrd wrd)
     (make-instance 'lexterm :eui (ensure-integer eui)
                   :wrd (copy-seq wrd))))
 
 ;; LEX SQL Read functions
 
 (defun find-labr-eui (eui)
-  (with-umlisp-query ('lrabr (bas abr eui2 bas2) nil 'eui eui) 
+  (with-umlisp-query (lrabr (bas abr eui2 bas2) nil eui eui) 
     (make-instance 'labr :eui eui :bas bas :abr abr :bas2 bas2
                   :eui2 (ensure-integer eui2))))
 
 (defun find-labr-bas (bas)
-  (with-umlisp-query ('labr (eui abr eui2 bas2) nil 'bas bas)
+  (with-umlisp-query (labr (eui abr eui2 bas2) nil bas bas)
     (make-instance 'labr :eui (ensure-integer eui) :abr abr :bas2 bas2
                   :bas (copy-seq bas) :eui2 (ensure-integer eui2))))
 
 (defun find-lagr-eui (eui)
-  (with-umlisp-query ('lragr (str sca agr cit bas) nil 'eui eui)
+  (with-umlisp-query (lragr (str sca agr cit bas) nil eui eui)
     (make-instance 'lagr :eui eui :str str :sca sca :agr agr
                   :cit cit :bas bas)))
 
 (defun find-lcmp-eui (eui)
-  (with-umlisp-query ('lrcmp (bas sca com) nil 'eui eui)
+  (with-umlisp-query (lrcmp (bas sca com) nil eui eui)
     (make-instance 'lcmp :eui eui :bas bas :sca sca :com com)))
 
 (defun find-lmod-eui (eui)
-  (with-umlisp-query ('lrmod (bas sca psn_mod fea) nil 'eui eui)
+  (with-umlisp-query (lrmod (bas sca psn_mod fea) nil eui eui)
     (make-instance 'lmod :eui eui :bas bas :sca sca :psnmod psn_mod :fea fea)))
 
 (defun find-lnom-eui (eui)
-  (with-umlisp-query ('lrnom (bas sca eui2 bas2 sca2) nil 'eui eui)
+  (with-umlisp-query (lrnom (bas sca eui2 bas2 sca2) nil eui eui)
     (make-instance 'lnom :eui eui :bas bas :sca sca :bas2 bas2 :sca2 sca2
                   :eui2 (ensure-integer eui2))))
 
 (defun find-lprn-eui (eui)
-  (with-umlisp-query ('lrprn (bas num gnd cas pos qnt fea) nil 'eui eui)
+  (with-umlisp-query (lrprn (bas num gnd cas pos qnt fea) nil eui eui)
     (make-instance 'lprn :eui eui :bas bas :num num :gnd gnd
                   :cas cas :pos pos :qnt qnt :fea fea)))
 
 (defun find-lprp-eui (eui)
-  (with-umlisp-query ('lrprp (bas str sca fea) nil 'eui eui)
+  (with-umlisp-query (lrprp (bas str sca fea) nil eui eui)
     (make-instance 'lprp :eui eui :bas bas :str str :sca sca :fea fea)))
 
 (defun find-lspl-eui (eui)
-  (with-umlisp-query ('lrspl (spv bas) nil 'eui eui)
+  (with-umlisp-query (lrspl (spv bas) nil eui eui)
     (make-instance 'lspl :eui eui :spv spv :bas bas)))
 
 (defun find-ltrm-eui (eui)
-  (with-umlisp-query ('lrtrm (bas gen) nil 'eui eui) 
+  (with-umlisp-query (lrtrm (bas gen) nil eui eui) 
     (make-instance 'ltrm :eui eui :bas bas :gen gen)))
 
 (defun find-ltyp-eui (eui)
-  (with-umlisp-query ('lrtyp (bas sca typ) nil 'eui eui)
+  (with-umlisp-query (lrtyp (bas sca typ) nil eui eui)
     (make-instance 'ltyp :eui eui :bas bas :sca sca :typ typ)))
 
 (defun find-lwd-wrd (wrd)
   (make-instance 'lwd :wrd
-                :euilist (with-umlisp-query ('lrwd (eui) nil 'wrd wrd)
+                :euilist (with-umlisp-query (lrwd (eui) nil wrd wrd)
                            (ensure-integer eui))))
 
 ;;; Semantic Network SQL access functions
 
 (defun find-sdef-ui (ui)
-  (with-umlisp-query ('srdef (rt sty_rl stn_rtn def ex un rh abr rin)
-                            nil 'ui ui :single t)
+  (with-umlisp-query (srdef (rt sty_rl stn_rtn def ex un rh abr rin)
+                           nil ui ui :single t)
     (make-instance 'sdef :rt rt :ui ui :styrl sty_rl :stnrtn stn_rtn
                   :def def :ex ex :un un :rh rh :abr abr :rin rin)))
 
 (defun find-sstre1-ui (ui)
-  (with-umlisp-query ('srstre1 (ui2 ui3) nil 'ui ui)
+  (with-umlisp-query (srstre1 (ui2 ui3) nil ui ui)
     (make-instance 'sstre1 :ui ui :ui2 (ensure-integer ui2)
                   :ui3 (ensure-integer ui3))))
 
 (defun find-sstre1-ui2 (ui2)
-  (with-umlisp-query ('srstre1 (ui ui3) nil 'ui2 ui2)
+  (with-umlisp-query (srstre1 (ui ui3) nil ui2 ui2)
     (make-instance 'sstre1 :ui (ensure-integer ui) :ui2 ui2
                   :ui3 (ensure-integer ui3))))
 
 (defun find-sstr-rl (rl)
-  (with-umlisp-query ('srstre (sty_rl sty_rl2 ls) nil 'rl rl)
+  (with-umlisp-query (srstre (sty_rl sty_rl2 ls) nil rl rl)
     (make-instance 'sstr :rl rl :styrl sty_rl :styrl2 sty_rl2 :ls ls)))
 
 (defun find-sstre2-sty (sty)
-  (with-umlisp-query ('srstre2 (rl sty2) nil 'sty sty)
+  (with-umlisp-query (srstre2 (rl sty2) nil sty sty)
     (make-instance 'sstre2 :sty (copy-seq sty) :rl rl :sty2 sty2)))
 
 (defun find-sstr-styrl (styrl)
-  (with-umlisp-query ('srstr (rl sty_rl2 ls) nil 'styrl styrl)
+  (with-umlisp-query (srstr (rl sty_rl2 ls) nil styrl styrl)
     (make-instance 'sstr :styrl styrl :rl rl :styrl2 sty_rl2 :ls ls)))