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();
}
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);
}
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()));
}
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;
}
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;
}
Aggregations