Skip to content
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, 3단계] 히이로(문제웅) 미션 제출합니다. #455

Merged
merged 10 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 23 additions & 26 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public JdbcTemplate(final DataSource dataSource) {
this.dataSource = dataSource;
}

public int update(String sql, Object... args) {
public int update(final String sql, Object... args) {
try (
Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)
Expand All @@ -39,7 +39,7 @@ public int update(String sql, Object... args) {
}
}

public int update(Connection conn, String sql, Object... args) {
public int update(final Connection conn, final String sql, Object... args) {
try (
PreparedStatement pstmt = conn.prepareStatement(sql)
) {
Expand All @@ -55,48 +55,45 @@ public int update(Connection conn, String sql, Object... args) {
}
}

public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) {
try (
Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)
) {
log.debug("query : {}", sql);
public <T> T queryForObject(final String sql, final RowMapper<T> rowMapper, Object... args) {
log.debug("query : {}", sql);

ArgumentPreparedStatementSetter pss = new ArgumentPreparedStatementSetter(args);
pss.setValues(pstmt);
PreparedStatementCallback<T> callback = pstmt -> {
List<T> results = extractResultSet(pstmt.executeQuery(), rowMapper);

if (results.size() == 1) {
return results.get(0);
}
throw new IncorrectResultSizeDataAccessException(results.size());
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw SQLExceptionTranslator.translate(sql, e);
};
return executeQuery(callback, sql, args);
}

public <T> List<T> query(final String sql, final RowMapper<T> rowMapper, Object... args) {
PreparedStatementCallback<List<T>> callBack = pstmt ->
extractResultSet(pstmt.executeQuery(), rowMapper);
return executeQuery(callBack, sql, args);
}

private <T> List<T> extractResultSet(final ResultSet rs, final RowMapper<T> rowMapper) throws SQLException {
List<T> results = new ArrayList<>();
int rowNum = 0;
while (rs.next()) {
results.add(rowMapper.mapRow(rs, rowNum++));
}
return results;
}

public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) {
private <T> T executeQuery(final PreparedStatementCallback<T> action, final String sql, Object... args) {
try (
Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)
) {
ArgumentPreparedStatementSetter pss = new ArgumentPreparedStatementSetter(args);
pss.setValues(pstmt);

return extractResultSet(pstmt.executeQuery(), rowMapper);
return action.doInPreparedStatement(pstmt);
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw SQLExceptionTranslator.translate(sql, e);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

깔끔하게 분리가 된거 같아요!👍👍


private <T> List<T> extractResultSet(ResultSet rs, RowMapper<T> rowMapper) throws SQLException {
List<T> results = new ArrayList<>();
int rowNum = 0;
while (rs.next()) {
results.add(rowMapper.mapRow(rs, rowNum++));
}
return results;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.springframework.jdbc.core;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.dao.DataAccessException;

@FunctionalInterface
public interface PreparedStatementCallback<T> {

T doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 여기서 DataAccessException을 throws를 해주시는 이유가 있으신가요?
상위에서 이를 이용하는 execute 메소드는 현재 SQLException을 catch해서 DataAccessException으로 변경하는 역할을 합니다. 즉, 현재 doInPreparedStatement는 SQLException만 발생해서 저는 DataAccessException을 throws를 할 필요가 없다고 생각하는데 히이로는 어떻게 생각하시나요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 제가 확실히 생각하지 못한 부분이었네요! 현재는 아코가 말씀해주신 것처럼 SQLException만 외부로 throw되도록 하는 것이 더 올바른 방향인 것 같습니다. 반영했습니다~!

}