Removed ServerTable.Col

This commit is contained in:
Rsl1122 2019-01-24 15:06:14 +02:00
parent 84dc8e4c3b
commit 4a0a8b60b2
3 changed files with 50 additions and 93 deletions

View File

@ -42,10 +42,10 @@ public class OptionalFetchQueries {
public static Query<Optional<Server>> matchingServerIdentifier(String identifier) { public static Query<Optional<Server>> matchingServerIdentifier(String identifier) {
String sql = "SELECT * FROM " + ServerTable.TABLE_NAME + String sql = "SELECT * FROM " + ServerTable.TABLE_NAME +
" WHERE (" + ServerTable.Col.SERVER_ID + "=?" + " WHERE (" + ServerTable.SERVER_ID + "=?" +
" OR LOWER(" + ServerTable.Col.NAME + ") LIKE LOWER(?)" + " OR LOWER(" + ServerTable.NAME + ") LIKE LOWER(?)" +
" OR LOWER(" + ServerTable.Col.SERVER_UUID + ") LIKE LOWER(?))" + " OR LOWER(" + ServerTable.SERVER_UUID + ") LIKE LOWER(?))" +
" AND " + ServerTable.Col.INSTALLED + "=?" + " AND " + ServerTable.INSTALLED + "=?" +
" LIMIT 1"; " LIMIT 1";
return new QueryStatement<Optional<Server>>(sql) { return new QueryStatement<Optional<Server>>(sql) {
@Override @Override
@ -60,11 +60,11 @@ public class OptionalFetchQueries {
public Optional<Server> processResults(ResultSet set) throws SQLException { public Optional<Server> processResults(ResultSet set) throws SQLException {
if (set.next()) { if (set.next()) {
return Optional.of(new Server( return Optional.of(new Server(
set.getInt(ServerTable.Col.SERVER_ID.get()), set.getInt(ServerTable.SERVER_ID),
UUID.fromString(set.getString(ServerTable.Col.SERVER_UUID.get())), UUID.fromString(set.getString(ServerTable.SERVER_UUID)),
set.getString(ServerTable.Col.NAME.get()), set.getString(ServerTable.NAME),
set.getString(ServerTable.Col.WEBSERVER_ADDRESS.get()), set.getString(ServerTable.WEB_ADDRESS),
set.getInt(ServerTable.Col.MAX_PLAYERS.get()) set.getInt(ServerTable.MAX_PLAYERS)
)); ));
} }
return Optional.empty(); return Optional.empty();

View File

@ -35,8 +35,6 @@ import java.util.*;
* Table for managing multiple server's data in the database. * Table for managing multiple server's data in the database.
* <p> * <p>
* Table Name: plan_servers * Table Name: plan_servers
* <p>
* For contained columns {@link Col}
* *
* @author Rsl1122 * @author Rsl1122
* @see Server * @see Server
@ -54,14 +52,14 @@ public class ServerTable extends Table {
public ServerTable(SQLDB db) { public ServerTable(SQLDB db) {
super(TABLE_NAME, db); super(TABLE_NAME, db);
statementSelectServerID = "(" + Select.from(tableName, tableName + "." + Col.SERVER_ID).where(tableName + "." + Col.SERVER_UUID + "=?").toString() + " LIMIT 1)"; statementSelectServerID = "(" + Select.from(tableName, tableName + "." + SERVER_ID).where(tableName + "." + SERVER_UUID + "=?").toString() + " LIMIT 1)";
statementSelectServerNameID = "(" + Select.from(tableName, tableName + "." + Col.NAME).where(tableName + "." + Col.SERVER_ID + "=?").toString() + " LIMIT 1)"; statementSelectServerNameID = "(" + Select.from(tableName, tableName + "." + NAME).where(tableName + "." + SERVER_ID + "=?").toString() + " LIMIT 1)";
insertStatement = Insert.values(tableName, insertStatement = Insert.values(tableName,
Col.SERVER_UUID, SERVER_UUID,
Col.NAME, NAME,
Col.WEBSERVER_ADDRESS, WEB_ADDRESS,
Col.INSTALLED, INSTALLED,
Col.MAX_PLAYERS); MAX_PLAYERS);
} }
public final String statementSelectServerID; public final String statementSelectServerID;
@ -81,26 +79,17 @@ public class ServerTable extends Table {
@Override @Override
public void createTable() throws DBInitException { public void createTable() throws DBInitException {
createTable(TableSqlParser.createTable(tableName) createTable(createTableSQL(db.getType()));
.primaryKeyIDColumn(supportsMySQLQueries, Col.SERVER_ID)
.column(Col.SERVER_UUID, Sql.varchar(36)).notNull().unique()
.column(Col.NAME, Sql.varchar(100))
.column(Col.WEBSERVER_ADDRESS, Sql.varchar(100))
.column(Col.INSTALLED, Sql.BOOL).notNull().defaultValue(true)
.column(Col.MAX_PLAYERS, Sql.INT).notNull().defaultValue("-1")
.primaryKey(supportsMySQLQueries, Col.SERVER_ID)
.toString()
);
} }
private void updateServerInfo(Server info) { private void updateServerInfo(Server info) {
String sql = Update.values(tableName, String sql = Update.values(tableName,
Col.SERVER_UUID, SERVER_UUID,
Col.NAME, NAME,
Col.WEBSERVER_ADDRESS, WEB_ADDRESS,
Col.INSTALLED, INSTALLED,
Col.MAX_PLAYERS) MAX_PLAYERS)
.where(Col.SERVER_ID + "=?") .where(SERVER_ID + "=?")
.toString(); .toString();
execute(new ExecStatement(sql) { execute(new ExecStatement(sql) {
@ -132,8 +121,8 @@ public class ServerTable extends Table {
*/ */
public Optional<Integer> getServerID(UUID serverUUID) { public Optional<Integer> getServerID(UUID serverUUID) {
String sql = Select.from(tableName, String sql = Select.from(tableName,
Col.SERVER_ID) SERVER_ID)
.where(Col.SERVER_UUID + "=?") .where(SERVER_UUID + "=?")
.toString(); .toString();
return query(new QueryStatement<Optional<Integer>>(sql) { return query(new QueryStatement<Optional<Integer>>(sql) {
@ -145,7 +134,7 @@ public class ServerTable extends Table {
@Override @Override
public Optional<Integer> processResults(ResultSet set) throws SQLException { public Optional<Integer> processResults(ResultSet set) throws SQLException {
if (set.next()) { if (set.next()) {
return Optional.of(set.getInt(Col.SERVER_ID.get())); return Optional.of(set.getInt(SERVER_ID));
} else { } else {
return Optional.empty(); return Optional.empty();
} }
@ -185,8 +174,8 @@ public class ServerTable extends Table {
*/ */
public Optional<String> getServerName(UUID serverUUID) { public Optional<String> getServerName(UUID serverUUID) {
String sql = Select.from(tableName, String sql = Select.from(tableName,
Col.NAME) NAME)
.where(Col.SERVER_UUID + "=?") .where(SERVER_UUID + "=?")
.toString(); .toString();
return query(new QueryStatement<Optional<String>>(sql) { return query(new QueryStatement<Optional<String>>(sql) {
@ -198,7 +187,7 @@ public class ServerTable extends Table {
@Override @Override
public Optional<String> processResults(ResultSet set) throws SQLException { public Optional<String> processResults(ResultSet set) throws SQLException {
if (set.next()) { if (set.next()) {
return Optional.of(set.getString(Col.NAME.get())); return Optional.of(set.getString(NAME));
} else { } else {
return Optional.empty(); return Optional.empty();
} }
@ -208,7 +197,7 @@ public class ServerTable extends Table {
public Map<UUID, String> getServerNames() { public Map<UUID, String> getServerNames() {
String sql = Select.from(tableName, String sql = Select.from(tableName,
Col.SERVER_UUID, Col.NAME) SERVER_UUID, NAME)
.toString(); .toString();
return query(new QueryAllStatement<Map<UUID, String>>(sql) { return query(new QueryAllStatement<Map<UUID, String>>(sql) {
@ -216,8 +205,8 @@ public class ServerTable extends Table {
public Map<UUID, String> processResults(ResultSet set) throws SQLException { public Map<UUID, String> processResults(ResultSet set) throws SQLException {
Map<UUID, String> names = new HashMap<>(); Map<UUID, String> names = new HashMap<>();
while (set.next()) { while (set.next()) {
UUID serverUUID = UUID.fromString(set.getString(Col.SERVER_UUID.get())); UUID serverUUID = UUID.fromString(set.getString(SERVER_UUID));
names.put(serverUUID, set.getString(Col.NAME.get())); names.put(serverUUID, set.getString(NAME));
} }
return names; return names;
} }
@ -225,7 +214,7 @@ public class ServerTable extends Table {
} }
public List<UUID> getServerUUIDs() { public List<UUID> getServerUUIDs() {
String sql = Select.from(tableName, Col.SERVER_UUID) String sql = Select.from(tableName, SERVER_UUID)
.toString(); .toString();
return query(new QueryAllStatement<List<UUID>>(sql) { return query(new QueryAllStatement<List<UUID>>(sql) {
@ -233,7 +222,7 @@ public class ServerTable extends Table {
public List<UUID> processResults(ResultSet set) throws SQLException { public List<UUID> processResults(ResultSet set) throws SQLException {
List<UUID> uuids = new ArrayList<>(); List<UUID> uuids = new ArrayList<>();
while (set.next()) { while (set.next()) {
uuids.add(UUID.fromString(set.getString(Col.SERVER_UUID.get()))); uuids.add(UUID.fromString(set.getString(SERVER_UUID)));
} }
return uuids; return uuids;
} }
@ -270,8 +259,8 @@ public class ServerTable extends Table {
public Optional<UUID> getServerUUID(String serverName) { public Optional<UUID> getServerUUID(String serverName) {
String sql = Select.from(tableName, String sql = Select.from(tableName,
Col.SERVER_UUID) SERVER_UUID)
.where(Col.NAME + "=?") .where(NAME + "=?")
.toString(); .toString();
return query(new QueryStatement<Optional<UUID>>(sql) { return query(new QueryStatement<Optional<UUID>>(sql) {
@ -283,7 +272,7 @@ public class ServerTable extends Table {
@Override @Override
public Optional<UUID> processResults(ResultSet set) throws SQLException { public Optional<UUID> processResults(ResultSet set) throws SQLException {
if (set.next()) { if (set.next()) {
return Optional.of(UUID.fromString(set.getString(Col.SERVER_UUID.get()))); return Optional.of(UUID.fromString(set.getString(SERVER_UUID)));
} else { } else {
return Optional.empty(); return Optional.empty();
} }
@ -293,7 +282,7 @@ public class ServerTable extends Table {
public Optional<Server> getServerInfo(UUID serverUUID) { public Optional<Server> getServerInfo(UUID serverUUID) {
String sql = Select.from(tableName, "*") String sql = Select.from(tableName, "*")
.where(Col.SERVER_UUID + "=?") .where(SERVER_UUID + "=?")
.toString(); .toString();
return query(new QueryStatement<Optional<Server>>(sql) { return query(new QueryStatement<Optional<Server>>(sql) {
@ -306,11 +295,11 @@ public class ServerTable extends Table {
public Optional<Server> processResults(ResultSet set) throws SQLException { public Optional<Server> processResults(ResultSet set) throws SQLException {
if (set.next()) { if (set.next()) {
return Optional.of(new Server( return Optional.of(new Server(
set.getInt(Col.SERVER_ID.get()), set.getInt(SERVER_ID),
UUID.fromString(set.getString(Col.SERVER_UUID.get())), UUID.fromString(set.getString(SERVER_UUID)),
set.getString(Col.NAME.get()), set.getString(NAME),
set.getString(Col.WEBSERVER_ADDRESS.get()), set.getString(WEB_ADDRESS),
set.getInt(Col.MAX_PLAYERS.get()))); set.getInt(MAX_PLAYERS)));
} }
return Optional.empty(); return Optional.empty();
} }
@ -318,7 +307,7 @@ public class ServerTable extends Table {
} }
public void setAsUninstalled(UUID serverUUID) { public void setAsUninstalled(UUID serverUUID) {
String sql = "UPDATE " + tableName + " SET " + Col.INSTALLED + "=? WHERE " + Col.SERVER_UUID + "=?"; String sql = "UPDATE " + tableName + " SET " + INSTALLED + "=? WHERE " + SERVER_UUID + "=?";
execute(new ExecStatement(sql) { execute(new ExecStatement(sql) {
@Override @Override
@ -328,36 +317,4 @@ public class ServerTable extends Table {
} }
}); });
} }
@Deprecated
public enum Col implements Column {
@Deprecated
SERVER_ID("id"),
@Deprecated
SERVER_UUID("uuid"),
@Deprecated
NAME("name"),
@Deprecated
WEBSERVER_ADDRESS("web_address"),
@Deprecated
INSTALLED("is_installed"),
@Deprecated
MAX_PLAYERS("max_players");
private final String column;
Col(String column) {
this.column = column;
}
@Override
public String get() {
return toString();
}
@Override
public String toString() {
return column;
}
}
} }

View File

@ -36,13 +36,13 @@ class CreateTableParserTest {
void createsSameTablesAsOldParser() { void createsSameTablesAsOldParser() {
String expected = "CREATE TABLE IF NOT EXISTS plan_servers (id integer NOT NULL AUTO_INCREMENT, uuid varchar(36) NOT NULL UNIQUE, name varchar(100), web_address varchar(100), is_installed boolean NOT NULL DEFAULT 1, max_players integer NOT NULL DEFAULT -1, PRIMARY KEY (id))"; String expected = "CREATE TABLE IF NOT EXISTS plan_servers (id integer NOT NULL AUTO_INCREMENT, uuid varchar(36) NOT NULL UNIQUE, name varchar(100), web_address varchar(100), is_installed boolean NOT NULL DEFAULT 1, max_players integer NOT NULL DEFAULT -1, PRIMARY KEY (id))";
String result = CreateTableParser.create(ServerTable.TABLE_NAME, DBType.MYSQL) String result = CreateTableParser.create(ServerTable.TABLE_NAME, DBType.MYSQL)
.column(ServerTable.Col.SERVER_ID.get(), Sql.INT) .column(ServerTable.SERVER_ID, Sql.INT)
.primaryKey() .primaryKey()
.column(ServerTable.Col.SERVER_UUID.get(), Sql.varchar(36)).notNull().unique() .column(ServerTable.SERVER_UUID, Sql.varchar(36)).notNull().unique()
.column(ServerTable.Col.NAME.get(), Sql.varchar(100)) .column(ServerTable.NAME, Sql.varchar(100))
.column(ServerTable.Col.WEBSERVER_ADDRESS.get(), Sql.varchar(100)) .column(ServerTable.WEB_ADDRESS, Sql.varchar(100))
.column(ServerTable.Col.INSTALLED.get(), Sql.BOOL).notNull().defaultValue(true) .column(ServerTable.INSTALLED, Sql.BOOL).notNull().defaultValue(true)
.column(ServerTable.Col.MAX_PLAYERS.get(), Sql.INT).notNull().defaultValue("-1") .column(ServerTable.MAX_PLAYERS, Sql.INT).notNull().defaultValue("-1")
.toString(); .toString();
assertEquals(expected, result); assertEquals(expected, result);
} }