diff --git a/Plan/src/main/java/com/djrapitops/plan/database/sql/Select.java b/Plan/src/main/java/com/djrapitops/plan/database/sql/Select.java new file mode 100644 index 000000000..54bdbd029 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/database/sql/Select.java @@ -0,0 +1,39 @@ +package main.java.com.djrapitops.plan.database.sql; + +import main.java.com.djrapitops.plan.Log; + +public class Select extends SqlParser { + + private int conditions = 0; + + public Select(String start) { + super(start); + } + + public static Select from(String table, String... columns) { + Select parser = new Select("SELECT "); + int size = columns.length; + for (int i = 0; i < size; i++) { + if (size > 1 && i > 0) { + parser.append(", "); + } + parser.append(columns[i]); + } + + parser.append(" FROM ").append(table); + Log.debug(parser.toString()); + return parser; + } + + public Select where(String... conditions) { + append(" WHERE "); + for (int i = 0; i < conditions.length; i++) { + if (this.conditions > 0) { + append(" AND "); + } + append("(").append(conditions[i]).append(")"); + this.conditions++; + } + return this; + } +} diff --git a/Plan/src/main/java/com/djrapitops/plan/database/sql/Sql.java b/Plan/src/main/java/com/djrapitops/plan/database/sql/Sql.java index 33dcf5800..87d563770 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/sql/Sql.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/sql/Sql.java @@ -6,7 +6,11 @@ public class Sql { public static final String LONG = "bigint"; public static final String BOOL = "boolean"; - public static String VARCHAR(int length) { + private Sql() { + throw new IllegalStateException("Variable Class"); + } + + public static String varchar(int length) { return "varchar("+length+")"; } } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/sql/TableSqlParser.java b/Plan/src/main/java/com/djrapitops/plan/database/sql/TableSqlParser.java index ad4960ddd..523ff9a09 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/sql/TableSqlParser.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/sql/TableSqlParser.java @@ -93,6 +93,16 @@ public class TableSqlParser extends SqlParser { return this; } + /** + * Used for ALTER TABLE sql statements. + * + * @param column column to modify + * @return TableSqlParser object + */ + public static TableSqlParser newColumn(String column, String type) { + return new TableSqlParser("").column(column, type); + } + @Override public String toString() { append(")"); diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/CommandUseTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/CommandUseTable.java index 04f2755aa..dab6450a0 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/CommandUseTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/CommandUseTable.java @@ -38,7 +38,7 @@ public class CommandUseTable extends Table { public boolean createTable() { try { execute(TableSqlParser.createTable(tableName) - .column(columnCommand, Sql.VARCHAR(20)).notNull() + .column(columnCommand, Sql.varchar(20)).notNull() .column(columnTimesUsed, Sql.INT).notNull() .toString() ); diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/IPsTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/IPsTable.java index c08c65281..c2cd9d055 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/IPsTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/IPsTable.java @@ -40,7 +40,7 @@ public class IPsTable extends UserIDTable { try { execute(TableSqlParser.createTable(tableName) .column(columnUserID, Sql.INT).notNull() - .column(columnIP, Sql.VARCHAR(20)).notNull() + .column(columnIP, Sql.varchar(20)).notNull() .foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID()) .toString() ); diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java index 5a9f2f900..9028b5fd7 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/KillsTable.java @@ -45,7 +45,7 @@ public class KillsTable extends Table { execute(TableSqlParser.createTable(tableName) .column(columnKillerUserID, Sql.INT).notNull() .column(columnVictimUserID, Sql.INT).notNull() - .column(columnWeapon, Sql.VARCHAR(30)).notNull() + .column(columnWeapon, Sql.varchar(30)).notNull() .column(columnDate, Sql.LONG).notNull() .foreignKey(columnKillerUserID, usersTable.getTableName(), usersTable.getColumnID()) .foreignKey(columnVictimUserID, usersTable.getTableName(), usersTable.getColumnID()) diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java index 976ef5393..63b221fcf 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java @@ -40,7 +40,7 @@ public class NicknamesTable extends UserIDTable { try { execute(TableSqlParser.createTable(tableName) .column(columnUserID, Sql.INT).notNull() - .column(columnNick, Sql.VARCHAR(75)).notNull() + .column(columnNick, Sql.varchar(75)).notNull() .column(columnCurrent, Sql.BOOL).notNull().defaultValue(false) .foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID()) .toString() diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/SecurityTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/SecurityTable.java index 7a9cdbdab..ee3acf084 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/SecurityTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/SecurityTable.java @@ -37,8 +37,8 @@ public class SecurityTable extends Table { public boolean createTable() { try { execute(TableSqlParser.createTable(tableName) - .column(columnUser, Sql.VARCHAR(100)).notNull().unique() - .column(columnSaltedHash, Sql.VARCHAR(100)).notNull().unique() + .column(columnUser, Sql.varchar(100)).notNull().unique() + .column(columnSaltedHash, Sql.varchar(100)).notNull().unique() .column(columnPermLevel, Sql.INT).notNull() .toString() ); diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java index 4af049c05..da7248f34 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java @@ -99,9 +99,9 @@ public class UsersTable extends Table { try { execute(TableSqlParser.createTable(tableName) .primaryKeyIDColumn(usingMySQL, columnID, Sql.INT) - .column(columnUUID, Sql.VARCHAR(36)).notNull().unique() - .column(columnGeolocation, Sql.VARCHAR(50)).notNull() - .column(columnLastGM, Sql.VARCHAR(15)).notNull() + .column(columnUUID, Sql.varchar(36)).notNull().unique() + .column(columnGeolocation, Sql.varchar(50)).notNull() + .column(columnLastGM, Sql.varchar(15)).notNull() .column(columnLastGMSwapTime, Sql.LONG).notNull() .column(columnPlayTime, Sql.LONG).notNull() .column(columnLoginTimes, Sql.INT).notNull() @@ -110,10 +110,10 @@ public class UsersTable extends Table { .column(columnMobKills, Sql.INT).notNull() .column(columnRegistered, Sql.LONG).notNull() .column(columnOP, Sql.BOOL).notNull().defaultValue(false) - .column(columnName, Sql.VARCHAR(16)).notNull() + .column(columnName, Sql.varchar(16)).notNull() .column(columnBanned, Sql.BOOL).notNull().defaultValue(false) .column(columnContainsBukkitData, Sql.BOOL).notNull().defaultValue(false) - .column(columnLastWorld, Sql.VARCHAR(255)).notNull() + .column(columnLastWorld, Sql.varchar(255)).notNull() .column(columnLastWorldSwapTime, Sql.LONG).notNull() .primaryKey(usingMySQL, columnID) .toString() diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTable.java index 9cdf6eec1..1059e408d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTable.java @@ -43,7 +43,7 @@ public class WorldTable extends Table { try { execute(TableSqlParser.createTable(tableName) .primaryKeyIDColumn(usingMySQL, columnWorldId, Sql.INT) - .column(columnWorldName, Sql.VARCHAR(100)).notNull() + .column(columnWorldName, Sql.varchar(100)).notNull() .primaryKey(usingMySQL, columnWorldId) .toString() );