win.doyto.query.core.JdbcDataAccess Maven / Gradle / Ivy
package win.doyto.query.core;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import win.doyto.query.entity.Persistable;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
* JdbcDataAccess
*
* @author f0rb
*/
final class JdbcDataAccess, I extends Serializable, Q> implements DataAccess {
private final JdbcOperations jdbcOperations;
private final RowMapper rowMapper;
private final CrudBuilder crudBuilder;
public JdbcDataAccess(JdbcOperations jdbcOperations, Class entityClass) {
this.jdbcOperations = jdbcOperations;
rowMapper = new BeanPropertyRowMapper<>(entityClass);
crudBuilder = new CrudBuilder<>(entityClass);
}
@Override
public final List query(Q q) {
return queryColumns(q, rowMapper, "*");
}
@Override
public final List queryColumns(Q q, RowMapper rowMapper, String... columns) {
SqlAndArgs sqlAndArgs = crudBuilder.buildSelectColumnsAndArgs(q, columns);
return jdbcOperations.query(sqlAndArgs.sql, sqlAndArgs.args, rowMapper);
}
@Override
public final long count(Q q) {
SqlAndArgs sqlAndArgs = crudBuilder.buildCountAndArgs(q);
return jdbcOperations.queryForObject(sqlAndArgs.sql, sqlAndArgs.args, Long.class);
}
@Override
public final int delete(Q q) {
return doUpdate(crudBuilder.buildDeleteAndArgs(q));
}
@Override
public final E get(I id) {
return getEntity(crudBuilder.buildSelectById(), id);
}
@Override
public final int delete(I id) {
return jdbcOperations.update(crudBuilder.buildDeleteById(), id);
}
@Override
public final E get(E e) {
return getEntity(crudBuilder.buildSelectById(e), e.getId());
}
private E getEntity(String sql, I id) {
try {
return jdbcOperations.queryForObject(sql, rowMapper, id);
} catch (DataAccessException ex) {
return null;
}
}
@Override
public final int delete(E e) {
return jdbcOperations.update(crudBuilder.buildDeleteById(e), e.getId());
}
@Override
@SuppressWarnings("unchecked")
public final void create(E e) {
List