r11624: improve find-uconso-code
[umlisp.git] / sql-classes.lisp
index 594103fcd95c0d9a7b3303f5119b1f3512854136..6bd0012f9809c2cbb3c32b2dd5293e2dfc0ac05a 100644 (file)
@@ -25,7 +25,7 @@
 (defun current-srl! (srl)
   (setq *current-srl* srl))
 
-(defmacro query-string (table fields &optional srl where-name where-value
+(defmacro query-string (table fields srl where-name where-value
                        &key (lrl "KCUILRL") single distinct order like limit
                         filter)
   (let* ((%%fields (format nil "SELECT ~A~{~:@(~A~)~^,~} FROM ~:@(~A~)"
@@ -35,7 +35,7 @@
                      ""))
         (%%lrl (format nil " AND ~:@(~A~)<=" lrl))
         (%%where (when where-name
-                   (format nil " where ~:@(~A~)~A" where-name
+                   (format nil " WHERE ~:@(~A~)~A" where-name
                          (if like " like " ""))))
          (%filter (gensym "FILTER-"))
          (%single (gensym "SINGLE-"))
@@ -55,7 +55,7 @@
                 (number
                  (concatenate 'string "='" (write-to-string ,where-value) "'"))
                 (null
-                 " is null")
+                 " IS NULL")
                 (t
                  (format nil ,(if like "'%~A%'" "='~A'") ,where-value)))))
         (if ,%filter (concatenate 'string
@@ -65,7 +65,7 @@
         ,@(when %%order (list %%order))
         (cond
          ((and ,%single ,%limit)
-          (error "Can't set single and limit"))
+          (error  "Can't set single (~S) and limit (~S)" ,%single ,%limit))
          (,%single
           " LIMIT 1")
          (,%limit
          (t
           ""))))))
 
-(defun query-string-eval (table fields &optional srl where-name where-value
+(defun query-string-eval (table fields srl where-name where-value
                          &key (lrl "KCUILRL") single distinct order like limit filter)
   (when single (setq limit 1))
   (concatenate
    'string
-   (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)"
-          (if distinct "distinct " "") fields table)
-   (if where-name (format nil " where ~:@(~A~)" where-name) "")
+   (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
               (typecase where-value
                 (number "='~D'")
-                (null " is null")
+                (null " IS NULL")
                 (t
-                 (if like " like '%~A%""='~A'")))
+                 (if like " LINK '%~A%""='~A'")))
               where-value)
        "")
    (if filter (concatenate 'string " AND " filter) nil)
-   (if srl (format nil " and ~:@(~A~)<=~D" lrl srl) "")
-   (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) "")
-   (if limit (format nil " limit ~D" limit) "")))
+   (if srl (format nil " AND ~:@(~A~)<=~D" lrl srl) "")
+   (if order (format nil " ORDER BY ~{~:@(~A~) ~(~A~)~^,~}" order) "")
+   (if limit (format nil " LIMIT ~D" limit) "")))
 
 
 (defmacro umlisp-query (table fields srl where-name where-value
@@ -224,25 +224,70 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
     (setq ,eui (parse-eui ,eui))
     ,eui))
 
