r4840: Auto commit for Debian build
[umlisp.git] / sql-create.lisp
index 73605a9f285c7db454aece139825fb72272bf0d2..af0dd994e73921240b466d3c69e61b10c560d64a 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; Programmer:    Kevin M. Rosenberg
 ;;;; Date Started:  Apr 2000
 ;;;;
-;;;; $Id: sql-create.lisp,v 1.1 2002/10/18 03:57:39 kevin Exp $
+;;;; $Id: sql-create.lisp,v 1.19 2003/05/06 07:17:35 kevin Exp $
 ;;;;
 ;;;; This file, part of UMLisp, is
 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
 ;;;; *************************************************************************
 
 (in-package :umlisp)
-(declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3)))
 
+(eval-when (:compile-toplevel)
+  (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3))))
 
 (defun create-table-cmd (file)
-"Return sql command to create a table"
+  "Return sql command to create a table"
   (let ((col-func 
         (lambda (c) 
-          (let ((sqltype (umls-col-sqltype c)))
-            (concatenate 'string (umls-col-col c)
-               " "
-               (if (or (string-equal sqltype "VARCHAR")
-                       (string-equal sqltype "CHAR"))
-                    (format nil "~a (~a)" sqltype (umls-col-max c))
-                 sqltype)
-               ",")))))
-    (format nil "CREATE TABLE ~a (~a)" (umls-file-table file)
-           (string-trim-last-character
-            (mapcar-append-string col-func (umls-cols-for-umls-file file))))))
+          (let ((sqltype (sqltype c)))
+            (concatenate 'string
+                         (col c)
+                         " "
+                         (if (or (string-equal sqltype "VARCHAR")
+                                 (string-equal sqltype "CHAR"))
+                             (format nil "~a (~a)" sqltype (cmax c))
+                             sqltype))))))
+    (format nil "CREATE TABLE ~a (~{~a~^,~})" (table file)
+           (mapcar col-func (ucols-for-umls-file file)))))
 
 (defun create-custom-table-cmd (tablename sql-cmd)
-"Return SQL command to create a custom table"
+  "Return SQL command to create a custom table"
   (format nil "CREATE TABLE ~a AS ~a;" tablename sql-cmd))
 
