/** * This implementation calls {@link #setValuesIfAvailable} * and sets this instance's exhaustion flag accordingly. */ @Override public final void setValues(PreparedStatement ps, int i) throws SQLException { this.exhausted = !setValuesIfAvailable(ps, i); }
/** * Set the JdbcTemplate for this DAO explicitly, * as an alternative to specifying a DataSource. */ public final void setJdbcTemplate(@Nullable JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; initTemplateConfig(); }
/** * Set the JDBC DataSource to be used by this DAO. */ public final void setDataSource(DataSource dataSource) { if (this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource()) { this.jdbcTemplate = createJdbcTemplate(dataSource); initTemplateConfig(); } }
@Test public void testOtherSqlType() throws SQLException { SqlLobValue lob = new SqlLobValue("Bla", handler); thrown.expect(IllegalArgumentException.class); lob.setTypeValue(preparedStatement, 1, Types.SMALLINT, "test"); }
@Test public void testCorrectCleanup() throws SQLException { SqlLobValue lob = new SqlLobValue("Bla", handler); lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test"); lob.cleanup(); verify(creator).setClobAsString(preparedStatement, 1, "Bla"); verify(creator).close(); }
@Override protected void store(String code, OAuth2Authentication authentication) { jdbcTemplate.update(insertAuthenticationSql, new Object[] { code, new SqlLobValue(SerializationUtils.serialize(authentication)) }, new int[] { Types.VARCHAR, Types.BLOB }); }
/** * Return the SQLExceptionTranslator of this DAO's JdbcTemplate, * for translating SQLExceptions in custom JDBC access code. * @see org.springframework.jdbc.core.JdbcTemplate#getExceptionTranslator() */ protected final SQLExceptionTranslator getExceptionTranslator() { JdbcTemplate jdbcTemplate = getJdbcTemplate(); Assert.state(jdbcTemplate != null, "No JdbcTemplate set"); return jdbcTemplate.getExceptionTranslator(); }
/** * Close the given JDBC Connection, created via this DAO's DataSource, * if it isn't bound to the thread. * @param con the Connection to close * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection */ protected final void releaseConnection(Connection con) { DataSourceUtils.releaseConnection(con, getDataSource()); }
@Test public void testAbstractLobStreamingResultSetExtractorNoRows() throws SQLException { ResultSet rset = mock(ResultSet.class); AbstractLobStreamingResultSetExtractor<Void> lobRse = getResultSetExtractor(false); thrown.expect(IncorrectResultSizeDataAccessException.class); try { lobRse.extractData(rset); } finally { verify(rset).next(); } }
@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 final Integer doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { LobCreator lobCreator = this.lobHandler.getLobCreator(); try { setValues(ps, lobCreator); return ps.executeUpdate(); } finally { lobCreator.close(); } }
@Test public void test7() throws SQLException { SqlLobValue lob = new SqlLobValue("bla".getBytes()); thrown.expect(IllegalArgumentException.class); lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test"); }
/** * Get a JDBC Connection, either from the current transaction or a new one. * @return the JDBC Connection * @throws CannotGetJdbcConnectionException if the attempt to get a Connection failed * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection(javax.sql.DataSource) */ protected final Connection getConnection() throws CannotGetJdbcConnectionException { DataSource dataSource = getDataSource(); Assert.state(dataSource != null, "No DataSource set"); return DataSourceUtils.getConnection(dataSource); }
@Test public void testAbstractLobStreamingResultSetExtractorOneRow() throws SQLException { ResultSet rset = mock(ResultSet.class); given(rset.next()).willReturn(true, false); AbstractLobStreamingResultSetExtractor<Void> lobRse = getResultSetExtractor(false); lobRse.extractData(rset); verify(rset).clearWarnings(); }
@Test public void test3() throws SQLException { SqlLobValue lob = new SqlLobValue(new InputStreamReader(new ByteArrayInputStream("Bla".getBytes())), 12); thrown.expect(IllegalArgumentException.class); lob.setTypeValue(preparedStatement, 1, Types.BLOB, "test"); }
@Test public void testAbstractLobStreamingResultSetExtractorCorrectException() throws SQLException { ResultSet rset = mock(ResultSet.class); given(rset.next()).willReturn(true); AbstractLobStreamingResultSetExtractor<Void> lobRse = getResultSetExtractor(true); thrown.expect(LobRetrievalFailureException.class); lobRse.extractData(rset); }
@Test public void test4() throws SQLException { String testContent = "Bla"; SqlLobValue lob = new SqlLobValue(testContent, handler); lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test"); verify(creator).setClobAsString(preparedStatement, 1, testContent); }
@Test public void testAbstractLobStreamingResultSetExtractorMultipleRows() throws SQLException { ResultSet rset = mock(ResultSet.class); given(rset.next()).willReturn(true, true, false); AbstractLobStreamingResultSetExtractor<Void> lobRse = getResultSetExtractor(false); thrown.expect(IncorrectResultSizeDataAccessException.class); try { lobRse.extractData(rset); } finally { verify(rset).clearWarnings(); } }
@Test public void test1() throws SQLException { byte[] testBytes = "Bla".getBytes(); SqlLobValue lob = new SqlLobValue(testBytes, handler); lob.setTypeValue(preparedStatement, 1, Types.BLOB, "test"); verify(creator).setBlobAsBytes(preparedStatement, 1, testBytes); }
@Test public void test2() throws SQLException { String testString = "Bla"; SqlLobValue lob = new SqlLobValue(testString, handler); lob.setTypeValue(preparedStatement, 1, Types.BLOB, "test"); verify(creator).setBlobAsBytes(preparedStatement, 1, testString.getBytes()); }