From 6316e14544b2c8e5f3e2da06675151832df8a917 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sun, 16 Sep 2018 00:24:51 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20H2PlayerDAO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/mc/core/h2db/H2PlayerDAO.java | 73 +++++++++++++------ .../src/main/resources/sqls/create_tables.sql | 2 +- .../resources/sqls/select_player_byId.sql | 3 + .../resources/sqls/select_player_byName.sql | 2 +- .../src/test/java/mc/core/h2db/TestDAO.java | 49 ++++++++----- .../resources/sqls/drop_table_players.sql | 3 + 6 files changed, 92 insertions(+), 40 deletions(-) create mode 100644 h2_playermanager/src/main/resources/sqls/select_player_byId.sql create mode 100644 h2_playermanager/src/test/resources/sqls/drop_table_players.sql diff --git a/h2_playermanager/src/main/java/mc/core/h2db/H2PlayerDAO.java b/h2_playermanager/src/main/java/mc/core/h2db/H2PlayerDAO.java index aa9135b..279c3d6 100644 --- a/h2_playermanager/src/main/java/mc/core/h2db/H2PlayerDAO.java +++ b/h2_playermanager/src/main/java/mc/core/h2db/H2PlayerDAO.java @@ -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 { + 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( + 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")); } - jdbcTemplate.query(SELECT_SQL, + 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"))); - 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()); - 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") - )); - playerBuffer.setWorld(world); - } + 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); diff --git a/h2_playermanager/src/main/resources/sqls/create_tables.sql b/h2_playermanager/src/main/resources/sqls/create_tables.sql index f302295..a53b23d 100644 --- a/h2_playermanager/src/main/resources/sqls/create_tables.sql +++ b/h2_playermanager/src/main/resources/sqls/create_tables.sql @@ -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); diff --git a/h2_playermanager/src/main/resources/sqls/select_player_byId.sql b/h2_playermanager/src/main/resources/sqls/select_player_byId.sql new file mode 100644 index 0000000..0267ce6 --- /dev/null +++ b/h2_playermanager/src/main/resources/sqls/select_player_byId.sql @@ -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; \ No newline at end of file diff --git a/h2_playermanager/src/main/resources/sqls/select_player_byName.sql b/h2_playermanager/src/main/resources/sqls/select_player_byName.sql index 1a8f47a..36a9b3d 100644 --- a/h2_playermanager/src/main/resources/sqls/select_player_byName.sql +++ b/h2_playermanager/src/main/resources/sqls/select_player_byName.sql @@ -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; \ No newline at end of file diff --git a/h2_playermanager/src/test/java/mc/core/h2db/TestDAO.java b/h2_playermanager/src/test/java/mc/core/h2db/TestDAO.java index 355cfe8..4c9c094 100644 --- a/h2_playermanager/src/test/java/mc/core/h2db/TestDAO.java +++ b/h2_playermanager/src/test/java/mc/core/h2db/TestDAO.java @@ -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 diff --git a/h2_playermanager/src/test/resources/sqls/drop_table_players.sql b/h2_playermanager/src/test/resources/sqls/drop_table_players.sql new file mode 100644 index 0000000..dd4ab91 --- /dev/null +++ b/h2_playermanager/src/test/resources/sqls/drop_table_players.sql @@ -0,0 +1,3 @@ +DROP INDEX IF EXISTS idx_players_uuid; +DROP INDEX IF EXISTS idx_players_name; +DROP TABLE IF EXISTS players; \ No newline at end of file