Search in sources :

Example 11 with SqlParameterValue

use of org.springframework.jdbc.core.SqlParameterValue in project spring-framework by spring-projects.

the class NamedParameterJdbcTemplateTests method testQueryWithResultSetExtractor.

@Test
public void testQueryWithResultSetExtractor() throws SQLException {
    given(resultSet.next()).willReturn(true);
    given(resultSet.getInt("id")).willReturn(1);
    given(resultSet.getString("forename")).willReturn("rod");
    params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
    params.put("country", "UK");
    Customer cust = namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params, new ResultSetExtractor<Customer>() {

        @Override
        public Customer extractData(ResultSet rs) throws SQLException, DataAccessException {
            rs.next();
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            return cust;
        }
    });
    assertTrue("Customer id was assigned correctly", cust.getId() == 1);
    assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
    verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
    verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
    verify(preparedStatement).setString(2, "UK");
    verify(preparedStatement).close();
    verify(connection).close();
}
Also used : SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) Customer(org.springframework.jdbc.Customer) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 12 with SqlParameterValue

use of org.springframework.jdbc.core.SqlParameterValue in project spring-framework by spring-projects.

the class NamedParameterJdbcTemplateTests method testQueryWithRowCallbackHandler.

@Test
public void testQueryWithRowCallbackHandler() throws SQLException {
    given(resultSet.next()).willReturn(true, false);
    given(resultSet.getInt("id")).willReturn(1);
    given(resultSet.getString("forename")).willReturn("rod");
    params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
    params.put("country", "UK");
    final List<Customer> customers = new LinkedList<>();
    namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params, new RowCallbackHandler() {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            customers.add(cust);
        }
    });
    assertEquals(1, customers.size());
    assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
    assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
    verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
    verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
    verify(preparedStatement).setString(2, "UK");
    verify(preparedStatement).close();
    verify(connection).close();
}
Also used : SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) Customer(org.springframework.jdbc.Customer) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 13 with SqlParameterValue

use of org.springframework.jdbc.core.SqlParameterValue in project spring-framework by spring-projects.

the class NamedParameterJdbcTemplateTests method testUpdateWithTypedParameters.

@Test
public void testUpdateWithTypedParameters() throws SQLException {
    given(preparedStatement.executeUpdate()).willReturn(1);
    params.put("perfId", new SqlParameterValue(Types.DECIMAL, 1));
    params.put("priceId", new SqlParameterValue(Types.INTEGER, 1));
    int rowsAffected = namedParameterTemplate.update(UPDATE_NAMED_PARAMETERS, params);
    assertEquals(1, rowsAffected);
    verify(connection).prepareStatement(UPDATE_NAMED_PARAMETERS_PARSED);
    verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
    verify(preparedStatement).setObject(2, 1, Types.INTEGER);
    verify(preparedStatement).close();
    verify(connection).close();
}
Also used : SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) Test(org.junit.Test)

Example 14 with SqlParameterValue

use of org.springframework.jdbc.core.SqlParameterValue in project metacat by Netflix.

the class MySqlTagService method delete.

@Override
public void delete(final QualifiedName name, final boolean updateUserMetadata) {
    try {
        jdbcTemplate.update(SQL_DELETE_TAG_ITEM_TAGS_BY_NAME, new SqlParameterValue(Types.VARCHAR, name.toString()));
        jdbcTemplate.update(SQL_DELETE_TAG_ITEM, new SqlParameterValue(Types.VARCHAR, name.toString()));
        if (updateUserMetadata) {
            // Set the tags in user metadata
            final Map<String, Set<String>> data = Maps.newHashMap();
            data.put(NAME_TAGS, Sets.newHashSet());
            userMetadataService.saveDefinitionMetadata(name, "admin", Optional.of(metacatJson.toJsonObject(data)), true);
        }
    } catch (Exception e) {
        final String message = String.format("Failed to delete all tags for name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) Set(java.util.Set) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 15 with SqlParameterValue

use of org.springframework.jdbc.core.SqlParameterValue in project metacat by Netflix.

the class MysqlUserMetadataService method searchByOwners.

@Override
@Transactional(readOnly = true)
public List<QualifiedName> searchByOwners(final Set<String> owners) {
    final List<QualifiedName> result = Lists.newArrayList();
    final StringBuilder query = new StringBuilder(SQL.SEARCH_DEFINITION_METADATA_NAMES);
    final List<SqlParameterValue> paramList = Lists.newArrayList();
    query.append(" where 1=0");
    owners.forEach(s -> {
        query.append(" or data like ?");
        paramList.add(new SqlParameterValue(Types.VARCHAR, "%\"userId\":\"" + s.trim() + "\"%"));
    });
    final SqlParameterValue[] params = new SqlParameterValue[paramList.size()];
    try {
        // Handler for reading the result set
        final ResultSetExtractor<Void> handler = rs -> {
            while (rs.next()) {
                final String definitionName = rs.getString("name");
                result.add(QualifiedName.fromString(definitionName, false));
            }
            return null;
        };
        jdbcTemplate.query(query.toString(), paramList.toArray(params), handler);
    } catch (Exception e) {
        log.error("Failed to search by owners", e);
        throw new UserMetadataServiceException("Failed to search by owners", e);
    }
    return result;
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) HasDataMetadata(com.netflix.metacat.common.dto.HasDataMetadata) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DefinitionMetadataDto(com.netflix.metacat.common.dto.DefinitionMetadataDto) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) Map(java.util.Map) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) Config(com.netflix.metacat.common.server.properties.Config) BaseUserMetadataService(com.netflix.metacat.common.server.usermetadata.BaseUserMetadataService) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) InvalidMetadataException(com.netflix.metacat.common.server.connectors.exception.InvalidMetadataException) MetacatJson(com.netflix.metacat.common.json.MetacatJson) HasDefinitionMetadata(com.netflix.metacat.common.dto.HasDefinitionMetadata) Set(java.util.Set) QualifiedName(com.netflix.metacat.common.QualifiedName) Maps(com.google.common.collect.Maps) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) Collectors(java.util.stream.Collectors) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) HasMetadata(com.netflix.metacat.common.dto.HasMetadata) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Data(lombok.Data) GetMetadataInterceptorParameters(com.netflix.metacat.common.server.usermetadata.GetMetadataInterceptorParameters) MetadataInterceptor(com.netflix.metacat.common.server.usermetadata.MetadataInterceptor) Optional(java.util.Optional) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) Joiner(com.google.common.base.Joiner) Transactional(org.springframework.transaction.annotation.Transactional) Types(java.sql.Types) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) QualifiedName(com.netflix.metacat.common.QualifiedName) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) InvalidMetadataException(com.netflix.metacat.common.server.connectors.exception.InvalidMetadataException) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

SqlParameterValue (org.springframework.jdbc.core.SqlParameterValue)19 Test (org.junit.Test)8 SQLException (java.sql.SQLException)7 Map (java.util.Map)6 Lists (com.google.common.collect.Lists)5 QualifiedName (com.netflix.metacat.common.QualifiedName)5 Types (java.sql.Types)5 List (java.util.List)5 Set (java.util.Set)5 Slf4j (lombok.extern.slf4j.Slf4j)5 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)5 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)5 Transactional (org.springframework.transaction.annotation.Transactional)5 Joiner (com.google.common.base.Joiner)4 Maps (com.google.common.collect.Maps)4 ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)4 UserMetadataServiceException (com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)4 Date (java.util.Date)4 Collectors (java.util.stream.Collectors)4 ResultSetExtractor (org.springframework.jdbc.core.ResultSetExtractor)4