Removed GeoInfoTable.Col

This commit is contained in:
Rsl1122 2019-01-24 14:53:11 +02:00
parent b32851c433
commit 2b5e3c61f3
5 changed files with 47 additions and 77 deletions

View File

@ -27,13 +27,13 @@ public class GeoInfoLastUsedPatch extends Patch {
@Override @Override
public boolean hasBeenApplied() { public boolean hasBeenApplied() {
return hasColumn(GeoInfoTable.TABLE_NAME, GeoInfoTable.Col.LAST_USED.get()); return hasColumn(GeoInfoTable.TABLE_NAME, GeoInfoTable.LAST_USED);
} }
@Override @Override
protected void applyPatch() { protected void applyPatch() {
addColumn(GeoInfoTable.TABLE_NAME, addColumn(GeoInfoTable.TABLE_NAME,
GeoInfoTable.Col.LAST_USED + " bigint NOT NULL DEFAULT 0" GeoInfoTable.LAST_USED + " bigint NOT NULL DEFAULT 0"
); );
} }
} }

View File

@ -19,7 +19,6 @@ package com.djrapitops.plan.db.patches;
import com.djrapitops.plan.api.exceptions.database.DBOpException; import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.sql.tables.GeoInfoTable; import com.djrapitops.plan.db.sql.tables.GeoInfoTable;
import com.djrapitops.plan.db.sql.tables.GeoInfoTable.Col;
public class GeoInfoOptimizationPatch extends Patch { public class GeoInfoOptimizationPatch extends Patch {
@ -34,8 +33,8 @@ public class GeoInfoOptimizationPatch extends Patch {
@Override @Override
public boolean hasBeenApplied() { public boolean hasBeenApplied() {
return hasColumn(tableName, Col.ID.get()) return hasColumn(tableName, GeoInfoTable.ID)
&& hasColumn(tableName, Col.UUID.get()) && hasColumn(tableName, GeoInfoTable.USER_UUID)
&& !hasColumn(tableName, "user_id") && !hasColumn(tableName, "user_id")
&& !hasTable(tempTableName); // If this table exists the patch has failed to finish. && !hasTable(tempTableName); // If this table exists the patch has failed to finish.
} }
@ -47,17 +46,17 @@ public class GeoInfoOptimizationPatch extends Patch {
db.getGeoInfoTable().createTable(); db.getGeoInfoTable().createTable();
execute("INSERT INTO " + tableName + " (" + execute("INSERT INTO " + tableName + " (" +
Col.UUID + ", " + GeoInfoTable.USER_UUID + ", " +
Col.IP + ", " + GeoInfoTable.IP + ", " +
Col.IP_HASH + ", " + GeoInfoTable.IP_HASH + ", " +
Col.LAST_USED + ", " + GeoInfoTable.LAST_USED + ", " +
Col.GEOLOCATION + GeoInfoTable.GEOLOCATION +
") SELECT " + ") SELECT " +
"(SELECT plan_users.uuid FROM plan_users WHERE plan_users.id = " + tempTableName + ".user_id LIMIT 1), " + "(SELECT plan_users.uuid FROM plan_users WHERE plan_users.id = " + tempTableName + ".user_id LIMIT 1), " +
Col.IP + ", " + GeoInfoTable.IP + ", " +
Col.IP_HASH + ", " + GeoInfoTable.IP_HASH + ", " +
Col.LAST_USED + ", " + GeoInfoTable.LAST_USED + ", " +
Col.GEOLOCATION + GeoInfoTable.GEOLOCATION +
" FROM " + tempTableName " FROM " + tempTableName
); );

View File

@ -53,7 +53,7 @@ public class IPAnonPatch extends Patch {
private Boolean containsUnAnonymizedIPs() { private Boolean containsUnAnonymizedIPs() {
String sql = "SELECT * FROM " + tableName + String sql = "SELECT * FROM " + tableName +
" WHERE " + GeoInfoTable.Col.IP + " NOT LIKE ? LIMIT 1"; " WHERE " + GeoInfoTable.IP + " NOT LIKE ? LIMIT 1";
return query(new QueryStatement<Boolean>(sql) { return query(new QueryStatement<Boolean>(sql) {
@Override @Override
@ -77,9 +77,9 @@ public class IPAnonPatch extends Patch {
private void anonymizeIPs(Map<UUID, List<GeoInfo>> allGeoInfo) { private void anonymizeIPs(Map<UUID, List<GeoInfo>> allGeoInfo) {
String sql = "UPDATE " + GeoInfoTable.TABLE_NAME + " SET " + String sql = "UPDATE " + GeoInfoTable.TABLE_NAME + " SET " +
GeoInfoTable.Col.IP + "=?, " + GeoInfoTable.IP + "=?, " +
GeoInfoTable.Col.IP_HASH + "=? " + GeoInfoTable.IP_HASH + "=? " +
"WHERE " + GeoInfoTable.Col.IP + "=?"; "WHERE " + GeoInfoTable.IP + "=?";
executeBatch(new ExecStatement(sql) { executeBatch(new ExecStatement(sql) {
@Override @Override

View File

@ -27,11 +27,11 @@ public class IPHashPatch extends Patch {
@Override @Override
public boolean hasBeenApplied() { public boolean hasBeenApplied() {
return hasColumn(GeoInfoTable.TABLE_NAME, GeoInfoTable.Col.IP_HASH.get()); return hasColumn(GeoInfoTable.TABLE_NAME, GeoInfoTable.IP_HASH);
} }
@Override @Override
protected void applyPatch() { protected void applyPatch() {
addColumn(GeoInfoTable.TABLE_NAME, GeoInfoTable.Col.IP_HASH.get() + " varchar(200) DEFAULT ''"); addColumn(GeoInfoTable.TABLE_NAME, GeoInfoTable.IP_HASH + " varchar(200) DEFAULT ''");
} }
} }

View File

@ -23,7 +23,10 @@ import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement; import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.QueryStatement; import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.patches.*; import com.djrapitops.plan.db.patches.*;
import com.djrapitops.plan.db.sql.parsing.*; import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
import com.djrapitops.plan.db.sql.parsing.Select;
import com.djrapitops.plan.db.sql.parsing.Sql;
import com.djrapitops.plan.db.sql.parsing.TableSqlParser;
import com.djrapitops.plan.db.sql.queries.LargeFetchQueries; import com.djrapitops.plan.db.sql.queries.LargeFetchQueries;
import com.djrapitops.plan.utilities.comparators.GeoInfoComparator; import com.djrapitops.plan.utilities.comparators.GeoInfoComparator;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
@ -61,11 +64,11 @@ public class GeoInfoTable extends UserUUIDTable {
public GeoInfoTable(SQLDB db) { public GeoInfoTable(SQLDB db) {
super(TABLE_NAME, db); super(TABLE_NAME, db);
insertStatement = "INSERT INTO " + tableName + " (" insertStatement = "INSERT INTO " + tableName + " ("
+ Col.UUID + ", " + USER_UUID + ", "
+ Col.IP + ", " + IP + ", "
+ Col.IP_HASH + ", " + IP_HASH + ", "
+ Col.GEOLOCATION + ", " + GEOLOCATION + ", "
+ Col.LAST_USED + LAST_USED
+ ") VALUES (?, ?, ?, ?, ?)"; + ") VALUES (?, ?, ?, ?, ?)";
} }
@ -85,20 +88,20 @@ public class GeoInfoTable extends UserUUIDTable {
@Override @Override
public void createTable() throws DBInitException { public void createTable() throws DBInitException {
createTable(TableSqlParser.createTable(tableName) createTable(TableSqlParser.createTable(tableName)
.primaryKeyIDColumn(supportsMySQLQueries, Col.ID) .primaryKeyIDColumn(supportsMySQLQueries, ID)
.column(Col.UUID, Sql.varchar(36)).notNull() .column(USER_UUID, Sql.varchar(36)).notNull()
.column(Col.IP, Sql.varchar(39)).notNull() .column(IP, Sql.varchar(39)).notNull()
.column(Col.GEOLOCATION, Sql.varchar(50)).notNull() .column(GEOLOCATION, Sql.varchar(50)).notNull()
.column(Col.IP_HASH, Sql.varchar(200)) .column(IP_HASH, Sql.varchar(200))
.column(Col.LAST_USED, Sql.LONG).notNull().defaultValue("0") .column(LAST_USED, Sql.LONG).notNull().defaultValue("0")
.primaryKey(supportsMySQLQueries, Col.ID) .primaryKey(supportsMySQLQueries, ID)
.toString() .toString()
); );
} }
public List<GeoInfo> getGeoInfo(UUID uuid) { public List<GeoInfo> getGeoInfo(UUID uuid) {
String sql = "SELECT DISTINCT * FROM " + tableName + String sql = "SELECT DISTINCT * FROM " + tableName +
" WHERE " + Col.UUID + "=?"; " WHERE " + USER_UUID + "=?";
return query(new QueryStatement<List<GeoInfo>>(sql, 100) { return query(new QueryStatement<List<GeoInfo>>(sql, 100) {
@Override @Override
@ -110,10 +113,10 @@ public class GeoInfoTable extends UserUUIDTable {
public List<GeoInfo> processResults(ResultSet set) throws SQLException { public List<GeoInfo> processResults(ResultSet set) throws SQLException {
List<GeoInfo> geoInfo = new ArrayList<>(); List<GeoInfo> geoInfo = new ArrayList<>();
while (set.next()) { while (set.next()) {
String ip = set.getString(Col.IP.get()); String ip = set.getString(IP);
String geolocation = set.getString(Col.GEOLOCATION.get()); String geolocation = set.getString(GEOLOCATION);
String ipHash = set.getString(Col.IP_HASH.get()); String ipHash = set.getString(IP_HASH);
long lastUsed = set.getLong(Col.LAST_USED.get()); long lastUsed = set.getLong(LAST_USED);
geoInfo.add(new GeoInfo(ip, geolocation, lastUsed, ipHash)); geoInfo.add(new GeoInfo(ip, geolocation, lastUsed, ipHash));
} }
return geoInfo; return geoInfo;
@ -123,10 +126,10 @@ public class GeoInfoTable extends UserUUIDTable {
private void updateGeoInfo(UUID uuid, GeoInfo info) { private void updateGeoInfo(UUID uuid, GeoInfo info) {
String sql = "UPDATE " + tableName + " SET " String sql = "UPDATE " + tableName + " SET "
+ Col.LAST_USED + "=?" + + LAST_USED + "=?" +
" WHERE " + Col.UUID + "=?" + " WHERE " + USER_UUID + "=?" +
" AND " + Col.IP_HASH + "=?" + " AND " + IP_HASH + "=?" +
" AND " + Col.GEOLOCATION + "=?"; " AND " + GEOLOCATION + "=?";
execute(new ExecStatement(sql) { execute(new ExecStatement(sql) {
@Override @Override
@ -162,8 +165,8 @@ public class GeoInfoTable extends UserUUIDTable {
} }
public Optional<String> getGeolocation(String ip) { public Optional<String> getGeolocation(String ip) {
String sql = Select.from(tableName, Col.GEOLOCATION) String sql = Select.from(tableName, GEOLOCATION)
.where(Col.IP + "=?") .where(IP + "=?")
.toString(); .toString();
return query(new QueryStatement<Optional<String>>(sql) { return query(new QueryStatement<Optional<String>>(sql) {
@ -175,7 +178,7 @@ public class GeoInfoTable extends UserUUIDTable {
@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.GEOLOCATION.get())); return Optional.of(set.getString(GEOLOCATION));
} }
return Optional.empty(); return Optional.empty();
} }
@ -226,36 +229,4 @@ public class GeoInfoTable extends UserUUIDTable {
} }
}); });
} }
@Deprecated
public enum Col implements Column {
@Deprecated
ID("id"),
@Deprecated
UUID(UserUUIDTable.Col.UUID.get()),
@Deprecated
IP("ip"),
@Deprecated
IP_HASH("ip_hash"),
@Deprecated
GEOLOCATION("geolocation"),
@Deprecated
LAST_USED("last_used");
private final String column;
Col(String column) {
this.column = column;
}
@Override
public String get() {
return toString();
}
@Override
public String toString() {
return column;
}
}
} }