Java Code Examples for org.springframework.jdbc.core.namedparam.MapSqlParameterSource#registerSqlType()

The following examples show how to use org.springframework.jdbc.core.namedparam.MapSqlParameterSource#registerSqlType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TableMetaDataContextTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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();
}
 
Example 2
Source File: TableMetaDataContextTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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();
}
 
Example 3
Source File: TableMetaDataContextTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@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<String>(), 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();
}
 
Example 4
Source File: TableMetaDataContextTests.java    From effectivejava with Apache License 2.0 4 votes vote down vote up
@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<String>(), 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();
}