+(defun insert-col-value (col value)
+  (if (null (parse-fun col)) 
+      value
+      (format nil "~A" (funcall (parse-fun col) value))))
+
 (defun insert-values-cmd (file values)
-"Return sql insert command for a row of values"  
+  "Return sql insert command for a row of values"  
   (let ((insert-func
         (lambda (col value)
-          (concatenate
-           'string
-           (umls-col-quotechar col)
-           (if (null (umls-col-parsefunc col)) 
-               value
-             (format nil "~A" (funcall (umls-col-parsefunc col) value)))
-           (umls-col-quotechar col)
-           ","))))
+          (let ((q (quotechar col)))
+            (concatenate 'string q (insert-col-value col value) q)))))
     (format
-     nil "INSERT INTO ~a (~a) VALUES (~a)"
-     (umls-file-table file)
-     (string-trim-last-character
-      (mapcar-append-string (lambda (c) (concatenate 'string c ","))
-                           (umls-file-fields file)))
-     (string-trim-last-character
-      (concatenate 'string
-       (mapcar2-append-string insert-func
-                              (remove-custom-cols (umls-file-colstructs file)) 
-                              values)
-       (custom-col-values (custom-colstructs-for-file file) values "," t)))
-     )))
-
-(defun custom-col-values (colstructs values delim doquote)
-  "Returns string of column values for SQL inserts for custom columns"
-  (let ((result ""))
-    (dolist (col colstructs)
-      (let* ((func (umls-col-custom-value-func col))
-            (custom-value (funcall func values)))
-       (string-append result 
-                      (if doquote (umls-col-quotechar col))
-                      (escape-backslashes custom-value)
-                      (if doquote (umls-col-quotechar col))
-                      delim)))
-    result))
+     nil "INSERT INTO ~a (~{~a~^,~}) VALUES (~A)"
+     (table file)
+     (fields file)
+     (concat-separated-strings
+      "," 
+      (mapcar insert-func (remove-custom-cols (ucols file)) values)
+      (custom-col-values (custom-ucols-for-file file) values t)))))
+
+
+(defun custom-col-value (col values doquote)
+  (let ((custom-value (funcall (custom-value-fun col) values)))
+    (if custom-value
+       (if doquote
+           (let ((q (quotechar col)))
+             (concatenate 'string q (escape-backslashes custom-value) q))
+           (escape-backslashes custom-value))
+       "")))
+
+(defun custom-col-values (ucols values doquote)
+  "Returns a list of string column values for SQL inserts for custom columns"
+  (loop for col in ucols collect (custom-col-value col values doquote)))
 
 (defun remove-custom-cols (cols)
   "Remove custom cols from a list col umls-cols"
-  (remove-if #'umls-col-custom-value-func cols))
+  (remove-if #'custom-value-fun cols))
 
 (defun find-custom-cols-for-filename (filename)
   (remove-if-not (lambda (x) (string-equal filename (car x))) +custom-cols+))
 (defun custom-colnames-for-filename (filename)
   (mapcar #'cadr (find-custom-cols-for-filename filename)))
 
-(defun custom-colstructs-for-file (file)
-  (remove-if-not #'umls-col-custom-value-func (umls-file-colstructs file)))
+(defun custom-ucols-for-file (file)
+  (remove-if-not #'custom-value-fun (ucols file)))
 
 (defun noneng-lang-index-files ()
-  (remove-if-not (lambda (f) (and (> (length (umls-file-fil f)) 4)
-                             (string-equal (umls-file-fil f) "MRXW." :end1 5) 
-                             (not (string-equal (umls-file-fil f) "MRXW.ENG"))
-                             (not (string-equal (umls-file-fil f) "MRXW.NONENG"))))
-                *umls-files*))
+  (remove-if-not
+   (lambda (f) (and (> (length (fil f)) 4)
+                   (string-equal (fil f) "MRXW." :end1 5) 
+                   (not (string-equal (fil f) "MRXW.ENG"))
+                   (not (string-equal (fil f) "MRXW.NONENG"))))
+   *umls-files*))
 
 ;;; SQL Command Functions
 
 (defun create-index-cmd (colname tablename length)
-"Return sql create index command"
+  "Return sql create index command"
   (format nil "CREATE INDEX ~a ON ~a (~a ~a)"
-    (concatenate 'string tablename "_" colname "_X") tablename colname
-    (if (integerp length)
-       (format nil "(~d)" length)
-      "")))
+         (concatenate 'string tablename "_" colname "_X")
+         tablename colname
+         (if (integerp length) (format nil "(~d)" length) "")))
 
 (defun create-all-tables-cmdfile ()
-"Return sql commands to create all tables. Not need for automated SQL import"
+  "Return sql commands to create all tables. Not need for automated SQL import"
   (mapcar (lambda (f) (format nil "~a~%~%" (create-table-cmd f))) *umls-files*))
 
-
 ;; SQL Execution functions
 
 (defun sql-drop-tables (conn)
-"SQL Databases: drop all tables"
-  (mapcar
-   (lambda (file)
-     (ignore-errors 
-      (sql-execute (format nil "DROP TABLE ~a" (umls-file-table file)) conn)))
-   *umls-files*))
+  "SQL Databases: drop all tables"
+  (dolist (file *umls-files*)
+    (ignore-errors 
+      (sql-execute (format nil "DROP TABLE ~a" (table file)) conn))))
 
 (defun sql-create-tables (conn)
-"SQL Databases: create all tables" 
-  (mapcar (lambda (file) (sql-execute (create-table-cmd file) conn)) *umls-files*))
+  "SQL Databases: create all tables" 
+  (dolist (file *umls-files*)
+    (sql-execute (create-table-cmd file) conn)))
 
 (defun sql-create-custom-tables (conn)
-"SQL Databases: create all custom tables"
-  (mapcar (lambda (ct)
-     (sql-execute (create-custom-table-cmd (car ct) (cadr ct)) conn))
-   +custom-tables+))
+  "SQL Databases: create all custom tables"
+  (dolist (ct +custom-tables+)
+    (sql-execute (create-custom-table-cmd (car ct) (cadr ct)) conn)))
   
 (defun sql-insert-values (conn file)
-"SQL Databases: inserts all values for a file"  
-  (with-umls-file (line (umls-file-fil file))
-                 (sql-execute (insert-values-cmd file line) conn)))
+  "SQL Databases: inserts all values for a file"  
+  (with-umls-file (line (fil file))
+    (sql-execute (insert-values-cmd file line) conn)))
 
 (defun sql-insert-all-values (conn)
-"SQL Databases: inserts all values for all files"  
-  (mapcar (lambda (file) (sql-insert-values conn file)) *umls-files*))
+  "SQL Databases: inserts all values for all files"  
+  (dolist (file *umls-files*)
+    (sql-insert-values conn file)))
 
 (defun sql-create-indexes (conn &optional (indexes +index-cols+))
-"SQL Databases: create all indexes"
-(mapcar 
- (lambda (idx) 
-   (sql-execute (create-index-cmd (car idx) (cadr idx) (caddr idx)) conn)) 
- indexes))
+  "SQL Databases: create all indexes"
+  (dolist (idx indexes)
+    (sql-execute (create-index-cmd (car idx) (cadr idx) (caddr idx)) conn))) 
 
 (defun make-usrl (conn)
   (sql-execute "drop table if exists USRL" conn)
   (sql-execute "create table USRL (sab varchar(80), srl integer)" conn)
-  (dolist (tuple (mutex-sql-query "select distinct SAB,SRL from MRSO order by SAB asc"))
+  (dolist (tuple (mutex-sql-query
+                 "select distinct SAB,SRL from MRSO order by SAB asc"))
     (sql-execute (format nil "insert into USRL (sab,srl) values ('~a',~d)" 
                         (car tuple) (ensure-integer (cadr tuple)))
                 conn)))
   (make-usrl conn))
 
 (defun create-umls-db-by-insert ()
-"SQL Databases: initializes entire database via SQL insert commands"
+  "SQL Databases: initializes entire database via SQL insert commands"
   (init-umls)
   (init-hash-table)
   (with-sql-connection (conn)
-;;   (sql-drop-tables conn)
-;;   (sql-create-tables conn)
-;;   (sql-insert-all-values conn)
-   (sql-create-indexes conn)
-   (sql-create-custom-tables conn)
-   (sql-create-indexes conn +custom-index-cols+)
-   (sql-create-special-tables)))
+    (sql-drop-tables conn)
+    (sql-create-tables conn)
+    (sql-insert-all-values conn)
+    (sql-create-indexes conn)
+    (sql-create-custom-tables conn)
+    (sql-create-indexes conn +custom-index-cols+)
+    (sql-create-special-tables conn)))
 
 (defun create-umls-db (&optional (extension ".trans") 
-                                (copy-cmd #'mysql-copy-cmd))
+                      (copy-cmd #'mysql-copy-cmd))
   "SQL Databases: initializes entire database via SQL copy commands. 
 This is much faster that using create-umls-db-insert."
   (init-umls)
@@ -189,7 +181,7 @@ This is much faster that using create-umls-db-insert."
   (with-sql-connection (conn)
     (sql-drop-tables conn)
     (sql-create-tables conn)
-    (mapcar 
+    (map 'nil 
      #'(lambda (file) (sql-execute (funcall copy-cmd file extension) conn)) 
      *umls-files*)
     (sql-create-indexes conn)
@@ -198,67 +190,61 @@ This is much faster that using create-umls-db-insert."
     (sql-create-special-tables conn)))
 
 (defun translate-all-files (&optional (extension ".trans"))
-"Copy translated files and return postgresql copy commands to import"
+  "Copy translated files and return postgresql copy commands to import"
   (make-noneng-index-file extension)
-  (mapcar (lambda (f) (translate-file f extension)) *umls-files*))
+  (dolist (f *umls-files*) (translate-umls-file f extension)))
 
-(defun translate-file (file extension)
+(defun translate-umls-file (file extension)
   "Translate a umls file into a format suitable for sql copy cmd"
-  (let ((path (umls-pathname (umls-file-fil file) extension)))
-    (if (probe-file path)
-       (progn
-         (format t "File ~A already exists: skipping~%" path)
-         nil)
-      (with-open-file (ostream path :direction :output)
-       (with-umls-file (line (umls-file-fil file))
-         (princ (umls-translate file line) ostream)
-         (princ #\newline ostream))
-       t))))
+  (translate-files (fil file) extension (list file)))
 
 (defun make-noneng-index-file (extension)
   "Make non-english index file"
-  (let* ((outfile (find-umls-file "MRXW.NONENG"))
-        (path (umls-pathname (umls-file-fil outfile) extension)))
-       
-    (if (probe-file path)
-       (progn
-         (format t "File ~A already exists: skipping~%" path)
-         nil)
-      (progn
-       (with-open-file (ostream path :direction :output)
-         (dolist (inputfile (noneng-lang-index-files))
-           (with-umls-file (line (umls-file-fil inputfile))
-             (princ (umls-translate outfile line) ostream) ;; use outfile for custom cols
-             (princ #\newline ostream))))
-       t))))
+  (translate-files "MRXW.NONENG" extension (noneng-lang-index-files)))
+
+(defun translate-files (output-basename extension input-files)
+  "Translate a umls file into a format suitable for sql copy cmd"
+  (let ((output-path (umls-pathname output-basename extension)))
+    (if (probe-file output-path)
+       (format t "File ~A already exists: skipping~%" output-path)
+      (with-open-file (ostream output-path :direction :output)
+       (dolist (input-file input-files)
+         (with-umls-file (line (fil input-file))
+           (umls-translate input-file line ostream)
+           (princ #\newline ostream)))))))
 
 (defun pg-copy-cmd (file extension)
-"Return postgresql copy statement for a file"  
-  (format nil "COPY ~a FROM '~a' using delimiters '|' with null as ''"
-         (umls-file-table file) (umls-pathname (umls-file-fil file) extension)))
+  "Return postgresql copy statement for a file"  
+  (format
+   nil "COPY ~a FROM '~a' using delimiters '|' with null as ''"
+   (table file) (umls-pathname (fil file) extension)))
 
 (defun mysql-copy-cmd (file extension)
-"Return mysql copy statement for a file"  
-  (format nil "LOAD DATA LOCAL INFILE \"~a\" INTO TABLE ~a FIELDS TERMINATED BY \"|\""
-    (umls-pathname (umls-file-fil file) extension) (umls-file-table file)))
-
-(defun umls-translate (file line)
-"Translate a single line for sql output"
-(string-trim-last-character
- (concatenate 'string
-   (mapcar2-append-string 
-    (lambda (col value)
-      (concatenate
-         'string
-       (if (eq (umls-col-datatype col) 'sql-u)
-           (format nil "~d" (parse-ui value ""))
-         (escape-backslashes value))
-       "|"))
-    (remove-custom-cols (umls-file-colstructs file)) 
-    line)
-   (custom-col-values (custom-colstructs-for-file file) line "|" nil))))
+  "Return mysql copy statement for a file"  
+  (format
+   nil
+   "LOAD DATA LOCAL INFILE \"~a\" INTO TABLE ~a FIELDS TERMINATED BY \"|\""
+   (umls-pathname (fil file) extension) (table file)))
+
+(defun col-value (col value)
+  (if (eq (datatype col) 'sql-u)
+      (let ((ui (parse-ui value "")))
+       (if (stringp ui)
+           ui
+         (write-to-string ui)))
+    (escape-backslashes value)))
+
+(defun umls-translate (file line strm)
+  "Translate a single line for sql output"
+  (print-separated-strings
+   strm "|" 
+   (mapcar #'col-value (remove-custom-cols (ucols file)) line)
+   (custom-col-values (custom-ucols-for-file file) line nil)))
    
 
+;;; Routines for analyzing cost of fixed size storage
+
+
 (defun umls-fixed-size-waste ()
   "Display storage waste if using all fixed size storage"
   (let ((totalwaste 0)
@@ -267,19 +253,24 @@ This is much faster that using create-umls-db-insert."
        (unavoidable '())
        (avoidable '()))
     (dolist (file *umls-files*)
-      (dolist (col (umls-file-colstructs file))
-       (let* ((avwaste (- (umls-col-max col) (umls-col-av col)))
-              (cwaste (* avwaste (umls-file-rws file))))
-         (unless (zerop cwaste)
+      (dolist (col (ucols file))
+       (let* ((avwaste (- (cmax col) (av col)))
+              (cwaste (* avwaste (rws file))))
+         (when (plusp cwaste)
            (if (<= avwaste 6)
                (progn
                  (incf totalunavoidable cwaste)
-                 (setq unavoidable (append unavoidable (list (list (umls-file-fil file) (umls-col-col col) avwaste cwaste)))))
-             (progn
+                 (push (list (fil file) (col col)
+                             avwaste cwaste)
+                       unavoidable))
+               (progn
                  (incf totalavoidable cwaste)
-                 (setq avoidable (append avoidable (list (list (umls-file-fil file) (umls-col-col col) avwaste cwaste))))))
+                 (push (list (fil file) (col col)
+                             avwaste cwaste)
+                       avoidable)))
            (incf totalwaste cwaste)))))
-    (values totalwaste totalavoidable totalunavoidable avoidable unavoidable)))
+    (values totalwaste totalavoidable totalunavoidable
+           (nreverse avoidable) (nreverse unavoidable))))
 
 (defun display-waste ()
   (unless *umls-files*
@@ -294,7 +285,7 @@ This is much faster that using create-umls-db-insert."
     (format t "Unavoidable:~%")
     (dolist (w ul)
       (format t "  (~a,~a): ~a,~a~%" (car w) (cadr w) (caddr w) (cadddr w)))
-  ))
+    ))
 
 (defun max-umls-field ()
   "Return length of longest field"
@@ -303,22 +294,19 @@ This is much faster that using create-umls-db-insert."
   (let ((max 0))
     (declare (fixnum max))
     (dolist (col *umls-cols*)
-      (when (> (umls-col-max col) max)
-       (setq max (umls-col-max col))))
+      (when (> (cmax col) max)
+       (setq max (cmax col))))
     max))
 
 (defun max-umls-row ()
   "Return length of longest row"
-  (if t
-      6000  ;;; hack to use on systems without MRCOLS/MRFILES -- ok for UMLS2001
-    (progn
-      (unless *umls-files*
-       (init-umls))
-      (let ((rowsizes '()))
-       (dolist (file *umls-files*)
-         (let ((row 0)
-               (fields (umls-file-colstructs file)))
-           (dolist (field fields)
-             (incf row (1+ (umls-col-max field))))
-           (push row rowsizes)))
-       (car (sort rowsizes #'>))))))
+  (unless *umls-files*
+    (init-umls))
+  (let ((rowsizes '()))
+    (dolist (file *umls-files*)
+      (let ((row 0)
+           (fields (ucols file)))
+       (dolist (field fields)
+         (incf row (1+ (cmax field))))
+       (push row rowsizes)))
+    (car (sort rowsizes #'>))))