Search in sources :

Example 41 with LoopingAlphabetReader

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

the class LobStreamsTest method testGettingClobTwice.

/**
 * Tests that accessing the same Clob multiple times on a row results in
 * an exception being thrown.
 *
 * @throws Exception if something goes wrong
 */
public void testGettingClobTwice() throws SQLException {
    // We need a few Clobs.
    int length = 71 * 1024 + 7;
    PreparedStatement ps = prepareStatement("insert into testBlobX1(a,c) values (?,?)");
    ps.setInt(1, 3);
    ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
    ps.executeUpdate();
    ps.setInt(1, 4);
    ps.setString(2, "short clob");
    ps.executeUpdate();
    ps.close();
    // Get a result set with the Clobs.
    final int clobCount = 2;
    int count = 0;
    ps = prepareStatement("select c from testBlobX1 where a >= ? and a <= ?");
    ps.setInt(1, 3);
    ps.setInt(2, 4);
    ResultSet rs = ps.executeQuery();
    assertTrue(rs.next());
    do {
        count++;
        // First get a Clob.
        Clob c = rs.getClob(1);
        // Get a second Clob, which should fail.
        try {
            rs.getClob(1);
            fail("Getting the Clob the second time should have failed");
        } catch (SQLException sqle) {
            assertSQLState("XCL18", sqle);
        }
        // Finally try to access the column as a stream.
        try {
            rs.getCharacterStream(1);
            fail("Getting the Clob the third time should have failed");
        } catch (SQLException sqle) {
            assertSQLState("XCL18", sqle);
        }
    } while (rs.next());
    rs.close();
    assertEquals(clobCount, count);
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Clob(java.sql.Clob) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 42 with LoopingAlphabetReader

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

the class ParameterMappingTest method helperTestDerby6237.

// numberOfRowsToUpdate - value 1 or 2
// testVariation - if 1 then update CLOB/VARCHAR with short data
// if 2 then update CLOB/VARCHAR with large data
// testCLOB - true means test setCharacterStream on CLOB
// - false means test setCharacterStream on VARCHAR
private void helperTestDerby6237(int numberOfRowsToUpdate, int testVariation, boolean testCLOB) throws Exception {
    CharAlphabet a1 = CharAlphabet.singleChar('a');
    // Following will update one or 2 rows depending on the 1st param
    // Following will update CLOB column or VARCHAR column with short
    // or large data depending on param 2
    // Following will update CLOB column or VARCHAR column depending
    // on 3rd param
    PreparedStatement ps = prepareStatement("UPDATE TestUpdateCharStream SET " + (testCLOB == true ? "c3" : "c4") + " = ?, " + "c2 = c2 + 1 WHERE c1 IN (?, ?)");
    switch(testVariation) {
        case 1:
            // test short data
            ps.setCharacterStream(1, new LoopingAlphabetReader(50, a1), 50);
            break;
        case 2:
            // test large data
            if (testCLOB) {
                // for CLOB column, use 50K data
                ps.setCharacterStream(1, new LoopingAlphabetReader(50000, a1), 50000);
            } else {
                // for VARCHAR column, use 32K data
                ps.setCharacterStream(1, new LoopingAlphabetReader(32000, a1), 32000);
            }
            break;
    }
    // First value in IN clause is getting set to 'AAAAA'
    // Using setCharacterStream on VARCHAR to set the value
    ps.setCharacterStream(2, new CharArrayReader("AAAAA".toCharArray()), 5);
    if (numberOfRowsToUpdate == 1) {
        // Second value in IN clause is also getting set to 'AAAAA', which
        // means prepared statement will update only one row
        ps.setObject(3, "AAAAA", Types.VARCHAR);
    } else {
        // Second value in IN clause is also getting set to 'EEEEE', which
        // means prepared statement will update two rows
        ps.setObject(3, "EEEEE", Types.VARCHAR);
    }
    ps.execute();
    // verify updated data. Update happened to either CLOB column or VARCHAR
    // column. It is decided by param 3
    ResultSet rs;
    ps = prepareStatement("select " + (testCLOB == true ? "c3 " : "c4 ") + "from TestUpdateCharStream " + "WHERE c1 IN (?, ?)");
    ps.setCharacterStream(1, new CharArrayReader("AAAAA".toCharArray()), 5);
    if (numberOfRowsToUpdate == 1) {
        ps.setObject(2, "AAAAA", Types.VARCHAR);
    } else {
        ps.setObject(2, "EEEEE", Types.VARCHAR);
    }
    rs = ps.executeQuery();
    char[] c;
    if (testVariation == 1) {
        // we are here to test short data
        c = new char[50];
    } else {
        // we are here to test large data
        if (testCLOB)
            c = new char[50000];
        else
            c = new char[32000];
    }
    Arrays.fill(c, 'a');
    for (int i = 0; i < numberOfRowsToUpdate; i++) {
        rs.next();
        if (!compareClobReader2CharArray(c, rs.getCharacterStream(1))) {
            System.out.println("FAIL: " + (testCLOB ? "CLOB " : "VARCHAR ") + "data should have matched");
            rs.close();
            ps.close();
            return;
        }
    }
    rs.close();
    ps.close();
}
Also used : CharArrayReader(java.io.CharArrayReader) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CharAlphabet(org.apache.derbyTesting.functionTests.util.streams.CharAlphabet) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 43 with LoopingAlphabetReader

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

the class ClobTest method testPositionWithString_CJK.

public void testPositionWithString_CJK() throws IOException, SQLException {
    final long prefix = 11L;
    final long postfix = 90L;
    char[] tmpChar = new char[1];
    LoopingAlphabetReader tokenSrc = new LoopingAlphabetReader(1L, CharAlphabet.cjkSubset());
    tokenSrc.read(tmpChar);
    String token = String.copyValueOf(tmpChar);
    insertDataWithToken(token, prefix, postfix, SET_CHARACTER_STREAM);
    // insertDataWithToken(token, prefix, 2*1024-7, SET_CHARACTER_STREAM);
    executeTestPositionWithStringToken(token, prefix);
}
Also used : LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 44 with LoopingAlphabetReader

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

the class ClobTest method testGetSubString_BiggerThanInternalBuffer.

/**
 * Tests if big strings can be handled.
 * <p>
 * The motivation for the test is to make sure big buffers are filled with
 * the call to read inside a loop. Big in this sense means bigger than some
 * internal buffer. This is typically around 8 KB or so, but we try
 * something considerably bigger. If a char/byte array is attempted filled
 * with a single call to read, the resulting string wil typically contain
 * \u0000 at positions after the size of the internal buffer.
 */
public void testGetSubString_BiggerThanInternalBuffer() throws IOException, SQLException {
    // 1 M characters
    int stringLength = 1 * 1024 * 1024;
    transferData(new LoopingAlphabetReader(stringLength), this.clob.setCharacterStream(1L), TRANSFER_BUFFER_SIZE);
    String obtained = this.clob.getSubString(1, stringLength);
    assertEquals("Incorrect string length", stringLength, obtained.length());
    // Obtain the string we inserted for comparison.
    CharArrayWriter charWriter = new CharArrayWriter();
    transferData(new LoopingAlphabetReader(stringLength), charWriter, TRANSFER_BUFFER_SIZE);
    assertEquals("String do not match", charWriter.toString(), obtained);
}
Also used : CharArrayWriter(java.io.CharArrayWriter) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 45 with LoopingAlphabetReader

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

the class ClobTruncateTest method insertClobs.

private void insertClobs() throws SQLException, IOException {
    PreparedStatement ps = prepareStatement("insert into truncateclob" + " values (?,?,?)");
    // insert a small clob
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < 100; i++) sb.append("small clob");
    int length = sb.length();
    ps.setInt(1, length);
    ps.setCharacterStream(2, new StringReader(sb.toString()), length);
    ps.setInt(3, length / 2);
    ps.execute();
    // insert a large clob
    LoopingAlphabetReader reader = new LoopingAlphabetReader(1024 * 1024);
    ps.setInt(1, 1024 * 1024);
    ps.setCharacterStream(2, reader, 1024 * 1024);
    ps.setInt(3, 1024 * 1024 / 2);
    ps.execute();
    // insert a non ascii clob
    LoopingAlphabetReader uReader = new LoopingAlphabetReader(300000, CharAlphabet.tamil());
    ps.setInt(1, 300000);
    ps.setCharacterStream(2, uReader, 300000);
    ps.setInt(3, 150000);
    ps.execute();
}
Also used : StringReader(java.io.StringReader) 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