r9521: rrf updates
[umlisp.git] / create-sql.lisp
index fd513c4660064e31f37da3f01102ecab463a0764..3b8b39ca912589b131958be15331f42f7794bcae 100644 (file)
   (let ((col-func 
         (lambda (c) 
           (let ((sqltype (sqltype c)))
+            (case *umls-sql-type*
+              (:oracle
+               (cond
+                ((string-equal sqltype "VARCHAR")
+                 (setq sqltype "VARCHAR2"))
+                ((string-equal sqltype "BIGINT")
+                 (setq sqltype "VARCHAR2(20)")))))
+            
             (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 file)))))
+              (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~^,~})~A" 
+           (table file)
+           (mapcar col-func (ucols file))
+           (if (and (eq *umls-sql-type* :mysql)
+                    (string-equal (table file) "MRCXT"))
+               " MAX_ROWS=200000000"
+             ""))))
 
 (defun create-custom-table-cmd (tablename sql-cmd)
   "Return SQL command to create a custom table"
 (defun noneng-lang-index-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"))))
+                   (string-equal (fil f) "MRXW_" :end1 5) 
+                   (not (string-equal (fil f) "MRXW_ENG.RRF"))
+                   (not (string-equal (fil f) "MRXW_NONENG.RRF"))))
    *umls-files*))
 
 ;;; SQL Command Functions
   (dolist (file *umls-files*)
     (sql-execute (create-table-cmd file) conn)))
 
+#+ignore
+(defun sql-create-kcon-table (conn)
+  "Create concept table, one row per concept."
+  (ignore-errors (execute-command "DROP TABLE KCON" :database conn))
+  (execute-command
+   (format nil "CREATE TABLE KCON (CUI INTEGER, STR ~A, LRL ~A)"
+          (case *umls-sql-type*
+            (:oracle
+             (format nil "VARCHAR2(~D)"
+                     (slot-value (find-ucol "STR" "MRCONSO.RRF") 'max)))
+            (t "TEXT"))
+          (case *umls-sql-type*
+            (:mysql "TINYINT")
+            ((:postgresql :postgresql-socket) "INT2")
+            (:oracle "NUMBER(2,0)")
+            (t "INTEGER")))
+   :database conn)
+  (dolist (tuple (query "select distinct cui from MRCONSO order by cui"
+                       :database conn))
+    (let ((cui (car tuple)))
+      (execute-command 
+       (format nil "INSERT into KCON VALUES (~D,'~A',~D)"
+              cui
+              (add-sql-quotes (pfstr-hash cui) )
+              (cui-lrl cui))
+       :database conn))))
+
 (defun sql-create-custom-tables (conn)
   "SQL Databases: create all custom tables"
+  ;;(sql-create-kcon-table conn)
   (dolist (ct +custom-tables+)
     (sql-execute (create-custom-table-cmd (car ct) (cadr ct)) conn)))
   
     (sql-create-indexes conn +custom-index-cols+)
     (sql-create-special-tables conn)))
 
-(defun create-umls-db (&optional (extension ".trans"))
+(defun create-umls-db (&key (extension ".trans") (skip-translation nil))
   "SQL Databases: initializes entire database via SQL copy commands. 
 This is much faster that using create-umls-db-insert."
-  (ignore-errors
-    (clsql:destroy-database (list *umls-sql-host* (lookup-db-name *umls-sql-db*)
-                                 *umls-sql-user* *umls-sql-passwd*) 
-                           :database-type *umls-sql-type*))
-  (clsql:create-database (list *umls-sql-host* (lookup-db-name *umls-sql-db*)
-                              *umls-sql-user* *umls-sql-passwd*) 
-                        :database-type *umls-sql-type*)
   (ensure-ucols+ufiles)
   (ensure-preparse)
-  (translate-all-files extension)
+  (unless skip-translation
+    (translate-all-files extension))
   (let ((copy-cmd
         (ecase (umls-sql-type)
           (:mysql #'mysql-copy-cmd)
           (:postgresql #'pg-copy-cmd))))
     (with-sql-connection (conn)
+      (clsql:truncate-database :database conn)
       (sql-drop-tables conn)
       (sql-create-tables conn)
       (dolist (file *umls-files*)
@@ -226,7 +262,7 @@ This is much faster that using create-umls-db-insert."
 (defun translate-all-files (&optional (extension ".trans"))
   "Copy translated files and return postgresql copy commands to import"
   (make-noneng-index-file extension)
-  (dolist (f (remove "MRXW.NONENG" *umls-files* :test #'string= :key #'fil))
+  (dolist (f (remove "MRXW_NONENG.RRF" *umls-files* :test #'string= :key #'fil))
     (translate-umls-file f extension)))
 
 (defun translate-umls-file (file extension)
@@ -235,17 +271,17 @@ This is much faster that using create-umls-db-insert."
 
 (defun make-noneng-index-file (extension)
   "Make non-english index file"
-  (translate-files (find-ufile "MRXW.NONENG")
+  (translate-files (find-ufile "MRXW_NONENG.RRF")
                   extension (noneng-lang-index-files)))
 
 (defun translate-files (out-ufile extension input-ufiles)
   "Translate a umls file into a format suitable for sql copy cmd"
-  (let ((output-path (umls-pathname (fil out-ufile) extension)))
+  (let ((output-path (ufile-pathname out-ufile 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-ufile input-ufiles)
-         (with-umls-file (line (fil input-ufile))
+         (with-umls-ufile (line input-ufile)
            (translate-line out-ufile line ostream)
            (princ #\newline ostream)))))))
 
@@ -267,14 +303,15 @@ This is much faster that using create-umls-db-insert."
   "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)))
+   (table file) (ufile-pathname file extension)))
 
-(defun mysql-copy-cmd (file extension)
+(defun mysql-copy-cmd (file extension &key local-file)
   "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)))
+   "LOAD DATA ~AINFILE \"~a\" INTO TABLE ~a FIELDS TERMINATED BY \"|\""
+   (if local-file "LOCAL " "")
+   (ufile-pathname file extension) (table file)))
 
    
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;