Archived
0

обновление H2PlayerDAO

This commit is contained in:
2018-09-16 00:24:51 +03:00
parent 5a0b29c2ba
commit 6316e14544
6 changed files with 92 additions and 40 deletions

View File

@@ -13,6 +13,7 @@ import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.UUID; import java.util.UUID;
@@ -20,7 +21,10 @@ import java.util.UUID;
@Slf4j @Slf4j
@Component @Component
public class H2PlayerDAO { 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 @Autowired
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
@@ -102,31 +106,57 @@ public class H2PlayerDAO {
} }
} }
public void getByName(H2Player playerBuffer) throws SQLException { private void locationDeserialize(H2Player playerBuffer, ResultSet resultSet) throws SQLException {
if (playerBuffer.getName() == null || playerBuffer.getName().isEmpty()) { if (!world.getName().equals(resultSet.getString("location_world"))) {
throw new SQLException("Field 'name' is " + (playerBuffer.getName() == null ? "null" : "empty")); log.warn("Unknown world \"{}\"", resultSet.getString("location_world"));
}
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"));
log.warn("Using default (spawn) location for user \"{}\"", playerBuffer.getName()); log.warn("Using default (spawn) location for user \"{}\"", playerBuffer.getName());
playerBuffer.setLocation(world.getSpawn().clone()); playerBuffer.setLocation(world.getSpawn().clone());
} else { } else {
playerBuffer.setLocation(new EntityLocation( playerBuffer.setLocation(new EntityLocation(
rs.getDouble("location_x"), resultSet.getDouble("location_x"),
rs.getDouble("location_y"), resultSet.getDouble("location_y"),
rs.getDouble("location_z"), resultSet.getDouble("location_z"),
rs.getFloat("location_yaw"), resultSet.getFloat("location_yaw"),
rs.getFloat("location_pitch") resultSet.getFloat("location_pitch")
)); ));
playerBuffer.setWorld(world); 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 { public void update(H2Player player) throws SQLException {
@@ -149,7 +179,7 @@ public class H2PlayerDAO {
}); });
if (affectedRows == 0) { 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) { 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())); int affectedRows = jdbcTemplate.update(DELETE_SQL, pss -> pss.setInt(1, player.getId()));
if (affectedRows == 0) { if (affectedRows == 0) {
throw new SQLException("Serialize player failed, no rows affected."); throw new SQLException("Remove player failed, no rows affected.");
} }
player.setId(0); player.setId(0);
@@ -211,7 +241,8 @@ public class H2PlayerDAO {
try { try {
INSERT_SQL = IOUtils.resourceToString("/sqls/insert_player.sql", StandardCharsets.UTF_8); INSERT_SQL = IOUtils.resourceToString("/sqls/insert_player.sql", StandardCharsets.UTF_8);
INSERT2_SQL = IOUtils.resourceToString("/sqls/insert_player_withId.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_SQL = IOUtils.resourceToString("/sqls/update_player.sql", StandardCharsets.UTF_8);
UPDATE_LOCATION_SQL = IOUtils.resourceToString("/sqls/update_player_location.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); DELETE_SQL = IOUtils.resourceToString("/sqls/delete_player.sql", StandardCharsets.UTF_8);

View File

@@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS players (
location_z DOUBLE NOT NULL, location_z DOUBLE NOT NULL,
location_yaw FLOAT NOT NULL, location_yaw FLOAT NOT NULL,
location_pitch 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); CREATE INDEX idx_players_uuid ON players(uuid);

View File

@@ -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;

View File

@@ -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 ? FROM players WHERE name LIKE ?
LIMIT 1; LIMIT 1;

View File

@@ -67,10 +67,8 @@ public class TestDAO {
} }
@Before @Before
public void init() throws IOException { public void before() throws IOException {
jdbcTemplate.execute("DROP INDEX IF EXISTS idx_players_uuid;"); jdbcTemplate.execute(IOUtils.resourceToString("/sqls/drop_table_players.sql", StandardCharsets.UTF_8));
jdbcTemplate.execute("DROP INDEX IF EXISTS idx_players_name;");
jdbcTemplate.execute("DROP TABLE IF EXISTS players;");
jdbcTemplate.execute(IOUtils.resourceToString("/sqls/create_tables.sql", StandardCharsets.UTF_8)); jdbcTemplate.execute(IOUtils.resourceToString("/sqls/create_tables.sql", StandardCharsets.UTF_8));
createPlayer(); createPlayer();
assertEquals(0, player.getId()); assertEquals(0, player.getId());
@@ -125,33 +123,50 @@ public class TestDAO {
@Test @Test
public void testGetByName() throws SQLException { public void testGetByName() throws SQLException {
playerDAO.insert(this.player); playerDAO.insert(this.player);
assertNotEquals(0, player.getId());
H2Player player = new H2Player(); H2Player queryPlayer = new H2Player();
player.setName(this.player.getName()); queryPlayer.setName(player.getName());
assertNotNull(player.getName()); boolean result = playerDAO.getByName(queryPlayer);
assertFalse(player.getName().isEmpty());
assertEquals(this.player.getName(), player.getName());
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 @Test
public void testGetByNonExistsName() throws SQLException { public void testGetByNonExistsName() throws SQLException {
playerDAO.insert(this.player); playerDAO.insert(this.player);
assertNotEquals(0, player.getId());
H2Player player = new H2Player(); H2Player player = new H2Player();
player.setName("NON_EXISTS_NAME"); player.setName("NON_EXISTS_NAME");
assertNotNull(player.getName()); boolean result = playerDAO.getByName(player);
assertFalse(player.getName().isEmpty()); assertFalse(result);
}
playerDAO.getByName(player); @Test
assertEquals(0, player.getId()); 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 @Test

View File

@@ -0,0 +1,3 @@
DROP INDEX IF EXISTS idx_players_uuid;
DROP INDEX IF EXISTS idx_players_name;
DROP TABLE IF EXISTS players;