X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=sql-classes.lisp;h=207d7001c822f1a12e5d663bbb2869f564a8c2f1;hb=3f5b0b9907c347bf2c8476455a83cf59aefcf02c;hp=304bf08a9915ddb994eb3846d34522abc97e08e8;hpb=c81a7273514766d6eb7268c437a0566dab6e9f34;p=umlisp.git diff --git a/sql-classes.lisp b/sql-classes.lisp index 304bf08..207d700 100644 --- a/sql-classes.lisp +++ b/sql-classes.lisp @@ -7,7 +7,7 @@ ;;;; Author: Kevin M. Rosenberg ;;;; Date Started: Apr 2000 ;;;; -;;;; $Id: sql-classes.lisp,v 1.49 2003/05/03 22:05:00 kevin Exp $ +;;;; $Id: sql-classes.lisp,v 1.63 2003/05/04 01:25:24 kevin Exp $ ;;;; ;;;; This file, part of UMLisp, is ;;;; Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D. @@ -31,53 +31,92 @@ &key (lrl "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 - :lrl ,lrl :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 - :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like) - collect (destructuring-bind ,fields tuple - ,@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" - `(when (or (not ,where-name) ,where-value) - (mutex-sql-query - (query-string-macro ,table ,fields ,srl ,where-name ,where-value - :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))) + `(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-macro (table fields &optional srl where-name where-value +(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~)<=" lrl)) - (%%where (format nil " where ~:@(~A~)~A" where-name - (if like " like " "=")))) + (%%where (when where-name + (format nil " where ~:@(~A~)~A" where-name + (if like " like " "="))))) `(concatenate 'string ,%%fields ,@(when %%where (list %%where)) ,@(when %%where - `((if (numberp where-value) - (write-to-string where-value) - (format nil ,(if like "'%~A%'" "'~A'") where-value)))) + `((if (numberp ,where-value) + (write-to-string ,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"))))) -(defun query-string (table fields &optional srl where-name where-value - &key (lrl "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~)" @@ -165,7 +204,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 + (query-string mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc) :distinct t) :database db))) @@ -179,7 +218,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))) @@ -323,7 +362,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" :stt stt :lrl lrl :str pfstr)) - (query-string 'mrcon '(cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl :distinct t + (query-string mrcon (cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl :distinct t :order (sui asc)) :database db))) @@ -355,7 +394,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (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) usats)))) + (make-instance 'usat :code code :atn atn :sab sab :atv atv))))) (defun find-usty-tui (tui) "Find usty for tui" @@ -386,7 +425,7 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (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 @@ -398,11 +437,11 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (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")))) @@ -417,14 +456,14 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (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 - :lrl 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 - :lrl 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*))