-;;; Accessors (read on demand)
-
-;; defines a slot-unbound method for class and slot-name, fills
-;; the slot by calling reader function with the slot values of
-;; the instance's reader-keys
-(defmacro def-lazy-reader (class slot-name reader &rest reader-keys)
- (let* ((the-slot-name (gensym))
- (the-class (gensym))
- (the-instance (gensym))
- (keys '()))
- (dolist (key reader-keys)
- (push (list 'slot-value the-instance (list 'quote key)) keys))
- (setq keys (nreverse keys))
- `(defmethod slot-unbound (,the-class (,the-instance ,class)
- (,the-slot-name (eql ',slot-name)))
- (declare (ignore ,the-class))
- (setf (slot-value ,the-instance ,the-slot-name)
- (,reader ,@keys)))))
-
-(def-lazy-reader ucon s#term find-uterm-cui cui)
-(def-lazy-reader ucon s#def find-udef-cui cui)
-(def-lazy-reader ucon s#sty find-usty-cui cui)
-(def-lazy-reader ucon s#rel find-urel-cui cui)
-(def-lazy-reader ucon s#coc find-ucoc-cui cui)
-(def-lazy-reader ucon s#lo find-ulo-cui cui)
-(def-lazy-reader ucon s#atx find-uatx-cui cui)
-(def-lazy-reader ucon s#sat find-usat-ui cui)
-
-;; For uterms
-(def-lazy-reader uterm s#str find-ustr-cuilui cui lui)
-(def-lazy-reader uterm s#sat find-usat-ui cui lui)
-
-;; For ustrs
-(def-lazy-reader ustr s#sat find-usat-ui cui lui sui)
-(def-lazy-reader ustr s#cxt find-ucxt-cuisui cui sui)
-(def-lazy-reader ustr s#so find-uso-cuisui cui sui)
-
-;;; Object lookups
-
-;;; Lookup functions for uterms,ustr in ucons
-
-(defun find-uterm-in-ucon (ucon lui)
- (find lui (s#term ucon) :key #'lui :test 'equal))
-
-(defun find-ustr-in-uterm (uterm sui)
- (find sui (s#str uterm) :key #'sui :test 'equal))
-
-(defun find-ustr-in-ucon (ucon sui)
- (let ((found-ustr nil))
- (dolist (uterm (s#term ucon))
- (unless found-ustr
- (dolist (ustr (s#str uterm))
- (unless found-ustr
- (when (string-equal sui (sui ustr))
- (setq found-ustr ustr))))))
- found-ustr))
-
+
+(defmacro query-string-macro (table fields &optional srl where-name where-value
+ &key (lrlname "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)))
+ `(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 %%order (list %%order))
+ ,@(when ,single (" limit 1")))))
+
+(defun query-string (table fields &optional srl where-name where-value
+ &key (lrlname "KCUILRL") single distinct order like)
+ (concatenate
+ 'string
+ (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)"
+ (if distinct "distinct " "") fields table)
+ (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 " and ~:@(~A~) <= ~D" lrlname srl) "")
+ (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) "")
+ (if single " limit 1" "")))