1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Create SQL database for UMLisp
7 ;;;; Author: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; $Id: create-sql.lisp,v 1.6 2003/06/10 22:30:16 kevin Exp $
12 ;;;; This file, part of UMLisp, is
13 ;;;; Copyright (c) 2000-2003 by Kevin M. Rosenberg, M.D.
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 ;;;; *************************************************************************
21 (defun create-table-cmd (file)
22 "Return sql command to create a table"
25 (let ((sqltype (sqltype c)))
29 (if (or (string-equal sqltype "VARCHAR")
30 (string-equal sqltype "CHAR"))
31 (format nil "~a (~a)" sqltype (cmax c))
33 (format nil "CREATE TABLE ~a (~{~a~^,~})" (table file)
34 (mapcar col-func (ucols file)))))
36 (defun create-custom-table-cmd (tablename sql-cmd)
37 "Return SQL command to create a custom table"
38 (format nil "CREATE TABLE ~a AS ~a;" tablename sql-cmd))
40 (defun insert-col-value (col value)
41 (if (null (parse-fun col))
43 (format nil "~A" (funcall (parse-fun col) value))))
45 (defun insert-values-cmd (file values)
46 "Return sql insert command for a row of values"
49 (concatenate 'string (quote-str col)
50 (insert-col-value col value)
53 nil "INSERT INTO ~a (~{~a~^,~}) VALUES (~A)"
56 (concat-separated-strings
58 (mapcar insert-func (remove-custom-cols (ucols file)) values)
59 (custom-col-values (custom-ucols-for-file file) values t)))))
62 (defun custom-col-value (col values doquote)
63 (let ((custom-value (funcall (custom-value-fun col) values)))
66 (concatenate 'string (quote-str col)
67 (escape-backslashes custom-value)
69 (escape-backslashes custom-value))
72 (defun custom-col-values (ucols values doquote)
73 "Returns a list of string column values for SQL inserts for custom columns"
74 (loop for col in ucols collect (custom-col-value col values doquote)))
76 (defun remove-custom-cols (cols)
77 "Remove custom cols from a list col umls-cols"
78 (remove-if #'custom-value-fun cols))
80 (defun find-custom-cols-for-filename (filename)
81 (remove-if-not (lambda (x) (string-equal filename (car x))) +custom-cols+))
83 (defun find-custom-col (filename col)
84 (find-if (lambda (x) (and (string-equal filename (car x))
85 (string-equal col (cadr x)))) +custom-cols+))
87 (defun custom-colnames-for-filename (filename)
88 (mapcar #'cadr (find-custom-cols-for-filename filename)))
90 (defun custom-ucols-for-file (file)
91 (remove-if-not #'custom-value-fun (ucols file)))
93 (defun noneng-lang-index-files ()
95 (lambda (f) (and (> (length (fil f)) 4)
96 (string-equal (fil f) "MRXW." :end1 5)
97 (not (string-equal (fil f) "MRXW.ENG"))
98 (not (string-equal (fil f) "MRXW.NONENG"))))
101 ;;; SQL Command Functions
103 (defun create-index-cmd (colname tablename length)
104 "Return sql create index command"
105 (format nil "CREATE INDEX ~a ON ~a (~a ~a)"
106 (concatenate 'string tablename "_" colname "_X")
108 (if (integerp length) (format nil "(~d)" length) "")))
110 (defun create-all-tables-cmdfile ()
111 "Return sql commands to create all tables. Not need for automated SQL import"
112 (mapcar (lambda (f) (format nil "~a~%~%" (create-table-cmd f))) *umls-files*))
114 ;; SQL Execution functions
116 (defun sql-drop-tables (conn)
117 "SQL Databases: drop all tables"
118 (dolist (file *umls-files*)
120 (sql-execute (format nil "DROP TABLE ~a" (table file)) conn))))
122 (defun sql-create-tables (conn)
123 "SQL Databases: create all tables"
124 (dolist (file *umls-files*)
125 (sql-execute (create-table-cmd file) conn)))
127 (defun sql-create-custom-tables (conn)
128 "SQL Databases: create all custom tables"
129 (dolist (ct +custom-tables+)
130 (sql-execute (create-custom-table-cmd (car ct) (cadr ct)) conn)))
132 (defun sql-insert-values (conn file)
133 "SQL Databases: inserts all values for a file"
134 (with-umls-file (line (fil file))
135 (sql-execute (insert-values-cmd file line) conn)))
137 (defun sql-insert-all-values (conn)
138 "SQL Databases: inserts all values for all files"
139 (dolist (file *umls-files*)
140 (sql-insert-values conn file)))
142 (defun sql-create-indexes (conn &optional (indexes +index-cols+))
143 "SQL Databases: create all indexes"
144 (dolist (idx indexes)
145 (sql-execute (create-index-cmd (car idx) (cadr idx) (caddr idx)) conn)))
147 (defun make-usrl (conn)
148 (sql-execute "drop table if exists USRL" conn)
149 (sql-execute "create table USRL (sab varchar(80), srl integer)" conn)
150 (dolist (tuple (mutex-sql-query
151 "select distinct SAB,SRL from MRSO order by SAB asc"))
152 (sql-execute (format nil "insert into USRL (sab,srl) values ('~a',~d)"
153 (car tuple) (ensure-integer (cadr tuple)))
156 (defun sql-create-special-tables (conn)
159 (defun create-umls-db-by-insert ()
160 "SQL Databases: initializes entire database via SQL insert commands"
161 (ensure-ucols+ufiles)
163 (with-sql-connection (conn)
164 (sql-drop-tables conn)
165 (sql-create-tables conn)
166 (sql-insert-all-values conn)
167 (sql-create-indexes conn)
168 (sql-create-custom-tables conn)
169 (sql-create-indexes conn +custom-index-cols+)
170 (sql-create-special-tables conn)))
172 (defun create-umls-db (&optional (extension ".trans")
173 (copy-cmd #'mysql-copy-cmd))
174 "SQL Databases: initializes entire database via SQL copy commands.
175 This is much faster that using create-umls-db-insert."
176 (ensure-ucols+ufiles)
178 (translate-all-files extension)
179 (with-sql-connection (conn)
180 (sql-drop-tables conn)
181 (sql-create-tables conn)
182 (dolist (file *umls-files*)
183 (sql-execute (funcall copy-cmd file extension) conn))
184 (sql-create-indexes conn)
185 (sql-create-custom-tables conn)
186 (sql-create-indexes conn +custom-index-cols+)
187 (sql-create-special-tables conn)))
189 (defun translate-all-files (&optional (extension ".trans"))
190 "Copy translated files and return postgresql copy commands to import"
191 (make-noneng-index-file extension)
192 (dolist (f (remove "MRXW.NONENG" *umls-files* :test #'string= :key #'fil))
193 (translate-umls-file f extension)))
195 (defun translate-umls-file (file extension)
196 "Translate a umls file into a format suitable for sql copy cmd"
197 (translate-files file extension (list file)))
199 (defun make-noneng-index-file (extension)
200 "Make non-english index file"
201 (translate-files (find-ufile "MRXW.NONENG")
202 extension (noneng-lang-index-files)))
204 (defun translate-files (out-ufile extension input-ufiles)
205 "Translate a umls file into a format suitable for sql copy cmd"
206 (let ((output-path (umls-pathname (fil out-ufile) extension)))
207 (if (probe-file output-path)
208 (format t "File ~A already exists: skipping~%" output-path)
209 (with-open-file (ostream output-path :direction :output)
210 (dolist (input-ufile input-ufiles)
211 (with-umls-file (line (fil input-ufile))
212 (translate-line out-ufile line ostream)
213 (princ #\newline ostream)))))))
215 (defun translate-line (file line strm)
216 "Translate a single line for sql output"
217 (flet ((col-value (col value)
218 (if (eq (datatype col) 'sql-u)
219 (let ((ui (parse-ui value "")))
222 (write-to-string ui)))
223 (escape-backslashes value))))
224 (print-separated-strings
226 (mapcar #'col-value (remove-custom-cols (ucols file)) line)
227 (custom-col-values (custom-ucols-for-file file) line nil))))
229 (defun pg-copy-cmd (file extension)
230 "Return postgresql copy statement for a file"
232 nil "COPY ~a FROM '~a' using delimiters '|' with null as ''"
233 (table file) (umls-pathname (fil file) extension)))
235 (defun mysql-copy-cmd (file extension)
236 "Return mysql copy statement for a file"
239 "LOAD DATA LOCAL INFILE \"~a\" INTO TABLE ~a FIELDS TERMINATED BY \"|\""
240 (umls-pathname (fil file) extension) (table file)))
243 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
245 ;;; Routines for analyzing cost of fixed size storage
247 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
249 (defun umls-fixed-size-waste ()
250 "Display storage waste if using all fixed size storage"
256 (dolist (file *umls-files*)
257 (dolist (col (ucols file))
258 (let* ((avwaste (- (cmax col) (av col)))
259 (cwaste (* avwaste (rws file))))
263 (incf totalunavoidable cwaste)
264 (push (list (fil file) (col col)
268 (incf totalavoidable cwaste)
269 (push (list (fil file) (col col)
272 (incf totalwaste cwaste)))))
273 (values totalwaste totalavoidable totalunavoidable
274 (nreverse avoidable) (nreverse unavoidable))))
276 (defun display-waste ()
277 (ensure-ucols+ufiles)
278 (multiple-value-bind (tw ta tu al ul) (umls-fixed-size-waste)
279 (format t "Total waste: ~d~%" tw)
280 (format t "Total avoidable: ~d~%" ta)
281 (format t "Total unavoidable: ~d~%" tu)
282 (format t "Avoidable:~%")
284 (format t " (~a,~a): ~a,~a~%" (car w) (cadr w) (caddr w) (cadddr w)))
285 (format t "Unavoidable:~%")
287 (format t " (~a,~a): ~a,~a~%" (car w) (cadr w) (caddr w) (cadddr w)))
290 (defun max-umls-field ()
291 "Return length of longest field"
292 (declare (optimize (speed 3) (space 0)))
293 (ensure-ucols+ufiles)
295 (declare (fixnum max))
296 (dolist (ucol *umls-cols*)
297 (when (> (cmax ucol) max)
298 (setq max (cmax ucol))))
301 (defun max-umls-row ()
302 "Return length of longest row"
303 (declare (optimize (speed 3) (space 0)))
304 (ensure-ucols+ufiles)
305 (let ((rowsizes '()))
306 (dolist (file *umls-files*)
308 (dolist (ucol (ucols file))
309 (incf row (1+ (cmax ucol))))
310 (push row rowsizes)))
311 (car (sort rowsizes #'>))))