Example usage for org.springframework.jdbc.core.support SqlLobValue SqlLobValue

List of usage examples for org.springframework.jdbc.core.support SqlLobValue SqlLobValue

Introduction

In this page you can find the example usage for org.springframework.jdbc.core.support SqlLobValue SqlLobValue.

Prototype

public SqlLobValue(Reader reader, int length) 

Source Link

Document

Create a new CLOB value with the given character stream, using a DefaultLobHandler.

Usage

From source file:architecture.ee.jdbc.sqlquery.SqlQueryHelper.java

public SqlQueryHelper lob(byte[] bytes) {
    SqlLobValue value = new SqlLobValue(bytes, lobHandler);
    values.add(value);
    return this;
}

From source file:architecture.ee.jdbc.sqlquery.SqlQueryHelper.java

public SqlQueryHelper lob(String content) {
    SqlLobValue value = new SqlLobValue(content, lobHandler);
    values.add(value);
    return this;
}

From source file:org.apache.camel.processor.aggregate.jdbc.OptimisticLockingJdbcAggregationRepository.java

@Override
public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
    return getTransactionTemplate().execute(new TransactionCallback<Exchange>() {

        @Override//from ww w .j  av  a 2s.  c o  m
        public Exchange doInTransaction(TransactionStatus status) {
            Exchange result = get(camelContext, key);
            try {
                log.debug(String.format("Adding exchange with key: [%s]", key));

                byte[] marshalledExchange = getCodec().marshallExchange(camelContext, exchange, true);

                boolean present = getJdbcTemplate().queryForObject(
                        String.format("SELECT COUNT(*) FROM %1$s WHERE %2$s=?", getRepositoryName(), ID_KEY),
                        new Object[] { key }, new int[] { Types.VARCHAR }, Integer.class) != 0;
                if (present) {
                    long version = exchange.getProperty(VERSION_EXCHANGE_PROPERTY, Long.class);
                    log.debug(String.format("Updating record with key: [%s] and version: [%s].", key, version));
                    int affectedRows = getJdbcTemplate().update(
                            String.format("UPDATE %1$s SET %2$s=?, %3$s=? WHERE %4$s=? AND %3$s=?",
                                    getRepositoryName(), EXCHANGE_KEY, VERSION_KEY, ID_KEY),
                            new Object[] { new SqlLobValue(marshalledExchange, getLobHandler()), version + 1,
                                    key, version },
                            new int[] { Types.BLOB, Types.BIGINT, Types.VARCHAR, Types.BIGINT });
                    if (affectedRows < 1) {
                        throw new RuntimeException(String.format(
                                "Error updating record with key: [%s] and version: [%s]. Stale version...", key,
                                version));
                    }
                } else {
                    log.debug(String.format("Inserting record with key: [%s].", key));
                    getJdbcTemplate().update(
                            String.format("INSERT INTO %1$s (%2$s, %3$s, %4$s) VALUES (?, ?, ?)",
                                    getRepositoryName(), ID_KEY, EXCHANGE_KEY, VERSION_KEY),
                            new Object[] { key, new SqlLobValue(marshalledExchange, getLobHandler()), 1L },
                            new int[] { Types.VARCHAR, Types.BLOB, Types.BIGINT });
                }
            } catch (Exception e) {
                throw new RuntimeException(String.format("Error adding to repository [%s] with key [%s].",
                        getRepositoryName(), key), e);
            }

            return result;
        }
    });
}

From source file:com.opengamma.masterdb.exchange.DbExchangeMaster.java

/**
 * Inserts a new document./*from   w ww . j a  v a 2 s  . c om*/
 * 
 * @param document  the document, not null
 * @return the new document, not null
 */
@Override
protected ExchangeDocument insert(final ExchangeDocument document) {
    ArgumentChecker.notNull(document.getExchange(), "document.exchange");
    ArgumentChecker.notNull(document.getName(), "document.name");

    final ManageableExchange exchange = document.getExchange();
    final long docId = nextId("exg_exchange_seq");
    final long docOid = (document.getUniqueId() != null ? extractOid(document.getUniqueId()) : docId);
    // set the uniqueId (needs to go in Fudge message)
    final UniqueId uniqueId = createUniqueId(docOid, docId);
    exchange.setUniqueId(uniqueId);
    document.setUniqueId(uniqueId);
    // the arguments for inserting into the exchange table
    FudgeMsgEnvelope env = FUDGE_CONTEXT.toFudgeMsg(exchange);
    byte[] bytes = FUDGE_CONTEXT.toByteArray(env.getMessage());
    final DbMapSqlParameterSource docArgs = new DbMapSqlParameterSource().addValue("doc_id", docId)
            .addValue("doc_oid", docOid).addTimestamp("ver_from_instant", document.getVersionFromInstant())
            .addTimestampNullFuture("ver_to_instant", document.getVersionToInstant())
            .addTimestamp("corr_from_instant", document.getCorrectionFromInstant())
            .addTimestampNullFuture("corr_to_instant", document.getCorrectionToInstant())
            .addValue("name", document.getName())
            .addValue("time_zone", exchange.getTimeZone() != null ? exchange.getTimeZone().getId() : null,
                    Types.VARCHAR)
            .addValue("detail", new SqlLobValue(bytes, getDialect().getLobHandler()), Types.BLOB);
    // the arguments for inserting into the idkey tables
    final List<DbMapSqlParameterSource> assocList = new ArrayList<DbMapSqlParameterSource>();
    final List<DbMapSqlParameterSource> idKeyList = new ArrayList<DbMapSqlParameterSource>();
    final String sqlSelectIdKey = getElSqlBundle().getSql("SelectIdKey");
    for (ExternalId id : exchange.getExternalIdBundle()) {
        final DbMapSqlParameterSource assocArgs = new DbMapSqlParameterSource().addValue("doc_id", docId)
                .addValue("key_scheme", id.getScheme().getName()).addValue("key_value", id.getValue());
        assocList.add(assocArgs);
        if (getJdbcTemplate().queryForList(sqlSelectIdKey, assocArgs).isEmpty()) {
            // select avoids creating unecessary id, but id may still not be used
            final long idKeyId = nextId("exg_idkey_seq");
            final DbMapSqlParameterSource idkeyArgs = new DbMapSqlParameterSource()
                    .addValue("idkey_id", idKeyId).addValue("key_scheme", id.getScheme().getName())
                    .addValue("key_value", id.getValue());
            idKeyList.add(idkeyArgs);
        }
    }
    final String sqlDoc = getElSqlBundle().getSql("Insert", docArgs);
    final String sqlIdKey = getElSqlBundle().getSql("InsertIdKey");
    final String sqlDoc2IdKey = getElSqlBundle().getSql("InsertDoc2IdKey");
    getJdbcTemplate().update(sqlDoc, docArgs);
    getJdbcTemplate().batchUpdate(sqlIdKey, idKeyList.toArray(new DbMapSqlParameterSource[idKeyList.size()]));
    getJdbcTemplate().batchUpdate(sqlDoc2IdKey,
            assocList.toArray(new DbMapSqlParameterSource[assocList.size()]));
    return document;
}

