Skip to content

Commit

Permalink
More postgres fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFEEDC0DE64 committed Apr 3, 2024
1 parent 4c10647 commit 4efc658
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,28 @@ protected String getAddFloodgateColumnStmt() {
.replace("`", "\"")
.replace("INTEGER(3)", "INTEGER");
}

@Override
protected String getLoadByNameStmt() {
return LOAD_BY_NAME_STMT
.replace("`", "\"");
}

@Override
protected String getLoadByUuidStmt() {
return LOAD_BY_UUID_STMT
.replace("`", "\"");
}

@Override
protected String getInsertProfileStmt() {
return INSERT_PROFILE_STMT
.replace("`", "\"");
}

@Override
protected String getUpdateProfileStmt() {
return UPDATE_PROFILE_STMT
.replace("`", "\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ public abstract class SQLStorage implements AuthStorage {
protected static final String ADD_FLOODGATE_COLUMN_STMT = "ALTER TABLE `" + PREMIUM_TABLE
+ "` ADD COLUMN `Floodgate` INTEGER(3)";

protected static final String LOAD_BY_NAME = "SELECT * FROM `" + PREMIUM_TABLE
protected static final String LOAD_BY_NAME_STMT = "SELECT * FROM `" + PREMIUM_TABLE
+ "` WHERE `Name`=? LIMIT 1";
protected static final String LOAD_BY_UUID = "SELECT * FROM `" + PREMIUM_TABLE
protected static final String LOAD_BY_UUID_STMT = "SELECT * FROM `" + PREMIUM_TABLE
+ "` WHERE `UUID`=? LIMIT 1";
protected static final String INSERT_PROFILE = "INSERT INTO `" + PREMIUM_TABLE
protected static final String INSERT_PROFILE_STMT = "INSERT INTO `" + PREMIUM_TABLE
+ "` (`UUID`, `Name`, `Premium`, `Floodgate`, `LastIp`) " + "VALUES (?, ?, ?, ?, ?) ";
// limit not necessary here, because it's unique
protected static final String UPDATE_PROFILE = "UPDATE `" + PREMIUM_TABLE
protected static final String UPDATE_PROFILE_STMT = "UPDATE `" + PREMIUM_TABLE
+ "` SET `UUID`=?, `Name`=?, `Premium`=?, `Floodgate`=?, `LastIp`=?, "
+ "`LastLogin`=CURRENT_TIMESTAMP WHERE `UserID`=?";

Expand Down Expand Up @@ -111,8 +111,9 @@ private boolean isColumnMissing(DatabaseMetaData metaData, String columnName) th

@Override
public StoredProfile loadProfile(String name) {
log.error("loadProfile " + name);
try (Connection con = dataSource.getConnection();
PreparedStatement loadStmt = con.prepareStatement(LOAD_BY_NAME)
PreparedStatement loadStmt = con.prepareStatement(getLoadByNameStmt())
) {
loadStmt.setString(1, name);

Expand All @@ -129,8 +130,9 @@ public StoredProfile loadProfile(String name) {

@Override
public StoredProfile loadProfile(UUID uuid) {
log.error("loadProfile " + uuid.toString());
try (Connection con = dataSource.getConnection();
PreparedStatement loadStmt = con.prepareStatement(LOAD_BY_UUID)) {
PreparedStatement loadStmt = con.prepareStatement(getLoadByUuidStmt())) {
loadStmt.setString(1, UUIDAdapter.toMojangId(uuid));

try (ResultSet resultSet = loadStmt.executeQuery()) {
Expand Down Expand Up @@ -171,13 +173,14 @@ private Optional<StoredProfile> parseResult(ResultSet resultSet) throws SQLExcep

@Override
public void save(StoredProfile playerProfile) {
log.error("save");
try (Connection con = dataSource.getConnection()) {
String uuid = playerProfile.getOptId().map(UUIDAdapter::toMojangId).orElse(null);

playerProfile.getSaveLock().lock();
try {
if (playerProfile.isSaved()) {
try (PreparedStatement saveStmt = con.prepareStatement(UPDATE_PROFILE)) {
try (PreparedStatement saveStmt = con.prepareStatement(getUpdateProfileStmt())) {
saveStmt.setString(1, uuid);
saveStmt.setString(2, playerProfile.getName());
saveStmt.setBoolean(3, playerProfile.isPremium());
Expand All @@ -188,7 +191,8 @@ public void save(StoredProfile playerProfile) {
saveStmt.execute();
}
} else {
try (PreparedStatement saveStmt = con.prepareStatement(INSERT_PROFILE, RETURN_GENERATED_KEYS)) {
try (PreparedStatement saveStmt = con.prepareStatement(getInsertProfileStmt(),
RETURN_GENERATED_KEYS)) {
saveStmt.setString(1, uuid);

saveStmt.setString(2, playerProfile.getName());
Expand Down Expand Up @@ -218,6 +222,7 @@ public void save(StoredProfile playerProfile) {
* @return An SQL Statement to create the `premium` table
*/
protected String getCreateTableStmt() {
log.error("getCreateTableStmt");
return CREATE_TABLE_STMT;
}

Expand All @@ -226,9 +231,30 @@ protected String getCreateTableStmt() {
* @return An SQL Statement to create the `premium` table
*/
protected String getAddFloodgateColumnStmt() {
log.error("getAddFloodgateColumnStmt");
return ADD_FLOODGATE_COLUMN_STMT;
}

protected String getLoadByNameStmt() {
log.error("getLoadByNameStmt");
return LOAD_BY_NAME_STMT;
}

protected String getLoadByUuidStmt() {
log.error("getLoadByUuidStmt");
return LOAD_BY_UUID_STMT;
}

protected String getInsertProfileStmt() {
log.error("getInsertProfileStmt");
return INSERT_PROFILE_STMT;
}

protected String getUpdateProfileStmt() {
log.error("getUpdateProfileStmt");
return UPDATE_PROFILE_STMT;
}

@Override
public void close() {
dataSource.close();
Expand Down

0 comments on commit 4efc658

Please sign in to comment.