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단계] 디노(신종화) 미션 제출합니다. #350

Merged
merged 7 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
37 changes: 19 additions & 18 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;

public class JdbcTemplate {

Expand All @@ -23,54 +24,54 @@ public JdbcTemplate(final DataSource dataSource) {

public void execute(final String sql, final Object... objects) {
try (final Connection conn = dataSource.getConnection();
final PreparedStatement pstmt = conn.prepareStatement(sql)
final PreparedStatement pstmt = preparedStatementAndSetValue(conn, sql, objects)
) {
log.debug("query : {}", sql);
setValues(pstmt, objects);
pstmt.executeUpdate();
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
throw new DataAccessException(e.getMessage());
Copy link

@hanueleee hanueleee Oct 4, 2023

Choose a reason for hiding this comment

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

지금 당장 크게 중요한 부분은 아니지만..!!

예외를 전환할 때에는 기존예외를 포함해주는게 좋다고 합니다!!
기존 예외를 포함해야 추후에 에러에 대한 로그를 찍을 때 기존 예외의 스택 트레이스를 확인할 수 있기 때문입니당 (Caused by 부분)

image

현재 뼈대코드에서 제공되는 DataAccessException에는 Throwable cause를 파라미터로 받는 생성자도 열려있어서 얘를 사용하면 좋을 것 같아요 ㅎㅎ

Copy link
Member Author

Choose a reason for hiding this comment

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

Caused by에 저런 식으로 나올거라고 생각하지 못했네요.. e를 담아서 던지도록 변경했습니다.!

c62edf6

}
}

public <T> List<T> query(final String sql, final RowMapper<T> rowMapper, final Object... objects) {
try (final Connection conn = dataSource.getConnection();
final PreparedStatement pstmt = conn.prepareStatement(sql)
final ResultSet rs = preparedStatementAndSetValue(conn, sql, objects).executeQuery()
) {
log.debug("query : {}", sql);
setValues(pstmt, objects);
final ResultSet rs = pstmt.executeQuery();
final List<T> list = new ArrayList<>();
while (rs.next()) {
list.add(rowMapper.mapRow(rs));
}
return list;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
throw new DataAccessException(e.getMessage());
}
}

@Nullable
public <T> T queryForObject(final String sql, final RowMapper<T> rowMapper, final Object... objects) {
try (final Connection conn = dataSource.getConnection();
final PreparedStatement pstmt = conn.prepareStatement(sql)
final ResultSet rs = preparedStatementAndSetValue(conn, sql, objects).executeQuery()

Choose a reason for hiding this comment

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

Suggested change
final ResultSet rs = preparedStatementAndSetValue(conn, sql, objects).executeQuery()
final PreparedStatement pstmt = preparedStatementAndSetValue(conn, sql, objects)
final ResultSet rs = pstmt.executeQuery()

조그마한 제안입니다 ㅎㅎ
한 번 끊어주는 것은 어떨까요?!

Copy link
Member Author

Choose a reason for hiding this comment

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

아주 좋습니다ㅎㅎ

) {
log.debug("query : {}", sql);
setValues(pstmt, objects);
final ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return rowMapper.mapRow(rs);
}
return null;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
throw new DataAccessException(e.getMessage());
}
}

private void setValues(final PreparedStatement pstmt, Object... objects) throws SQLException {
private PreparedStatement preparedStatementAndSetValue(
final Connection conn,
final String sql,
final Object... objects
) throws SQLException {
final PreparedStatement pstmt = conn.prepareStatement(sql);
log.debug("query : {}", sql);
setValues(pstmt, objects);
return pstmt;
}

private void setValues(final PreparedStatement pstmt, final Object... objects) throws SQLException {
for (int i = 0; i < objects.length; i++) {
pstmt.setObject(i + 1, objects[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;

@FunctionalInterface
public interface RowMapper<T> {

T mapRow(final ResultSet rs) throws SQLException;
Expand Down
Loading