Skip to content

Commit

Permalink
refactor: prepare statement 추상화
Browse files Browse the repository at this point in the history
  • Loading branch information
drunkenhw committed Sep 27, 2023
1 parent e320796 commit f033beb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 36 deletions.
75 changes: 47 additions & 28 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,72 @@ public JdbcTemplate(final DataSource dataSource) {
}

public void update(String sql, Object... args) {
try (
Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = getPreparedStatement(sql, connection, args);
) {
log.debug("query : {}", sql);
preparedStatement.executeUpdate();
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
log.debug("query : {}", sql);
execute(sql, new PreparedStatementExecutor<>() {
@Override
public Object fetchData(ResultSet resultSet) throws SQLException {
return null;
}

@Override
public ResultSet fetchResultSet(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.execute();
return null;
}
}, args);
}

public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) {
log.debug("query : {}", sql);

return execute(sql, (resultSet) -> {
if (resultSet.next()) {
return rowMapper.mapRow(resultSet);
return execute(sql, new PreparedStatementExecutor<T>() {
@Override
public T fetchData(ResultSet resultSet) throws SQLException {
if (resultSet.next()) {
return rowMapper.mapRow(resultSet);
}
return null;
}

@Override
public ResultSet fetchResultSet(PreparedStatement preparedStatement) throws SQLException {
return preparedStatement.executeQuery();
}
return null;
}, args);
}

private <T> T execute(String sql, ResultSetExecutor<T> resultSetExecutor, Object... args) {
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) {
log.debug("query : {}", sql);
return execute(sql, new PreparedStatementExecutor<>() {
@Override
public List<T> fetchData(ResultSet resultSet) throws SQLException {
List<T> results = new ArrayList<>();
while (resultSet.next()) {
results.add(rowMapper.mapRow(resultSet));
}
return results;
}

@Override
public ResultSet fetchResultSet(PreparedStatement preparedStatement) throws SQLException {
return preparedStatement.executeQuery();
}
}, args);
}

private <T> T execute(String sql, PreparedStatementExecutor<T> preparedStatementExecutor, Object... args) {
try (
Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = getPreparedStatement(sql, connection, args);
ResultSet rs = preparedStatement.executeQuery();
ResultSet rs = preparedStatementExecutor.fetchResultSet(preparedStatement);
) {
return resultSetExecutor.execute(rs);
return preparedStatementExecutor.fetchData(rs);
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}

public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) {
log.debug("query : {}", sql);
return execute(sql, (resultSet) -> {
List<T> results = new ArrayList<>();
while (resultSet.next()) {
results.add(rowMapper.mapRow(resultSet));
}
return results;
}, args);
}

private PreparedStatement getPreparedStatement(String sql, Connection connection, Object... args) throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
setValues(preparedStatement, args);
Expand Down
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.ResultSet;
import java.sql.SQLException;

public interface PreparedStatementExecutor<T> {
T fetchData(ResultSet resultSet) throws SQLException;

ResultSet fetchResultSet(PreparedStatement preparedStatement) throws SQLException;
}

This file was deleted.

0 comments on commit f033beb

Please sign in to comment.