/** * Set the value for the prepared statement's specified parameter position using the passed in * value and type. This method can be overridden by sub-classes if needed. * @param ps the PreparedStatement * @param parameterPosition index of the parameter position * @param argType the argument type * @param argValue the argument value * @throws SQLException if thrown by PreparedStatement methods */ protected void doSetValue(PreparedStatement ps, int parameterPosition, int argType, Object argValue) throws SQLException { StatementCreatorUtils.setParameterValue(ps, parameterPosition, argType, argValue); }
@Test public void testSetParameterValueWithDateAndCalendar() throws SQLException { java.util.Calendar cal = new GregorianCalendar(); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.DATE, null, cal); verify(preparedStatement).setDate(1, new java.sql.Date(cal.getTime().getTime()), cal); }
@Test public void testSetParameterValueWithTimeAndCalendar() throws SQLException { java.util.Calendar cal = new GregorianCalendar(); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIME, null, cal); verify(preparedStatement).setTime(1, new java.sql.Time(cal.getTime().getTime()), cal); }
@Test public void testSetParameterValueWithCalendarAndUnknownType() throws SQLException { java.util.Calendar cal = new GregorianCalendar(); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, cal); verify(preparedStatement).setTimestamp(1, new java.sql.Timestamp(cal.getTime().getTime()), cal); }
@Test public void testSetParameterValueWithTimestampAndCalendar() throws SQLException { java.util.Calendar cal = new GregorianCalendar(); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIMESTAMP, null, cal); verify(preparedStatement).setTimestamp(1, new java.sql.Timestamp(cal.getTime().getTime()), cal); }
@Test public void testSetParameterValueWithDateAndUtilDate() throws SQLException { java.util.Date date = new java.util.Date(1000); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.DATE, null, date); verify(preparedStatement).setDate(1, new java.sql.Date(1000)); }
@Test public void testSetParameterValueWithTimeAndUtilDate() throws SQLException { java.util.Date date = new java.util.Date(1000); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIME, null, date); verify(preparedStatement).setTime(1, new java.sql.Time(1000)); }
@Test public void testSetParameterValueWithTimestampAndUtilDate() throws SQLException { java.util.Date date = new java.util.Date(1000); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIMESTAMP, null, date); verify(preparedStatement).setTimestamp(1, new java.sql.Timestamp(1000)); }
@Test public void testSetParameterValueWithDateAndUnknownType() throws SQLException { java.util.Date date = new java.util.Date(1000); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, date); verify(preparedStatement).setTimestamp(1, new java.sql.Timestamp(1000)); }
@Test public void testSetParameterValueWithSqlDate() throws SQLException { java.sql.Date date = new java.sql.Date(1000); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.DATE, null, date); verify(preparedStatement).setDate(1, date); }
@Test public void testSetParameterValueWithSqlTimestamp() throws SQLException { java.sql.Timestamp timestamp = new java.sql.Timestamp(1000); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIMESTAMP, null, timestamp); verify(preparedStatement).setTimestamp(1, timestamp); }
@Test public void testSetParameterValueWithSqlTime() throws SQLException { java.sql.Time time = new java.sql.Time(1000); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIME, null, time); verify(preparedStatement).setTime(1, time); }
@Test public void testSetParameterValueWithNullAndTypeName() throws SQLException { StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.VARCHAR, "mytype", null); verify(preparedStatement).setNull(1, Types.VARCHAR, "mytype"); }
@Test public void testSetParameterValueWithNullAndType() throws SQLException { StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.VARCHAR, null, null); verify(preparedStatement).setNull(1, Types.VARCHAR); }
@Test public void testSetParameterValueWithString() throws SQLException { StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.VARCHAR, null, "test"); verify(preparedStatement).setString(1, "test"); }
@Test public void testSetParameterValueWithStringAndSpecialType() throws SQLException { StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.CHAR, null, "test"); verify(preparedStatement).setObject(1, "test", Types.CHAR); }
@Test public void testSetParameterValueWithStringAndUnknownType() throws SQLException { StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, "test"); verify(preparedStatement).setString(1, "test"); }
@Test // SPR-8571 public void testSetParameterValueWithStringAndVendorSpecificType() throws SQLException { Connection con = mock(Connection.class); DatabaseMetaData dbmd = mock(DatabaseMetaData.class); given(preparedStatement.getConnection()).willReturn(con); given(dbmd.getDatabaseProductName()).willReturn("Oracle"); given(con.getMetaData()).willReturn(dbmd); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.OTHER, null, "test"); verify(preparedStatement).setString(1, "test"); }
@Test public void testSetParameterValueWithNullAndUnknownType() throws SQLException { StatementCreatorUtils.shouldIgnoreGetParameterType = true; Connection con = mock(Connection.class); DatabaseMetaData dbmd = mock(DatabaseMetaData.class); given(preparedStatement.getConnection()).willReturn(con); given(dbmd.getDatabaseProductName()).willReturn("Oracle"); given(dbmd.getDriverName()).willReturn("Oracle Driver"); given(con.getMetaData()).willReturn(dbmd); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, null); verify(preparedStatement).setNull(1, Types.NULL); StatementCreatorUtils.shouldIgnoreGetParameterType = false; }
@Test public void testSetParameterValueWithNullAndGetParameterTypeWorking() throws SQLException { ParameterMetaData pmd = mock(ParameterMetaData.class); given(preparedStatement.getParameterMetaData()).willReturn(pmd); given(pmd.getParameterType(1)).willReturn(Types.SMALLINT); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, null); verify(pmd).getParameterType(1); verify(preparedStatement, never()).getConnection(); verify(preparedStatement).setNull(1, Types.SMALLINT); }