Refactored IPTable

This commit is contained in:
Rsl1122 2017-10-04 17:36:23 +03:00
parent e2ad414b88
commit d872fcf395

View File

@ -3,11 +3,13 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.processing.ExecStatement;
import main.java.com.djrapitops.plan.database.processing.QueryAllStatement;
import main.java.com.djrapitops.plan.database.processing.QueryStatement;
import main.java.com.djrapitops.plan.database.sql.Select;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -62,25 +64,25 @@ public class IPsTable extends UserIDTable {
}
private List<String> getStringList(UUID uuid, String column) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()) {
List<String> stringList = new ArrayList<>();
statement = connection.prepareStatement(Select.from(tableName, column)
String sql = Select.from(tableName, column)
.where(columnUserID + "=" + usersTable.statementSelectID)
.toString());
statement.setFetchSize(50);
.toString();
return query(new QueryStatement<List<String>>(sql, 100) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
set = statement.executeQuery();
}
@Override
public List<String> processQuery(ResultSet set) throws SQLException {
List<String> stringList = new ArrayList<>();
while (set.next()) {
stringList.add(set.getString(column));
}
return stringList;
} finally {
close(set, statement);
}
});
}
public void saveIP(UUID uuid, String ip, String geolocation) throws SQLException {
@ -92,56 +94,50 @@ public class IPsTable extends UserIDTable {
}
private void insertIp(UUID uuid, String ip, String geolocation) throws SQLException {
PreparedStatement statement = null;
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(insertStatement);
execute(new ExecStatement(insertStatement) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
statement.setString(2, ip);
statement.setString(3, geolocation);
statement.execute();
commit(connection);
} finally {
close(statement);
}
});
}
public Optional<String> getGeolocation(String ip) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Select.from(tableName, columnGeolocation)
String sql = Select.from(tableName, columnGeolocation)
.where(columnIP + "=?")
.toString());
.toString();
return query(new QueryStatement<Optional<String>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, ip);
set = statement.executeQuery();
}
@Override
public Optional<String> processQuery(ResultSet set) throws SQLException {
if (set.next()) {
return Optional.of(set.getString(columnGeolocation));
}
return Optional.empty();
} finally {
close(set, statement);
}
});
}
public Map<UUID, List<String>> getAllGeolocations() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()) {
Map<UUID, List<String>> geoLocations = new HashMap<>();
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
statement = connection.prepareStatement("SELECT " +
String sql = "SELECT " +
columnGeolocation + ", " +
usersUUIDColumn +
" FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID
);
statement.setFetchSize(50000);
set = statement.executeQuery();
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID;
return query(new QueryAllStatement<Map<UUID, List<String>>>(sql, 50000) {
@Override
public Map<UUID, List<String>> processQuery(ResultSet set) throws SQLException {
Map<UUID, List<String>> geoLocations = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString("uuid"));
@ -150,27 +146,23 @@ public class IPsTable extends UserIDTable {
geoLocations.put(uuid, userGeoLocs);
}
return geoLocations;
} finally {
close(set, statement);
}
});
}
public Map<UUID, Map<String, String>> getAllIPsAndGeolocations() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try (Connection connection = getConnection()){
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
statement = connection.prepareStatement("SELECT " +
String sql = "SELECT " +
columnGeolocation + ", " +
columnIP + ", " +
usersUUIDColumn +
" FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID
);
statement.setFetchSize(5000);
set = statement.executeQuery();
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID;
return query(new QueryAllStatement<Map<UUID, Map<String, String>>>(sql, 50000) {
@Override
public Map<UUID, Map<String, String>> processQuery(ResultSet set) throws SQLException {
Map<UUID, Map<String, String>> map = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString("uuid"));
@ -184,19 +176,18 @@ public class IPsTable extends UserIDTable {
map.put(uuid, userMap);
}
return map;
} finally {
close(set, statement);
}
});
}
public void insertIPsAndGeolocations(Map<UUID, Map<String, String>> allIPsAndGeolocations) throws SQLException {
if (Verify.isEmpty(allIPsAndGeolocations)) {
return;
}
PreparedStatement statement = null;
try (Connection connection = getConnection()){
statement = connection.prepareStatement(insertStatement);
executeBatch(new ExecStatement(insertStatement) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
// Every User
for (UUID uuid : allIPsAndGeolocations.keySet()) {
// Every IP & Geolocation
@ -211,11 +202,7 @@ public class IPsTable extends UserIDTable {
statement.addBatch();
}
}
statement.executeBatch();
commit(connection);
} finally {
close(statement);
}
}
});
}
}