public String getClobAsString(ResultSet rs, int columnIndex) throws SQLException { Clob clob = rs.getClob(columnIndex); if (clob == null) { return null; } return clob.getSubString(1, (int) clob.length()); }
@Override @Nullable public String getClobAsString(ResultSet rs, int columnIndex) throws SQLException { logger.debug("Returning CLOB as string"); if (this.wrapAsLob) { Clob clob = rs.getClob(columnIndex); return clob.getSubString(1, (int) clob.length()); } else { return rs.getString(columnIndex); } }
/** * Determine a buffer size for reading the underlying character stream. * * @param value The Clob value * * @return The appropriate buffer size ({@link java.sql.Clob#length()} by default. * * @throws SQLException */ private static long determineLengthForBufferSizing(Clob value) throws SQLException { try { return value.length(); } catch ( SQLFeatureNotSupportedException e ) { return BUFFER_SIZE; } }
private String readClobAsString(Clob logClob) throws SQLException { if (logClob == null) { return StringUtils.EMPTY; } long length = logClob.length(); return logClob.getSubString(1, (int) length); }
static String extractSqlClob(Object value) { if (value == null) return null; try { if (value instanceof Clob) { // we trim the Clob value to a max length an int can hold int maxLength = (((Clob)value).length() < Integer.MAX_VALUE - 2) ? (int)((Clob)value).length() : Integer.MAX_VALUE - 2; return ((Clob)value).getSubString(1L, maxLength); } else { return value.toString(); } } catch (SQLException sqle) { return null; } }
private static String asString(final Clob in) throws SQLException { return in.getSubString(1, (int) in.length()); }
ModelMapEntry(String name, Clob description) { key = name; value = new Model(); value.name = name; try { value.description = description.getSubString(1, (int) description.length()); } catch (SQLException se) { throw new RuntimeException(se); } }
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);} } }
obj = clob.getSubString(1, (int) clob.length());
@Override public String toJavaType(Column column, Class<?> entityType, Clob value) { if (value == null) { return null; } else try { if (value.length() < Integer.MAX_VALUE) { return value.getSubString(1, (int) value.length()); } else { throw new SpeedmentTypeMapperException( "The provided Clob contains too many characters >" + Integer.MAX_VALUE ); } } catch (final SQLException sqle) { throw new SpeedmentTypeMapperException("Unable to convert Clob to String.", sqle); } }
@Override public long position(final Clob clob, final long start) throws SQLException { requireNonNull(clob); assertNotFreeNotCalled(); return position(clob.getSubString(1, (int) clob.length()), start); }
/** * Returns the value as a String. * * @param columnIndex (1,2,...) * @return the value */ @Override public String getString(int columnIndex) throws SQLException { Object o = get(columnIndex); if (o == null) { return null; } switch (columns.get(columnIndex - 1).sqlType) { case Types.CLOB: Clob c = (Clob) o; return c.getSubString(1, MathUtils.convertLongToInt(c.length())); } return o.toString(); }
@Override public long clob_length(ClobProxy clob) throws SQLException { if (this.pos < filterSize) { return nextFilter().clob_length(this, clob); } return clob.getRawClob().length(); }
protected final String extractString(Clob clob) throws SQLException { if ( StringClobImpl.class.isInstance( clob ) ) { return ( (StringClobImpl) clob ).getValue(); } return clob.getSubString( 1, (int)clob.length() ); }
@Override protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException { Clob rsClob; if ( HANAClobTypeDescriptor.this.useUnicodeStringTypes ) { rsClob = rs.getNClob( name ); } else { rsClob = rs.getClob( name ); } if ( rsClob == null || rsClob.length() < HANAClobTypeDescriptor.this.maxLobPrefetchSize ) { return javaTypeDescriptor.wrap( rsClob, options ); } Clob clob = new MaterializedNClob( DataHelper.extractString( rsClob ) ); return javaTypeDescriptor.wrap( clob, options ); }
Clob clob = (Clob) value; try { long length = clob.length(); if (length > Integer.MAX_VALUE) { throw new TypeConversionException("Clob is too big.");
@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" ); } }
@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 void testStringConverter() throws SQLException { StringConverter stringConverter = new StringConverter(); assertNull(stringConverter.convert(null)); assertEquals("123", stringConverter.convert("123")); assertEquals("65,66", stringConverter.convert(arrb(65, 66))); assertEquals("Ab", stringConverter.convert(arrc('A', 'b'))); assertEquals("One,two", stringConverter.convert(arrs("One", "two"))); assertEquals("123", stringConverter.convert(123)); assertEquals("java.lang.String", stringConverter.convert(String.class)); assertEquals("123,456", stringConverter.convert(arri(123,456))); assertEquals("123,456", stringConverter.convert(arrl(123L,456L))); assertEquals("777777.6,-32321.7", stringConverter.convert(arrf(777777.6f, -32321.7F))); assertEquals("777777.6676732,-32321.700985", stringConverter.convert(arrd(777777.6676732D, -32321.700985D))); assertEquals("12,-66", stringConverter.convert(arrs(12,-66))); assertEquals("true,false,true", stringConverter.convert(arrl(true,false,true))); { // Clob via Mock final Clob mock = Mockito.mock(Clob.class); Mockito.when(mock.length()).thenReturn(123456789L); Mockito.when(mock.getSubString(Mockito.eq(1L), Mockito.eq(123456789))).thenReturn("Hello there :-)"); assertEquals("Hello there :-)", stringConverter.convert(mock)); } } }