Search in sources :

Example 1 with Clob

use of java.sql.Clob in project sonarqube by SonarSource.

the class AbstractDbTester method getHashMap.

private static List<Map<String, Object>> getHashMap(ResultSet resultSet) throws Exception {
    ResultSetMetaData metaData = resultSet.getMetaData();
    int colCount = metaData.getColumnCount();
    List<Map<String, Object>> rows = newArrayList();
    while (resultSet.next()) {
        Map<String, Object> columns = newHashMap();
        for (int i = 1; i <= colCount; i++) {
            Object value = resultSet.getObject(i);
            if (value instanceof Clob) {
                Clob clob = (Clob) value;
                value = IOUtils.toString((clob.getAsciiStream()));
                doClobFree(clob);
            } else if (value instanceof BigDecimal) {
                // In Oracle, INTEGER types are mapped as BigDecimal
                BigDecimal bgValue = ((BigDecimal) value);
                if (bgValue.scale() == 0) {
                    value = bgValue.longValue();
                } else {
                    value = bgValue.doubleValue();
                }
            } else if (value instanceof Integer) {
                // To be consistent, all INTEGER types are mapped as Long
                value = ((Integer) value).longValue();
            }
            columns.put(metaData.getColumnLabel(i), value);
        }
        rows.add(columns);
    }
    return rows;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Clob(java.sql.Clob) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) BigDecimal(java.math.BigDecimal)

Example 2 with Clob

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

the class StatFilter method buildSlowParameters.

private String buildSlowParameters(StatementProxy statement) {
    JSONWriter out = new JSONWriter();
    out.writeArrayStart();
    for (int i = 0, parametersSize = statement.getParametersSize(); i < parametersSize; ++i) {
        JdbcParameter parameter = statement.getParameter(i);
        if (i != 0) {
            out.writeComma();
        }
        if (parameter == null) {
            continue;
        }
        Object value = parameter.getValue();
        if (value == null) {
            out.writeNull();
        } else if (value instanceof String) {
            String text = (String) value;
            if (text.length() > 100) {
                out.writeString(text.substring(0, 97) + "...");
            } else {
                out.writeString(text);
            }
        } else if (value instanceof Number) {
            out.writeObject(value);
        } else if (value instanceof java.util.Date) {
            out.writeObject(value);
        } else if (value instanceof Boolean) {
            out.writeObject(value);
        } else if (value instanceof InputStream) {
            out.writeString("<InputStream>");
        } else if (value instanceof NClob) {
            out.writeString("<NClob>");
        } else if (value instanceof Clob) {
            out.writeString("<Clob>");
        } else if (value instanceof Blob) {
            out.writeString("<Blob>");
        } else {
            out.writeString('<' + value.getClass().getName() + '>');
        }
    }
    out.writeArrayEnd();
    return out.toString();
}
Also used : JSONWriter(com.alibaba.druid.support.json.JSONWriter) NClob(java.sql.NClob) Blob(java.sql.Blob) JdbcParameter(com.alibaba.druid.proxy.jdbc.JdbcParameter) Date(java.util.Date) InputStream(java.io.InputStream) Savepoint(java.sql.Savepoint) NClob(java.sql.NClob) Clob(java.sql.Clob)

Example 3 with Clob

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

the class ResultSetProxyImpl method getClob.

@Override
public Clob getClob(int columnIndex) throws SQLException {
    FilterChainImpl chain = createChain();
    Clob value = chain.resultSet_getClob(this, columnIndex);
    recycleFilterChain(chain);
    return value;
}
Also used : FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) NClob(java.sql.NClob) Clob(java.sql.Clob)

Example 4 with Clob

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

the class DruidLobCreator method setClobAsAsciiStream.

@Override
public void setClobAsAsciiStream(PreparedStatement ps, int paramIndex, InputStream asciiStream, int contentLength) throws SQLException {
    if (asciiStream != null) {
        Clob clob = ps.getConnection().createClob();
        OutputStream out = clob.setAsciiStream(1);
        final int BUFFER_SIZE = 4096;
        try {
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead = -1;
            while ((bytesRead = asciiStream.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
            out.flush();
        } catch (Exception e) {
            throw new SQLException("setClob error", e);
        } finally {
            JdbcUtils.close(asciiStream);
            JdbcUtils.close(out);
        }
        ps.setClob(paramIndex, clob);
    } else {
        ps.setClob(paramIndex, (Clob) null);
    }
}
Also used : SQLException(java.sql.SQLException) OutputStream(java.io.OutputStream) Clob(java.sql.Clob) SQLException(java.sql.SQLException)

Example 5 with Clob

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

the class FilterChainTest_Clob method test_resultSet_getClob_1.

public void test_resultSet_getClob_1() throws Exception {
    FilterChainImpl chain = new FilterChainImpl(dataSource);
    Clob clob = chain.resultSet_getClob(new ResultSetProxyImpl(statement, mockResultSet, 1, null), "1");
    Assert.assertTrue(clob instanceof ClobProxy);
    Assert.assertEquals(1, invokeCount);
}
Also used : FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) ClobProxy(com.alibaba.druid.proxy.jdbc.ClobProxy) MockClob(com.alibaba.druid.mock.MockClob) Clob(java.sql.Clob) ResultSetProxyImpl(com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl)

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