-(defun find-ucon-cui (cui &key (srl *current-srl*))
-  "Find ucon for a cui"
+(defun make-ucon-cui (cui)
+  (ensure-cui-integer cui)
+  (when cui
+    (make-instance 'ucon :cui cui)))
+
+(defun find-ucon-cui (cui &key (srl *current-srl*) without-pfstr)
+  "Find ucon for a cui. If set SAB, the without-pfstr is on by default"
   (ensure-cui-integer cui)
   (unless cui (return-from find-ucon-cui nil))
 
-  (or
-   (collect-umlisp-query (mrconso (kcuilrl str) srl cui cui :single t :filter "KPFENG=1")
-    (make-instance 'ucon :cui cui :pfstr str
-                   :lrl kcuilrl))
-   (collect-umlisp-query (mrconso (kcuilrl str) srl cui cui :single t)
-    (make-instance 'ucon :cui cui :pfstr str
-                   :lrl kcuilrl))))
-
-(defun find-ucon-cui-sans-pfstr (cui &key (srl *current-srl*))
-  "Find ucon for a cui"
+  (if without-pfstr
+      (collect-umlisp-query (mrconso (kcuilrl) srl cui cui :single t)
+                            (make-instance 'ucon :cui cui :lrl (ensure-integer kcuilrl)
+                                           :pfstr nil)))
+   (or
+     (collect-umlisp-query (mrconso (kcuilrl str) srl cui cui :single t :filter "KPFENG=1")
+        (make-instance 'ucon :cui cui :pfstr str
+                       :lrl kcuilrl))
+     (collect-umlisp-query (mrconso (kcuilrl str) srl cui cui :single t)
+        (make-instance 'ucon :cui cui :pfstr str
+                       :lrl kcuilrl))))
+
+(defun find-uconso-cui (cui &key sab (srl *current-srl*))
+  "Find uconso for a cui."
   (ensure-cui-integer cui)
-  (collect-umlisp-query (mrconso (kcuilrl) srl cui cui :single t)
-    (make-instance 'ucon :cui cui :lrl (ensure-integer kcuilrl)
-                  :pfstr nil)))
+  (unless cui (return-from find-uconso-cui nil))
+
+  (collect-umlisp-query (mrconso (lat ts lui stt sui ispref aui saui scui sdui sab tty code str
+                                      srl suppress cvf kpfeng kcuisui kcuilui kcuilrl
+                                      kluilrl ksuilrl) srl cui cui
+                                      :filter (if sab (concatenate 'string "SAB='" sab "'") nil))
+    (make-instance 'uconso :cui cui :lat lat :ts ts :lui lui :stt stt :sui sui :ispref ispref
+                   :aui aui :saui saui :scui scui :sdui sdui :sab sab :tty tty :code code
+                   :str str :srl srl :suppress suppress :cvf cvf :kpfeng kpfeng
+                   :kcuisui kcuisui :kcuilui kcuilui :kcuilrl kcuilrl :kluilrl kluilrl
+                   :ksuilrl ksuilrl)))
+
+
+(defun find-uconso-code (code &key first sab (srl *current-srl*) (like nil))
+  "Return list of uconso objects that match code. Optional, filter for SAB. Optionally, use SQL's LIKE syntax"
+  (collect-umlisp-query (mrconso (cui sab) srl code code :like like :distinct t
+                                 :lrl klrl
+                                 :filter (if sab (concatenate 'string "SAB='" sab "'") nil))
+    (let ((uconsos (find-uconso-cui cui :sab sab :srl srl)))
+      (if first
+          (first uconsos)
+          uconsos))))
+
+(defun find-uconso-sui (sui &key sab (srl *current-srl*))
+  "Find uconso for a sui. If set SAB, the without-pfstr is on by default"
+  (ensure-sui-integer sui)
+  (unless (and sui (stringp sab))
+    (return-from find-uconso-sui nil))
+
+  (collect-umlisp-query (mrconso (cui lat ts lui stt sui ispref aui saui scui sdui sab tty code str
+                                      srl suppress cvf kpfeng kcuisui kcuilui kcuilrl
+                                      kluilrl ksuilrl) srl sui sui
+                                      :distinct t
+                                      :filter (if sab (concatenate 'string "SAB='" sab "'") nil))
+    (make-instance 'uconso :cui cui :lat lat :ts ts :lui lui :stt stt :sui sui :ispref ispref
+                   :aui aui :saui saui :scui scui :sdui sdui :sab sab :tty tty :code code
+                   :str str :srl srl :suppress suppress :cvf cvf :kpfeng kpfeng
+                   :kcuisui kcuisui :kcuilui kcuilui :kcuilrl kcuilrl :kluilrl kluilrl
+                   :ksuilrl ksuilrl)))
 
 (defun find-pfstr-cui (cui &key (srl *current-srl*))
   "Find preferred string for a cui"
@@ -253,7 +298,13 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
       str)
    (collect-umlisp-query (mrconso (str) srl cui cui :distinct t
                                   :single t)
-      str)))
+                         str)))
+
+(defun find-lrl-cui (cui &key (srl *current-srl*))
+  "Find LRL for a cui"
+  (ensure-cui-integer cui)
+  (collect-umlisp-query (mrconso (kcuilrl) srl cui cui :distinct t :single t)
+                        (ensure-integer kcuilrl)))
 
 (defun find-ucon-lui (lui &key (srl *current-srl*))
   "Find list of ucon for lui"
@@ -380,15 +431,19 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
                            :distinct t)
     (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
 
-(defun find-urel-cui (cui &key (srl *current-srl*))
+(defun find-urel-cui (cui &key (srl *current-srl*) filter without-pfstr2)
   "Return a list of urel for cui"
   (ensure-cui-integer cui)
   (collect-umlisp-query (mrrel (aui1 rel stype1 cui2 aui2 stype2 rela rui srui sab sl rg dir suppress cvf)
-                              srl cui1 cui :lrl "KSRL")
-    (make-instance 'urel :cui1 cui :aui1 (ensure-integer aui1) :stype1 stype1 :rel rel
-                  :cui2 (ensure-integer cui2) :aui2 (ensure-integer aui2) :stype2 stype2
-                  :rui (ensure-integer rui) :srui srui :rela rela :sab sab :sl sl :rg rg :dir dir
-                  :suppress suppress :cvf cvf :pfstr2 (find-pfstr-cui cui2))))
+                              srl cui1 cui :lrl "KSRL" :filter filter)
+    (let ((rel
+      (make-instance 'urel :cui1 cui :aui1 (ensure-integer aui1) :stype1 stype1 :rel rel
+                     :cui2 (ensure-integer cui2) :aui2 (ensure-integer aui2) :stype2 stype2
+                     :rui (ensure-integer rui) :srui srui :rela rela :sab sab :sl sl :rg rg :dir dir
+                     :suppress suppress :cvf cvf)))
+      (unless without-pfstr2
+        (setf (slot-value rel 'pfstr2) (find-pfstr-cui cui2)))
+      rel)))
 
 (defun find-urel-rui (rui &key (srl *current-srl*))
   "Return the urel for a rui"
@@ -572,17 +627,6 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
                   :sui sui :saui saui :sdui sdui :scui scui :lat lat
                    :str str)))
 
-(defun find-ucxt-cuisui (cui sui &key (srl *current-srl*))
-  (ensure-cui-integer cui)
-  (ensure-sui-integer sui)
-  (collect-umlisp-query (mrcxt (sab rank code cxn cxl cxs cui2 hcd rela xc)
-                           srl kcuisui (make-cuisui cui sui) :lrl ksrl)
-                       (make-instance 'ucxt :sab sab :code code
-               :rank rank
-                  :cxn (ensure-integer cxn) :cxl cxl :cxs cxs :hcd hcd
-                  :rela rela :xc xc
-                  :cui2 (ensure-integer cui2))))
-
 (defun find-uhier-cui (cui &key (srl *current-srl*))
   (ensure-cui-integer cui)
   (collect-umlisp-query (mrhier (aui cxn paui sab rela ptr hcd cvf)
@@ -600,11 +644,11 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
                   :paui (ensure-integer paui)
                   :sab sab :rela rela :ptr ptr :hcd hcd :cvf cvf)))
 
-(defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*))
+(defun find-usat-ui (cui &key lui sui (srl *current-srl*))
   (ensure-cui-integer cui)
   (ensure-lui-integer lui)
   (ensure-sui-integer sui)
-  (let ((ls "select CODE,ATN,SAB,ATV from MRSAT where "))
+  (let ((ls "SELECT CODE,ATN,SAB,ATV FROM MRSAT WHERE "))
     (cond
       (sui (string-append ls "KCUISUI='"
                          (integer-string (make-cuisui cui sui) 14)
@@ -707,11 +751,59 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
   (collect-umlisp-query (mrsty (cui) srl 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"
-  (collect-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 mrconso-query-word-cui (word sab srl like)
+  (format nil "SELECT DISTINCT c.cui FROM MRCONSO c,MRXW_ENG x WHERE x.WD~A'~A' AND x.cui=c.cui~A~A"
+          (if like " LIKE " "=")
+          (clsql-sys::sql-escape-quotes word)
+          (etypecase sab
+            (string
+             (format nil " AND c.sab='~A'" (clsql-sys::sql-escape-quotes sab)))
+            (cons
+             (format nil " AND c.sab IN (~{'~A'~^,~})"
+                     (mapcar 'clsql-sys::sql-escape-quotes sab)))
+            (null
+             ""))
+          (if srl (format nil " AND KCUILRL <= ~A" srl) "")))
+
+(defun mrconso-query-word-sui (word sab srl like)
+  (format nil "SELECT DISTINCT c.sui FROM MRCONSO c,MRXW_ENG x WHERE x.WD~A'~A' AND x.sui=c.sui~A~A"
+          (if like " LIKE " "=")
+          (clsql-sys::sql-escape-quotes word)
+          (etypecase sab
+            (string
+             (format nil " AND c.sab='~A'" (clsql-sys::sql-escape-quotes sab)))
+            (cons
+             (format nil " AND c.sab IN (~{'~A'~^,~})"
+                     (mapcar 'clsql-sys::sql-escape-quotes sab)))
+            (null
+             ""))
+          (if srl (format nil " AND KCUILRL <= ~A" srl) "")))
+
+(defun find-uconso-word (word &key sab (srl *current-srl*) (like nil))
+  "Return list of uconso that match word. Optionally, matching SAB. Optionally, use SQL's LIKE syntax"
+  (cond
+    (sab
+     (let ((sui-query (mrconso-query-word-sui word sab srl like))
+           (uconsos nil))
+       (dolist (sui (remove-duplicates (sort (mapcar 'car (mutex-sql-query sui-query)) #'<)))
+         (setq uconsos (nconc uconsos (find-uconso-sui sui :sab sab))))
+       (remove-duplicates uconsos :key 'cui)))
+    (t
+     (collect-umlisp-query-eval ('mrxw_eng '(cui) srl 'wd word :like like :distinct t
+                                           :lrl 'klrl :order '(cui asc))
+       (find-uconso-cui cui :srl srl)))))
+
+(defun find-ucon-word (word &key sab (srl *current-srl*) (like nil))
+  "Return list of ucon that match word in matching SAB. Optionally, use SQL's LIKE syntax"
+  (cond
+    (sab
+     (let ((query (mrconso-query-word-cui word sab srl like)))
+       (loop for tuple in (mutex-sql-query query)
+             collect (make-instance 'ucon :cui (first tuple)))))
+    (t
+     (collect-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"
@@ -737,11 +829,30 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
                                         :lrl 'klrl :order '(cui asc))
                             sui))
 
-(defun find-ustr-word (word &key (srl *current-srl*))
-  "Return list of ustrs that match word"
-  (collect-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-word (word &key sab (srl *current-srl*) (like nil))
+  "Return list of ustr that match word in matching SAB. Optionally, use SQL's LIKE syntax"
+  (cond
+    (sab
+     (let ((query (format nil "SELECT c.sui,c.cui,c.lui,c.str,c.lrl,c.stt,c.suppress,c.cuisui FROM MRCONSO c,MRXW_ENG x WHERE x.WD ~A '~A' AND x.cui=c.cui AND x.lui=c.lui AND x.sui=c.sui~A~A"
+                          (if like "LIKE" "=")
+                          (clsql-sys::sql-escape-quotes word)
+                          (typecase sab
+                            (string
+                             (format nil " AND c.sab='~A'" (clsql-sys::sql-escape-quotes sab)))
+                            (cons
+                             (format nil " AND c.sab IN (~('~A'~^,~))"
+                                     (mapcar 'clsql-sys::sql-escape-quotes sab)))
+                            (null
+                             ""))
+                          (if srl (format nil " AND KCUILRL <= ~D" srl) ""))))
+       (loop for tuple in (mutex-sql-query query)
+             collect (destructuring-bind (sui cui lui str lrl stt suppress cuisui) tuple
+                       (make-instance 'ustr :sui sui :cui cui :lui lui :str str :lrl lrl
+                                      :stt stt :suppress suppress :cuisui cuisui)))))
+    (t
+     (collect-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"
@@ -782,10 +893,10 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 ;;; Multiword lookup and score functions
 
 (defun find-uobj-multiword (str obj-lookup-fun sort-fun key srl
-                           only-exact-if-match limit)
+                                only-exact-if-match limit &key extra-lookup-args)
   (let ((uobjs '()))
     (dolist (word (delimited-string-to-list str #\space))
-      (setq uobjs (append uobjs (funcall obj-lookup-fun word :srl srl))))
+      (setq uobjs (append uobjs (apply obj-lookup-fun word :srl srl extra-lookup-args))))
     (let ((sorted
           (funcall sort-fun str
                    (delete-duplicates uobjs :test #'= :key key))))
@@ -804,9 +915,19 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 
 (defun find-ucon-multiword (str &key (srl *current-srl*)
                                     (only-exact-if-match t)
-                                     limit)
+                                     limit
+                                     sab)
   (find-uobj-multiword str #'find-ucon-word #'sort-score-pfstr-str
-                      #'cui srl only-exact-if-match limit))
+                       #'cui srl only-exact-if-match limit
+                       :extra-lookup-args (list :sab sab)))
+
+(defun find-uconso-multiword (str &key (srl *current-srl*)
+                                    (only-exact-if-match t)
+                                     limit
+                                     sab)
+  (find-uobj-multiword str #'find-uconso-word #'sort-score-pfstr-str
+                       #'cui srl only-exact-if-match limit
+                       :extra-lookup-args (list :sab sab)))
 
 (defun find-uterm-multiword (str &key (srl *current-srl*)
                                      (only-exact-if-match t)
@@ -816,9 +937,11 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 
 (defun find-ustr-multiword (str &key (srl *current-srl*)
                                     (only-exact-if-match t)
-                                     limit)
+                                     limit
+                                     sab)
   (find-uobj-multiword str #'find-ustr-word #'sort-score-ustr-str
-                      #'sui srl only-exact-if-match limit))
+                       #'sui srl only-exact-if-match limit
+                       :extra-lookup-args (list :sab sab)))
 
 (defun sort-score-pfstr-str (str uobjs)
   "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
@@ -1019,25 +1142,3 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
   (collect-umlisp-query (ustats (name count) nil srl srl :order (name asc))
                           (make-instance 'ustats :name name :hits (ensure-integer count))))
 
-
-
-(defun find-bsab-sab (sab)
- (collect-umlisp-query (bonus_sab (name count) nil sab sab :single t)
-     (make-instance 'bsab :sab sab :name name :hits (ensure-integer count))))
-
-(defun find-bsab-all ()
- (collect-umlisp-query (bonus_sab (sab name count) nil nil nil :order (sab asc))
-     (make-instance 'bsab :sab sab :name name :hits (ensure-integer count))))
-
-(defun find-btty-tty (tty)
- (collect-umlisp-query (bonus_tty (name count) nil tty tty :single t)
-     (make-instance 'btty :tty tty :name name :hits (ensure-integer count))))
-
-(defun find-btty-all ()
- (collect-umlisp-query (bonus_tty (tty name count) nil nil nil :order (tty asc))
-  (make-instance 'btty :tty tty :name name :hits (ensure-integer count))))
-
-(defun find-brel-rel (rel)
-  (collect-umlisp-query (bonus_rel (sab sl rel rela count) nil rel rel)
-    (make-instance 'brel :sab sab :sl sl :rel rel :rela rela
-                   :hits (ensure-integer count))))