X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fdatabase%2FSQLFileManager.java;h=61732182015ddcd007b4b3892750415ab949e8e1;hb=a1618d15f672d5ced82d6f4f6f06d2f58412fd1f;hp=676d9e66e129f992217bf74ed92c0ec8ac57e3be;hpb=53a66914a544d38be7e626aec2e74542745d4fda;p=gigi.git diff --git a/src/org/cacert/gigi/database/SQLFileManager.java b/src/org/cacert/gigi/database/SQLFileManager.java index 676d9e66..61732182 100644 --- a/src/org/cacert/gigi/database/SQLFileManager.java +++ b/src/org/cacert/gigi/database/SQLFileManager.java @@ -12,36 +12,56 @@ public class SQLFileManager { public static enum ImportType { /** - * Execute Script as-as + * Execute Script as-is */ PRODUCTION, /** - * Execute Script, but changing Engine=InnoDB to Engine=Memory + * Execute Script, but change Engine=InnoDB to Engine=Memory */ TEST, /** * Execute INSERT statements as-is, and TRUNCATE instead of DROPPING */ - TRUNCATE + TRUNCATE, + /** + * Execute Script as-is if db version is >= specified version in + * optional header + */ + SAMPLE_DATA, } public static void addFile(Statement stmt, InputStream f, ImportType type) throws IOException, SQLException { String sql = readFile(f); - sql = sql.replaceAll("--[^\n]+\n", "\n"); + if (type == ImportType.SAMPLE_DATA) { + String fl = sql.split("\n")[0]; + if (fl.matches("--Version: ([0-9]+)")) { + int v0 = Integer.parseInt(fl.substring(11)); + if (DatabaseConnection.CURRENT_SCHEMA_VERSION < v0) { + System.out.println("skipping sample data (data has version " + v0 + ", db has version " + DatabaseConnection.CURRENT_SCHEMA_VERSION + ")"); + return; + } + } + } + sql = sql.replaceAll("--[^\n]*\n", "\n"); + sql = sql.replaceAll("#[^\n]*\n", "\n"); String[] stmts = sql.split(";"); - Pattern p = Pattern.compile("\\s*DROP TABLE IF EXISTS `([^`]+)`"); + 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 ((string.contains("profiles") || string.contains("cacerts") || string.contains("cats_type") || string.contains("countryIsoCode")) && type == ImportType.TRUNCATE) { + continue; + } + string = DatabaseConnection.preprocessQuery(string); if (m.matches() && type == ImportType.TRUNCATE) { - String sql2 = "TRUNCATE `" + m.group(1) + "`"; + String sql2 = "DELETE FROM \"" + m.group(1) + "\""; stmt.addBatch(sql2); continue; } - if (type == ImportType.PRODUCTION || string.startsWith("INSERT")) { + if (type == ImportType.PRODUCTION || type == ImportType.SAMPLE_DATA || string.startsWith("INSERT")) { stmt.addBatch(string); } else if (type == ImportType.TEST) { stmt.addBatch(string.replace("ENGINE=InnoDB", "ENGINE=Memory")); @@ -56,6 +76,6 @@ public class SQLFileManager { while ((len = f.read(buf)) > 0) { baos.write(buf, 0, len); } - return new String(baos.toByteArray()); + return new String(baos.toByteArray(), "UTF-8"); } }