/** * Create a new CLOB value with the given character stream. * @param reader the character stream containing the CLOB value * @param length the length of the CLOB value * @param lobHandler the LobHandler to be used */ public SqlLobValue(Reader reader, int length, LobHandler lobHandler) { this.content = reader; this.length = length; this.lobCreator = lobHandler.getLobCreator(); }
/** * Create a new BLOB/CLOB value with the given stream. * @param stream the stream containing the LOB value * @param length the length of the LOB value * @param lobHandler the LobHandler to be used */ public SqlLobValue(InputStream stream, int length, LobHandler lobHandler) { this.content = stream; this.length = length; this.lobCreator = lobHandler.getLobCreator(); }
/** * Create a new BLOB value with the given byte array. * @param bytes the byte array containing the BLOB value * @param lobHandler the LobHandler to be used */ public SqlLobValue(@Nullable byte[] bytes, LobHandler lobHandler) { this.content = bytes; this.length = (bytes != null ? bytes.length : 0); this.lobCreator = lobHandler.getLobCreator(); }
/** * Create a new CLOB value with the given content string. * @param content the String containing the CLOB value * @param lobHandler the LobHandler to be used */ public SqlLobValue(@Nullable String content, LobHandler lobHandler) { this.content = content; this.length = (content != null ? content.length() : 0); this.lobCreator = lobHandler.getLobCreator(); }
@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(); } }
@Override public void setValues(PreparedStatement ps) throws SQLException { ps.setString(1, shortContext); if (longContext != null) { lobHandler.getLobCreator().setClobAsString(ps, 2, longContext); } else { ps.setNull(2, getClobTypeToUse()); } ps.setLong(3, executionId); } });
@Override public void setValues(PreparedStatement ps, int i) throws SQLException { Long executionId = executionIdIterator.next(); String serializedContext = serializedContexts.get(executionId); String shortContext; String longContext; if (serializedContext.length() > shortContextLength) { // Overestimate length of ellipsis to be on the safe side with // 2-byte chars shortContext = serializedContext.substring(0, shortContextLength - 8) + " ..."; longContext = serializedContext; } else { shortContext = serializedContext; longContext = null; } ps.setString(1, shortContext); if (longContext != null) { lobHandler.getLobCreator().setClobAsString(ps, 2, longContext); } else { ps.setNull(2, getClobTypeToUse()); } ps.setLong(3, executionId); }
protected Integer execute(final CanalEntry.Header header, final List<CanalEntry.Column> columns) { final String sql = getSql(header, columns); final LobCreator lobCreator = dbDialect.getLobHandler().getLobCreator(); dbDialect.getTransactionTemplate().execute(new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { try { JdbcTemplate template = dbDialect.getJdbcTemplate(); int affect = template.update(sql, new PreparedStatementSetter() { public void setValues(PreparedStatement ps) throws SQLException { doPreparedStatement(ps, dbDialect, lobCreator, header, columns); } }); return affect; } finally { lobCreator.close(); } } }); return 0; }
private void setObjectAsBlob(PreparedStatement ps, int paramIndex, Object object) throws SQLException { byte[] bytes = (byte[]) this.conversionService.convert(object, TypeDescriptor.valueOf(Object.class), TypeDescriptor.valueOf(byte[].class)); this.lobHandler.getLobCreator().setBlobAsBytes(ps, paramIndex, bytes); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); preparedStatement = mock(PreparedStatement.class); handler = mock(LobHandler.class); creator = mock(LobCreator.class); given(handler.getLobCreator()).willReturn(creator); }
@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(); }
@Override @SuppressWarnings("unchecked") public <T> Message<T> addMessage(final Message<T> message) { UUID id = message.getHeaders().getId(); Assert.notNull(id, "Cannot store messages without an ID header"); final String messageId = getKey(id); final byte[] messageBytes = this.serializer.convert(message); if (logger.isDebugEnabled()) { logger.debug("Inserting message with id key=" + messageId); } try { this.jdbcTemplate.update(getQuery(Query.CREATE_MESSAGE), ps -> { ps.setString(1, messageId); ps.setString(2, this.region); ps.setTimestamp(3, new Timestamp(System.currentTimeMillis())); this.lobHandler.getLobCreator().setBlobAsBytes(ps, 4, messageBytes); }); } catch (DuplicateKeyException e) { if (logger.isDebugEnabled()) { logger.debug("The Message with id [" + id + "] already exists.\n" + "Ignoring INSERT and SELECT existing..."); } return (Message<T>) getMessage(id); } return message; }
this.lobHandler.getLobCreator().setBlobAsBytes(preparedStatement, 6, messageBytes);
@Override public void setValues(PreparedStatement preparedStatement, Message<?> requestMessage, Object groupId, String region, boolean priorityEnabled) throws SQLException { super.setValues(preparedStatement, requestMessage, groupId, region, priorityEnabled); byte[] messageBytes = this.serializer.convert(requestMessage); this.lobHandler.getLobCreator().setBlobAsBytes(preparedStatement, 6, messageBytes); }
/** * Create a new BLOB value with the given byte array. * @param bytes the byte array containing the BLOB value * @param lobHandler the LobHandler to be used */ public SqlLobValue(byte[] bytes, LobHandler lobHandler) { this.content = bytes; this.length = (bytes != null ? bytes.length : 0); this.lobCreator = lobHandler.getLobCreator(); }
/** * Create a new CLOB value with the given character stream. * @param reader the character stream containing the CLOB value * @param length the length of the CLOB value * @param lobHandler the LobHandler to be used */ public SqlLobValue(Reader reader, int length, LobHandler lobHandler) { this.content = reader; this.length = length; this.lobCreator = lobHandler.getLobCreator(); }
/** * Create a new BLOB/CLOB value with the given stream. * @param stream the stream containing the LOB value * @param length the length of the LOB value * @param lobHandler the LobHandler to be used */ public SqlLobValue(InputStream stream, int length, LobHandler lobHandler) { this.content = stream; this.length = length; this.lobCreator = lobHandler.getLobCreator(); }
/** * Create a new BLOB/CLOB value with the given stream. * @param stream the stream containing the LOB value * @param length the length of the LOB value * @param lobHandler the LobHandler to be used */ public SqlLobValue(InputStream stream, int length, LobHandler lobHandler) { this.content = stream; this.length = length; this.lobCreator = lobHandler.getLobCreator(); }
@Override public void setValues(PreparedStatement ps) throws SQLException { ps.setString(1, shortContext); if (longContext != null) { lobHandler.getLobCreator().setClobAsString(ps, 2, longContext); } else { ps.setNull(2, getClobTypeToUse()); } ps.setLong(3, executionId); } });
public final Integer doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { LobCreator lobCreator = this.lobHandler.getLobCreator(); try { setValues(ps, lobCreator); return ps.executeUpdate(); } finally { lobCreator.close(); } }