From source file:org.apache.camel.processor.aggregate.jdbc.OptimisticLockingJdbcAggregationRepository.java

@Override
public void remove(CamelContext camelContext, String key, Exchange exchange) {
    getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {

        @Override//from  w w w  .java 2  s  .  co m
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                log.debug(String.format("Removing key [%s]", key));

                String confirmKey = exchange.getExchangeId();
                long version = exchange.getProperty(VERSION_EXCHANGE_PROPERTY, Long.class);
                byte[] marshalledExchange = getCodec().marshallExchange(camelContext, exchange, true);

                getJdbcTemplate().update(
                        String.format("DELETE FROM %1$s WHERE %2$s = ? AND %3$s = ?", getRepositoryName(),
                                ID_KEY, VERSION_KEY),
                        new Object[] { key, version }, new int[] { Types.VARCHAR, Types.BIGINT });

                getJdbcTemplate().update(
                        String.format("INSERT INTO %1$s (%2$s, %3$s, %4$s) VALUES (?, ?, ?)",
                                getRepositoryNameCompleted(), ID_KEY, EXCHANGE_KEY, VERSION_KEY),
                        new Object[] { confirmKey, new SqlLobValue(marshalledExchange, getLobHandler()), 1L },
                        new int[] { Types.VARCHAR, Types.BLOB, Types.BIGINT });
            } catch (Exception e) {
                throw new RuntimeException(
                        String.format("Error removing key [%s] from repository [%s]", key, getRepositoryName()),
                        e);
            }
        }
    });
}

From source file:architecture.ee.web.community.page.dao.jdbc.JdbcPageDao.java

private void insertPageBody(final Page page) {
    long bodyId = -1L;
    if (page.getBodyText() != null) {
        boolean newBodyRequired = false;
        if (page.getVersionId() == 1) {
            newBodyRequired = true;//from   w w  w .j a  va  2 s. c  om
        } else {
            // load body from database ...
            List<BodyContent> results = getExtendedJdbcTemplate().query(
                    getBoundSql("ARCHITECTURE_COMMUNITY.SELECT_PAGE_BODY").getSql(), bodyContentMapper,
                    new SqlParameterValue(Types.NUMERIC, page.getPageId()),
                    new SqlParameterValue(Types.NUMERIC, Integer.valueOf(page.getVersionId() - 1)));
            String preTextBody = null;
            for (BodyContent bodyContent : results) {
                bodyId = bodyContent.getBodyId();
                preTextBody = bodyContent.getBodyText();
            }
            String textBody = page.getBodyText();
            if (preTextBody == null || !preTextBody.equals(textBody))
                newBodyRequired = true;
        }

        if (newBodyRequired) {
            bodyId = getNextId("PAGE_BODY");
            getExtendedJdbcTemplate().update(getBoundSql("ARCHITECTURE_COMMUNITY.INSERT_PAGE_BODY").getSql(),
                    new SqlParameterValue(Types.NUMERIC, bodyId),
                    new SqlParameterValue(Types.NUMERIC, page.getPageId()),
                    new SqlParameterValue(Types.NUMERIC, page.getBodyContent().getBodyType().getId()),
                    new SqlParameterValue(Types.CLOB, new SqlLobValue(page.getBodyText(), getLobHandler())));
            getExtendedJdbcTemplate().update(
                    getBoundSql("ARCHITECTURE_COMMUNITY.INSERT_PAGE_BODY_VERSION").getSql(),
                    new SqlParameterValue(Types.NUMERIC, bodyId),
                    new SqlParameterValue(Types.NUMERIC, page.getPageId()),
                    new SqlParameterValue(Types.NUMERIC, page.getVersionId()));
        }
    }
}

From source file:com.opengamma.masterdb.security.DbSecurityMaster.java

private void storeRawSecurityDetail(RawSecurity security) {
    final DbMapSqlParameterSource rawArgs = new DbMapSqlParameterSource()
            .addValue("security_id", extractRowId(security.getUniqueId())).addValue("raw_data",
                    new SqlLobValue(security.getRawData(), getDialect().getLobHandler()), Types.BLOB);
    final String sqlRaw = getElSqlBundle().getSql("InsertRaw", rawArgs);
    getJdbcTemplate().update(sqlRaw, rawArgs);
}