- (dolist (col cols)
- (when (and (string-equal filename (umls-col-fil col))
- (string-equal colname (umls-col-col col)))
- (return-from find-col-in-columns col)))
- nil)
-
-(defun find-or-make-col-in-columns (colname filename cols)
- (let ((col (find-col-in-columns colname filename cols)))
- (if col
- col
- ;; try to find column name without a terminal digit
- (let* ((last-char (char colname (1- (length colname))))
- (digit (- (char-code last-char) (char-code #\0))))
- (if (and (>= digit 0) (<= digit 9))
- (let ((base-colname (subseq colname 0 (1- (length colname)))))
- (setq col (find-col-in-columns base-colname filename cols))
- (if col
- (let ((new-col (make-umls-col
- :col (copy-seq colname)
- :des (copy-seq (umls-col-des col))
- :ref (copy-seq (umls-col-ref col))
- :min (umls-col-min col)
- :max (umls-col-max col)
- :fil (copy-seq (umls-col-fil col))
- :sqltype (copy-seq (umls-col-sqltype col))
- :dty (copy-seq (umls-col-dty col))
- :parsefunc (umls-col-parsefunc col)
- :quotechar (copy-seq (umls-col-quotechar col))
- :datatype (umls-col-datatype col)
- :custom-value-func (umls-col-custom-value-func col))))
- (push new-col *umls-cols*)
- new-col)
- (error "Couldn't find a base column for col ~A in file ~A"
- colname filename)))
- (let ((new-col (make-umls-col
- :col (copy-seq colname)
- :des "Unknown"
- :ref ""
- :min nil
- :max nil
- :fil filename
- :sqltype "VARCHAR"
- :dty nil
- :parsefunc #'add-sql-quotes
- :quotechar "'"
- :datatype nil
- :custom-value-func nil)))
- (push new-col *umls-cols*)
- new-col))))))
-
-(defun find-umls-col (colname filename)
+ (dolist (ucol ucols nil)
+ (when (and (string-equal filename (fil ucol))
+ (string-equal colname (col ucol)))
+ (return-from find-ucol-of-colname ucol))))
+
+(defun ensure-col-in-columns (colname filename ucols)
+ (aif (find-ucol-of-colname colname filename ucols)
+ it
+ (add-ucols (make-ucol-for-column colname filename ucols))))
+
+(defun make-ucol-for-column (colname filename ucols)
+ ;; try to find column name without a terminal digit
+ (let* ((len (length colname))
+ (last-digit? (digit-char-p (schar colname (1- len))))
+ (base-colname (if last-digit?
+ (subseq colname 0 (1- len))
+ colname))
+ (ucol (when last-digit?
+ (find-ucol-of-colname base-colname filename ucols))))
+ (when (and last-digit? (null ucol))
+ (error "Couldn't find a base column for col ~A in file ~A"
+ colname filename))
+ (copy-or-new-ucol colname filename ucol)))
+
+(defun copy-or-new-ucol (colname filename ucol)
+ (if ucol
+ (make-instance
+ 'ucol
+ :col (copy-seq colname) :des (copy-seq (des ucol)) :ref (copy-seq (ref ucol))
+ :min (cmin ucol) :max (cmax ucol) :fil (copy-seq (fil ucol))
+ :sqltype (copy-seq (sqltype ucol)) :dty (copy-seq (dty ucol))
+ :parse-fun (parse-fun ucol) :quote-str (copy-seq (quote-str ucol))
+ :datatype (datatype ucol) :custom-value-fun (custom-value-fun ucol))
+ (make-empty-ucol colname filename)))
+
+(defun ensure-compiled-fun (fun)
+ "Ensure that a function is compiled"
+ (etypecase fun
+ (null
+ nil)
+ (function
+ (if (compiled-function-p fun)
+ fun
+ (compile nil fun)))
+ (list
+ (compile nil fun))))
+
+(defun make-ucol (col des ref min av max fil dty
+ &key (sqltype "VARCHAR") (parse-fun #'add-sql-quotes)
+ (quote-str "'") (custom-value-fun))
+ (let ((ucol (make-instance
+ 'ucol
+ :col col :des des :ref ref :min min :av av
+ :max (if (eql max 0) 1 max) ;; ensure at least one char wide
+ :fil fil
+ :dty dty
+ :sqltype sqltype
+ :quote-str quote-str
+ :parse-fun (ensure-compiled-fun parse-fun)
+ :custom-value-fun (ensure-compiled-fun custom-value-fun))))
+ (ensure-ucol-datatype ucol (datatype-for-colname col))
+ ucol))
+
+(defun make-empty-ucol (colname filename)
+ ;;(format "call in make-empty-ucol: ~A/~A" colname filename)
+ (make-ucol (copy-seq colname) "Unknown" "" nil nil nil filename nil))
+
+(defun find-ucol (colname filename)