-
Notifications
You must be signed in to change notification settings - Fork 3
[4단계 - DB 적용] 이유빈 미션 제출합니다 #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: leeshin20
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package chess.dao; | ||
|
|
||
| import chess.board.Board; | ||
| import chess.utils.DatabaseUtil; | ||
| import chess.piece.Team; | ||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
|
|
||
| public class ChessGameDao { | ||
| public void createNewRoom() { | ||
| String query = "INSERT INTO game (boardState, turn, status) VALUES (?, ?, ?)"; | ||
| Board board = new Board(); | ||
|
|
||
| try (Connection conn = DatabaseUtil.getConnection(); PreparedStatement pstmt = conn.prepareStatement(query)) { | ||
| pstmt.setString(1, board.convertBoardStateToString()); | ||
| pstmt.setBoolean(2, true); | ||
| pstmt.setString(3, "ready"); | ||
| pstmt.executeUpdate(); | ||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| public int selectRoom(int gameId) { | ||
| String query = "SELECT status FROM game WHERE gameId = ?"; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 보드 상태를 하나의 문자열로 쭉 이어서 저장하는 방법이 너무 참신하네요ㅋㅋㅋ 이 방식에는 다른 방식과 비교했을 때 어떤 장단점이 있을까요? |
||
|
|
||
| try (Connection conn = DatabaseUtil.getConnection(); | ||
| PreparedStatement pstmt = conn.prepareStatement(query)) { | ||
| pstmt.setInt(1, gameId); // gameId를 쿼리에 설정 | ||
| try (ResultSet rs = pstmt.executeQuery()) { | ||
| if (rs.next()) { | ||
| return gameId; | ||
| } | ||
| } | ||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| throw new IllegalStateException("존재하지 않는 방 번호입니다."); | ||
| } | ||
|
|
||
| public void saveGame(int gameId, String boardState, boolean turn, String status) { | ||
| String query = "INSERT INTO game (gameId, boardState, turn, status) VALUES (?, ?, ?, ?) " + "ON DUPLICATE KEY UPDATE boardState = VALUES(boardState), turn = VALUES(turn), status = VALUES(status)"; | ||
|
|
||
|
Comment on lines
+44
to
+45
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| try (Connection conn = DatabaseUtil.getConnection(); PreparedStatement pstmt = conn.prepareStatement(query)) { | ||
| pstmt.setInt(1, gameId); | ||
| pstmt.setString(2, boardState); | ||
| pstmt.setBoolean(3, turn); | ||
| pstmt.setString(4, status); | ||
| pstmt.executeUpdate(); | ||
|
Comment on lines
+46
to
+51
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. preparedStatement 사용 👍👍 |
||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| public void updateCurrentBoard(int gameId, Board board) { | ||
| String query = "SELECT boardState, status FROM game WHERE gameId = ?"; | ||
| try (Connection conn = DatabaseUtil.getConnection(); PreparedStatement pstmt = conn.prepareStatement(query)) { | ||
| pstmt.setInt(1, gameId); | ||
| try (ResultSet rs = pstmt.executeQuery()) { | ||
| if (rs.next()) { | ||
| String boardState = rs.getString("boardState"); | ||
| board.loadBoardState(boardState); | ||
| } | ||
| } | ||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| public Team getCurrentTurn(int gameId, Team turn) { | ||
| String query = "SELECT turn FROM game WHERE gameID = ?"; | ||
| try (Connection conn = DatabaseUtil.getConnection(); | ||
| PreparedStatement pstmt = conn.prepareStatement(query)) { | ||
| pstmt.setInt(1, gameId); | ||
| try (ResultSet rs = pstmt.executeQuery()) { | ||
| if (rs.next()) { | ||
| return turn.getTurnByBinary(rs.getInt("turn")); | ||
| } | ||
| } | ||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| return turn; | ||
| } | ||
|
|
||
| public String getCurrentStatus(int gameId) { | ||
| String query = "SELECT status FROM game WHERE gameId = ?"; | ||
|
|
||
| try (Connection conn = DatabaseUtil.getConnection(); | ||
| PreparedStatement pstmt = conn.prepareStatement(query)) { | ||
| pstmt.setInt(1, gameId); | ||
| try (ResultSet rs = pstmt.executeQuery()) { | ||
| if (rs.next()) { | ||
| return rs.getString("status"); | ||
| } | ||
| } | ||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| throw new IllegalStateException("존재하지 않는 방입니다."); | ||
| } | ||
|
|
||
| public void changeStatus(int gameId) { | ||
| String query = "UPDATE game SET status = ? WHERE gameId = ?"; | ||
| try (Connection conn = DatabaseUtil.getConnection(); | ||
| PreparedStatement pstmt = conn.prepareStatement(query)) { | ||
| pstmt.setString(1, "end"); | ||
| pstmt.setInt(2, gameId); | ||
| pstmt.executeUpdate(); | ||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,4 +15,9 @@ public Team changeTurn() { | |
| } | ||
| return BLACK; | ||
| } | ||
|
|
||
| public Team getTurnByBinary(int n) { | ||
| if (n == 0) return Team.BLACK; | ||
| return Team.WHITE; | ||
| } | ||
|
Comment on lines
+19
to
+22
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게 데이터베이스와 직접 소통하는 기능에 대한 테스트는 왜 없나요?
어떻게 테스트할 수 있을까요?