обновление H2PlayerDAO
This commit is contained in:
@@ -13,6 +13,7 @@ import org.springframework.stereotype.Component;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.UUID;
|
||||
@@ -20,7 +21,10 @@ import java.util.UUID;
|
||||
@Slf4j
|
||||
@Component
|
||||
public class H2PlayerDAO {
|
||||
private static String INSERT_SQL, INSERT2_SQL, SELECT_SQL, UPDATE_SQL, UPDATE_LOCATION_SQL, DELETE_SQL;
|
||||
private static String INSERT_SQL, INSERT2_SQL,
|
||||
SELECT_BYNAME_SQL, SELECT_BYID_SQL,
|
||||
UPDATE_SQL, UPDATE_LOCATION_SQL,
|
||||
DELETE_SQL;
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
@@ -102,31 +106,57 @@ public class H2PlayerDAO {
|
||||
}
|
||||
}
|
||||
|
||||
public void getByName(H2Player playerBuffer) throws SQLException {
|
||||
if (playerBuffer.getName() == null || playerBuffer.getName().isEmpty()) {
|
||||
throw new SQLException("Field 'name' is " + (playerBuffer.getName() == null ? "null" : "empty"));
|
||||
}
|
||||
|
||||
jdbcTemplate.query(SELECT_SQL,
|
||||
ps -> ps.setString(1, playerBuffer.getName()),
|
||||
rs -> {
|
||||
playerBuffer.setId(rs.getInt("id"));
|
||||
playerBuffer.setUuid(UUID.fromString(rs.getString("uuid")));
|
||||
if (!world.getName().equals(rs.getString("location_world"))) {
|
||||
log.warn("Unknown world \"{}\"", rs.getString("location_world"));
|
||||
private void locationDeserialize(H2Player playerBuffer, ResultSet resultSet) throws SQLException {
|
||||
if (!world.getName().equals(resultSet.getString("location_world"))) {
|
||||
log.warn("Unknown world \"{}\"", resultSet.getString("location_world"));
|
||||
log.warn("Using default (spawn) location for user \"{}\"", playerBuffer.getName());
|
||||
playerBuffer.setLocation(world.getSpawn().clone());
|
||||
} else {
|
||||
playerBuffer.setLocation(new EntityLocation(
|
||||
rs.getDouble("location_x"),
|
||||
rs.getDouble("location_y"),
|
||||
rs.getDouble("location_z"),
|
||||
rs.getFloat("location_yaw"),
|
||||
rs.getFloat("location_pitch")
|
||||
resultSet.getDouble("location_x"),
|
||||
resultSet.getDouble("location_y"),
|
||||
resultSet.getDouble("location_z"),
|
||||
resultSet.getFloat("location_yaw"),
|
||||
resultSet.getFloat("location_pitch")
|
||||
));
|
||||
playerBuffer.setWorld(world);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean getByName(H2Player playerBuffer) throws SQLException {
|
||||
if (playerBuffer.getName() == null || playerBuffer.getName().isEmpty()) {
|
||||
throw new SQLException("Field 'name' is " + (playerBuffer.getName() == null ? "null" : "empty"));
|
||||
}
|
||||
|
||||
final boolean[] result = new boolean[]{false};
|
||||
jdbcTemplate.query(SELECT_BYNAME_SQL,
|
||||
ps -> ps.setString(1, playerBuffer.getName()),
|
||||
rs -> {
|
||||
playerBuffer.setId(rs.getInt("id"));
|
||||
playerBuffer.setUuid(UUID.fromString(rs.getString("uuid")));
|
||||
locationDeserialize(playerBuffer, rs);
|
||||
result[0] = true;
|
||||
});
|
||||
|
||||
return result[0];
|
||||
}
|
||||
|
||||
public boolean getById(H2Player playerBuffer) throws SQLException {
|
||||
if (playerBuffer.getId() == 0) {
|
||||
throw new SQLException("Field 'id' is zero");
|
||||
}
|
||||
|
||||
final boolean[] result = new boolean[]{false};
|
||||
jdbcTemplate.query(SELECT_BYID_SQL,
|
||||
ps -> ps.setInt(1, playerBuffer.getId()),
|
||||
rs -> {
|
||||
playerBuffer.setUuid(UUID.fromString(rs.getString("uuid")));
|
||||
playerBuffer.setName(rs.getString("name"));
|
||||
locationDeserialize(playerBuffer, rs);
|
||||
result[0] = true;
|
||||
});
|
||||
|
||||
return result[0];
|
||||
}
|
||||
|
||||
public void update(H2Player player) throws SQLException {
|
||||
@@ -149,7 +179,7 @@ public class H2PlayerDAO {
|
||||
});
|
||||
|
||||
if (affectedRows == 0) {
|
||||
throw new SQLException("Serialize player failed, no rows affected.");
|
||||
throw new SQLException("Update player failed, no rows affected.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,7 +219,7 @@ public class H2PlayerDAO {
|
||||
});
|
||||
|
||||
if (affectedRows == 0) {
|
||||
throw new SQLException("Serialize player failed, no rows affected.");
|
||||
throw new SQLException("Update player failed, no rows affected.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,7 +231,7 @@ public class H2PlayerDAO {
|
||||
int affectedRows = jdbcTemplate.update(DELETE_SQL, pss -> pss.setInt(1, player.getId()));
|
||||
|
||||
if (affectedRows == 0) {
|
||||
throw new SQLException("Serialize player failed, no rows affected.");
|
||||
throw new SQLException("Remove player failed, no rows affected.");
|
||||
}
|
||||
|
||||
player.setId(0);
|
||||
@@ -211,7 +241,8 @@ public class H2PlayerDAO {
|
||||
try {
|
||||
INSERT_SQL = IOUtils.resourceToString("/sqls/insert_player.sql", StandardCharsets.UTF_8);
|
||||
INSERT2_SQL = IOUtils.resourceToString("/sqls/insert_player_withId.sql", StandardCharsets.UTF_8);
|
||||
SELECT_SQL = IOUtils.resourceToString("/sqls/select_player_byName.sql", StandardCharsets.UTF_8);
|
||||
SELECT_BYNAME_SQL = IOUtils.resourceToString("/sqls/select_player_byName.sql", StandardCharsets.UTF_8);
|
||||
SELECT_BYID_SQL = IOUtils.resourceToString("/sqls/select_player_byId.sql", StandardCharsets.UTF_8);
|
||||
UPDATE_SQL = IOUtils.resourceToString("/sqls/update_player.sql", StandardCharsets.UTF_8);
|
||||
UPDATE_LOCATION_SQL = IOUtils.resourceToString("/sqls/update_player_location.sql", StandardCharsets.UTF_8);
|
||||
DELETE_SQL = IOUtils.resourceToString("/sqls/delete_player.sql", StandardCharsets.UTF_8);
|
||||
|
||||
@@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS players (
|
||||
location_z DOUBLE NOT NULL,
|
||||
location_yaw FLOAT NOT NULL,
|
||||
location_pitch FLOAT NOT NULL,
|
||||
location_world varchar(64) NOT NULL
|
||||
location_world VARCHAR(64) NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX idx_players_uuid ON players(uuid);
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
SELECT uuid, name, location_x, location_y, location_z, location_yaw, location_pitch, location_world
|
||||
FROM players WHERE id = ?
|
||||
LIMIT 1;
|
||||
@@ -1,3 +1,3 @@
|
||||
SELECT id, uuid, name, location_x, location_y, location_z, location_yaw, location_pitch, location_world
|
||||
SELECT id, uuid, location_x, location_y, location_z, location_yaw, location_pitch, location_world
|
||||
FROM players WHERE name LIKE ?
|
||||
LIMIT 1;
|
||||
@@ -67,10 +67,8 @@ public class TestDAO {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() throws IOException {
|
||||
jdbcTemplate.execute("DROP INDEX IF EXISTS idx_players_uuid;");
|
||||
jdbcTemplate.execute("DROP INDEX IF EXISTS idx_players_name;");
|
||||
jdbcTemplate.execute("DROP TABLE IF EXISTS players;");
|
||||
public void before() throws IOException {
|
||||
jdbcTemplate.execute(IOUtils.resourceToString("/sqls/drop_table_players.sql", StandardCharsets.UTF_8));
|
||||
jdbcTemplate.execute(IOUtils.resourceToString("/sqls/create_tables.sql", StandardCharsets.UTF_8));
|
||||
createPlayer();
|
||||
assertEquals(0, player.getId());
|
||||
@@ -125,33 +123,50 @@ public class TestDAO {
|
||||
@Test
|
||||
public void testGetByName() throws SQLException {
|
||||
playerDAO.insert(this.player);
|
||||
assertNotEquals(0, player.getId());
|
||||
|
||||
H2Player player = new H2Player();
|
||||
player.setName(this.player.getName());
|
||||
H2Player queryPlayer = new H2Player();
|
||||
queryPlayer.setName(player.getName());
|
||||
|
||||
assertNotNull(player.getName());
|
||||
assertFalse(player.getName().isEmpty());
|
||||
assertEquals(this.player.getName(), player.getName());
|
||||
boolean result = playerDAO.getByName(queryPlayer);
|
||||
|
||||
playerDAO.getByName(player);
|
||||
assertTrue(result);
|
||||
assertEquals(player, queryPlayer);
|
||||
}
|
||||
|
||||
assertEquals(player, this.player);
|
||||
@Test
|
||||
public void testGetById() throws SQLException {
|
||||
playerDAO.insert(this.player);
|
||||
|
||||
H2Player queryPlayer = new H2Player();
|
||||
queryPlayer.setId(player.getId());
|
||||
|
||||
boolean result = playerDAO.getById(queryPlayer);
|
||||
|
||||
assertTrue(result);
|
||||
assertEquals(player, queryPlayer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetByNonExistsName() throws SQLException {
|
||||
playerDAO.insert(this.player);
|
||||
assertNotEquals(0, player.getId());
|
||||
|
||||
H2Player player = new H2Player();
|
||||
player.setName("NON_EXISTS_NAME");
|
||||
|
||||
assertNotNull(player.getName());
|
||||
assertFalse(player.getName().isEmpty());
|
||||
boolean result = playerDAO.getByName(player);
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
playerDAO.getByName(player);
|
||||
assertEquals(0, player.getId());
|
||||
@Test
|
||||
public void testGetByNonExistsId() throws SQLException {
|
||||
playerDAO.insert(player);
|
||||
assertEquals(1, player.getId());
|
||||
|
||||
H2Player queryPlayer = new H2Player();
|
||||
queryPlayer.setId(999);
|
||||
|
||||
boolean result = playerDAO.getById(queryPlayer);
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
DROP INDEX IF EXISTS idx_players_uuid;
|
||||
DROP INDEX IF EXISTS idx_players_name;
|
||||
DROP TABLE IF EXISTS players;
|
||||
Reference in New Issue
Block a user