]> WPIA git - gigi.git/blobdiff - util/org/cacert/gigi/util/DatabaseManager.java
fix: show import problems in databaseManager more detailed
[gigi.git] / util / org / cacert / gigi / util / DatabaseManager.java
index 0dadd00aacb61f719fc689a84f56996d0d8b11f2..20df139ac9f8b6f13aa9fc528dd2371d6ab0d177 100644 (file)
@@ -1,22 +1,22 @@
 package org.cacert.gigi.util;
 
 import java.io.File;
-import java.io.FileReader;
+import java.io.FileInputStream;
 import java.io.IOException;
-import java.nio.file.Files;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.Properties;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
-public class DatabaseManager {
+import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.SQLFileManager;
+import org.cacert.gigi.database.SQLFileManager.ImportType;
 
-    public static String readFile(File f) throws IOException {
-        return new String(Files.readAllBytes(f.toPath()));
-    }
+public class DatabaseManager {
 
     public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
         boolean test = false;
@@ -28,7 +28,9 @@ public class DatabaseManager {
         }
         if (args.length == 0) {
             Properties p = new Properties();
-            p.load(new FileReader("config/gigi.properties"));
+            try (Reader reader = new InputStreamReader(new FileInputStream("config/gigi.properties"), "UTF-8")) {
+                p.load(reader);
+            }
             args = new String[] {
                     p.getProperty("sql.driver"), p.getProperty("sql.url"), p.getProperty("sql.user"), p.getProperty("sql.password")
             };
@@ -40,57 +42,32 @@ public class DatabaseManager {
         run(args, test ? ImportType.TEST : ImportType.PRODUCTION);
     }
 
-    public static enum ImportType {
-        /**
-         * Execute Script as-as
-         */
-        PRODUCTION,
-        /**
-         * Execute Script, but changing Engine=InnoDB to Engine=Memory
-         */
-        TEST,
-        /**
-         * Execute INSERT statements as-is, and TRUNCATE instead of DROPPING
-         */
-        TRUNCATE
-    }
-
     public static void run(String[] args, ImportType truncate) throws ClassNotFoundException, SQLException, IOException {
         Class.forName(args[0]);
-        Connection conn = DriverManager.getConnection(args[1], args[2], args[3]);
-        conn.setAutoCommit(false);
-        Statement stmt = conn.createStatement();
-        addFile(stmt, new File("doc/tableStructure.sql"), truncate);
-        File localData = new File("doc/sampleData.sql");
-        if (localData.exists()) {
-            addFile(stmt, localData, ImportType.PRODUCTION);
-        }
-        stmt.executeBatch();
-        conn.commit();
-        stmt.close();
-    }
-
-    private static void addFile(Statement stmt, File f, ImportType type) throws IOException, SQLException {
-        String sql = readFile(f);
-        sql = sql.replaceAll("--[^\n]+\n", "\n");
-        String[] stmts = sql.split(";");
-        Pattern p = Pattern.compile("\\s*DROP TABLE IF EXISTS `([^`]+)`");
-        for (String string : stmts) {
-            Matcher m = p.matcher(string);
-            string = string.trim();
-            if (string.equals("")) {
-                continue;
-            }
-            if (m.matches() && type == ImportType.TRUNCATE) {
-                String sql2 = "TRUNCATE `" + m.group(1) + "`";
-                stmt.addBatch(sql2);
-                continue;
-            }
-            if (type == ImportType.PRODUCTION || string.startsWith("INSERT")) {
-                stmt.addBatch(string);
-            } else if (type == ImportType.TEST) {
-                stmt.addBatch(string.replace("ENGINE=InnoDB", "ENGINE=Memory"));
+        final Connection conn = DriverManager.getConnection(args[1], args[2], args[3]);
+        try {
+            conn.setAutoCommit(false);
+            Statement stmt = conn.createStatement();
+            try {
+                try (InputStream structure = DatabaseConnection.class.getResourceAsStream("tableStructure.sql")) {
+                    SQLFileManager.addFile(stmt, structure, truncate);
+                }
+                File localData = new File("doc/sampleData.sql");
+                if (localData.exists()) {
+                    try (FileInputStream f = new FileInputStream(localData)) {
+                        SQLFileManager.addFile(stmt, f, ImportType.PRODUCTION);
+                    }
+                }
+                stmt.executeBatch();
+                conn.commit();
+            } finally {
+                stmt.close();
             }
+        } catch (SQLException e) {
+            e.getNextException().printStackTrace();
+            throw e;
+        } finally {
+            conn.close();
         }
     }
 }