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단계] 허브(방대의) 미션 제출합니다. #358

Merged
merged 8 commits into from
Oct 2, 2023

Conversation

greeng00se
Copy link
Member

안녕하세요 호이 😃
추석 잘 보내셨나요?

꼼꼼하게 남겨주신 리뷰 잘 반영했습니다! 후후

JdbcTemplate은 다음과 같이 구현해보았습니다!

graph LR
  JdbcTemplate --> DataSource
  JdbcTemplate --> StatementCreator
  JdbcTemplate --> StatementExecutor
Loading

PreparedStatementCallback: prepareStatement가 어떤 행동을 할지 전달하는 콜백 인터페이스입니다.
StatementCreator: connection, sql문, 파라미터를 받아 PreparedStatement를 생성해주는 책임을 가지도록 했습니다!
StatementExecutor: PreparedStatementCallback 전달 용도로 preparedStatement와 rowMapper를 받아 실행 후 결과를 매핑해주는 책임을 가지도록 했습니다!

이전 코드와 다르게 중복되는 부분을 제거하기 위해 다음과 같이 하나의 메서드를 사용하도록 했습니다!

private <T> T query(
        final String sql,
        final PreparedStatementCallback<T> preparedStatementCallback,
        final Object... parameters
) {
    try (final Connection connection = dataSource.getConnection();
         final PreparedStatement preparedStatement = statementCreator.create(connection, sql, parameters)) {
        return preparedStatementCallback.execute(preparedStatement);
    } catch (final SQLException e) {
        log.error(e.getMessage(), e);
        throw new DataAccessException(e);
    }
}

2단계도 리뷰 잘 부탁드립니다~~~

Copy link

@This2sho This2sho left a comment

Choose a reason for hiding this comment

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

역시 허브신 🌿 대단하십니다.🙇‍♂️
1단계 제출하신걸로도 충분히 2단계에서 리펙터링할 부분이 없다고 생각했는데..
이렇게 또 깔끔한 코드를 보여주시다니 많이 배웠습니다! 👍

QueryForObject에서 Optional로 감싸주는 부분만 한번 확인해주시면 감사하겠습니다!
이 부분은 다음 단계에서 반영하셔도 충분할 거 같아서 바로 어프로브 하겠습니다 🌟

// given
final Connection connection = mock(Connection.class);
final String sql = "select * from woowacourse where name = ? and course = ?";
final Object[] parameters = {"호이", "backend"};
Copy link

Choose a reason for hiding this comment

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

😱

log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
return query(sql, statement -> statementExecutor.execute(statement, rowMapper), parameters);
Copy link

Choose a reason for hiding this comment

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

이것이 Template Callback 패턴인가유?!

Copy link
Member Author

Choose a reason for hiding this comment

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

그렇지 않을까유!? 😄

}
return preparedStatement;
return Optional.ofNullable(results.iterator().next());
Copy link

Choose a reason for hiding this comment

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

여기서 results의 개수가 0개이면 next()로 가져올 때, NoSuchElementException이 발생하지 않을까유??

Copy link
Member Author

Choose a reason for hiding this comment

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

그렇네요😢 비었을 때 테스트를 작성하지 않아서 몰랐습니다 ㅠㅠ
3단계 제출하면서 get을 사용하는 방향으로 수정해보겠습니다!

final String sql,
final PreparedStatementCallback<T> preparedStatementCallback,
final Object... parameters
) {
Copy link

Choose a reason for hiding this comment

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

깔끔 하네유 👍

@This2sho This2sho merged commit 4717bbb into woowacourse:greeng00se Oct 2, 2023
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants