add abstract dao

This commit is contained in:
vbanin 2019-04-24 15:40:01 +03:00
parent 60e3e3ec18
commit cb982e1816
5 changed files with 417 additions and 4 deletions

29
pom.xml
View File

@ -10,15 +10,15 @@
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging> <packaging>jar</packaging>
<groupId>com.rbkmoney.db.common.lib</groupId>
<artifactId>db-common-lib</artifactId> <artifactId>db-common-lib</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<description></description>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jooq.version>3.11.5</jooq.version>
<geck.common.version>0.6.9</geck.common.version>
</properties> </properties>
<dependencyManagement> <dependencyManagement>
@ -31,4 +31,25 @@
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>1.5.10.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>${jooq.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.rbkmoney.geck</groupId>
<artifactId>common</artifactId>
<version>${geck.common.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project> </project>

View File

@ -0,0 +1,24 @@
package com.rbkmoney.dao;
public class DaoException extends Exception {
public DaoException() {
}
public DaoException(String message) {
super(message);
}
public DaoException(String message, Throwable cause) {
super(message, cause);
}
public DaoException(Throwable cause) {
super(cause);
}
public DaoException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@ -0,0 +1,69 @@
package com.rbkmoney.dao;
import org.jooq.Query;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.KeyHolder;
import java.util.List;
public interface GenericDao {
<T> T fetchOne(Query query, Class<T> type) throws DaoException;
<T> T fetchOne(Query query, Class<T> type, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException;
<T> T fetchOne(Query query, RowMapper<T> rowMapper) throws DaoException;
<T> T fetchOne(String namedSql, SqlParameterSource parameterSource, RowMapper<T> rowMapper) throws DaoException;
<T> T fetchOne(Query query, RowMapper<T> rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException;
<T> T fetchOne(String namedSql, SqlParameterSource parameterSource, RowMapper<T> rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException;
<T> List<T> fetch(Query query, RowMapper<T> rowMapper) throws DaoException;
<T> List<T> fetch(String namedSql, SqlParameterSource parameterSource, RowMapper<T> rowMapper) throws DaoException;
<T> List<T> fetch(Query query, RowMapper<T> rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException;
<T> List<T> fetch(String namedSql, SqlParameterSource parameterSource, RowMapper<T> rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException;
void executeOne(Query query) throws DaoException;
void executeOne(String namedSql, SqlParameterSource parameterSource) throws DaoException;
void executeOne(Query query, KeyHolder keyHolder) throws DaoException;
void executeOne(String namedSql, SqlParameterSource parameterSource, KeyHolder keyHolder) throws DaoException;
int execute(Query query) throws DaoException;
int execute(Query query, int expectedRowsAffected) throws DaoException;
int execute(Query query, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException;
int execute(String namedSql) throws DaoException;
int execute(String namedSql, SqlParameterSource parameterSource) throws DaoException;
int execute(String namedSql, SqlParameterSource parameterSource, int expectedRowsAffected) throws DaoException;
int execute(String namedSql, SqlParameterSource parameterSource, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException;
int execute(Query query, KeyHolder keyHolder) throws DaoException;
int execute(Query query, int expectedRowsAffected, KeyHolder keyHolder) throws DaoException;
int execute(Query query, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate, KeyHolder keyHolder) throws DaoException;
int execute(String namedSql, KeyHolder keyHolder) throws DaoException;
int execute(String namedSql, SqlParameterSource parameterSource, KeyHolder keyHolder) throws DaoException;
int execute(String namedSql, SqlParameterSource parameterSource, int expectedRowsAffected, KeyHolder keyHolder) throws DaoException;
int execute(String namedSql, SqlParameterSource parameterSource, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate, KeyHolder keyHolder) throws DaoException;
}

View File

@ -0,0 +1,247 @@
package com.rbkmoney.dao.impl;
import com.rbkmoney.dao.DaoException;
import com.rbkmoney.dao.GenericDao;
import org.jooq.*;
import org.jooq.conf.ParamType;
import org.jooq.impl.DSL;
import org.jooq.impl.DefaultConfiguration;
import org.springframework.core.NestedRuntimeException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SingleColumnRowMapper;
import org.springframework.jdbc.core.namedparam.*;
import org.springframework.jdbc.support.KeyHolder;
import javax.sql.DataSource;
import java.sql.Types;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public abstract class AbstractGenericDao extends NamedParameterJdbcDaoSupport implements GenericDao {
private final DSLContext dslContext;
public AbstractGenericDao(DataSource dataSource) {
setDataSource(dataSource);
Configuration configuration = new DefaultConfiguration();
configuration.set(SQLDialect.POSTGRES);
this.dslContext = DSL.using(configuration);
}
protected DSLContext getDslContext() {
return dslContext;
}
@Override
public <T> T fetchOne(Query query, Class<T> type) throws DaoException {
return fetchOne(query, type, getNamedParameterJdbcTemplate());
}
@Override
public <T> T fetchOne(Query query, Class<T> type, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException {
return fetchOne(query, new SingleColumnRowMapper<>(type), namedParameterJdbcTemplate);
}
@Override
public <T> T fetchOne(Query query, RowMapper<T> rowMapper) throws DaoException {
return fetchOne(query, rowMapper, getNamedParameterJdbcTemplate());
}
@Override
public <T> T fetchOne(String namedSql, SqlParameterSource parameterSource, RowMapper<T> rowMapper) throws DaoException {
return fetchOne(namedSql, parameterSource, rowMapper, getNamedParameterJdbcTemplate());
}
@Override
public <T> T fetchOne(Query query, RowMapper<T> rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException {
return fetchOne(query.getSQL(ParamType.NAMED), toSqlParameterSource(query.getParams()), rowMapper, namedParameterJdbcTemplate);
}
@Override
public <T> T fetchOne(String namedSql, SqlParameterSource parameterSource, RowMapper<T> rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException {
try {
return namedParameterJdbcTemplate.queryForObject(
namedSql,
parameterSource,
rowMapper
);
} catch (EmptyResultDataAccessException ex) {
return null;
} catch (NestedRuntimeException ex) {
throw new DaoException(ex);
}
}
@Override
public <T> List<T> fetch(Query query, RowMapper<T> rowMapper) throws DaoException {
return fetch(query, rowMapper, getNamedParameterJdbcTemplate());
}
@Override
public <T> List<T> fetch(String namedSql, SqlParameterSource parameterSource, RowMapper<T> rowMapper) throws DaoException {
return fetch(namedSql, parameterSource, rowMapper, getNamedParameterJdbcTemplate());
}
@Override
public <T> List<T> fetch(Query query, RowMapper<T> rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException {
return fetch(query.getSQL(ParamType.NAMED), toSqlParameterSource(query.getParams()), rowMapper, namedParameterJdbcTemplate);
}
@Override
public <T> List<T> fetch(String namedSql, SqlParameterSource parameterSource, RowMapper<T> rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException {
try {
return namedParameterJdbcTemplate.query(
namedSql,
parameterSource,
rowMapper
);
} catch (NestedRuntimeException e) {
throw new DaoException(e);
}
}
@Override
public void executeOne(Query query) throws DaoException {
execute(query, 1);
}
@Override
public void executeOne(String namedSql, SqlParameterSource parameterSource) throws DaoException {
execute(namedSql, parameterSource, 1);
}
@Override
public void executeOne(Query query, KeyHolder keyHolder) throws DaoException {
executeOne(query.getSQL(ParamType.NAMED), toSqlParameterSource(query.getParams()), keyHolder);
}
@Override
public void executeOne(String namedSql, SqlParameterSource parameterSource, KeyHolder keyHolder) throws DaoException {
execute(namedSql, parameterSource, 1, keyHolder);
}
@Override
public int execute(Query query) throws DaoException {
return execute(query, -1);
}
@Override
public int execute(Query query, int expectedRowsAffected) throws DaoException {
return execute(query, expectedRowsAffected, getNamedParameterJdbcTemplate());
}
@Override
public int execute(Query query, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException {
return execute(query.getSQL(ParamType.NAMED), toSqlParameterSource(query.getParams()), expectedRowsAffected, namedParameterJdbcTemplate);
}
@Override
public int execute(String namedSql) throws DaoException {
return execute(namedSql, EmptySqlParameterSource.INSTANCE);
}
@Override
public int execute(String namedSql, SqlParameterSource parameterSource) throws DaoException {
return execute(namedSql, parameterSource, -1);
}
@Override
public int execute(String namedSql, SqlParameterSource parameterSource, int expectedRowsAffected) throws DaoException {
return execute(namedSql, parameterSource, expectedRowsAffected, getNamedParameterJdbcTemplate());
}
@Override
public int execute(String namedSql, SqlParameterSource parameterSource, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException {
try {
int rowsAffected = namedParameterJdbcTemplate.update(
namedSql,
parameterSource);
if (expectedRowsAffected != -1 && rowsAffected != expectedRowsAffected) {
throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(namedSql, expectedRowsAffected, rowsAffected);
}
return rowsAffected;
} catch (NestedRuntimeException ex) {
throw new DaoException(ex);
}
}
@Override
public int execute(Query query, KeyHolder keyHolder) throws DaoException {
return execute(query.getSQL(ParamType.NAMED), toSqlParameterSource(query.getParams()), -1, keyHolder);
}
@Override
public int execute(Query query, int expectedRowsAffected, KeyHolder keyHolder) throws DaoException {
return execute(query.getSQL(ParamType.NAMED), toSqlParameterSource(query.getParams()), expectedRowsAffected, getNamedParameterJdbcTemplate(), keyHolder);
}
@Override
public int execute(Query query, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate, KeyHolder keyHolder) throws DaoException {
return execute(query.getSQL(ParamType.NAMED), toSqlParameterSource(query.getParams()), expectedRowsAffected, namedParameterJdbcTemplate, keyHolder);
}
@Override
public int execute(String namedSql, KeyHolder keyHolder) throws DaoException {
return execute(namedSql, EmptySqlParameterSource.INSTANCE, keyHolder);
}
@Override
public int execute(String namedSql, SqlParameterSource parameterSource, KeyHolder keyHolder) throws DaoException {
return execute(namedSql, parameterSource, -1, keyHolder);
}
@Override
public int execute(String namedSql, SqlParameterSource parameterSource, int expectedRowsAffected, KeyHolder keyHolder) throws DaoException {
return execute(namedSql, parameterSource, expectedRowsAffected, getNamedParameterJdbcTemplate(), keyHolder);
}
@Override
public int execute(String namedSql, SqlParameterSource parameterSource, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate, KeyHolder keyHolder) throws DaoException {
try {
int rowsAffected = namedParameterJdbcTemplate.update(
namedSql,
parameterSource,
keyHolder);
if (expectedRowsAffected != -1 && rowsAffected != expectedRowsAffected) {
throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(namedSql, expectedRowsAffected, rowsAffected);
}
return rowsAffected;
} catch (NestedRuntimeException ex) {
throw new DaoException(ex);
}
}
protected Condition appendDateTimeRangeConditions(Condition condition,
Field<LocalDateTime> field,
Optional<LocalDateTime> fromTime,
Optional<LocalDateTime> toTime) {
if (fromTime.isPresent()) {
condition = condition.and(field.ge(fromTime.get()));
}
if (toTime.isPresent()) {
condition = condition.and(field.lt(toTime.get()));
}
return condition;
}
protected SqlParameterSource toSqlParameterSource(Map<String, Param<?>> params) {
MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource();
for (Map.Entry<String, Param<?>> entry : params.entrySet()) {
Param<?> param = entry.getValue();
if (param.getValue() instanceof LocalDateTime || param.getValue() instanceof EnumType) {
sqlParameterSource.addValue(entry.getKey(), param.getValue(), Types.OTHER);
} else {
sqlParameterSource.addValue(entry.getKey(), param.getValue());
}
}
return sqlParameterSource;
}
}

View File

@ -0,0 +1,52 @@
package com.rbkmoney.mapper;
import com.rbkmoney.geck.common.util.TypeUtil;
import org.jooq.Field;
import org.jooq.Table;
import org.jooq.TableRecord;
import org.jooq.impl.TableRecordImpl;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class RecordRowMapper<T> implements RowMapper<T> {
private final Table table;
private final Class<T> type;
public RecordRowMapper(Table table, Class<T> type) {
this.table = table;
this.type = type;
}
@Override
public T mapRow(ResultSet resultSet, int i) throws SQLException {
ResultSetMetaData rsMetaData = resultSet.getMetaData();
int columnCount = rsMetaData.getColumnCount();
TableRecord record = new TableRecordImpl(table);
for (int column = 1; column <= columnCount; column++) {
String columnName = rsMetaData.getColumnName(column);
Field field = record.field(columnName);
Object value = getFieldValue(field, resultSet);
if (!resultSet.wasNull()) {
record.set(field, value);
}
}
return record.into(type);
}
private Object getFieldValue(Field field, ResultSet resultSet) throws SQLException {
if (field.getDataType().isBinary()) {
return resultSet.getBytes(field.getName());
}
if (field.getType().isEnum()) {
return TypeUtil.toEnumField(resultSet.getString(field.getName()), field.getType());
}
return resultSet.getObject(field.getName(), field.getType());
}
}