-
Notifications
You must be signed in to change notification settings - Fork 300
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
[JDBC 라이브러리 구현하기 - 2단계] 제이미(임정수) 미션 제출합니다. #432
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
47d7909
refactor: rowmapper를 컬럼명을 통해 추출하도록 수정
JJ503 4b80842
refactor: 메서드 static 제거
JJ503 23e8180
feat: queryForObject 결과가 하나를 초과할 때 예외 처리 추가
JJ503 00768b5
refactor: 잘못된 예외 처리 제거
JJ503 f2d7474
refactor: 중복된 코드 제거
JJ503 18b497a
feat: queryForObject 예외 처리 추가
JJ503 f9889f7
refactor: 개행 제거
JJ503 1861cfe
refactor: 불필요한 변수 제거
JJ503 eec4c97
refactor: 필요없는 코드 제거
JJ503 73d0bfb
test: 테스트 격리를 통한 테스트 실패 해결 및 테스트 추가
JJ503 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,16 +5,30 @@ | |
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.exception.EmptyResultDataAccessException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class UserDaoTest { | ||
|
||
private static final String TRUNCATE_USER_TABLE_SQL = "DROP TABLE IF EXISTS users;" + | ||
"create table if not exists users ( " + | ||
" id bigint auto_increment," + | ||
" account varchar(100) not null," + | ||
" password varchar(100) not null," + | ||
" email varchar(100) not null," + | ||
" primary key(id)" + | ||
");"; | ||
|
||
private UserDao userDao; | ||
|
||
@BeforeEach | ||
void setup() { | ||
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); | ||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance()); | ||
jdbcTemplate.execute(TRUNCATE_USER_TABLE_SQL); | ||
|
||
userDao = new UserDao(DataSourceConfig.getInstance()); | ||
final var user = new User("gugu", "password", "[email protected]"); | ||
|
@@ -35,6 +49,13 @@ void findById() { | |
assertThat(user.getAccount()).isEqualTo("gugu"); | ||
} | ||
|
||
@Test | ||
void findByIdException() { | ||
assertThatThrownBy(() -> userDao.findById(999L)) | ||
.isInstanceOf(EmptyResultDataAccessException.class) | ||
.hasMessage("조회된 결과가 존재하지 않습니다."); | ||
} | ||
|
||
@Test | ||
void findByAccount() { | ||
final var account = "gugu"; | ||
|
@@ -43,6 +64,14 @@ void findByAccount() { | |
assertThat(user.getAccount()).isEqualTo(account); | ||
} | ||
|
||
@Test | ||
void findByAccountException() { | ||
final var account = "jamie"; | ||
assertThatThrownBy(() -> userDao.findByAccount(account)) | ||
.isInstanceOf(EmptyResultDataAccessException.class) | ||
.hasMessage("조회된 결과가 존재하지 않습니다."); | ||
} | ||
|
||
@Test | ||
void insert() { | ||
final var account = "insert-gugu"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
package org.springframework.jdbc.core; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.jdbc.core.exception.EmptyResultDataAccessException; | ||
import org.springframework.jdbc.core.exception.IncorrectResultSizeDataAccessException; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
@@ -13,83 +12,63 @@ | |
|
||
public class JdbcTemplate { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(JdbcTemplate.class); | ||
public static final int MAX_RESULT_SIZE = 1; | ||
public static final int FIRST_INDEX = 0; | ||
|
||
private final DataSource dataSource; | ||
private final PreparedStatementExecutor preparedStatementExecutor; | ||
|
||
public JdbcTemplate(final DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
this.preparedStatementExecutor = new PreparedStatementExecutor(dataSource); | ||
} | ||
|
||
public int update(final String sql, final Object... args) { | ||
try ( | ||
final Connection conn = dataSource.getConnection(); | ||
final PreparedStatement pstmt = conn.prepareStatement(sql) | ||
) { | ||
setArguments(pstmt, args); | ||
|
||
log.debug("query : {}", sql); | ||
|
||
return pstmt.executeUpdate(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return preparedStatementExecutor.execute(PreparedStatement::executeUpdate, sql, args); | ||
} | ||
|
||
private static void setArguments(final PreparedStatement pstmt, final Object[] args) throws SQLException { | ||
for (int i = 0; i < args.length; i++) { | ||
final int parameterIndex = i + 1; | ||
pstmt.setObject(parameterIndex, args[i]); | ||
} | ||
public <T> T queryForObject(final String sql, final RowMapper<T> rowMapper, final Object... args) { | ||
return preparedStatementExecutor.execute( | ||
pstmt -> { | ||
final ResultSet rs = pstmt.executeQuery(); | ||
final List<T> results = getResults(rowMapper, rs); | ||
|
||
return getSingleResult(results); | ||
}, sql, args | ||
Comment on lines
+30
to
+35
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. 익명 함수를 잘 사용해주셨네요 ! |
||
); | ||
} | ||
|
||
public <T> T queryForObject(final String sql, final RowMapper<T> rowMapper, final Object... args) { | ||
try ( | ||
final Connection conn = dataSource.getConnection(); | ||
final PreparedStatement pstmt = conn.prepareStatement(sql) | ||
) { | ||
setArguments(pstmt, args); | ||
ResultSet rs = pstmt.executeQuery(); | ||
|
||
log.debug("query : {}", sql); | ||
|
||
return calculateResult(rowMapper, rs); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
private <T> List<T> getResults(final RowMapper<T> rowMapper, final ResultSet rs) throws SQLException { | ||
final List<T> results = new ArrayList<>(); | ||
|
||
while (rs.next()) { | ||
final T result = rowMapper.mapRow(rs); | ||
results.add(result); | ||
} | ||
|
||
return results; | ||
} | ||
|
||
private static <T> T calculateResult(final RowMapper<T> rowMapper, final ResultSet rs) throws SQLException { | ||
if (rs.next()) { | ||
return rowMapper.mapRow(rs, rs.getRow()); | ||
private <T> T getSingleResult(final List<T> results) { | ||
if (results.isEmpty()) { | ||
throw new EmptyResultDataAccessException("조회된 결과가 존재하지 않습니다."); | ||
} | ||
if (results.size() > MAX_RESULT_SIZE) { | ||
throw new IncorrectResultSizeDataAccessException("조회된 결과의 개수가 적절하지 않습니다."); | ||
} | ||
|
||
return null; | ||
return results.get(FIRST_INDEX); | ||
} | ||
|
||
public <T> List<T> query(final String sql, final RowMapper<T> rowMapper) { | ||
try ( | ||
final Connection conn = dataSource.getConnection(); | ||
final PreparedStatement pstmt = conn.prepareStatement(sql) | ||
) { | ||
ResultSet rs = pstmt.executeQuery(); | ||
|
||
log.debug("query : {}", sql); | ||
return preparedStatementExecutor.execute( | ||
pstmt -> { | ||
final ResultSet rs = pstmt.executeQuery(); | ||
|
||
return getResults(rowMapper, rs); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return getResults(rowMapper, rs); | ||
}, sql | ||
); | ||
} | ||
|
||
private static <T> List<T> getResults(final RowMapper<T> rowMapper, final ResultSet rs) throws SQLException { | ||
List<T> results = new ArrayList<>(); | ||
|
||
while (rs.next()) { | ||
final T result = calculateResult(rowMapper, rs); | ||
results.add(result); | ||
} | ||
|
||
return results; | ||
public boolean execute(final String sql) { | ||
return preparedStatementExecutor.execute(PreparedStatement::execute, sql); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementExecuteStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.springframework.jdbc.core; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
@FunctionalInterface | ||
public interface PreparedStatementExecuteStrategy<T> { | ||
|
||
T strategy(final PreparedStatement pstmt) throws SQLException; | ||
} |
42 changes: 42 additions & 0 deletions
42
jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.springframework.jdbc.core; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
public class PreparedStatementExecutor { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(JdbcTemplate.class); | ||
|
||
private final DataSource dataSource; | ||
|
||
public PreparedStatementExecutor(final DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public <T> T execute(final PreparedStatementExecuteStrategy<T> executeStrategy, final String sql, final Object... args) { | ||
try ( | ||
final Connection conn = dataSource.getConnection(); | ||
final PreparedStatement pstmt = conn.prepareStatement(sql) | ||
) { | ||
setArguments(pstmt, args); | ||
|
||
log.debug("query : {}", sql); | ||
|
||
return executeStrategy.strategy(pstmt); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void setArguments(final PreparedStatement pstmt, final Object[] args) throws SQLException { | ||
for (int i = 0; i < args.length; i++) { | ||
final int parameterIndex = i + 1; | ||
pstmt.setObject(parameterIndex, args[i]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...src/main/java/org/springframework/jdbc/core/exception/EmptyResultDataAccessException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.springframework.jdbc.core.exception; | ||
|
||
import org.springframework.dao.DataAccessException; | ||
|
||
public class EmptyResultDataAccessException extends DataAccessException { | ||
|
||
public EmptyResultDataAccessException(final String message) { | ||
super(message); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
.../java/org/springframework/jdbc/core/exception/IncorrectResultSizeDataAccessException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.springframework.jdbc.core.exception; | ||
|
||
import org.springframework.dao.DataAccessException; | ||
|
||
public class IncorrectResultSizeDataAccessException extends DataAccessException { | ||
|
||
public IncorrectResultSizeDataAccessException(final String message) { | ||
super(message); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
jdbc/src/main/java/org/springframework/jdbc/core/exception/ResultSetOverflowException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.springframework.jdbc.core.exception; | ||
|
||
public class ResultSetOverflowException extends IllegalArgumentException { | ||
|
||
public ResultSetOverflowException(final String message) { | ||
super(message); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