diff --git a/Jenkinsfile b/Jenkinsfile index f602833..067a2ae 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -8,6 +8,6 @@ build('db-common-lib', 'docker-host') { javaLibPipeline = load("build_utils/jenkins_lib/pipeJavaLib.groovy") } - def buildImageTag = "b04c5291d101132e53e578d96e1628d2e6dab0c0" + def buildImageTag = "917afcdd0c0a07bf4155d597bbba72e962e1a34a" javaLibPipeline(buildImageTag) -} \ No newline at end of file +} diff --git a/build_utils b/build_utils index 5cb25f0..a7655bc 160000 --- a/build_utils +++ b/build_utils @@ -1 +1 @@ -Subproject commit 5cb25f049c719a608276a99fc4fbe852187019ca +Subproject commit a7655bc60c877a65cdfe3d9b668021d970d88a76 diff --git a/pom.xml b/pom.xml index a23359a..2c34f1a 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ jar db-common-lib - 0.0.3 + 0.0.4 db-common-lib Abstract dao and row mappers @@ -61,7 +61,7 @@ org.springframework.boot spring-boot-starter-jdbc - 2.3.0.RELEASE + 2.5.3 provided diff --git a/src/main/java/com/rbkmoney/dao/GenericDao.java b/src/main/java/com/rbkmoney/dao/GenericDao.java index 50215b9..4975351 100644 --- a/src/main/java/com/rbkmoney/dao/GenericDao.java +++ b/src/main/java/com/rbkmoney/dao/GenericDao.java @@ -12,23 +12,32 @@ public interface GenericDao { T fetchOne(Query query, Class type) throws DaoException; - T fetchOne(Query query, Class type, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException; + T fetchOne(Query query, Class type, NamedParameterJdbcTemplate namedParameterJdbcTemplate) + throws DaoException; T fetchOne(Query query, RowMapper rowMapper) throws DaoException; T fetchOne(String namedSql, SqlParameterSource parameterSource, RowMapper rowMapper) throws DaoException; - T fetchOne(Query query, RowMapper rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException; + T fetchOne(Query query, RowMapper rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) + throws DaoException; - T fetchOne(String namedSql, SqlParameterSource parameterSource, RowMapper rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException; + T fetchOne(String namedSql, + SqlParameterSource parameterSource, + RowMapper rowMapper, + NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException; List fetch(Query query, RowMapper rowMapper) throws DaoException; List fetch(String namedSql, SqlParameterSource parameterSource, RowMapper rowMapper) throws DaoException; - List fetch(Query query, RowMapper rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException; + List fetch(Query query, RowMapper rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) + throws DaoException; - List fetch(String namedSql, SqlParameterSource parameterSource, RowMapper rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException; + List fetch(String namedSql, + SqlParameterSource parameterSource, + RowMapper rowMapper, + NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException; void executeOne(Query query) throws DaoException; @@ -42,7 +51,8 @@ public interface GenericDao { int execute(Query query, int expectedRowsAffected) throws DaoException; - int execute(Query query, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException; + int execute(Query query, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate) + throws DaoException; int execute(String namedSql) throws DaoException; @@ -50,31 +60,48 @@ public interface GenericDao { int execute(String namedSql, SqlParameterSource parameterSource, int expectedRowsAffected) throws DaoException; - int execute(String namedSql, SqlParameterSource parameterSource, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate) 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(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, KeyHolder keyHolder) + throws DaoException; - int execute(String namedSql, SqlParameterSource parameterSource, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate, KeyHolder keyHolder) throws DaoException; + int execute(String namedSql, + SqlParameterSource parameterSource, + int expectedRowsAffected, + NamedParameterJdbcTemplate namedParameterJdbcTemplate, + KeyHolder keyHolder) throws DaoException; long batchExecute(List queries) throws DaoException; long batchExecute(List queries, int expectedRowsPerQueryAffected) throws DaoException; - long batchExecute(List queries, int expectedRowsPerQueryAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException; + long batchExecute(List queries, + int expectedRowsPerQueryAffected, + NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException; long batchExecute(String namedSql, List parameterSources) throws DaoException; - long batchExecute(String namedSql, List parameterSources, int expectedRowsPerQueryAffected) throws DaoException; + long batchExecute(String namedSql, List parameterSources, int expectedRowsPerQueryAffected) + throws DaoException; - long batchExecute(String namedSql, List parameterSources, int expectedRowsPerQueryAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException; + long batchExecute(String namedSql, + List parameterSources, + int expectedRowsPerQueryAffected, + NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException; } diff --git a/src/main/java/com/rbkmoney/dao/impl/AbstractGenericDao.java b/src/main/java/com/rbkmoney/dao/impl/AbstractGenericDao.java index 80ae99e..0e44edc 100644 --- a/src/main/java/com/rbkmoney/dao/impl/AbstractGenericDao.java +++ b/src/main/java/com/rbkmoney/dao/impl/AbstractGenericDao.java @@ -45,7 +45,8 @@ public abstract class AbstractGenericDao extends NamedParameterJdbcDaoSupport im } @Override - public T fetchOne(Query query, Class type, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException { + public T fetchOne(Query query, Class type, NamedParameterJdbcTemplate namedParameterJdbcTemplate) + throws DaoException { return fetchOne(query, new SingleColumnRowMapper<>(type), namedParameterJdbcTemplate); } @@ -55,17 +56,21 @@ public abstract class AbstractGenericDao extends NamedParameterJdbcDaoSupport im } @Override - public T fetchOne(String namedSql, SqlParameterSource parameterSource, RowMapper rowMapper) throws DaoException { + public T fetchOne(String namedSql, SqlParameterSource parameterSource, RowMapper rowMapper) + throws DaoException { return fetchOne(namedSql, parameterSource, rowMapper, getNamedParameterJdbcTemplate()); } @Override - public T fetchOne(Query query, RowMapper rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException { - return fetchOne(query.getSQL(ParamType.NAMED), toSqlParameterSource(query.getParams()), rowMapper, namedParameterJdbcTemplate); + public T fetchOne(Query query, RowMapper rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) + throws DaoException { + return fetchOne(query.getSQL(ParamType.NAMED), toSqlParameterSource(query.getParams()), rowMapper, + namedParameterJdbcTemplate); } @Override - public T fetchOne(String namedSql, SqlParameterSource parameterSource, RowMapper rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException { + public T fetchOne(String namedSql, SqlParameterSource parameterSource, RowMapper rowMapper, + NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException { try { return namedParameterJdbcTemplate.queryForObject( namedSql, @@ -85,17 +90,24 @@ public abstract class AbstractGenericDao extends NamedParameterJdbcDaoSupport im } @Override - public List fetch(String namedSql, SqlParameterSource parameterSource, RowMapper rowMapper) throws DaoException { + public List fetch(String namedSql, SqlParameterSource parameterSource, RowMapper rowMapper) + throws DaoException { return fetch(namedSql, parameterSource, rowMapper, getNamedParameterJdbcTemplate()); } @Override - public List fetch(Query query, RowMapper rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException { - return fetch(query.getSQL(ParamType.NAMED), toSqlParameterSource(query.getParams()), rowMapper, namedParameterJdbcTemplate); + public List fetch(Query query, RowMapper rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) + throws DaoException { + return fetch(query.getSQL(ParamType.NAMED), toSqlParameterSource(query.getParams()), rowMapper, + namedParameterJdbcTemplate); } @Override - public List fetch(String namedSql, SqlParameterSource parameterSource, RowMapper rowMapper, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException { + public List fetch( + String namedSql, + SqlParameterSource parameterSource, + RowMapper rowMapper, + NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException { try { return namedParameterJdbcTemplate.query( namedSql, @@ -123,7 +135,8 @@ public abstract class AbstractGenericDao extends NamedParameterJdbcDaoSupport im } @Override - public void executeOne(String namedSql, SqlParameterSource parameterSource, KeyHolder keyHolder) throws DaoException { + public void executeOne(String namedSql, SqlParameterSource parameterSource, KeyHolder keyHolder) + throws DaoException { execute(namedSql, parameterSource, 1, keyHolder); } @@ -138,8 +151,10 @@ public abstract class AbstractGenericDao extends NamedParameterJdbcDaoSupport im } @Override - public int execute(Query query, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException { - return execute(query.getSQL(ParamType.NAMED), toSqlParameterSource(query.getParams()), expectedRowsAffected, namedParameterJdbcTemplate); + public int execute(Query query, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate) + throws DaoException { + return execute(query.getSQL(ParamType.NAMED), toSqlParameterSource(query.getParams()), expectedRowsAffected, + namedParameterJdbcTemplate); } @Override @@ -153,19 +168,22 @@ public abstract class AbstractGenericDao extends NamedParameterJdbcDaoSupport im } @Override - public int execute(String namedSql, SqlParameterSource parameterSource, int expectedRowsAffected) throws DaoException { + 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 { + 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); + throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(namedSql, expectedRowsAffected, + rowsAffected); } return rowsAffected; } catch (NestedRuntimeException ex) { @@ -180,12 +198,15 @@ public abstract class AbstractGenericDao extends NamedParameterJdbcDaoSupport im @Override public int execute(Query query, int expectedRowsAffected, KeyHolder keyHolder) throws DaoException { - return execute(query.getSQL(ParamType.NAMED), toSqlParameterSource(query.getParams()), expectedRowsAffected, getNamedParameterJdbcTemplate(), keyHolder); + 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); + 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 @@ -199,12 +220,14 @@ public abstract class AbstractGenericDao extends NamedParameterJdbcDaoSupport im } @Override - public int execute(String namedSql, SqlParameterSource parameterSource, int expectedRowsAffected, KeyHolder keyHolder) throws DaoException { + 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 { + public int execute(String namedSql, SqlParameterSource parameterSource, int expectedRowsAffected, + NamedParameterJdbcTemplate namedParameterJdbcTemplate, KeyHolder keyHolder) throws DaoException { try { int rowsAffected = namedParameterJdbcTemplate.update( namedSql, @@ -212,7 +235,8 @@ public abstract class AbstractGenericDao extends NamedParameterJdbcDaoSupport im keyHolder); if (expectedRowsAffected != -1 && rowsAffected != expectedRowsAffected) { - throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(namedSql, expectedRowsAffected, rowsAffected); + throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(namedSql, expectedRowsAffected, + rowsAffected); } return rowsAffected; } catch (NestedRuntimeException ex) { @@ -231,14 +255,17 @@ public abstract class AbstractGenericDao extends NamedParameterJdbcDaoSupport im } @Override - public long batchExecute(List queries, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException { + public long batchExecute(List queries, + int expectedRowsAffected, + NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException { AtomicLong affectedRowCounter = new AtomicLong(); queries.stream() .collect( Collectors.groupingBy( query -> query.getSQL(ParamType.NAMED), LinkedHashMap::new, - Collectors.mapping(query -> toSqlParameterSource(query.getParams()), Collectors.toList()) + Collectors + .mapping(query -> toSqlParameterSource(query.getParams()), Collectors.toList()) ) ) .forEach( @@ -261,17 +288,21 @@ public abstract class AbstractGenericDao extends NamedParameterJdbcDaoSupport im } @Override - public long batchExecute(String namedSql, List parameterSources, int expectedRowsAffected) throws DaoException { + public long batchExecute(String namedSql, List parameterSources, int expectedRowsAffected) + throws DaoException { return batchExecute(namedSql, parameterSources, expectedRowsAffected, getNamedParameterJdbcTemplate()); } @Override - public long batchExecute(String namedSql, List parameterSources, int expectedRowsAffected, NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException { + public long batchExecute(String namedSql, List parameterSources, int expectedRowsAffected, + NamedParameterJdbcTemplate namedParameterJdbcTemplate) throws DaoException { try { - int[] rowsPerBatchAffected = namedParameterJdbcTemplate.batchUpdate(namedSql, parameterSources.toArray(new SqlParameterSource[0])); + int[] rowsPerBatchAffected = namedParameterJdbcTemplate + .batchUpdate(namedSql, parameterSources.toArray(new SqlParameterSource[0])); if (rowsPerBatchAffected.length != parameterSources.size()) { - throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(namedSql, parameterSources.size(), rowsPerBatchAffected.length); + throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(namedSql, parameterSources.size(), + rowsPerBatchAffected.length); } int count = 0; diff --git a/src/main/java/com/rbkmoney/mapper/RecordRowMapper.java b/src/main/java/com/rbkmoney/mapper/RecordRowMapper.java index 913b746..c63f55b 100644 --- a/src/main/java/com/rbkmoney/mapper/RecordRowMapper.java +++ b/src/main/java/com/rbkmoney/mapper/RecordRowMapper.java @@ -32,9 +32,11 @@ public class RecordRowMapper implements RowMapper { String columnName = rsMetaData.getColumnName(column); Field field = record.field(columnName); - Object value = getFieldValue(field, resultSet); - if (!resultSet.wasNull()) { - record.set(field, value); + if (field != null) { + Object value = getFieldValue(field, resultSet); + if (!resultSet.wasNull()) { + record.set(field, value); + } } } return record.into(type); diff --git a/test/java/com/rbkmoney/dao/impl/AbstractGenericDaoTest.java b/test/java/com/rbkmoney/dao/impl/AbstractGenericDaoTest.java index 2113393..962fb87 100644 --- a/test/java/com/rbkmoney/dao/impl/AbstractGenericDaoTest.java +++ b/test/java/com/rbkmoney/dao/impl/AbstractGenericDaoTest.java @@ -33,8 +33,9 @@ public class AbstractGenericDaoTest { @Test public void batchExecuteTest() { final NamedParameterJdbcTemplate namedParameterJdbcTemplateMock = mock(NamedParameterJdbcTemplate.class); - int[] rowPerBatchAffected = { 1, 1 }; - when(namedParameterJdbcTemplateMock.batchUpdate(anyString(), any(SqlParameterSource[].class))).thenReturn(rowPerBatchAffected); + int[] rowPerBatchAffected = {1, 1}; + when(namedParameterJdbcTemplateMock.batchUpdate(anyString(), any(SqlParameterSource[].class))) + .thenReturn(rowPerBatchAffected); when(testDaoSpy.getNamedParameterJdbcTemplate()).thenReturn(namedParameterJdbcTemplateMock); final Map> firstParamMap = paramMapMock("testString", "testValue", String.class); @@ -59,8 +60,9 @@ public class AbstractGenericDaoTest { @Test(expected = DaoException.class) public void batchExceptionTest() { final NamedParameterJdbcTemplate namedParameterJdbcTemplateMock = mock(NamedParameterJdbcTemplate.class); - int[] rowPerBatchAffected = { 1 }; - when(namedParameterJdbcTemplateMock.batchUpdate(anyString(), any(SqlParameterSource[].class))).thenReturn(rowPerBatchAffected); + int[] rowPerBatchAffected = {1}; + when(namedParameterJdbcTemplateMock.batchUpdate(anyString(), any(SqlParameterSource[].class))) + .thenReturn(rowPerBatchAffected); when(testDaoSpy.getNamedParameterJdbcTemplate()).thenReturn(namedParameterJdbcTemplateMock); final Map> firstParamMap = paramMapMock("testString", "testValue", String.class);