Search in sources :

Example 41 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project spring-data-jdbc by spring-projects.

the class JdbcQueryLookupStrategyUnitTests method typeBasedRowMapperGetsUsedForQuery.

// DATAJDBC-166
@Test
@SuppressWarnings("unchecked")
void typeBasedRowMapperGetsUsedForQuery() {
    RowMapper<? extends NumberFormat> numberFormatMapper = mock(RowMapper.class);
    QueryMappingConfiguration mappingConfiguration = new DefaultQueryMappingConfiguration().registerRowMapper(NumberFormat.class, numberFormatMapper);
    RepositoryQuery repositoryQuery = getRepositoryQuery("returningNumberFormat", mappingConfiguration);
    repositoryQuery.execute(new Object[] {});
    verify(operations).queryForObject(anyString(), any(SqlParameterSource.class), any(RowMapper.class));
}
Also used : RepositoryQuery(org.springframework.data.repository.query.RepositoryQuery) QueryMappingConfiguration(org.springframework.data.jdbc.repository.QueryMappingConfiguration) DefaultQueryMappingConfiguration(org.springframework.data.jdbc.repository.config.DefaultQueryMappingConfiguration) SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) DefaultQueryMappingConfiguration(org.springframework.data.jdbc.repository.config.DefaultQueryMappingConfiguration) RowMapper(org.springframework.jdbc.core.RowMapper) Test(org.junit.jupiter.api.Test)

Example 42 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project spring-data-jdbc by spring-projects.

the class ConfigurableRowMapperMapUnitTests method prefersLatestRegistrationForSuperTypeMatch.

// DATAJDBC-166
@Test
public void prefersLatestRegistrationForSuperTypeMatch() {
    RowMapper rowMapper = mock(RowMapper.class);
    QueryMappingConfiguration map = // 
    new DefaultQueryMappingConfiguration().registerRowMapper(Integer.class, // 
    mock(RowMapper.class)).registerRowMapper(Number.class, rowMapper);
    assertThat(map.getRowMapper(Object.class)).isEqualTo(rowMapper);
}
Also used : QueryMappingConfiguration(org.springframework.data.jdbc.repository.QueryMappingConfiguration) RowMapper(org.springframework.jdbc.core.RowMapper) Test(org.junit.jupiter.api.Test)

Example 43 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project spring-data-jdbc by spring-projects.

the class ConfigurableRowMapperMapUnitTests method returnsConfiguredInstanceForClass.

// DATAJDBC-166
@Test
public void returnsConfiguredInstanceForClass() {
    RowMapper rowMapper = mock(RowMapper.class);
    QueryMappingConfiguration map = new DefaultQueryMappingConfiguration().registerRowMapper(Object.class, rowMapper);
    assertThat(map.getRowMapper(Object.class)).isEqualTo(rowMapper);
}
Also used : QueryMappingConfiguration(org.springframework.data.jdbc.repository.QueryMappingConfiguration) RowMapper(org.springframework.jdbc.core.RowMapper) Test(org.junit.jupiter.api.Test)

Example 44 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project iaf by ibissource.

the class Storage method getMetadata.

