Search in sources :

Example 21 with LoopingAlphabetReader

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

the class ClobTest method insertDataWithToken.

/**
 * Inserts data into the test Clob, referenced by {@code this.clob}.
 *
 * @param token a token to insert into the Clob, cannot be {@code null} but
 *      the empty string is accepted
 * @param pre number of characters to insert before the token, using the
 *      repeating alphabet stream (latin lower-case)
 * @param post number of characters to insert after the token, using the
 *      repeating alphabet stream (latin lower-case)
 * @param mode insertion mode; SET_STRING, SET_ASCII_STREAM or
 *      SET_CHARACTER_STREAM
 * @throws IOException if inserting data fails for some reason
 * @throws SQLException if inserting data fails for some reason
 */
private void insertDataWithToken(String token, long pre, long post, int mode) throws IOException, SQLException {
    long total = 0;
    switch(mode) {
        case SET_STRING:
            {
                Reader charIn = new LoopingAlphabetReader(pre);
                total += transferData(charIn, TRANSFER_BUFFER_SIZE);
                this.clob.setString(pre + 1, token);
                total += token.length();
                charIn = new LoopingAlphabetReader(post);
                total += transferData(charIn, TRANSFER_BUFFER_SIZE);
                break;
            }
        case SET_ASCII_STREAM:
            {
                OutputStream asciiOut = this.clob.setAsciiStream(1L);
                InputStream asciiIn = new LoopingAlphabetStream(pre);
                total += transferData(asciiIn, asciiOut, TRANSFER_BUFFER_SIZE);
                byte[] tokenBytes = token.getBytes("ISO-8859-1");
                asciiOut.write(tokenBytes, 0, tokenBytes.length);
                total += tokenBytes.length;
                asciiIn = new LoopingAlphabetStream(post);
                total += transferData(asciiIn, asciiOut, TRANSFER_BUFFER_SIZE);
                break;
            }
        case SET_CHARACTER_STREAM:
            {
                Writer charOut = this.clob.setCharacterStream(1L);
                Reader charIn = new LoopingAlphabetReader(pre);
                total += transferData(charIn, charOut, TRANSFER_BUFFER_SIZE);
                charOut.write(token);
                total += token.length();
                charIn = new LoopingAlphabetReader(post);
                total += transferData(charIn, charOut, TRANSFER_BUFFER_SIZE);
                break;
            }
        default:
            throw new IllegalArgumentException("Unknown insertion mode: " + mode);
    }
    assertEquals("Invalid length after insertion", pre + post + token.length(), this.clob.length());
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream) BufferedWriter(java.io.BufferedWriter) CharArrayWriter(java.io.CharArrayWriter) Writer(java.io.Writer) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 22 with LoopingAlphabetReader

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

the class ClobUpdatableReaderTest method testMultiplexedOperationProblem.

/**
 * Tests that the Clob can handle multiple streams and the length call
 * multiplexed.
 * <p>
 * This test was written after bug DERBY-2806 was reported, where getting
 * the length of the Clob after fetching a stream from it would exhaust
 * the stream and cause the next read to return -1.
 * <p>
 * The test is written to work on a Clob that operates on streams from
 * the store, which currently means that it must be over a certain size
 * and that no modifying methods can be called on it.
 */
public void testMultiplexedOperationProblem() throws IOException, SQLException {
    getConnection().setAutoCommit(false);
    int length = 266000;
    PreparedStatement ps = prepareStatement("insert into updateClob (id, data) values (?,?)");
    ps.setInt(1, length);
    ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
    assertEquals(1, ps.executeUpdate());
    ps.close();
    PreparedStatement psFetchClob = prepareStatement("select data from updateClob where id = ?");
    psFetchClob.setInt(1, length);
    ResultSet rs = psFetchClob.executeQuery();
    assertTrue("No Clob of length " + length + " in database", rs.next());
    Clob clob = rs.getClob(1);
    assertEquals(length, clob.length());
    Reader r = clob.getCharacterStream();
    int lastReadChar = r.read();
    lastReadChar = assertCorrectChar(lastReadChar, r.read());
    lastReadChar = assertCorrectChar(lastReadChar, r.read());
    assertEquals(length, clob.length());
    // Must be bigger than internal buffers might be.
    int nextChar;
    for (int i = 2; i < 160000; i++) {
        nextChar = r.read();
        // Check manually to report position where it fails.
        if (nextChar == -1) {
            fail("Failed at position " + i + ", stream should not be" + " exhausted now");
        }
        lastReadChar = assertCorrectChar(lastReadChar, nextChar);
    }
    lastReadChar = assertCorrectChar(lastReadChar, r.read());
    lastReadChar = assertCorrectChar(lastReadChar, r.read());
    InputStream ra = clob.getAsciiStream();
    assertEquals(length, clob.length());
    int lastReadAscii = ra.read();
    lastReadAscii = assertCorrectChar(lastReadAscii, ra.read());
    lastReadAscii = assertCorrectChar(lastReadAscii, ra.read());
    assertEquals(length, clob.length());
    lastReadAscii = assertCorrectChar(lastReadAscii, ra.read());
    lastReadChar = assertCorrectChar(lastReadChar, r.read());
    // Close resources.
    r.close();
    ra.close();
    rs.close();
    psFetchClob.close();
}
Also used : InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) StringReader(java.io.StringReader) PreparedStatement(java.sql.PreparedStatement) Clob(java.sql.Clob) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 23 with LoopingAlphabetReader

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

