Search in sources :

Example 21 with Clob

use of java.sql.Clob in project fastjson by alibaba.

the class ClobSeriliazer method write.

public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
    try {
        if (object == null) {
            serializer.writeNull();
            return;
        }
        Clob clob = (Clob) object;
        Reader reader = clob.getCharacterStream();
        StringBuilder buf = new StringBuilder();
        try {
            char[] chars = new char[2048];
            for (; ; ) {
                int len = reader.read(chars, 0, chars.length);
                if (len < 0) {
                    break;
                }
                buf.append(chars, 0, len);
            }
        } catch (Exception ex) {
            throw new JSONException("read string from reader error", ex);
        }
        String text = buf.toString();
        reader.close();
        serializer.write(text);
    } catch (SQLException e) {
        throw new IOException("write clob error", e);
    }
}
Also used : SQLException(java.sql.SQLException) Reader(java.io.Reader) JSONException(com.alibaba.fastjson.JSONException) IOException(java.io.IOException) Clob(java.sql.Clob) SQLException(java.sql.SQLException) JSONException(com.alibaba.fastjson.JSONException) IOException(java.io.IOException)

Example 22 with Clob

use of java.sql.Clob in project hibernate-orm by hibernate.

the class StringValueMappingTest method testNormalClobHandling.

@Test
public void testNormalClobHandling() throws SQLException {
    final ValueExtractor<String> extractor = clobSqlDescriptor.getExtractor(stringJavaDescriptor);
    final ValueBinder<String> binder = clobSqlDescriptor.getBinder(stringJavaDescriptor);
    final String fixture = "clob string";
    final Clob clob = new StringClobImpl(fixture);
    ResultSet resultSet = ResultSetProxy.generateProxy(clob);
    final String value = extractor.extract(resultSet, COLUMN_NAME, wrapperOptions);
    assertEquals(fixture, value);
    PreparedStatement ps = PreparedStatementProxy.generateProxy(clob);
    binder.bind(ps, fixture, BIND_POSITION, wrapperOptions);
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Clob(java.sql.Clob) Test(org.junit.Test)

Example 23 with Clob

use of java.sql.Clob in project jOOQ by jOOQ.

the class DefaultExecuteContext method clean.

/**
     * Clean up blobs, clobs and the local configuration.
     * <p>
     * <h5>BLOBS and CLOBS</h5>
     * <p>
     * [#1326] This is necessary in those dialects that have long-lived
     * temporary lob objects, which can cause memory leaks in certain contexts,
     * where the lobs' underlying session / connection is long-lived as well.
     * Specifically, Oracle and ojdbc have some trouble when streaming temporary
     * lobs to UDTs:
     * <ol>
     * <li>The lob cannot have a call-scoped life time with UDTs</li>
     * <li>Freeing the lob after binding will cause an ORA-22275</li>
     * <li>Not freeing the lob after execution will cause an
     * {@link OutOfMemoryError}</li>
     * </ol>
     * <p>
     * <h5>Local configuration</h5>
     * <p>
     * [#1544] There exist some corner-cases regarding the {@link SQLOutput}
     * API, used for UDT serialisation / deserialisation, which have no elegant
     * solutions of obtaining a {@link Configuration} and thus a JDBC
     * {@link Connection} object short of:
     * <ul>
     * <li>Making assumptions about the JDBC driver and using proprietary API,
     * e.g. that of ojdbc</li>
     * <li>Dealing with this problem globally by using such a local
     * configuration</li>
     * </ul>
     *
     * @see <a
     *      href="http://stackoverflow.com/q/11439543/521799">http://stackoverflow.com/q/11439543/521799</a>
     */
static final void clean() {
    List<Blob> blobs = BLOBS.get();
    List<Clob> clobs = CLOBS.get();
    List<SQLXML> xmls = SQLXMLS.get();
    List<Array> arrays = ARRAYS.get();
    if (blobs != null) {
        for (Blob blob : blobs) {
            JDBCUtils.safeFree(blob);
        }
        BLOBS.remove();
    }
    if (clobs != null) {
        for (Clob clob : clobs) {
            JDBCUtils.safeFree(clob);
        }
        CLOBS.remove();
    }
    if (xmls != null) {
        for (SQLXML xml : xmls) {
            JDBCUtils.safeFree(xml);
        }
        SQLXMLS.remove();
    }
    if (arrays != null) {
        for (Array array : arrays) {
            JDBCUtils.safeFree(array);
        }
        SQLXMLS.remove();
    }
    LOCAL_CONFIGURATION.remove();
    LOCAL_DATA.remove();
    LOCAL_CONNECTION.remove();
}
Also used : Array(java.sql.Array) Blob(java.sql.Blob) SQLXML(java.sql.SQLXML) Clob(java.sql.Clob)

Example 24 with Clob

use of java.sql.Clob in project jOOQ by jOOQ.

the class ClobBinding method set.

@Override
public final void set(BindingSetSQLOutputContext<String> ctx) throws SQLException {
    Clob clob = newClob(ctx.configuration(), ctx.value());
    DefaultExecuteContext.register(clob);
    ctx.output().writeClob(clob);
}
Also used : Clob(java.sql.Clob)

Example 25 with Clob

use of java.sql.Clob in project jOOQ by jOOQ.

the class ClobBinding method newClob.

private final Clob newClob(Configuration configuration, String string) throws SQLException {
    Connection c = configuration.connectionProvider().acquire();
    try {
        Clob clob = null;
        switch(configuration.family()) {
            default:
                {
                    clob = c.createClob();
                    break;
                }
        }
        clob.setString(1, string);
        return clob;
    } finally {
        configuration.connectionProvider().release(c);
    }
}
Also used : Connection(java.sql.Connection) Clob(java.sql.Clob)

Aggregations

Clob (java.sql.Clob)96 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)27 Blob (java.sql.Blob)24 SQLException (java.sql.SQLException)20 ResultSet (java.sql.ResultSet)16 NClob (java.sql.NClob)14 MockClob (com.alibaba.druid.mock.MockClob)13 ClobProxy (com.alibaba.druid.proxy.jdbc.ClobProxy)13 MockNClob (com.alibaba.druid.mock.MockNClob)12 NClobProxy (com.alibaba.druid.proxy.jdbc.NClobProxy)12 ResultSetProxyImpl (com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl)12 PreparedStatement (java.sql.PreparedStatement)10 IOException (java.io.IOException)8 InputStream (java.io.InputStream)8 Reader (java.io.Reader)7 Test (org.junit.Test)7 StringReader (java.io.StringReader)6 Connection (java.sql.Connection)6 Timestamp (java.sql.Timestamp)5 CUBRIDOIDProxy (com.cubrid.jdbc.proxy.driver.CUBRIDOIDProxy)4