@Override
public List<List<Object>> getMetadata(int maxNumberOfRecords, final List<String> metadataNames, List<String> searchValues, int metadataValueType) throws StorageException {
    // According to SimpleDateFormat javadoc it needs to be synchronised
    // when accessed by multiple threads, hence instantiate it here instead
    // of instantiating it at class level and synchronising it.
    final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIMESTAMP_PATTERN);
    List<String> rangeSearchValues = new ArrayList<String>();
    List<String> regexSearchValues = new ArrayList<String>();
    for (int i = 0; i < searchValues.size(); i++) {
        String searchValue = searchValues.get(i);
        if (searchValue != null && searchValue.startsWith("<") && searchValue.endsWith(">")) {
            rangeSearchValues.add(searchValue);
            regexSearchValues.add(null);
            searchValues.remove(i);
            searchValues.add(i, null);
        } else if (searchValue != null && searchValue.startsWith("(") && searchValue.endsWith(")")) {
            rangeSearchValues.add(null);
            regexSearchValues.add(searchValue);
            searchValues.remove(i);
            searchValues.add(i, null);
        } else {
            rangeSearchValues.add(null);
            regexSearchValues.add(null);
        }
    }
    IDbmsSupport dbmsSupport = getDbmsSupport();
    StringBuilder query = new StringBuilder("select " + dbmsSupport.provideFirstRowsHintAfterFirstKeyword(maxNumberOfRecords) + " * from (select ");
    List<Object> args = new ArrayList<Object>();
    boolean first = true;
    for (String metadataName : metadataNames) {
        if (first) {
            first = false;
        } else {
            query.append(", ");
        }
        if (bigValueColumns.contains(metadataName)) {
            query.append("substr(" + metadataName + ", 1, 100)");
        } else {
            query.append(metadataName);
        }
    }
    String rowNumber = dbmsSupport.getRowNumber(metadataNames.get(0), "desc");
    if (StringUtils.isNotEmpty(rowNumber)) {
        if (first) {
            first = false;
        } else {
            query.append(", ");
        }
        query.append(rowNumber);
    }
    query.append(" from " + table);
    for (int i = 0; i < rangeSearchValues.size(); i++) {
        String searchValue = rangeSearchValues.get(i);
        if (searchValue != null) {
            int j = searchValue.indexOf('|');
            if (j != -1) {
                String column = metadataNames.get(i);
                String searchValueLeft = searchValue.substring(1, j);
                String searchValueRight = searchValue.substring(j + 1, searchValue.length() - 1);
                if (StringUtils.isNotEmpty(searchValueLeft)) {
                    if (integerColumns.contains(column)) {
                        addNumberExpression(query, args, column, ">=", searchValueLeft);
                    } else if (timestampColumns.contains(column)) {
                        addTimestampExpression(query, args, column, ">=", searchValueLeft, simpleDateFormat);
                    }
                }
                if (StringUtils.isNotEmpty(searchValueRight)) {
                    if (integerColumns.contains(column)) {
                        addNumberExpression(query, args, column, "<=", searchValueRight);
                    } else if (timestampColumns.contains(column)) {
                        addTimestampExpression(query, args, column, "<=", searchValueRight, simpleDateFormat);
                    }
                }
            } else {
                throw new StorageException("Separator | not found");
            }
        }
    }
    for (int i = 0; i < searchValues.size(); i++) {
        String searchValue = searchValues.get(i);
        if (StringUtils.isNotEmpty(searchValue)) {
            String column = metadataNames.get(i);
            if (integerColumns.contains(column)) {
                addNumberExpression(query, args, column, "<=", searchValue);
            } else if (timestampColumns.contains(column)) {
                addTimestampExpression(query, args, column, "<=", searchValue, simpleDateFormat);
            } else if (fixedStringColumns != null && fixedStringColumns.contains(column)) {
                addFixedStringExpression(query, args, column, searchValue);
            } else {
                addLikeExpression(query, args, column, searchValue);
            }
        }
    }
    query.append(")");
    if (StringUtils.isNotEmpty(rowNumber)) {
        query.append(" where " + dbmsSupport.getRowNumberShortName() + " < ?");
        args.add(maxNumberOfRecords + 1);
    }
    query.append(" order by ");
    query.append(metadataNames.get(0) + " desc");
    log.debug("Metadata query: " + query.toString());
    List metadata;
    try {
        metadata = jdbcTemplate.query(query.toString(), args.toArray(), new RowMapper() {

            public List mapRow(ResultSet rs, int rowNum) throws SQLException {
                List row = new ArrayList();
                for (int i = 0; i < metadataNames.size(); i++) {
                    if (integerColumns.contains(metadataNames.get(i))) {
                        row.add(rs.getInt(i + 1));
                    } else if (timestampColumns.contains(metadataNames.get(i))) {
                        row.add(simpleDateFormat.format(rs.getTimestamp(i + 1)));
                    } else {
                        row.add(getValue(rs, i + 1));
                    }
                }
                return row;
            }
        });
    } catch (DataAccessException e) {
        throw new StorageException("Could not read metadata", e);
    }
    for (int i = 0; i < metadata.size(); i++) {
        if (!SearchUtil.matches((List) metadata.get(i), regexSearchValues)) {
            metadata.remove(i);
            i--;
        }
    }
    return metadata;
}
Also used : ArrayList(java.util.ArrayList) Checkpoint(nl.nn.testtool.Checkpoint) IDbmsSupport(nl.nn.adapterframework.jdbc.dbms.IDbmsSupport) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat) StorageException(nl.nn.testtool.storage.StorageException) DataAccessException(org.springframework.dao.DataAccessException) RowMapper(org.springframework.jdbc.core.RowMapper)

Example 45 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project topcom-cloud by 545314690.

the class RowMappers method companyVORowMapper.

/**
 * 企业RowMapper
 * @return
 */
public static RowMapper<CompanyVO> companyVORowMapper() {
    return new RowMapper<CompanyVO>() {

        @Override
        public CompanyVO mapRow(ResultSet resultSet, int i) throws SQLException {
            CompanyVO company = new CompanyVO();
            company.setId(resultSet.getLong("id"));
            company.setCompanyName(resultSet.getString("companyName"));
            company.setLng(resultSet.getDouble("lng"));
            company.setLat(resultSet.getDouble("lat"));
            return company;
        }
    };
}
Also used : ResultSet(java.sql.ResultSet) CompanyVO(com.topcom.tjs.vo.CompanyVO) RowMapper(org.springframework.jdbc.core.RowMapper)

Aggregations

RowMapper (org.springframework.jdbc.core.RowMapper)51 ResultSet (java.sql.ResultSet)27 SQLException (java.sql.SQLException)16 List (java.util.List)14 Named (javax.inject.Named)8 Singleton (javax.inject.Singleton)8 Test (org.junit.jupiter.api.Test)8 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)8 Guid (org.ovirt.engine.core.compat.Guid)7 QueryMappingConfiguration (org.springframework.data.jdbc.repository.QueryMappingConfiguration)7 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Tags (org.ovirt.engine.core.common.businessentities.Tags)5 TagsTemplateMap (org.ovirt.engine.core.common.businessentities.TagsTemplateMap)5 TagsType (org.ovirt.engine.core.common.businessentities.TagsType)5 TagsUserGroupMap (org.ovirt.engine.core.common.businessentities.TagsUserGroupMap)5 TagsUserMap (org.ovirt.engine.core.common.businessentities.TagsUserMap)5 TagsVdsMap (org.ovirt.engine.core.common.businessentities.TagsVdsMap)5 TagsVmMap (org.ovirt.engine.core.common.businessentities.TagsVmMap)5 DataAccessException (org.springframework.dao.DataAccessException)5