Search in sources :

Example 51 with LoopingAlphabetReader

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

the class ClobAccessTest method fetchPieceByPiece.

/**
 * Fetches a "large" Clob piece by piece using getSubString.
 *
 * @param modifyClob whether to modify the Clob before fetching it
 *      (determines the internal Derby Clob representation)
 */
private void fetchPieceByPiece(final boolean modifyClob) throws IOException, SQLException {
    // Select just one Clob.
    PreparedStatement ps = prepareStatement("select dClob, length from largeClobs where id = 4");
    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");
        }
        long pos = 1;
        while (remaining > 0) {
            String str = clob.getSubString(pos, Math.min(MAX_BSIZE, remaining));
            myReader.skip(Math.min(MAX_BSIZE, remaining) - 1);
            pos += str.length();
            remaining -= str.length();
            // Avoid failure on the last char when Clob is modified.
            if (!modifyClob || remaining != 0) {
                assertEquals(myReader.read(), str.charAt(str.length() - 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)

Example 52 with LoopingAlphabetReader

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

the class ReaderToUTF8StreamTest method getStream.

/**
 * Returns a stream to test, loaded with the repeating modern latin
 * lowercase alphabet.
 *
 * @param length the length of the stream in characters
 * @return A stream serving bytes.
 */
private InputStream getStream(int length) {
    Reader src = new LoopingAlphabetReader(length, CharAlphabet.modernLatinLowercase());
    InputStream is = new ReaderToUTF8Stream(src, length, 0, "CLOB", new ClobStreamHeaderGenerator(false));
    assertTrue("The stream doesn't support mark/reset", is.markSupported());
    return is;
}
Also used : ClobStreamHeaderGenerator(org.apache.derby.iapi.types.ClobStreamHeaderGenerator) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ReaderToUTF8Stream(org.apache.derby.iapi.types.ReaderToUTF8Stream) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 53 with LoopingAlphabetReader

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

the class UTF8UtilTest method testSkipFullyOnInvalidStreamCJK.

/**
 * Tests that <code>skipFully</code> throws exception if there is a UTF-8
 * encoding error in the stream
 *
 * @throws IOException if the test fails for some unexpected reason
 */
public void testSkipFullyOnInvalidStreamCJK() throws IOException {
    final int charLength = 10;
    InputStream in = new ReaderToUTF8Stream(new LoopingAlphabetReader(charLength, CharAlphabet.cjkSubset()), charLength, 0, TYPENAME, new CharStreamHeaderGenerator());
    // Skip encoded length added by ReaderToUTF8Stream.
    in.skip(HEADER_LENGTH);
    // Skip one more byte to trigger a UTF error.
    in.skip(1L);
    try {
        UTF8Util.skipFully(in, charLength);
        fail("Should have failed because of UTF error.");
    } catch (UTFDataFormatException udfe) {
    // As expected, do nothing.
    }
}
Also used : UTFDataFormatException(java.io.UTFDataFormatException) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ReaderToUTF8Stream(org.apache.derby.iapi.types.ReaderToUTF8Stream) CharStreamHeaderGenerator(org.apache.derby.iapi.types.CharStreamHeaderGenerator) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 54 with LoopingAlphabetReader

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

the class StreamTest method insertClobData.

private void insertClobData(int clobSize) throws SQLException {
    Statement stmt = createStatement();
    stmt.executeUpdate("CREATE TABLE testLob " + " (c clob(" + clobSize + "))");
    stmt.close();
    PreparedStatement ps = prepareStatement("insert into testLob values(?)");
    Reader reader = new LoopingAlphabetReader(clobSize);
    ps.setCharacterStream(1, reader, clobSize);
    ps.executeUpdate();
    ps.close();
}
Also used : LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 55 with LoopingAlphabetReader

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

the class TriggerTest method testUpdateTriggerOnClobColumn.

/* 
     * Test an update trigger on a Clob column
     * 
     */
public void testUpdateTriggerOnClobColumn() throws SQLException, IOException {
    // Alphabet used when inserting a CLOB.
    CharAlphabet a1 = CharAlphabet.singleChar('a');
    // Alphabet used when updating a CLOB.
    CharAlphabet a2 = CharAlphabet.singleChar('b');
    Connection conn = getConnection();
    Statement s = createStatement();
    String trig = " create trigger t_lob1 after update of str1 on lob1 ";
    trig = trig + " REFERENCING OLD AS old NEW AS new FOR EACH ROW MODE DB2SQL ";
    trig = trig + " insert into t_lob1_log(oldvalue, newvalue) values (old.str1, new.str1)";
    s.executeUpdate("create table LOB1 (str1 Varchar(80), C_lob CLOB(50M))");
    s.executeUpdate("create table t_lob1_log(oldvalue varchar(80), newvalue varchar(80), chng_time timestamp default current_timestamp)");
    s.executeUpdate(trig);
    conn.commit();
    PreparedStatement ps = prepareStatement("INSERT INTO LOB1 VALUES (?, ?)");
    int clobSize = 1024 * 64 + 1;
    ps.setString(1, clobSize + "");
    // - set the value of the input parameter to the input stream
    ps.setCharacterStream(2, new LoopingAlphabetReader(clobSize, a1), clobSize);
    ps.execute();
    conn.commit();
    PreparedStatement ps2 = prepareStatement("update LOB1 set c_lob = ? where str1 = '" + clobSize + "'");
    ps2.setCharacterStream(1, new LoopingAlphabetReader(clobSize, a2), clobSize);
    ps2.executeUpdate();
    conn.commit();
    // --- reading the clob make sure it was updated
    ResultSet rs = s.executeQuery("SELECT * FROM LOB1 where str1 = '" + clobSize + "'");
    rs.next();
    assertEquals(new LoopingAlphabetReader(clobSize, a2), rs.getCharacterStream(2));
    rs.close();
    s.executeUpdate("drop table lob1");
    s.executeUpdate("drop table t_lob1_log");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CharAlphabet(org.apache.derbyTesting.functionTests.util.streams.CharAlphabet) 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