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 라이브러리 구현하기 - 1단계] 로지(윤가영) 미션 제출합니다. #281

Merged
merged 3 commits into from
Sep 30, 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
12 changes: 10 additions & 2 deletions app/src/test/java/com/techcourse/dao/UserDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.techcourse.config.DataSourceConfig;
import com.techcourse.domain.User;
import com.techcourse.support.jdbc.init.DatabasePopulatorUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
Expand All @@ -12,16 +13,22 @@
class UserDaoTest {

private UserDao userDao;
private JdbcTemplate jdbcTemplate;

@BeforeEach
void setup() {
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance());

userDao = new UserDao(new JdbcTemplate(DataSourceConfig.getInstance()));
jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance());
userDao = new UserDao(jdbcTemplate);
final var user = new User("gugu", "password", "[email protected]");
userDao.insert(user);
}

@AfterEach
void tearDown() {
jdbcTemplate.execute("truncate table users restart identity");
}

@Test
void findAll() {
final var users = userDao.findAll();
Expand Down Expand Up @@ -67,4 +74,5 @@ void update() {

assertThat(actual.getPassword()).isEqualTo(newPassword);
}

}
65 changes: 14 additions & 51 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ public JdbcTemplate(final DataSource dataSource) {
}

public void execute(String sql, Object... parameters) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);

try (final Connection conn = dataSource.getConnection();
final PreparedStatement pstmt = conn.prepareStatement(sql)) {
Comment on lines +25 to +26
Copy link
Member

Choose a reason for hiding this comment

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

try-with-resource 도 적용해주셨군요!👍

log.debug("query : {}", sql);

for (int i = 0; i < parameters.length; i++) {
Expand All @@ -38,72 +34,39 @@ public void execute(String sql, Object... parameters) {
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException ignored) {}

try {
if (conn != null) {
conn.close();
}
} catch (SQLException ignored) {}
}
}

public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... params) {
List<T> query = query(sql, rowMapper, params);
if (query.size() > 1) {
throw new RuntimeException("too many result");
List<T> results = query(sql, rowMapper, params);
if (results.size() > 1) {
throw new RuntimeException("too many result. expected 1 but was " + results.size());
Copy link
Member

Choose a reason for hiding this comment

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

예외처리 꼼꼼하게 하시네요👍

}
if (query.isEmpty()) {
return null;
if (results.isEmpty()) {
throw new RuntimeException("no result");
}
return query.get(0);
return results.get(0);
}
Copy link
Member

Choose a reason for hiding this comment

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

변수명도 변경해주셨군요


public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... parameters) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
try (final Connection conn = dataSource.getConnection();
final PreparedStatement pstmt = conn.prepareStatement(sql)) {
for (int i = 0; i < parameters.length; i++) {
pstmt.setObject(i + 1, parameters[i]);
}
rs = pstmt.executeQuery();
ResultSet rs = pstmt.executeQuery();

log.debug("query : {}", sql);

final List<T> results = new ArrayList<>();
if (rs.next()) {
results.add(rowMapper.mapRow(rs, 0));
while (rs.next()) {
results.add(rowMapper.mapRow(rs, rs.getRow()));
}
return results;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException ignored) {}

try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException ignored) {}

try {
if (conn != null) {
conn.close();
}
} catch (SQLException ignored) {}
}
}

}
Loading