Search in sources :

Example 6 with LobCreator

use of org.springframework.jdbc.support.lob.LobCreator in project otter by alibaba.

the class DbDialectIntegration method doPreparedStatement.

private void doPreparedStatement(PreparedStatement ps, final DbDialect dbDialect, final Integer[] columnTypes, final String[] columnValues) throws SQLException {
    LobCreator lobCreator = null;
    for (int i = 0; i < columnTypes.length; i++) {
        int paramIndex = i + 1;
        String sqlValue = columnValues[i];
        int sqlType = columnTypes[i];
        Object param = SqlUtils.stringToSqlValue(sqlValue, sqlType, SqlUtils.isTextType(sqlType), dbDialect.isEmptyStringNulled());
        switch(sqlType) {
            case Types.CLOB:
                if (lobCreator == null) {
                    lobCreator = dbDialect.getLobHandler().getLobCreator();
                }
                lobCreator.setClobAsString(ps, paramIndex, (String) param);
                break;
            case Types.BLOB:
                if (lobCreator == null) {
                    lobCreator = dbDialect.getLobHandler().getLobCreator();
                }
                lobCreator.setBlobAsBytes(ps, paramIndex, (byte[]) param);
                break;
            default:
                StatementCreatorUtils.setParameterValue(ps, paramIndex, sqlType, null, param);
                break;
        }
    }
}
Also used : LobCreator(org.springframework.jdbc.support.lob.LobCreator)

Example 7 with LobCreator

use of org.springframework.jdbc.support.lob.LobCreator in project spring-framework by spring-projects.

the class LobSupportTests method testCreatingPreparedStatementCallback.

@Test
public void testCreatingPreparedStatementCallback() throws SQLException {
    LobHandler handler = mock(LobHandler.class);
    LobCreator creator = mock(LobCreator.class);
    PreparedStatement ps = mock(PreparedStatement.class);
    given(handler.getLobCreator()).willReturn(creator);
    given(ps.executeUpdate()).willReturn(3);
    class SetValuesCalled {

        boolean b = false;
    }
    final SetValuesCalled svc = new SetValuesCalled();
    AbstractLobCreatingPreparedStatementCallback psc = new AbstractLobCreatingPreparedStatementCallback(handler) {

        @Override
        protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException, DataAccessException {
            svc.b = true;
        }
    };
    assertEquals(Integer.valueOf(3), psc.doInPreparedStatement(ps));
    assertTrue(svc.b);
    verify(creator).close();
    verify(handler).getLobCreator();
    verify(ps).executeUpdate();
}
Also used : PreparedStatement(java.sql.PreparedStatement) LobHandler(org.springframework.jdbc.support.lob.LobHandler) LobCreator(org.springframework.jdbc.support.lob.LobCreator) Test(org.junit.Test)

Aggregations

LobCreator (org.springframework.jdbc.support.lob.LobCreator)7 PreparedStatement (java.sql.PreparedStatement)3 SQLException (java.sql.SQLException)2 AbstractLobCreatingPreparedStatementCallback (org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback)2 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 InternalErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException)1 Test (org.junit.Test)1 LobHandler (org.springframework.jdbc.support.lob.LobHandler)1