X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=util%2Forg%2Fcacert%2Fgigi%2Futil%2FDatabaseManager.java;h=0dadd00aacb61f719fc689a84f56996d0d8b11f2;hb=0d64798ee0a9c2091119335e0c6182f61c601173;hp=457e11ff27b0c42a3a92e89f42422eba917aedef;hpb=e65588dc9fc539683882a4b52bacb93cfe551d5d;p=gigi.git diff --git a/util/org/cacert/gigi/util/DatabaseManager.java b/util/org/cacert/gigi/util/DatabaseManager.java index 457e11ff..0dadd00a 100644 --- a/util/org/cacert/gigi/util/DatabaseManager.java +++ b/util/org/cacert/gigi/util/DatabaseManager.java @@ -19,6 +19,13 @@ public class DatabaseManager { } public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException { + boolean test = false; + if (args.length >= 1 && args[0].equals("--test")) { + test = true; + String[] ne = new String[args.length - 1]; + System.arraycopy(args, 1, ne, 0, ne.length); + args = ne; + } if (args.length == 0) { Properties p = new Properties(); p.load(new FileReader("config/gigi.properties")); @@ -30,34 +37,59 @@ public class DatabaseManager { System.err.println("Usage: com.mysql.jdbc.Driver jdbc:mysql://localhost/cacert user password"); return; } - run(args, false); + 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, boolean truncate) throws ClassNotFoundException, SQLException, IOException { + 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, false); + addFile(stmt, localData, ImportType.PRODUCTION); } stmt.executeBatch(); + conn.commit(); stmt.close(); } - private static void addFile(Statement stmt, File f, boolean truncate) throws IOException, SQLException { + 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); - if (m.matches()) { + string = string.trim(); + if (string.equals("")) { + continue; + } + if (m.matches() && type == ImportType.TRUNCATE) { String sql2 = "TRUNCATE `" + m.group(1) + "`"; stmt.addBatch(sql2); + continue; } - if ( !string.trim().equals("") && ( !truncate || string.contains("INSERT"))) { - stmt.addBatch(string.replace("ENGINE=Memory", "")); + if (type == ImportType.PRODUCTION || string.startsWith("INSERT")) { + stmt.addBatch(string); + } else if (type == ImportType.TEST) { + stmt.addBatch(string.replace("ENGINE=InnoDB", "ENGINE=Memory")); } } }