Search in sources :

Example 6 with MapSqlParameterSource

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

the class SimpleJdbcCallTests method testAddInvoiceProcWithMetaDataUsingMapParamSource.

@Test
public void testAddInvoiceProcWithMetaDataUsingMapParamSource() throws Exception {
    initializeAddInvoiceWithMetaData(false);
    SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice");
    Number newId = adder.executeObject(Number.class, new MapSqlParameterSource().addValue("amount", 1103).addValue("custid", 3));
    assertEquals(4, newId.intValue());
    verifyAddInvoiceWithMetaData(false);
    verify(connection, atLeastOnce()).close();
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Test(org.junit.Test)

Example 7 with MapSqlParameterSource

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

the class SimpleJdbcCallTests method testAddInvoiceFuncWithoutMetaDataUsingMapParamSource.

@Test
public void testAddInvoiceFuncWithoutMetaDataUsingMapParamSource() throws Exception {
    initializeAddInvoiceWithoutMetaData(true);
    SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice");
    adder.declareParameters(new SqlOutParameter("return", Types.INTEGER), new SqlParameter("amount", Types.INTEGER), new SqlParameter("custid", Types.INTEGER));
    Number newId = adder.executeFunction(Number.class, new MapSqlParameterSource().addValue("amount", 1103).addValue("custid", 3));
    assertEquals(4, newId.intValue());
    verifyAddInvoiceWithoutMetaData(true);
    verify(connection, atLeastOnce()).close();
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) SqlOutParameter(org.springframework.jdbc.core.SqlOutParameter) Test(org.junit.Test)

Example 8 with MapSqlParameterSource

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

the class TableMetaDataContextTests method testMatchInParametersAndSqlTypeInfoWrapping.

@Test
public void testMatchInParametersAndSqlTypeInfoWrapping() throws Exception {
    final String TABLE = "customers";
    final String USER = "me";
    ResultSet metaDataResultSet = mock(ResultSet.class);
    given(metaDataResultSet.next()).willReturn(true, false);
    given(metaDataResultSet.getString("TABLE_SCHEM")).willReturn(USER);
    given(metaDataResultSet.getString("TABLE_NAME")).willReturn(TABLE);
    given(metaDataResultSet.getString("TABLE_TYPE")).willReturn("TABLE");
    ResultSet columnsResultSet = mock(ResultSet.class);
    given(columnsResultSet.next()).willReturn(true, true, true, true, false);
    given(columnsResultSet.getString("COLUMN_NAME")).willReturn("id", "name", "customersince", "version");
    given(columnsResultSet.getInt("DATA_TYPE")).willReturn(Types.INTEGER, Types.VARCHAR, Types.DATE, Types.NUMERIC);
    given(columnsResultSet.getBoolean("NULLABLE")).willReturn(false, true, true, false);
    given(databaseMetaData.getDatabaseProductName()).willReturn("MyDB");
    given(databaseMetaData.getDatabaseProductName()).willReturn("1.0");
    given(databaseMetaData.getUserName()).willReturn(USER);
    given(databaseMetaData.storesLowerCaseIdentifiers()).willReturn(true);
    given(databaseMetaData.getTables(null, null, TABLE, null)).willReturn(metaDataResultSet);
    given(databaseMetaData.getColumns(null, USER, TABLE, null)).willReturn(columnsResultSet);
    MapSqlParameterSource map = new MapSqlParameterSource();
    map.addValue("id", 1);
    map.addValue("name", "Sven");
    map.addValue("customersince", new Date());
    map.addValue("version", 0);
    map.registerSqlType("customersince", Types.DATE);
    map.registerSqlType("version", Types.NUMERIC);
    context.setTableName(TABLE);
    context.processMetaData(dataSource, new ArrayList<>(), new String[] {});
    List<Object> values = context.matchInParameterValuesWithInsertColumns(map);
    assertEquals("wrong number of parameters: ", 4, values.size());
    assertTrue("id not wrapped with type info", values.get(0) instanceof Number);
    assertTrue("name not wrapped with type info", values.get(1) instanceof String);
    assertTrue("date wrapped with type info", values.get(2) instanceof SqlParameterValue);
    assertTrue("version wrapped with type info", values.get(3) instanceof SqlParameterValue);
    verify(metaDataResultSet, atLeastOnce()).next();
    verify(columnsResultSet, atLeastOnce()).next();
    verify(metaDataResultSet).close();
    verify(columnsResultSet).close();
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) ResultSet(java.sql.ResultSet) Date(java.util.Date) Test(org.junit.Test)

Example 9 with MapSqlParameterSource

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

the class TableMetaDataContextTests method testTableWithSingleColumnGeneratedKey.

@Test
public void testTableWithSingleColumnGeneratedKey() throws Exception {
    final String TABLE = "customers";
    final String USER = "me";
    ResultSet metaDataResultSet = mock(ResultSet.class);
    given(metaDataResultSet.next()).willReturn(true, false);
    given(metaDataResultSet.getString("TABLE_SCHEM")).willReturn(USER);
    given(metaDataResultSet.getString("TABLE_NAME")).willReturn(TABLE);
    given(metaDataResultSet.getString("TABLE_TYPE")).willReturn("TABLE");
    ResultSet columnsResultSet = mock(ResultSet.class);
    given(columnsResultSet.next()).willReturn(true, false);
    given(columnsResultSet.getString("COLUMN_NAME")).willReturn("id");
    given(columnsResultSet.getInt("DATA_TYPE")).willReturn(Types.INTEGER);
    given(columnsResultSet.getBoolean("NULLABLE")).willReturn(false);
    given(databaseMetaData.getDatabaseProductName()).willReturn("MyDB");
    given(databaseMetaData.getDatabaseProductName()).willReturn("1.0");
    given(databaseMetaData.getUserName()).willReturn(USER);
    given(databaseMetaData.storesLowerCaseIdentifiers()).willReturn(true);
    given(databaseMetaData.getTables(null, null, TABLE, null)).willReturn(metaDataResultSet);
    given(databaseMetaData.getColumns(null, USER, TABLE, null)).willReturn(columnsResultSet);
    MapSqlParameterSource map = new MapSqlParameterSource();
    String[] keyCols = new String[] { "id" };
    context.setTableName(TABLE);
    context.processMetaData(dataSource, new ArrayList<>(), keyCols);
    List<Object> values = context.matchInParameterValuesWithInsertColumns(map);
    String insertString = context.createInsertString(keyCols);
    assertEquals("wrong number of parameters: ", 0, values.size());
    assertEquals("empty insert not generated correctly", "INSERT INTO customers () VALUES()", insertString);
    verify(metaDataResultSet, atLeastOnce()).next();
    verify(columnsResultSet, atLeastOnce()).next();
    verify(metaDataResultSet).close();
    verify(columnsResultSet).close();
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 10 with MapSqlParameterSource

use of org.springframework.jdbc.core.namedparam.MapSqlParameterSource in project perun by CESNET.

the class AttributesManagerImpl method getAllAttributesStartWithNameWithoutNullValue.

public List<Attribute> getAllAttributesStartWithNameWithoutNullValue(PerunSession sess, Resource resource, String startPartOfName) throws InternalErrorException {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("rId", resource.getId());
    parameters.addValue("nSC", AttributesManager.NS_RESOURCE_ATTR_CORE);
    parameters.addValue("nSO", AttributesManager.NS_RESOURCE_ATTR_OPT);
    parameters.addValue("nSD", AttributesManager.NS_RESOURCE_ATTR_DEF);
    parameters.addValue("startPartOfName", startPartOfName + "%");
    try {
        return namedParameterJdbcTemplate.query("select " + getAttributeMappingSelectQuery("ret") + " from attr_names " + "left join resource_attr_values ret on id=ret.attr_id and resource_id=:rId " + "where namespace in ( :nSC,:nSO,:nSD ) and attr_names.attr_name LIKE :startPartOfName", parameters, new AttributeRowMapper(sess, this, resource));
    } catch (EmptyResultDataAccessException ex) {
        return new ArrayList<Attribute>();
    } catch (RuntimeException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ConsistencyErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException) InternalErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException) Attribute(cz.metacentrum.perun.core.api.Attribute) RichAttribute(cz.metacentrum.perun.core.api.RichAttribute) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Aggregations

MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)42 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)17 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)15 Attribute (cz.metacentrum.perun.core.api.Attribute)10 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)9 ConsistencyErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException)9 InternalErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 SimpleJdbcInsert (org.springframework.jdbc.core.simple.SimpleJdbcInsert)4 Member (cz.metacentrum.perun.core.api.Member)3 SqlOutParameter (org.springframework.jdbc.core.SqlOutParameter)3 SqlParameter (org.springframework.jdbc.core.SqlParameter)3 ParsedSql (org.springframework.jdbc.core.namedparam.ParsedSql)3 User (cz.metacentrum.perun.core.api.User)2 IOException (java.io.IOException)2 ResultSet (java.sql.ResultSet)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 GargoyleException (com.kyj.fx.voeditor.visual.exceptions.GargoyleException)1