Search in sources :

Example 46 with LoopingAlphabetReader

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.

the class Derby6884Test method testDerby6884ImportLargeExtfileClob.

/*
     * DERBY-6884: external file behaviors when the file contains more than
     * Integer.MAX_VALUE bytes of data.
     */
public void testDerby6884ImportLargeExtfileClob() throws SQLException {
    Statement s = createStatement();
    PreparedStatement ps = prepareStatement("insert into DERBY_6884_TESTCLOB values(? , ?)");
    int id = 1;
    long byteCount = 0;
    int clobSize = 0;
    while (byteCount < Integer.MAX_VALUE) {
        ps.setInt(1, id++);
        clobSize = (10000 * 1024) + (1024 * id);
        byteCount += clobSize;
        Reader reader = new LoopingAlphabetReader(clobSize);
        ps.setCharacterStream(2, reader, clobSize);
        ps.executeUpdate();
    }
    ps.setInt(1, id++);
    Reader reader = new LoopingAlphabetReader(clobSize);
    ps.setCharacterStream(2, reader, clobSize);
    ps.executeUpdate();
    commit();
    doExportTableLobsToExtFile("APP", "DERBY_6884_TESTCLOB", fileName6884, null, null, null, lobFile6884);
    s.execute("TRUNCATE TABLE DERBY_6884_TESTCLOB");
    doImportTableLobsFromExtFile("APP", "DERBY_6884_TESTCLOB", fileName6884, null, null, null, 0);
    SupportFilesSetup.deleteFile(fileName6884);
    SupportFilesSetup.deleteFile(lobFile6884);
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 47 with LoopingAlphabetReader

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.

the class MultiByteClobTest method testSmallMultiByteCharLob.

public void testSmallMultiByteCharLob() throws SQLException, IOException {
    setAutoCommit(false);
    Statement s = createStatement();
    PreparedStatement ps = prepareStatement("INSERT INTO MB_CLOBTABLE VALUES(?,?)");
    // We allocate 16MB for the test so use something bigger than that.
    ps.setInt(1, 1);
    LoopingAlphabetReader reader = new LoopingAlphabetReader(SHORT_CLOB_LENGTH, CharAlphabet.cjkSubset());
    ps.setCharacterStream(2, reader, SHORT_CLOB_LENGTH);
    ps.executeUpdate();
    ResultSet rs = s.executeQuery("SELECT K, LENGTH(C), C FROM MB_CLOBTABLE" + "-- DERBY-PROPERTIES constraint=pk\n ORDER BY K");
    rs.next();
    assertEquals(SHORT_CLOB_LENGTH_STRING, rs.getString(2));
    // make sure we can still access the clob after getting length.
    // It should be ok because we reset the stream
    Reader rsReader = rs.getCharacterStream(3);
    int len = 0;
    char[] buf = new char[32672];
    for (; ; ) {
        int size = rsReader.read(buf);
        if (size == -1)
            break;
        len += size;
        int expectedValue = ((len - 1) % 12) + '\u4E00';
        if (size != 0)
            assertEquals(expectedValue, buf[size - 1]);
    }
    assertEquals(SHORT_CLOB_LENGTH, len);
    rs.close();
    // Select just length without selecting the clob.
    rs = s.executeQuery("SELECT K, LENGTH(C)  FROM MB_CLOBTABLE " + "ORDER BY K");
    JDBC.assertFullResultSet(rs, new String[][] { { "1", SHORT_CLOB_LENGTH_STRING } });
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 48 with LoopingAlphabetReader

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.

the class MultiByteClobTest method testLargeMultiByteCharLob.

public void testLargeMultiByteCharLob() throws SQLException, IOException {
    setAutoCommit(false);
    Statement s = createStatement();
    PreparedStatement ps = prepareStatement("INSERT INTO MB_CLOBTABLE VALUES(?,?)");
    // We allocate 16MB for the test so use something bigger than that.
    ps.setInt(1, 1);
    LoopingAlphabetReader reader = new LoopingAlphabetReader(LONG_CLOB_LENGTH, CharAlphabet.cjkSubset());
    ps.setCharacterStream(2, reader, LONG_CLOB_LENGTH);
    ps.executeUpdate();
    ResultSet rs = s.executeQuery("SELECT K, LENGTH(C), C FROM MB_CLOBTABLE" + "-- DERBY-PROPERTIES constraint=pk\n ORDER BY K");
    rs.next();
    assertEquals(LONG_CLOB_LENGTH_STRING, rs.getString(2));
    // make sure we can still access the clob after getting length.
    // It should be ok because we reset the stream
    Reader rsReader = rs.getCharacterStream(3);
    int len = 0;
    char[] buf = new char[32672];
    for (; ; ) {
        int size = rsReader.read(buf);
        if (size == -1)
            break;
        len += size;
        int expectedValue = ((len - 1) % 12) + '\u4E00';
        if (size != 0)
            assertEquals(expectedValue, buf[size - 1]);
    }
    assertEquals(LONG_CLOB_LENGTH, len);
    rs.close();
    // Select just length without selecting the clob.
    rs = s.executeQuery("SELECT K, LENGTH(C)  FROM MB_CLOBTABLE " + "ORDER BY K");
    JDBC.assertFullResultSet(rs, new String[][] { { "1", LONG_CLOB_LENGTH_STRING } });
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 49 with LoopingAlphabetReader

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.

the class ClobMemTest method testDerby4477_3645_3646_Repro_lowmem_clob.

/**
 * Tests that a clob can be safely occur multiple times in a SQL
 * select and test that large objects streams are not being
 * materialized when cloned.  Same as
 * testDerby4477_3645_3646_Repro_lowmem, but now using clob rather
 * than blob.
 * @see BlobMemTest#testDerby4477_3645_3646_Repro_lowmem
 */
public void testDerby4477_3645_3646_Repro_lowmem_clob() throws SQLException, IOException {
    setAutoCommit(false);
    Statement s = createStatement();
    int clobsize = LONG_CLOB_LENGTH;
    s.executeUpdate("CREATE TABLE T_MAIN(" + "ID INT  GENERATED ALWAYS AS IDENTITY PRIMARY KEY, " + "V CLOB(" + clobsize + ") )");
    PreparedStatement ps = prepareStatement("INSERT INTO T_MAIN(V) VALUES (?)");
    int blobLen = clobsize;
    LoopingAlphabetReader stream = new LoopingAlphabetReader(blobLen);
    ps.setCharacterStream(1, stream, blobLen);
    ps.executeUpdate();
    ps.close();
    s.executeUpdate("CREATE TABLE T_COPY ( V1 CLOB(" + clobsize + "), V2 CLOB(" + clobsize + "))");
    // This failed in the repro for DERBY-3645 solved as part of
    // DERBY-4477:
    s.executeUpdate("INSERT INTO T_COPY SELECT  V, V FROM T_MAIN");
    // Check that the two results are identical:
    ResultSet rs = s.executeQuery("SELECT * FROM T_COPY");
    rs.next();
    Reader is = rs.getCharacterStream(1);
    stream = new LoopingAlphabetReader(blobLen);
    assertEquals(stream, is);
    is = rs.getCharacterStream(2);
    stream = new LoopingAlphabetReader(blobLen);
    assertEquals(stream, is);
    rs.close();
    // This failed in the repro for DERBY-3646 solved as part of
    // DERBY-4477 (repro slightly rewoked here):
    rs = s.executeQuery("SELECT 'I', V, ID, V from T_MAIN");
    rs.next();
    is = rs.getCharacterStream(2);
    stream = new LoopingAlphabetReader(blobLen);
    assertEquals(stream, is);
    is = rs.getCharacterStream(4);
    stream = new LoopingAlphabetReader(blobLen);
    assertEquals(stream, is);
    // clean up
    stream.close();
    is.close();
    s.close();
    rs.close();
    rollback();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 50 with LoopingAlphabetReader

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.

the class ClobAccessTest method testFetchLargeClobWithStream.

public void testFetchLargeClobWithStream() throws IOException, SQLException {
    boolean modifyClob = false;
    // Select just one Clob.
    PreparedStatement ps = prepareStatement("select dClob, length from largeClobs where id = 5");
    ResultSet rs = ps.executeQuery();
    while (rs.next()) {
        Clob clob = rs.getClob(1);
        int remaining = rs.getInt(2);
        Reader myReader = new LoopingAlphabetReader(remaining);
        if (modifyClob) {
            // Modify the Clob to create a temporary copy in memory or on
            // disk (depends on the Clob size).
            long modifyStart = System.currentTimeMillis();
            clob.setString(++remaining, "X");
            println("Clob modification duration: " + (System.currentTimeMillis() - modifyStart) + " ms");
        }
        Reader clobReader = clob.getCharacterStream();
        char[] buf = new char[MAX_BSIZE];
        while (remaining > 0) {
            int read = clobReader.read(buf, 0, Math.min(MAX_BSIZE, remaining));
            myReader.skip(read - 1);
            remaining -= read;
            assertEquals(myReader.read(), buf[read - 1]);
        }
    }
    rs.close();
}
Also used : ResultSet(java.sql.ResultSet) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) PreparedStatement(java.sql.PreparedStatement) Clob(java.sql.Clob) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Aggregations

LoopingAlphabetReader (org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)57 PreparedStatement (java.sql.PreparedStatement)35 Reader (java.io.Reader)23 ResultSet (java.sql.ResultSet)23 Statement (java.sql.Statement)20 InputStream (java.io.InputStream)15 SQLException (java.sql.SQLException)10 Clob (java.sql.Clob)9 LoopingAlphabetStream (org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 StringReader (java.io.StringReader)8 ReaderToUTF8Stream (org.apache.derby.iapi.types.ReaderToUTF8Stream)8 DataInputStream (java.io.DataInputStream)7 Connection (java.sql.Connection)6 CharAlphabet (org.apache.derbyTesting.functionTests.util.streams.CharAlphabet)6 CharStreamHeaderGenerator (org.apache.derby.iapi.types.CharStreamHeaderGenerator)5 BufferedReader (java.io.BufferedReader)4 CharArrayReader (java.io.CharArrayReader)4 CallableStatement (java.sql.CallableStatement)4 Timestamp (java.sql.Timestamp)3