package org.cacert.gigi.util;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
-import java.nio.file.Files;
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;
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);
+ SQLFileManager.addFile(stmt, DatabaseConnection.class.getResourceAsStream("tableStructure.sql"), truncate);
File localData = new File("doc/sampleData.sql");
if (localData.exists()) {
- addFile(stmt, localData, ImportType.PRODUCTION);
+ SQLFileManager.addFile(stmt, new FileInputStream(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"));
- }
- }
- }
}