X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=sql-classes.lisp;h=ec68c5e159066c798df3ece2e3223d2e13da7e79;hb=10aef4d2174b37725e5e517a55e5f7ee534fb142;hp=54519a02f44aca7943b91e76880378a164ad7ec6;hpb=12131484e6e61019e61a281e26d49aaf7dd83690;p=umlisp.git diff --git a/sql-classes.lisp b/sql-classes.lisp index 54519a0..ec68c5e 100644 --- a/sql-classes.lisp +++ b/sql-classes.lisp @@ -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~)" @@ -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 @@ -73,7 +73,7 @@ (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 @@ -228,7 +228,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (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) @@ -252,11 +252,11 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (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 + srl suppress cvf kpfeng kcuisui kcuilui kcuilrl kluilrl ksuilrl) srl cui cui) - (make-instance 'uconso :cui cui :lat lat :ts ts :lui lui :stt stt :sui sui :ispref ispref + (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 + :str str :srl srl :suppress suppress :cvf cvf :kpfeng kpfeng :kcuisui kcuisui :kcuilui kcuilui :kcuilrl kcuilrl :kluilrl kluilrl :ksuilrl ksuilrl))) @@ -407,7 +407,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (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" :filter filter) - (let ((rel + (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 @@ -598,17 +598,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) @@ -626,7 +615,7 @@ 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) @@ -739,22 +728,37 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" :lrl 'klrl :order '(cui asc)) (find-ucon-cui cui :srl srl))) +(defun find-uconso-word (word &key (srl *current-srl*) (like nil)) + "Return list of uconso objects 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-uconso-cui cui :srl srl))) + +(defun mrconso-query-word-cui (word sab srl like) + (format nil "SELECT c.cui FROM MRCONSO c,MRXW_ENG x WHERE x.WD ~A '~A' AND x.cui=c.cui AND ~A ~A" + (if like "LIKE" "=") + (clsql-sys::sql-escape-quotes word) + (etypecase sab + (string + (format nil " c.sab='~A'" (clsql-sys::sql-escape-quotes sab))) + (cons + (format nil " c.sab IN (~{'~A'~^,~})" + (mapcar 'clsql-sys::sql-escape-quotes sab))) + (null + (error "SAB missing"))) + (if srl (format nil "AND KCUILRL <= ~A" srl) ""))) + +(defun find-uconso-word-sab (word &key sab (srl *current-srl*) (like nil)) + "Return list of uconso that match word in matching SAB. Optionally, use SQL's LIKE syntax" + (let ((query (mrconso-query-word-cui word sab srl like))) + (loop for cui in (remove-duplicates (sort (mapcar 'car (mutex-sql-query query)) #'<)) + collect (find-uconso-cui cui)))) + (defun find-ucon-word-sab (word &key sab (srl *current-srl*) (like nil)) - "Return list of ustr that match word in matching SAB. Optionally, use SQL's LIKE syntax" - (let ((query (format nil "SELECT c.cui FROM MRCONSO c,MRXW_ENG x WHERE x.WD ~A '~A' AND x.cui=c.cui AND ~A ~A" - (if like "LIKE" "=") - (clsql-sys::sql-escape-quotes word) - (typecase sab - (stringp - (format nil " c.sab='" (clsql-sys::sql-escape-quotes sab))) - (cons - (format nil " c.sab IN (~('~A'~^,~))" - (mapcar 'clsql-sys::sql-escape-quotes sab))) - (t - (error "SAB missing"))) - (if srl (format nil "AND KCUILRL <= ~D" srl) "")))) + "Return list of ucon that match word in matching SAB. Optionally, use SQL's LIKE syntax" + (let ((query (mrconso-query-word-cui word sab srl like))) (loop for tuple in (mutex-sql-query query) - collect (make-instance 'ucon (first tuple))))) + collect (make-instance 'ucon :cui (first tuple))))) (defun find-ustr-word-sab (word &key sab (srl *current-srl*) (like nil)) "Return list of ustr that match word in matching SAB. Optionally, use SQL's LIKE syntax" @@ -762,8 +766,8 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (if like "LIKE" "=") (clsql-sys::sql-escape-quotes word) (typecase sab - (stringp - (format nil " c.sab='" (clsql-sys::sql-escape-quotes sab))) + (string + (format nil " c.sab='~A'" (clsql-sys::sql-escape-quotes sab))) (cons (format nil " c.sab IN (~('~A'~^,~))" (mapcar 'clsql-sys::sql-escape-quotes sab))) @@ -774,7 +778,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" 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))))) - + (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" @@ -876,6 +880,17 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (find-uobj-multiword str #'find-ucon-word #'sort-score-pfstr-str #'cui srl only-exact-if-match limit))) +(defun find-uconso-multiword (str &key (srl *current-srl*) + (only-exact-if-match t) + limit + sab) + (if sab + (find-uobj-multiword str #'find-uconso-word-sab #'sort-score-pfstr-str + #'cui srl only-exact-if-match limit + :extra-lookup-args (list :sab sab)) + (find-uobj-multiword str #'find-uconso-word #'sort-score-pfstr-str + #'cui srl only-exact-if-match limit))) + (defun find-uterm-multiword (str &key (srl *current-srl*) (only-exact-if-match t) limit) @@ -1092,25 +1107,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))))