From bc4099885befa32f8d1b0e6435175aa6a67348f0 Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Sat, 10 Sep 2016 08:46:58 -0500 Subject: [PATCH] Removed cfg from git and created a new IF logic for updating a sqlite database --- cfg/lightspeed.cfg | 4 - data/lightspeed.sql | 6 + .../common/info/RelationalServerFactory.java | 119 +++++++++++++++++- 3 files changed, 121 insertions(+), 8 deletions(-) delete mode 100644 cfg/lightspeed.cfg diff --git a/cfg/lightspeed.cfg b/cfg/lightspeed.cfg deleted file mode 100644 index 9d476b2..0000000 --- a/cfg/lightspeed.cfg +++ /dev/null @@ -1,4 +0,0 @@ -# 09/09/2016 16:30:15 CDT -ENABLE-LOGGING=true -MAVEN=/usr/bin/mvn -PORT=44444 diff --git a/data/lightspeed.sql b/data/lightspeed.sql index bac455f..cc0f0f8 100644 --- a/data/lightspeed.sql +++ b/data/lightspeed.sql @@ -1,8 +1,12 @@ CREATE TABLE IF NOT EXISTS servers ( id TEXT PRIMARY KEY, directory TEXT, + origin TEXT, jvm_arguments TEXT ); +IF user_version < 2; + ALTER TABLE servers ADD COLUMN origin TEXT; +ENDIF; CREATE TABLE IF NOT EXISTS builds ( id INTEGER PRIMARY KEY, @@ -29,3 +33,5 @@ CREATE TABLE IF NOT EXISTS deployments ( start_time INTEGER, end_time INTEGER ); + +PRAGMA user_version = 2; diff --git a/src/com/projectswg/common/info/RelationalServerFactory.java b/src/com/projectswg/common/info/RelationalServerFactory.java index 58d9947..2dab756 100644 --- a/src/com/projectswg/common/info/RelationalServerFactory.java +++ b/src/com/projectswg/common/info/RelationalServerFactory.java @@ -88,8 +88,9 @@ public class RelationalServerFactory { RelationalServerData data = new RelationalServerData(BASE_PATH + file); try { String [] commands = getCommandsFromSchema(f.getPath().substring(0, f.getPath().length()-3) + ".sql"); + ParserData parserData = new ParserData(); for (String command : commands) - executeCommand(data, command); + executeCommand(data, command, parserData); return data; } catch (Exception e) { e.printStackTrace(); @@ -121,19 +122,66 @@ public class RelationalServerFactory { return base + ".sdb"; } - private void executeCommand(RelationalServerData data, String command) { + private void executeCommand(RelationalServerData data, String command, ParserData parserData) { command = command.trim(); - if (command.startsWith("SELECT")) { + if (command.startsWith("SELECT") && parserData.getConditional()) { try (ResultSet set = data.executeQuery(command)) { } catch (SQLException e) { e.printStackTrace(); } - } else { + } else if (command.startsWith("IF")) { // VERY SIMPLE 'if' logic, no parenthesis and no AND/OR's - expects 3 arguments: + parserData.addConditional(evaluateIf(data, command.substring(2).trim())); + } else if (command.startsWith("ENDIF")) { + parserData.unwrapLastConditional(); + } else if (parserData.getConditional()) { data.updateQuery(command); } } + private boolean evaluateIf(RelationalServerData data, String statement) { + String [] args = statement.split(" ", 3); + if (args.length != 3) { + Log.e(this, "Invalid IF statement: %s", statement); + return false; + } + double num1 = parseToNumber(data, args[0]); + String comparator = args[1]; + double num2 = parseToNumber(data, args[2]); + switch (comparator) { + case "<": return num1 < num2; + case ">": return num1 > num2; + case "=": return num1 == num2; + case "==": return num1 == num2; + case "<=": return num1 <= num2; + case ">=": return num1 >= num2; + default: + Log.e(this, "Invalid comparator: %s", comparator); + return false; + } + } + + private double parseToNumber(RelationalServerData data, String str) { + if (str.equals("user_version")) { + ResultSet set = data.executeQuery("PRAGMA user_version"); + try { + if (set.next()) + return set.getDouble(1); + else + Log.e(this, "Variable 'user_version' has not been set!"); + } catch (SQLException e) { + Log.e(this, e); + } + } else { + try { + return Double.parseDouble(str); + } catch (NumberFormatException e) { + Log.e(this, "Number '%s' is not a valid number!", str); + } + } + return Double.NaN; + } + private String [] getCommandsFromSchema(String schema) throws IOException { String command; try (InputStream input = new FileInputStream(new File(schema))) { @@ -157,4 +205,67 @@ public class RelationalServerFactory { } } + private static class ParserData { + + private RecursiveBoolean conditionalValue; + + public ParserData() { + conditionalValue = null; + } + + public void addConditional(boolean val) { + if (conditionalValue == null) + conditionalValue = new RecursiveBoolean(val); + else + conditionalValue.createRecursive(val); + } + + public void unwrapLastConditional() { + if (conditionalValue != null && conditionalValue.unwrapLast()) + conditionalValue = null; + } + + public boolean getConditional() { + if (conditionalValue == null) + return true; + return conditionalValue.get(); + } + + } + + private static class RecursiveBoolean { + + private final boolean val; + private RecursiveBoolean recursive; + + public RecursiveBoolean(boolean val) { + this.val = val; + this.recursive = null; + } + + public boolean get() { + if (recursive != null) + return val && recursive.get(); + return val; + } + + public RecursiveBoolean createRecursive(boolean val) { + if (recursive != null) + return recursive.createRecursive(val); + return (recursive = new RecursiveBoolean(val)); + } + + public boolean unwrapLast() { + RecursiveBoolean recur = this; + while (recur.recursive != null && recur.recursive.recursive != null) + recur = recur.recursive; + if (recur != null && recur.recursive != null) { + recur.recursive = null; + return true; + } + return true; + } + + } + }