r4745: *** empty log message ***
authorKevin M. Rosenberg <kevin@rosenberg.net>
Sat, 3 May 2003 00:27:30 +0000 (00:27 +0000)
committerKevin M. Rosenberg <kevin@rosenberg.net>
Sat, 3 May 2003 00:27:30 +0000 (00:27 +0000)
sql-classes.lisp
tests.lisp

index c33a34362c4fd7f730508f6e931fcd2667bcae65..e954fe83278928f9ce63085d1a07b7047c038b6d 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; Author:        Kevin M. Rosenberg
 ;;;; Date Started:  Apr 2000
 ;;;;
-;;;; $Id: sql-classes.lisp,v 1.19 2003/05/02 22:30:34 kevin Exp $
+;;;; $Id: sql-classes.lisp,v 1.20 2003/05/03 00:27:30 kevin Exp $
 ;;;;
 ;;;; This file, part of UMLisp, is
 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
@@ -33,7 +33,7 @@
                             &body body)
   (let ((query (gensym)))
     `(unless (and ,where-name (not ,where-value)) 
-      (let ((,query (umlisp-query ,table ,fields ,srl ,where-name ,where-value
+      (let ((,query (umlisp-query ,table (quote ,fields) ,srl ,where-name ,where-value
                                  :lrlname ,lrlname :single ,single :distinct ,distinct
                                  :order ,order :like ,like)))
        (if ,single
@@ -75,13 +75,12 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
                                               lrlname srl))))
     (when order
       (setq qs (concatenate 'string qs
-                           (format nil " order by ~{~:@(~A) ~A~^,~}"
+                           (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}"
                                    (flatten
-                                    (loop for o in order
-                                          collect
+                                    (loop for o in order collect
                                           (if (atom o)
                                               (list o  'asc)
-                                              o)))))))
+                                              (list (car o) (cdr o)))))))))
     (when single
       (setq qs (concatenate 'string qs " limit 1")))
     qs))
@@ -90,7 +89,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
   "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 kpfstr2
+                  :pfstr kpfstr
                   :lrl (ensure-integer kcuilrl))))
 
 
@@ -111,14 +110,14 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
   (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 lrl))))
+                  :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
-                  :lrl (ensure-integer kcuilrl)))))
+                  :lrl (ensure-integer kcuilrl))))
 
 (defun find-ucon-cuisui (cui sui &key (srl *current-srl*))
   "Find ucon for cui/sui"
@@ -132,10 +131,27 @@ 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)
-    (make-instance 'ucon :cui (ensure-integer cui) 
-                  :pfstr kpfstr
+    (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
                   :lrl (ensure-integer kcuilrl))))
 
+(defun f2 (&key (srl *current-srl*))
+  "Return list of all ucon's"
+  (with-umlisp-query ('mrcon (cui kpfstr kcuilrl) srl nil nil)
+    (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
+                  :lrl (ensure-integer kcuilrl))))
+
+(defun f1 (&key (srl *current-srl*))
+  "Return list of all ucon's"
+  (with-sql-connection (db)
+    (clsql:map-query 
+     'list
+     #'(lambda (cui pfstr cuilrl)
+        (make-instance 'ucon :cui (ensure-integer cui)
+                       :pfstr pfstr
+                       :lrl (ensure-integer cuilrl)))
+     (query-string 'mrcon '(cui kpfstr kcuilrl) srl nil nil)
+     :database db)))
+
 (defun find-ucon-all (&key (srl *current-srl*))
   "Return list of all ucon's"
   (with-sql-connection (db)
@@ -145,7 +161,8 @@ 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 map-ucon-all (fn &key (srl *current-srl*))
@@ -158,7 +175,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 '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)))
 
 
@@ -187,7 +204,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
   "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))))
+                  :sab sab :sl sl :mg mg :pfstr2 kpfstr2)))
 
 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
   (mapcar 
@@ -197,7 +214,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 (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)))
+                            :lrlname "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
@@ -206,7 +223,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 (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)))
+                            :lrlname "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
@@ -240,7 +257,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
   (with-umlisp-query ('mrcon (lui lat ts kluilrl) srl 'cui (parse-cui cui) :lrlname 'kluilrl
                             :distinct t)
     (make-instance 'uterm :lui (ensure-integer lui) :cui (parse-cui cui)
-                  :lat lat :ts ts :lrl (ensure-integer lrl))))
+                  :lat lat :ts ts :lrl (ensure-integer kluilrl))))
 
 (defun find-uterm-lui (lui &key (srl *current-srl*))
   "Return a list of uterm for lui"
@@ -249,13 +266,12 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
     (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
                             (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)))))
+    (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"
@@ -280,8 +296,8 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
       
 (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)
-    (let ((cuisui (ensure-integer (car tuple))))
+  (with-umlisp-query ('mrso (kcuisui) srl 'sab sab :lrlname 'srl)
+    (let ((cuisui (ensure-integer kcuisui)))
       (apply #'find-ustr-cuisui 
             (append
              (multiple-value-list (decompose-cuisui cuisui)) (list :srl srl))))))
@@ -304,20 +320,20 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
                          :lrl lrl
                          :str pfstr))
        (query-string 'mrcon '(cui lui sui stt lrl kpfstr) srl nil nil :lrlname 'lrl :distinct t
-                    :order '((sui asc)))
+                    :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)
-    (car tuple)))
+  (with-umlisp-query ('mrcon (str) srl 'sui sui :lrlname '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)
       (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 cx) srl 'kcuisui
+  (with-umlisp-query ('mrcxt (sab code cxn cxl rnk cxs cui2 hcd rela xc) srl 'kcuisui
                             (make-cuisui cui sui) :lrlname 'ksrl)
     (make-instance 'ucxt :sab sab :code code
                   :cxn (ensure-integer cxn)
@@ -353,7 +369,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 
 (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 ()
@@ -395,39 +411,39 @@ 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) :lrlname '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)))
+                               :lrlname '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)))
+                               :lrlname '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)))
+                               :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)))
+                                :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))
-    (make-instance 'usrl :sab sab :srl (ensure-integer srl)) usrls))
+  (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
 
@@ -533,7 +549,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 
 (defun find-lmod-eui (eui)
   (with-umlisp-query ('lrmod (bas sca psn_mod fea) nil 'eui eui)
-    (make-instance 'lmod :eui eui :bas bas :sca sca :psnmod psnmod :fea fea)))
+    (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)
@@ -571,7 +587,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 (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)
-    (make-instance 'sdef :rt rt :ui ui :styrl styrl :stnrtn stnrtn
+    (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)
@@ -586,7 +602,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 
 (defun find-sstr-rl (rl)
   (with-umlisp-query ('srstre (sty_rl sty_rl2 ls) nil 'rl rl)
-    (make-instance 'sstr :rl rl :styrl styrl :styrl2 styrl2 :ls ls)))
+    (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)
@@ -594,4 +610,4 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
 
 (defun find-sstr-styrl (styrl)
   (with-umlisp-query ('srstr (rl sty_rl2 ls) nil 'styrl styrl)
-    (make-instance 'sstr :styrl styrl :rl rl :styrl2 styrl2 :ls ls)))
+    (make-instance 'sstr :styrl styrl :rl rl :styrl2 sty_rl2 :ls ls)))
index fdc174868105eb998dd76f2896a51d084b5f8a92..7366f8a485f6de0c56125fc18f4220a665ab386d 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; Author:        Kevin M. Rosenberg
 ;;;; Date Started:  May 2003
 ;;;;
-;;;; $Id: tests.lisp,v 1.1 2003/05/02 18:47:53 kevin Exp $
+;;;; $Id: tests.lisp,v 1.2 2003/05/03 00:27:30 kevin Exp $
 ;;;;
 ;;;; This file, part of UMLisp, is
 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
 (deftest qs.4 (umlisp::query-string 'mrcon '(cui lui) nil 'kpfstr "Abc")
   "select CUI,LUI from MRCON where KPFSTR='Abc'")
 
-(deftest qs.5 (umlisp::query-string 'mrcon '(cui lui) 2 'cui 5 "limit 1")
+(deftest qs.5 (umlisp::query-string 'mrcon '(cui lui) 2 'cui 5 :single t)
   "select CUI,LUI from MRCON where CUI=5 and KCUILRL <= 2 limit 1")
 
-(deftest qs.6 (umlisp::query-string 'mrdef '(sab def) 2 'cui 39 'srl "limit 1")
+(deftest qs.6 (umlisp::query-string 'mrdef '(sab def) 2 'cui 39 :lrlname 'ksrl :single t)
   "select SAB,DEF from MRDEF where CUI=39 and KSRL <= 2 limit 1")
 
+(deftest qs.7 (umlisp::query-string 'mrdef '(sab def) 2 'cui 39 :lrlname 'ksrl :order '((cui . asc)))
+  "select SAB,DEF from MRDEF where CUI=39 and KSRL <= 2 order by CUI asc")
+
+(deftest qs.8 (umlisp::query-string 'mrdef '(sab def) 2 'cui 39 :lrlname 'ksrl
+                                   :order '((cui . asc) (def . desc)))
+  "select SAB,DEF from MRDEF where CUI=39 and KSRL <= 2 order by CUI asc,DEF desc")
+