Search in sources :

Example 1 with LobCreator

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

the class BitTableIntegration 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;
            case Types.TIME:
            case Types.TIMESTAMP:
            case Types.DATE:
                ps.setObject(paramIndex, sqlValue);
                break;
            default:
                StatementCreatorUtils.setParameterValue(ps, paramIndex, sqlType, null, param);
                break;
        }
    }
}
Also used : LobCreator(org.springframework.jdbc.support.lob.LobCreator)

Example 2 with LobCreator

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

the class DbDialectTest 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 3 with LobCreator

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

the class TimeTableIntegration 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;
            case Types.TIME:
            case Types.TIMESTAMP:
            case Types.DATE:
                ps.setObject(paramIndex, sqlValue);
                break;
            default:
                StatementCreatorUtils.setParameterValue(ps, paramIndex, sqlType, null, param);
                break;
        }
    }
}
Also used : LobCreator(org.springframework.jdbc.support.lob.LobCreator)

Example 4 with LobCreator

use of org.springframework.jdbc.support.lob.LobCreator in project camel by apache.

the class JdbcAggregationRepository method insertAndUpdateHelper.

protected void insertAndUpdateHelper(final CamelContext camelContext, final String key, final Exchange exchange, String sql, final boolean idComesFirst) throws Exception {
    final byte[] data = codec.marshallExchange(camelContext, exchange, allowSerializedHeaders);
    jdbcTemplate.execute(sql, new AbstractLobCreatingPreparedStatementCallback(getLobHandler()) {

        @Override
        protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
            int totalParameterIndex = 0;
            lobCreator.setBlobAsBytes(ps, ++totalParameterIndex, data);
            if (idComesFirst) {
                ps.setString(++totalParameterIndex, key);
            }
            if (storeBodyAsText) {
                ps.setString(++totalParameterIndex, exchange.getIn().getBody(String.class));
            }
            if (hasHeadersToStoreAsText()) {
                for (String headerName : headersToStoreAsText) {
                    String headerValue = exchange.getIn().getHeader(headerName, String.class);
                    ps.setString(++totalParameterIndex, headerValue);
                }
            }
            if (!idComesFirst) {
                ps.setString(++totalParameterIndex, key);
            }
        }
    });
}
Also used : AbstractLobCreatingPreparedStatementCallback(org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) LobCreator(org.springframework.jdbc.support.lob.LobCreator)

Example 5 with LobCreator

use of org.springframework.jdbc.support.lob.LobCreator in project perun by CESNET.

the class Auditer method storeMessageToDb.

/**
	 * Store the message to the DB.
	 *
	 * @param sess
	 * @param message
	 */
public void storeMessageToDb(final PerunSession sess, final String message) {
    synchronized (LOCK_DB_TABLE_AUDITER_LOG) {
        try {
            final int msgId = Utils.getNewId(jdbc, "auditer_log_id_seq");
            jdbc.execute("insert into auditer_log (id, msg, actor, created_at, created_by_uid) values (?,?,?," + Compatibility.getSysdate() + ",?)", new AbstractLobCreatingPreparedStatementCallback(lobHandler) {

                public void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
                    ps.setInt(1, msgId);
                    lobCreator.setClobAsString(ps, 2, message);
                    ps.setString(3, sess.getPerunPrincipal().getActor());
                    ps.setInt(4, sess.getPerunPrincipal().getUserId());
                }
            });
        } catch (RuntimeException e) {
            log.error("Cannot store auditer log message ['{}'], exception: {}", message, e);
        } catch (InternalErrorException e) {
            log.error("Cannot get unique id for new auditer log message ['{}'], exception: {}", message, e);
        }
    }
}
Also used : AbstractLobCreatingPreparedStatementCallback(org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback) InternalErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) LobCreator(org.springframework.jdbc.support.lob.LobCreator)

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