Search in sources :

Example 31 with LoopingAlphabetReader

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

the class BlobClob4BlobTest method insertLoopingAlphabetStreamData.

private void insertLoopingAlphabetStreamData(PreparedStatement ps, CharAlphabet alphabet, int lobLength) throws Exception {
    ps.setCharacterStream(1, new LoopingAlphabetReader(lobLength, alphabet), lobLength);
    ps.setInt(2, lobLength);
    ps.setLong(3, -1);
    ps.executeUpdate();
}
Also used : LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 32 with LoopingAlphabetReader

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

the class LobSortTest method suite.

public static Test suite() {
    Properties props = new Properties();
    // Adjust sort buffer size to trigger the bug situation with less data.
    props.setProperty("derby.storage.sortBufferMax", "4");
    BaseTestSuite suite = new BaseTestSuite(LobSortTest.class, "LobSortTestEmbedded");
    return new CleanDatabaseTestSetup(new SystemPropertyTestSetup(suite, props, true)) {

        /**
         * Generates a table with Blob and Clobs of mixed size.
         */
        protected void decorateSQL(Statement s) throws SQLException {
            Random rnd = new Random(SEED);
            Connection con = s.getConnection();
            con.setAutoCommit(false);
            s.executeUpdate("create table MIXED_LOBS (" + "c clob, clen int, b blob, blen int, rnd int)");
            PreparedStatement ps = con.prepareStatement("insert into MIXED_LOBS values (?,?,?,?,?)");
            // Make sure we get at least one zero-length CLOB and BLOB.
            ps.setString(1, "");
            ps.setInt(2, 0);
            ps.setBytes(3, new byte[0]);
            ps.setInt(4, 0);
            ps.setInt(5, rnd.nextInt());
            ps.executeUpdate();
            for (int i = 0; i < 100; i++) {
                CharAlphabet ca = getCharAlphabet(1 + rnd.nextInt(3));
                int length = (int) (rnd.nextDouble() * 64.0 * 1024.0);
                if (rnd.nextInt(1000) < 500) {
                    // Specify the length.
                    ps.setCharacterStream(1, new LoopingAlphabetReader(length, ca), length);
                } else {
                    // Don't specify the length.
                    ps.setCharacterStream(1, new LoopingAlphabetReader(length, ca));
                }
                ps.setInt(2, length);
                length = (int) (rnd.nextDouble() * 64.0 * 1024.0);
                if (rnd.nextInt(1000) < 500) {
                    // Specify the length.
                    ps.setBinaryStream(3, new LoopingAlphabetStream(length), length);
                } else {
                    // Don't specify the length.
                    ps.setBinaryStream(3, new LoopingAlphabetStream(length));
                }
                ps.setInt(4, length);
                ps.setInt(5, rnd.nextInt());
                ps.executeUpdate();
            }
            con.commit();
            ps.close();
        }

        /**
         * Returns a character alphabet.
         */
        private CharAlphabet getCharAlphabet(int i) {
            switch(i) {
                case 1:
                    return CharAlphabet.modernLatinLowercase();
                case 2:
                    return CharAlphabet.tamil();
                case 3:
                    return CharAlphabet.cjkSubset();
                default:
                    fail("Unknown alphabet identifier: " + i);
            }
            // Will never be reached.
            return null;
        }
    };
}
Also used : Random(java.util.Random) CleanDatabaseTestSetup(org.apache.derbyTesting.junit.CleanDatabaseTestSetup) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SystemPropertyTestSetup(org.apache.derbyTesting.junit.SystemPropertyTestSetup) Connection(java.sql.Connection) BaseTestSuite(org.apache.derbyTesting.junit.BaseTestSuite) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream) Properties(java.util.Properties) CharAlphabet(org.apache.derbyTesting.functionTests.util.streams.CharAlphabet) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 33 with LoopingAlphabetReader

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

the class ClobTest method initializeLongClob.

/**
 * Insert a row with a large clob into the test table.  Read the row from
 * the database and assign the clob value to <code>clob</code>.
 * @return The id of the row that was inserted
 * @throws java.sql.SQLException
 */
private int initializeLongClob() throws SQLException {
    // Clob needs to be larger than one page for locking to occur
    final int lobLength = 40000;
    // Insert a long Clob
    PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)");
    int id = BlobClobTestSetup.getID();
    ps.setInt(1, id);
    ps.setCharacterStream(2, new LoopingAlphabetReader(lobLength), lobLength);
    ps.execute();
    ps.close();
    commit();
    // Fetch the Clob object from the database
    Statement st = createStatement();
    ResultSet rs = st.executeQuery("select CLOBDATA from BLOBCLOB where ID=" + id);
    rs.next();
    clob = rs.getClob(1);
    rs.close();
    st.close();
    return id;
}
Also used : LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 34 with LoopingAlphabetReader

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

the class ClobTest method getCharacterStreamLongLastChar.

/**
 * Obtains streams from the Clob and makes sure we can always read the
 * last char in the Clob.
 * <p>
 * See DERBY-4060.
 *
 * @param id id of the Clob to use
 * @param length the length of the Clob
 * @param alphabet the alphabet used to create the content
 * @throws IOException if reading from a stream fails
 * @throws SQLException if something goes wrong
 */
private void getCharacterStreamLongLastChar(int id, int length, CharAlphabet alphabet) throws IOException, SQLException {
    // Get last char from the source stream.
    Reader cmpReader = new LoopingAlphabetReader(length, alphabet);
    cmpReader.skip(length - 1);
    char srcLastChar = (char) cmpReader.read();
    assertTrue(cmpReader.read() == -1);
    PreparedStatement ps = prepareStatement("select CLOBDATA from BLOBCLOB where ID=?");
    ps.setInt(1, id);
    // Read everything first.
    int charsToRead = length;
    ResultSet rs = ps.executeQuery();
    rs.next();
    Reader reader = rs.getClob(1).getCharacterStream(length - charsToRead + 1, charsToRead);
    // Drain the stream, and make sure we are able to read the last char.
    char lastCharRead = getLastCharInStream(reader, charsToRead);
    assertEquals(srcLastChar, lastCharRead);
    reader.close();
    rs.close();
    // Read a portion of the stream.
    charsToRead = length / 4;
    rs = ps.executeQuery();
    rs.next();
    reader = rs.getClob(1).getCharacterStream(length - charsToRead + 1, charsToRead);
    lastCharRead = getLastCharInStream(reader, charsToRead);
    assertEquals(srcLastChar, lastCharRead);
    reader.close();
    rs.close();
    // Read a very small portion of the stream.
    charsToRead = 1;
    rs = ps.executeQuery();
    rs.next();
    reader = rs.getClob(1).getCharacterStream(length - charsToRead + 1, charsToRead);
    lastCharRead = getLastCharInStream(reader, charsToRead);
    assertEquals(srcLastChar, lastCharRead);
    reader.close();
    rs.close();
}
Also used : LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 35 with LoopingAlphabetReader

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

the class ClobTest method testGetCharacterStreamLongLastCharCJK.

/**
 * 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 Chinese/Japanese/Korean characters.
 */
public void testGetCharacterStreamLongLastCharCJK() throws IOException, SQLException {
    CharAlphabet alphabet = CharAlphabet.cjkSubset();
    // Insert a Clob
    int length = 9001;
    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)

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