the class ClobTest method insertAndFetchTest.

/**
 * Inserts a Clob with the specified length, using a stream source, then
 * fetches it from the database and checks the length.
 *
 * @param length number of characters in the Clob
 * @throws IOException if reading from the source fails
 * @throws SQLException if something goes wrong
 */
private void insertAndFetchTest(long length) throws IOException, SQLException {
    PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)");
    int id = BlobClobTestSetup.getID();
    ps.setInt(1, id);
    ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
    long tsStart = System.currentTimeMillis();
    ps.execute();
    println("Inserted " + length + " chars (length specified) in " + (System.currentTimeMillis() - tsStart) + " ms");
    Statement stmt = createStatement();
    tsStart = System.currentTimeMillis();
    ResultSet rs = stmt.executeQuery("select CLOBDATA from BLOBCLOB where id = " + id);
    assertTrue("Clob not inserted", rs.next());
    Clob aClob = rs.getClob(1);
    assertEquals("Invalid length", length, aClob.length());
    println("Fetched length (" + length + ") in " + (System.currentTimeMillis() - tsStart) + " ms");
    rs.close();
    // Insert same Clob again, using the lengthless override.
    id = BlobClobTestSetup.getID();
    ps.setInt(1, id);
    ps.setCharacterStream(2, new LoopingAlphabetReader(length));
    tsStart = System.currentTimeMillis();
    ps.executeUpdate();
    println("Inserted " + length + " chars (length unspecified) in " + (System.currentTimeMillis() - tsStart) + " ms");
    rs = stmt.executeQuery("select CLOBDATA from BLOBCLOB where id = " + id);
    assertTrue("Clob not inserted", rs.next());
    aClob = rs.getClob(1);
    assertEquals("Invalid length", length, aClob.length());
    println("Fetched length (" + length + ") in " + (System.currentTimeMillis() - tsStart) + " ms");
    rs.close();
    rollback();
}
Also used : LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 24 with LoopingAlphabetReader

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

the class ClobTest method testGetCharacterStreamLongLastCharLatin.

/**
 * Obtains streams from the Clob reading portions of the content, always
 * including the last character in the Clob.
 * <p>
 * This case fills the Clob with latin lowercase characters.
 */
public void testGetCharacterStreamLongLastCharLatin() throws IOException, SQLException {
    CharAlphabet alphabet = CharAlphabet.modernLatinLowercase();
    // Insert a Clob
    int length = 5000;
    PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)");
    int id = BlobClobTestSetup.getID();
    ps.setInt(1, id);
    ps.setCharacterStream(2, new LoopingAlphabetReader(length, alphabet), length);
    ps.execute();
    ps.close();
    // Perform the actual test.
    getCharacterStreamLongLastChar(id, length, alphabet);
}
Also used : CharAlphabet(org.apache.derbyTesting.functionTests.util.streams.CharAlphabet) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 25 with LoopingAlphabetReader

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

the class Derby2017LayerBTest method testStreamInsertCharBufferBoundary.

public void testStreamInsertCharBufferBoundary() throws IOException, SQLException {
    // NOTE: Many of these lengths are implementation dependent, and the
    // code paths in LayerBStreamedEXTDTAReaderInputStream may change
    // if the implementation of certain points of the DRDA protocol
    // changes.
    int[] lengths = new int[] { 1, 16383, 0, 32756, 36383, 16384, // Just a longer stream
    192 * 1024 };
    rollback();
    Statement stmt = createStatement();
    try {
        stmt.executeUpdate("create table t2017_len (len int, c clob)");
    } catch (SQLException sqle) {
        assertSQLState("X0Y32", sqle);
        stmt.executeUpdate("delete from t2017_len");
    }
    commit();
    setAutoCommit(false);
    PreparedStatement ps = prepareStatement("insert into t2017_len values (?,?)");
    for (int length : lengths) {
        ps.setInt(1, length);
        ps.setCharacterStream(2, new LoopingAlphabetReader(length));
        ps.executeUpdate();
    }
    // Verify the data, basically making sure the status flag isn't
    // included as part of the user data.
    ResultSet rs = stmt.executeQuery("select len, c from t2017_len");
    int rows = 0;
    while (rs.next()) {
        rows++;
        int length = rs.getInt(1);
        assertEquals(new LoopingAlphabetReader(length), rs.getCharacterStream(2));
    }
    assertEquals(lengths.length, rows);
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) 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