@Override public Upsert commit() throws SQLException { pstmt.getConnection().commit(); return this; }
@Override public final void setTypeValue(PreparedStatement ps, int paramIndex, int sqlType, @Nullable String typeName) throws SQLException { Object value = createTypeValue(ps.getConnection(), sqlType, typeName); if (sqlType == TYPE_UNKNOWN) { ps.setObject(paramIndex, value); } else { ps.setObject(paramIndex, value, sqlType); } }
@Override public void setClobAsString(PreparedStatement ps, int paramIndex, String content) throws SQLException { Clob clob = ps.getConnection().createClob(); clob.setString(1, content); ps.setClob(paramIndex, clob); }
@Override public void setBlobAsBytes(PreparedStatement ps, int paramIndex, byte[] content) throws SQLException { Blob blob = ps.getConnection().createBlob(); blob.setBytes(1, content); ps.setBlob(paramIndex, blob); }
@Override public void setBlobAsBytes(PreparedStatement ps, int paramIndex, @Nullable byte[] content) throws SQLException { if (content != null) { Blob blob = ps.getConnection().createBlob(); blob.setBytes(1, content); this.temporaryBlobs.add(blob); ps.setBlob(paramIndex, blob); } else { ps.setBlob(paramIndex, (Blob) null); } if (logger.isDebugEnabled()) { logger.debug(content != null ? "Copied bytes into temporary BLOB with length " + content.length : "Set BLOB to null"); } }
@Override public void setClobAsString(PreparedStatement ps, int paramIndex, @Nullable String content) throws SQLException { if (content != null) { Clob clob = ps.getConnection().createClob(); clob.setString(1, content); this.temporaryClobs.add(clob); ps.setClob(paramIndex, clob); } else { ps.setClob(paramIndex, (Clob) null); } if (logger.isDebugEnabled()) { logger.debug(content != null ? "Copied string into temporary CLOB with length " + content.length() : "Set CLOB to null"); } }
@Override public void setBlobAsBinaryStream( PreparedStatement ps, int paramIndex, @Nullable InputStream binaryStream, int contentLength) throws SQLException { if (binaryStream != null) { Blob blob = ps.getConnection().createBlob(); try { FileCopyUtils.copy(binaryStream, blob.setBinaryStream(1)); } catch (IOException ex) { throw new DataAccessResourceFailureException("Could not copy into LOB stream", ex); } this.temporaryBlobs.add(blob); ps.setBlob(paramIndex, blob); } else { ps.setBlob(paramIndex, (Blob) null); } if (logger.isDebugEnabled()) { logger.debug(binaryStream != null ? "Copied binary stream into temporary BLOB with length " + contentLength : "Set BLOB to null"); } }
@Override public void setClobAsCharacterStream( PreparedStatement ps, int paramIndex, @Nullable Reader characterStream, int contentLength) throws SQLException { if (characterStream != null) { Clob clob = ps.getConnection().createClob(); try { FileCopyUtils.copy(characterStream, clob.setCharacterStream(1)); } catch (IOException ex) { throw new DataAccessResourceFailureException("Could not copy into LOB stream", ex); } this.temporaryClobs.add(clob); ps.setClob(paramIndex, clob); } else { ps.setClob(paramIndex, (Clob) null); } if (logger.isDebugEnabled()) { logger.debug(characterStream != null ? "Copied character stream into temporary CLOB with length " + contentLength : "Set CLOB to null"); } }
@Override public void setClobAsAsciiStream( PreparedStatement ps, int paramIndex, @Nullable InputStream asciiStream, int contentLength) throws SQLException { if (asciiStream != null) { Clob clob = ps.getConnection().createClob(); try { FileCopyUtils.copy(asciiStream, clob.setAsciiStream(1)); } catch (IOException ex) { throw new DataAccessResourceFailureException("Could not copy into LOB stream", ex); } this.temporaryClobs.add(clob); ps.setClob(paramIndex, clob); } else { ps.setClob(paramIndex, (Clob) null); } if (logger.isDebugEnabled()) { logger.debug(asciiStream != null ? "Copied ASCII stream into temporary CLOB with length " + contentLength : "Set CLOB to null"); } }
@Override public void setValue(PreparedStatement ps, int paramIndex) throws SQLException { this.xmlObject = ps.getConnection().createSQLXML(); try { provideXml(this.xmlObject); } catch (IOException ex) { throw new DataAccessResourceFailureException("Failure encountered while providing XML", ex); } ps.setSQLXML(paramIndex, this.xmlObject); }
@Override public void bind(PreparedStatement st, J value, int index, WrapperOptions options) throws SQLException { if ( value == null ) { st.setNull( index, Types.STRUCT, SQL_TYPE_NAME ); } else { final Geometry geometry = javaTypeDescriptor.unwrap( value, Geometry.class, options ); final Object dbGeom = toNative( geometry, st.getConnection() ); st.setObject( index, dbGeom ); } }
@Override protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException { StringArrayTypeDescriptor arrayTypeDescriptor = (StringArrayTypeDescriptor) javaTypeDescriptor; st.setArray(index, st.getConnection().createArrayOf( arrayTypeDescriptor.getSqlArrayType(), arrayTypeDescriptor.unwrap((String[]) value, Object[].class, options) )); }
@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 testSetParameterValueWithNullAndUnknownTypeOnInformix() throws SQLException { StatementCreatorUtils.shouldIgnoreGetParameterType = true; Connection con = mock(Connection.class); DatabaseMetaData dbmd = mock(DatabaseMetaData.class); given(preparedStatement.getConnection()).willReturn(con); given(con.getMetaData()).willReturn(dbmd); given(dbmd.getDatabaseProductName()).willReturn("Informix Dynamic Server"); given(dbmd.getDriverName()).willReturn("Informix Driver"); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, null); verify(dbmd).getDatabaseProductName(); verify(dbmd).getDriverName(); verify(preparedStatement).setObject(1, null); StatementCreatorUtils.shouldIgnoreGetParameterType = false; }
@Test public void testSetParameterValueWithNullAndUnknownTypeOnDerbyEmbedded() throws SQLException { StatementCreatorUtils.shouldIgnoreGetParameterType = true; Connection con = mock(Connection.class); DatabaseMetaData dbmd = mock(DatabaseMetaData.class); given(preparedStatement.getConnection()).willReturn(con); given(con.getMetaData()).willReturn(dbmd); given(dbmd.getDatabaseProductName()).willReturn("Apache Derby"); given(dbmd.getDriverName()).willReturn("Apache Derby Embedded Driver"); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, null); verify(dbmd).getDatabaseProductName(); verify(dbmd).getDriverName(); verify(preparedStatement).setNull(1, Types.VARCHAR); StatementCreatorUtils.shouldIgnoreGetParameterType = false; }
@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 // SPR-8571 public void testSetParameterValueWithNullAndVendorSpecificType() 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, Types.OTHER, 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); }
@Before public void setup() throws Exception { connection = mock(Connection.class); dataSource = mock(DataSource.class); preparedStatement = mock(PreparedStatement.class); resultSet = mock(ResultSet.class); namedParameterTemplate = new NamedParameterJdbcTemplate(dataSource); databaseMetaData = mock(DatabaseMetaData.class); given(dataSource.getConnection()).willReturn(connection); given(connection.prepareStatement(anyString())).willReturn(preparedStatement); given(preparedStatement.getConnection()).willReturn(connection); given(preparedStatement.executeQuery()).willReturn(resultSet); given(databaseMetaData.getDatabaseProductName()).willReturn("MySQL"); given(databaseMetaData.supportsBatchUpdates()).willReturn(true); }
@Before public void setup() throws Exception { this.connection = mock(Connection.class); this.dataSource = mock(DataSource.class); this.preparedStatement = mock(PreparedStatement.class); this.statement = mock(Statement.class); this.resultSet = mock(ResultSet.class); this.template = new JdbcTemplate(this.dataSource); this.callableStatement = mock(CallableStatement.class); given(this.dataSource.getConnection()).willReturn(this.connection); given(this.connection.prepareStatement(anyString())).willReturn(this.preparedStatement); given(this.preparedStatement.executeQuery()).willReturn(this.resultSet); given(this.preparedStatement.executeQuery(anyString())).willReturn(this.resultSet); given(this.preparedStatement.getConnection()).willReturn(this.connection); given(this.statement.getConnection()).willReturn(this.connection); given(this.statement.executeQuery(anyString())).willReturn(this.resultSet); given(this.connection.prepareCall(anyString())).willReturn(this.callableStatement); given(this.callableStatement.getResultSet()).willReturn(this.resultSet); }