Skip to content

Commit

Permalink
refactor: 가독성을 위해 구현체 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
drunkenhw committed Sep 29, 2023
1 parent 903f8c1 commit b776e9c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 63 deletions.
87 changes: 27 additions & 60 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,69 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class JdbcTemplate {

private static final Logger log = LoggerFactory.getLogger(JdbcTemplate.class);
private static Logger log = LoggerFactory.getLogger(JdbcTemplate.class);

private final DataSource dataSource;

public JdbcTemplate(final DataSource dataSource) {
public JdbcTemplate(DataSource dataSource) {
this.dataSource = dataSource;
}

public void update(String sql, Object... args) {
log.debug("query : {}", sql);
execute(sql, new PreparedStatementExecutor<>() {
@Override
public Object fetchData(ResultSet resultSet) {
return null;
}

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

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

return Optional.ofNullable(execute(sql, new PreparedStatementExecutor<>() {
@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();
}
}, args));
public int update(String sql, Object... args) {
return execute(sql, PreparedStatement::executeUpdate, 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;
}
return execute(sql, statement -> fetchData(statement, new RowMapResultSetExecutor<>(rowMapper)), args);
}

@Override
public ResultSet fetchResultSet(PreparedStatement preparedStatement) throws SQLException {
return preparedStatement.executeQuery();
}
}, args);
public <T> Optional<T> queryForObject(String sql, RowMapper<T> rowMapper, Object... args) {
List<T> results = query(sql, rowMapper, args);
if (results.size() > 1) {
throw new DataAccessException();
}
return Optional.ofNullable(results.get(0));
}

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

private PreparedStatement getPreparedStatement(String sql, Connection connection, Object... args) throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
setValues(preparedStatement, args);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
return preparedStatement;
}

private void setValues(PreparedStatement preparedStatement, Object... args) throws SQLException {
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
private <T> T fetchData(PreparedStatement statement, ResultSetExecutor<T> executor) {
try (ResultSet resultSet = statement.executeQuery()) {
return executor.extractData(resultSet);
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new DataAccessException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
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;
T fetchData(PreparedStatement statement) throws SQLException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.springframework.jdbc.core;

import java.sql.ResultSet;
import java.sql.SQLException;

public interface ResultSetExecutor<T> {
T extractData(ResultSet resultSet) throws SQLException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.springframework.jdbc.core;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RowMapResultSetExecutor<T> implements ResultSetExecutor<List<T>> {

private final RowMapper<T> rowMapper;

public RowMapResultSetExecutor(RowMapper<T> rowMapper) {
this.rowMapper = rowMapper;
}

@Override
public List<T> extractData(final ResultSet resultSet) throws SQLException {
final List<T> results = new ArrayList<>();
while (resultSet.next()) {
results.add(rowMapper.mapRow(resultSet));
}
return results;
}
}

0 comments on commit b776e9c

Please sign in to comment.