Search in sources :

Example 21 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 22 with MapSqlParameterSource

use of org.springframework.jdbc.core.namedparam.MapSqlParameterSource in project Gargoyle by callakrsos.

the class VelocityTest method test.

@Test
public void test() throws SQLException, Exception {
    String sql = "SELECT table_name FROM information_schema.tables WHERE 1=1    AND  table_name in (:tableName)   limit 50";
    MapSqlParameterSource source = new MapSqlParameterSource();
    List<String> asList = Arrays.asList("tables", "columns");
    source.addValue("tableName", asList);
    List<HashMap<String, Object>> select = DbUtil.select(sql, source, (rs, r) -> {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("table_name", rs.getString("table_name"));
        return hashMap;
    });
    System.out.println(select);
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 23 with MapSqlParameterSource

use of org.springframework.jdbc.core.namedparam.MapSqlParameterSource in project sakuli by ConSol.

the class DaoTest method testCreateSqlSetStringForNamedParameter.

@Test
public void testCreateSqlSetStringForNamedParameter() throws Throwable {
    testling = new Dao(dataSource) {
    };
    MapSqlParameterSource source = new MapSqlParameterSource().addValue("testling2", "value").addValue("testling", "value");
    Assert.assertEquals("SET testling2=:testling2, testling=:testling ", testling.createSqlSetStringForNamedParameter(source.getValues()));
    source.addValue("nullable", null);
    Assert.assertEquals("SET testling2=:testling2, testling=:testling ", testling.createSqlSetStringForNamedParameter(source.getValues()));
}
Also used : Dao(org.sakuli.services.forwarder.database.dao.impl.Dao) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Test(org.testng.annotations.Test)

Example 24 with MapSqlParameterSource

use of org.springframework.jdbc.core.namedparam.MapSqlParameterSource in project sakuli by ConSol.

the class DaoTestSuiteImpl method getInitialDataParameters.

private MapSqlParameterSource getInitialDataParameters() {
    MapSqlParameterSource tcParameters = getGuidParameter();
    tcParameters.addValue("id", testSuite.getDbPrimaryKey());
    tcParameters.addValue("suiteID", testSuite.getId());
    if (testSuite.getState() != null) {
        tcParameters.addValue("result", testSuite.getState().getErrorCode());
        tcParameters.addValue("result_desc", testSuite.getState().toString());
    }
    tcParameters.addValue("name", testSuite.getName());
    tcParameters.addValue("browser", testSuite.getBrowserInfo());
    tcParameters.addValue("host", testSuite.getHost());
    tcParameters.addValue("start", testSuite.getStartDateAsUnixTimestamp());
    return tcParameters;
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource)

Example 25 with MapSqlParameterSource

use of org.springframework.jdbc.core.namedparam.MapSqlParameterSource in project sakuli by ConSol.

the class DaoTestSuiteImpl method getCompleteParameters.

private MapSqlParameterSource getCompleteParameters() {
    MapSqlParameterSource tcParameters = getInitialDataParameters();
    tcParameters.addValues(getGuidParameter().getValues());
    tcParameters.addValue("stop", testSuite.getStopDateAsUnixTimestamp());
    int warningTime = testSuite.getWarningTime();
    tcParameters.addValue("warning", (warningTime != 0) ? warningTime : null);
    int criticalTime = testSuite.getCriticalTime();
    tcParameters.addValue("critical", (criticalTime != 0) ? criticalTime : null);
    tcParameters.addValue("duration", testSuite.getDuration());
    //try to save the screenshot
    try {
        if (testSuite.getScreenShotPath() != null) {
            final InputStream blobIs = Files.newInputStream(testSuite.getScreenShotPath());
            final int length = (int) testSuite.getScreenShotPath().toFile().length();
            tcParameters.addValue("screenshot", new SqlLobValue(blobIs, length, lobHandler), Types.BLOB);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    tcParameters.addValue("msg", testSuite.getExceptionMessages(false));
    return tcParameters;
}
Also used : SqlLobValue(org.springframework.jdbc.core.support.SqlLobValue) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)51 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)20 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 HashMap (java.util.HashMap)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 HashSet (java.util.HashSet)2 Map (java.util.Map)2