r9093: changes for postgresql
[umlisp.git] / parse-common.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          parse-common.lisp
6 ;;;; Purpose:       Common, stable parsing routines for UMLisp
7 ;;;; Author:        Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of UMLisp, is
13 ;;;;    Copyright (c) 2000-2003 by Kevin M. Rosenberg, M.D.
14 ;;;;
15 ;;;; UMLisp users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the GNU General Public License.
17 ;;;; *************************************************************************
18
19 (in-package #:umlisp)
20
21 (eval-when (:compile-toplevel)
22   (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3))))
23
24 (defun ensure-ucols+ufiles (&optional (alwaysclear nil))
25 "Initialize all UMLS file and column structures if not already initialized"
26   (when (or alwaysclear (null *umls-files*))
27     (gen-ucols)
28     (gen-ufiles)
29     (ensure-field-lengths)))
30
31 (defun add-ucols (ucols)
32   "Adds a ucol or list of ucols to *umls-cols*. Returns input value."
33   (setq *umls-cols* (append (mklist ucols) *umls-cols*))
34   ucols)
35
36 (defun add-ufiles (ufiles)
37   "Adds a ufile or list of ufiles to *umls-filess*. Returns input value."
38   (setq *umls-files* (append (mklist ufiles) *umls-files*))
39   ufiles)
40
41 (defun umls-pathname (filename &optional (extension ""))
42 "Return pathname for a umls filename with an optional extension"
43   (etypecase filename
44     (string
45      (merge-pathnames 
46       (make-pathname :name (concatenate 'string filename extension)) 
47       (case (char filename 0)
48         ((#\M #\m)
49          *meta-path*)
50         ((#\L #\l)
51          *lex-path*)
52         ((#\S #\s)
53          *net-path*)
54         (t
55          *umls-path*))))
56     (pathname
57       filename)))
58
59 (defun read-umls-line (strm &optional (eof 'eof))
60   "Read a line from a UMLS stream, split into fields"
61   (let ((line (read-line strm nil eof)))
62     (if (eq line eof)
63         eof
64         (delimited-string-to-list line #\| t))))
65
66 ;;; Find field lengths for LEX and NET files
67
68 (defun ensure-field-lengths ()
69   "Initial colstruct field lengths for files that don't have a measurement.
70 Currently, these are the LEX and NET files."
71   (dolist (length-list (ufiles-field-lengths (ufiles-to-measure)))
72     (destructuring-bind (filename fields-max fields-av) length-list
73       (let ((file (find-ufile filename)))
74         (unless file
75           (error "Can't find ~A filename in ufiles" filename))
76         (unless (= (length fields-max) (length (fields file)))
77           (error
78            "Number of file fields ~A not equal to field count in ufile ~S" 
79            fields-max file))
80         (dotimes (i (length (fields file)))
81           (declare (fixnum i))
82           (let* ((field (nth i (fields file)))
83                  (col (find-ucol field filename)))
84             (unless col
85                 (error "can't find column ~A" field))
86             (setf (cmax col) (aref fields-max i))
87             (setf (av col) (aref fields-av i))
88             (ensure-ucol-datatype col (datatype-for-colname (col col)))))))))
89   
90 (defun ufiles-to-measure ()
91   "Returns a list of ufiles to measure"
92   (loop for ufile in *umls-files*
93         unless (or (char= #\M (char (fil ufile) 0))
94                    (char= #\m (char (fil ufile) 0)))
95         collect ufile))
96     
97   
98 (defun ufiles-field-lengths (ufiles)
99   "Returns a list of lists of containing (FILE MAX AV)"
100   (loop for ufile in ufiles collect (file-field-lengths (fil ufile))))
101
102 (defun file-field-lengths (filename)
103   "Returns a list of FILENAME MAX AV"
104   (let (fields-max fields-av num-fields (count-lines 0))
105     (with-umls-file (line filename)
106       (unless num-fields
107         (setq num-fields (length line))
108         (setq fields-max (make-array num-fields :element-type 'fixnum 
109                                      :initial-element 0))
110         (setq fields-av (make-array num-fields :element-type 'number
111                                     :initial-element 0)))
112       (dotimes (i num-fields)
113         (declare (fixnum i))
114         (let ((len (length (nth i line))))
115           (incf (aref fields-av i) len)
116           (when (> len (aref fields-max i))
117             (setf (aref fields-max i) len))))
118       (incf count-lines))
119     (dotimes (i num-fields)
120       (setf (aref fields-av i) (float (/ (aref fields-av i) count-lines))))
121     (list filename fields-max fields-av)))
122
123 ;;; UMLS column/file functions
124
125 (defun find-ucol-of-colname (colname filename ucols)
126 "Returns list of umls-col structure for a column name and a filename"
127   (dolist (ucol ucols nil)
128     (when (and (string-equal filename (fil ucol))
129                (string-equal colname (col ucol)))
130       (return-from find-ucol-of-colname ucol))))
131
132 (defun ensure-col-in-columns (colname filename ucols)
133   (aif (find-ucol-of-colname colname filename ucols)
134        it
135        (add-ucols (make-ucol-for-column colname filename ucols))))
136
137 (defun make-ucol-for-column (colname filename ucols)
138   ;; try to find column name without a terminal digit
139   (let* ((len (length colname))
140          (last-digit? (digit-char-p (char colname (1- len))))
141          (base-colname (if last-digit?
142                            (subseq colname 0 (1- len))
143                            colname))
144          (ucol (when last-digit?
145                  (find-ucol-of-colname base-colname filename ucols))))
146     (when (and last-digit? (null ucol))
147       (error "Couldn't find a base column for col ~A in file ~A"
148              colname filename))
149     (copy-or-new-ucol colname filename ucol)))
150
151 (defun copy-or-new-ucol (colname filename ucol)
152   (if ucol
153       (make-instance
154        'ucol
155        :col (copy-seq colname) :des (copy-seq (des ucol)) :ref (copy-seq (ref ucol))
156        :min (cmin ucol) :max (cmax ucol) :fil (copy-seq (fil ucol))
157        :sqltype (copy-seq (sqltype ucol)) :dty (copy-seq (dty ucol))
158        :parse-fun (parse-fun ucol) :quote-str (copy-seq (quote-str ucol))
159        :datatype (datatype ucol) :custom-value-fun (custom-value-fun ucol))
160       (make-empty-ucol colname filename)))
161
162 (defun ensure-compiled-fun (fun)
163   "Ensure that a function is compiled"
164   (etypecase fun
165     (null
166      nil)
167     (function
168      (if (compiled-function-p fun)
169          fun
170          (compile nil fun)))
171     (list
172      (compile nil fun))))
173
174 (defun make-ucol (col des ref min av max fil dty
175                   &key (sqltype "VARCHAR") (parse-fun #'add-sql-quotes)
176                   (quote-str "'") (custom-value-fun))
177   (let ((ucol (make-instance
178                'ucol
179                :col col :des des :ref ref :min min :av av 
180                :max (if (eql max 0) 1 max) ;; ensure at least one char wide
181                :fil fil
182                :dty dty :sqltype sqltype :quote-str quote-str
183                :parse-fun (ensure-compiled-fun parse-fun)
184                :custom-value-fun (ensure-compiled-fun custom-value-fun))))
185     (ensure-ucol-datatype ucol (datatype-for-colname col))
186     ucol))
187
188 (defun make-empty-ucol (colname filename)
189   ;;(format "call in make-empty-ucol: ~A/~A" colname filename)
190   (make-ucol (copy-seq colname) "Unknown" "" nil nil nil filename nil))
191
192 (defun find-ucol (colname filename)
193   "Returns list of umls-col structure for a column name and a filename"
194   (ensure-col-in-columns colname filename *umls-cols*))
195
196 (defun find-ufile (filename)
197   "Returns umls-file structure for a filename"  
198   (find-if #'(lambda (f) (string-equal filename (fil f))) *umls-files*))
199
200 (defun find-ucols-for-ufile (ufile)
201   "Returns list of umls-cols for a file structure"
202   (loop for colname in (fields ufile)
203         collect (find-ucol colname (fil ufile))))
204
205 (defun umls-field-string-to-list (fmt)
206   "Converts a comma delimited list of fields into a list of field names. Will
207 append a unique number (starting at 2) onto a column name that is repeated in the list"
208   (let ((col-counts (make-hash-table :test 'equal)))
209     (loop for colname in (delimited-string-to-list (escape-column-name fmt) #\,)
210           collect
211           (multiple-value-bind (value found) (gethash colname col-counts)
212             (cond
213               (found
214                 (incf (gethash colname col-counts))
215                 (concatenate 'string colname (write-to-string (1+ value))))
216               (t
217                (setf (gethash colname col-counts) 1)
218                colname))))))
219
220 (defun make-ufile (fil des table cls rws bts fields)
221   (let ((ufile (make-instance 'ufile :fil fil :des des :table table :cls cls
222                               :rws rws :bts bts :fields fields)))
223     (setf (ucols ufile) (find-ucols-for-ufile ufile))
224     ufile))
225
226 (defun datatype-for-colname (colname)
227 "Return datatype for column name"  
228   (second (find colname +col-datatypes+ :key #'car :test #'string-equal)))
229
230 (defun ensure-ucol-datatype (col datatype)
231 "Add data type information to column"
232   (setf (datatype col) datatype)
233   (case datatype
234     (sql-u (setf (sqltype col) "INTEGER"
235                  (parse-fun col) #'parse-ui
236                  (quote-str col) ""))
237     (sql-s (setf (sqltype col) "SMALLINT" 
238                  (parse-fun col) #'parse-integer
239                  (quote-str col) ""))
240     (sql-l (setf (sqltype col) "BIGINT" 
241                  (parse-fun col) #'parse-integer
242                  (quote-str col) ""))
243     (sql-i (setf (sqltype col) "INTEGER" 
244                  (parse-fun col) #'parse-integer
245                  (quote-str col) ""))
246     (sql-f (setf (sqltype col) "NUMERIC" 
247                  (parse-fun col) #'read-from-string
248                  (quote-str col) ""))
249     (t                       ; Default column type, optimized text storage
250      (setf (parse-fun col) #'add-sql-quotes 
251            (quote-str col) "'")
252      (when (and (cmax col) (av col))
253        (if (> (cmax col) 255)
254            (setf (sqltype col) "TEXT")
255          (if (< (- (cmax col) (av col)) 4) 
256              (setf (sqltype col) "CHAR") ; if average bytes wasted < 4
257            (setf (sqltype col) "VARCHAR")))))))
258
259 (defun escape-column-name (name)
260   (substitute #\_ #\/ name))