private static Reader asReader(Object o) throws SQLException { if (o == null) { return null; } else if (o instanceof Clob) { return ((Clob) o).getCharacterStream(); } return (Reader) o; }
public Reader getClobAsCharacterStream(ResultSet rs, int columnIndex) throws SQLException { Clob clob = rs.getClob(columnIndex); if (clob == null) { return null; } return clob.getCharacterStream(); }
@Override public Reader getClobAsCharacterStream(ResultSet rs, int columnIndex) throws SQLException { logger.debug("Returning CLOB as character stream"); if (this.wrapAsLob) { Clob clob = rs.getClob(columnIndex); return clob.getCharacterStream(); } else { return rs.getCharacterStream(columnIndex); } }
private static Reader asReader(Object o) throws SQLException { if (o == null) { return null; } else if (o instanceof Clob) { return ((Clob) o).getCharacterStream(); } return (Reader) o; }
public void set(PreparedStatement stat, Object obj, int index) throws SQLException { if (null == obj) { stat.setNull(index, Types.CLOB); } else { Clob clob = (Clob) obj; Jdbcs.setCharacterStream(index, clob.getCharacterStream(), stat); } }
public String handleClob(Clob clob) throws SQLException { if (clob == null) return null; Reader reader = null; try { reader = clob.getCharacterStream(); if (reader == null) return null; char[] buffer = new char[(int)clob.length()]; if (buffer.length == 0) return null; reader.read(buffer); return new String(buffer); } catch (IOException e) { throw new RuntimeException(e); } finally { if (reader != null) try {reader.close();} catch (IOException e) {throw new RuntimeException(e);} } }
@Override public Reader clob_getCharacterStream(ClobProxy clob, long pos, long length) throws SQLException { if (this.pos < filterSize) { return nextFilter().clob_getCharacterStream(this, clob, pos, length); } return clob.getRawClob().getCharacterStream(pos, length); }
@Override public java.io.Reader clob_getCharacterStream(ClobProxy clob) throws SQLException { if (this.pos < filterSize) { return nextFilter().clob_getCharacterStream(this, clob); } return clob.getRawClob().getCharacterStream(); }
public Object get(ResultSet rs, int columnIndex) throws SQLException { Clob clob = rs.getClob(columnIndex); if (clob == null) return null; File f = this.createTempFile(); Streams.writeAndClose(Streams.fileOutw(f), clob.getCharacterStream()); return new SimpleClob(f); }
public Object get(ResultSet rs, String colName) throws SQLException { Clob clob = rs.getClob(colName); if (clob == null) return null; File f = this.createTempFile(); Streams.writeAndClose(Streams.fileOutw(f), clob.getCharacterStream()); return new SimpleClob(f); }
/** * Clob字段值转字符串 * * @param clob {@link Clob} * @return 字符串 * @since 3.0.6 */ public static String clobToStr(Clob clob) { Reader reader = null; try { reader = clob.getCharacterStream(); return IoUtil.read(reader); } catch (SQLException e) { throw new DbRuntimeException(e); } finally { IoUtil.close(reader); } }
/** * Clob字段值转字符串 * * @param clob {@link Clob} * @return 字符串 * @since 3.0.6 */ public static String clobToStr(Clob clob) { Reader reader = null; try { reader = clob.getCharacterStream(); return IoUtil.read(reader); } catch (SQLException e) { throw new DbRuntimeException(e); } finally { IoUtil.close(reader); } }
@SuppressWarnings({ "unchecked" }) public <X> X unwrap(final Clob value, Class<X> type, WrapperOptions options) { if ( value == null ) { return null; } try { if ( CharacterStream.class.isAssignableFrom( type ) ) { if ( ClobImplementer.class.isInstance( value ) ) { // if the incoming Clob is a wrapper, just pass along its CharacterStream return (X) ( (ClobImplementer) value ).getUnderlyingStream(); } else { // otherwise we need to build a CharacterStream... return (X) new CharacterStreamImpl( DataHelper.extractString( value.getCharacterStream() ) ); } } else if (Clob.class.isAssignableFrom( type )) { final Clob clob = WrappedClob.class.isInstance( value ) ? ( (WrappedClob) value ).getWrappedClob() : value; return (X) clob; } } catch ( SQLException e ) { throw new HibernateException( "Unable to access clob stream", e ); } throw unknownUnwrap( type ); }
/** * Extract the contents of the given Clob as a string. * * @param value The clob to to be extracted from * * @return The content as string */ public static String extractString(final Clob value) { try { final Reader characterStream = value.getCharacterStream(); final long length = determineLengthForBufferSizing( value ); return length > Integer.MAX_VALUE ? extractString( characterStream, Integer.MAX_VALUE ) : extractString( characterStream, (int) length ); } catch ( SQLException e ) { throw new HibernateException( "Unable to access lob stream", e ); } }
@Override public Clob mergeClob(Clob original, Clob target, SharedSessionContractImplementor session) { if ( original == null && target == null ) { return null; } try { final LobCreator lobCreator = session.getFactory().getServiceRegistry().getService( JdbcServices.class ).getLobCreator( session ); return original == null ? lobCreator.createClob( "" ) : lobCreator.createClob( original.getCharacterStream(), original.length() ); } catch (SQLException e) { throw session.getFactory().getSQLExceptionHelper().convert( e, "unable to merge CLOB data" ); } }
/** * Updates a column in the current or insert row. * * @param columnIndex (1,2,...) * @param x the value * @throws SQLException if the result set is closed or not updatable */ @Override public void updateClob(int columnIndex, Clob x) throws SQLException { try { if (isDebugEnabled()) { debugCode("updateClob("+columnIndex+", x);"); } checkClosed(); Value v; if (x == null) { v = ValueNull.INSTANCE; } else { v = conn.createClob(x.getCharacterStream(), -1); } update(columnIndex, v); } catch (Exception e) { throw logAndConvert(e); } }
/** * Updates a column in the current or insert row. * * @param columnLabel the column label * @param x the value * @throws SQLException if the result set is closed or not updatable */ @Override public void updateClob(String columnLabel, Clob x) throws SQLException { try { if (isDebugEnabled()) { debugCode("updateClob("+quote(columnLabel)+", x);"); } checkClosed(); Value v; if (x == null) { v = ValueNull.INSTANCE; } else { v = conn.createClob(x.getCharacterStream(), -1); } update(columnLabel, v); } catch (Exception e) { throw logAndConvert(e); } }
/** * Sets the value of a parameter as a Clob. * * @param parameterIndex the parameter index (1, 2, ...) * @param x the value * @throws SQLException if this object is closed */ @Override public void setClob(int parameterIndex, Clob x) throws SQLException { try { if (isDebugEnabled()) { debugCode("setClob("+parameterIndex+", x);"); } checkClosedForWrite(); try { Value v; if (x == null) { v = ValueNull.INSTANCE; } else { v = conn.createClob(x.getCharacterStream(), -1); } setParameter(parameterIndex, v); } finally { afterWriting(); } } catch (Exception e) { throw logAndConvert(e); } }
@Test public void testClob() { final int id = doInHibernate( this::sessionFactory, s -> { FileClob file = new FileClob(); file.setClob( s.getLobHelper().createClob( "TEST CASE" ) ); // merge transient entity file = (FileClob) s.merge( file ); return file.getId(); } ); doInHibernate( this::sessionFactory, s -> { FileClob file = s.get( FileClob.class, id ); assertFalse( Hibernate.isPropertyInitialized( file, "clob" ) ); Clob clob = file.getClob(); try { final char[] chars = new char[(int) file.getClob().length()]; clob.getCharacterStream().read( chars ); assertTrue( Arrays.equals( "TEST CASE".toCharArray(), chars ) ); } catch (SQLException ex ) { fail( "could not determine Lob length" ); } catch (IOException ex) { fail( "could not read Lob" ); } }); }
@Test public void test() { Integer productId = doInJPA( this::entityManagerFactory, entityManager -> { Session session = entityManager.unwrap( Session.class ); //tag::basic-clob-persist-example[] String warranty = "My product warranty"; final Product product = new Product(); product.setId( 1 ); product.setName( "Mobile phone" ); product.setWarranty( ClobProxy.generateProxy( warranty ) ); entityManager.persist( product ); //end::basic-clob-persist-example[] return product.getId(); } ); doInJPA( this::entityManagerFactory, entityManager -> { try { //tag::basic-clob-find-example[] Product product = entityManager.find( Product.class, productId ); try (Reader reader = product.getWarranty().getCharacterStream()) { assertEquals( "My product warranty", toString( reader ) ); } //end::basic-clob-find-example[] } catch (Exception e) { fail( e.getMessage() ); } } ); }