Search in sources :

Example 1 with LoopingAlphabetReader

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

the class BiggerTemporaryClobTest method setUp.

/**
 * Creates a bigger read-write Clob that is being kept on disk due to its
 * size.
 */
public void setUp() throws Exception {
    super.initialCharLength = CLOBLENGTH;
    super.headerLength = 2 + 3;
    // All tamil letters. Also add the header bytes.
    super.initialByteLength = CLOBLENGTH * 3 + headerLength;
    super.bytesPerChar = BYTES_PER_CHAR;
    EmbedStatement embStmt = (EmbedStatement) createStatement();
    iClob = new TemporaryClob(embStmt);
    transferData(new LoopingAlphabetReader(CLOBLENGTH, CharAlphabet.cjkSubset()), iClob.getWriter(1L), CLOBLENGTH);
    assertEquals(CLOBLENGTH, iClob.getCharLength());
}
Also used : LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 2 with LoopingAlphabetReader

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

the class SmallTemporaryClobTest method setUp.

/**
 * Creates a small read-write Clob that is kept in memory.
 */
public void setUp() throws Exception {
    super.initialCharLength = CLOBLENGTH;
    super.headerLength = 2 + 3;
    // All tamil letters. Also add the header bytes.
    super.initialByteLength = CLOBLENGTH * 3 + headerLength;
    super.bytesPerChar = BYTES_PER_CHAR;
    EmbedStatement embStmt = (EmbedStatement) createStatement();
    iClob = new TemporaryClob(embStmt);
    transferData(new LoopingAlphabetReader(CLOBLENGTH, CharAlphabet.tamil()), iClob.getWriter(1L), CLOBLENGTH);
    assertEquals(CLOBLENGTH, iClob.getCharLength());
}
Also used : LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 3 with LoopingAlphabetReader

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

the class UTF8ReaderTest method checkInternalStream.

/**
 * Makes sure the data returned from the internal Clob matches the data
 * returned by a fresh looping alphabet stream.
 *
 * @param pos 1-based Clob position
 * @param clob internal store stream Clob representation
 */
private static void checkInternalStream(long pos, StoreStreamClob clob) throws IOException, SQLException {
    Reader canonStream = new LoopingAlphabetReader(pos + 100);
    // Convert to 0-based index.
    long toSkip = pos - 1;
    while (toSkip > 0) {
        long skipped = canonStream.skip(toSkip);
        if (skipped > 0) {
            toSkip -= skipped;
        }
    }
    Reader clobStream = clob.getInternalReader(pos);
    assertEquals("Data mismatch", canonStream.read(), clobStream.read());
    clobStream.close();
}
Also used : LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 4 with LoopingAlphabetReader

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

the class UTF8ReaderTest method insertTestData.

/**
 * Inserts data used by the tests.
 * <p>
 * Use the id to select a Clob with specific contents.
 */
private static void insertTestData(Statement stmt) throws SQLException {
    int[][] sizes = new int[][] { // 1M chars
    { 100, 1 * 1024 * 1024 }, // 32K chars
    { 101, 32 * 1024 } };
    stmt.executeUpdate("create table Utf8ReaderTest" + "(id int primary key, size int, dClob clob)");
    PreparedStatement ps = stmt.getConnection().prepareStatement("insert into Utf8ReaderTest values (?,?,?)");
    for (int i = 0; i < sizes.length; i++) {
        ps.setInt(1, sizes[i][0]);
        int size = sizes[i][1];
        ps.setInt(2, size);
        ps.setCharacterStream(3, new LoopingAlphabetReader(size), size);
        ps.executeUpdate();
    }
    // Insert some special pieces of text, repeat to get it represented as
    // a stream.
    ps.setInt(1, 1);
    int size = aintWeGotFun.length();
    ps.setInt(2, size);
    StringBuffer str = new StringBuffer(32 * 1024 + aintWeGotFun.length());
    while (str.length() < 32 * 1024) {
        str.append(aintWeGotFun);
    }
    ps.setString(3, str.toString());
    ps.executeUpdate();
}
Also used : PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 5 with LoopingAlphabetReader

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

the class Derby2017LayerATest method doInsertTest.

/**
 * Performs the base test cycle; insert 3 valid rows, try to insert 2
 * invalid rows, insert 2 valid rows.
 * <p>
 * The outcome depends on whether auto-commit is on, and whether a rollback
 * is issued when an insert fails.
 *
 * @param INSERT the data to insert
 * @param autoCommit the auto-commit state to use
 * @param rollbackOnError whether or not to issue a rollback if an insert
 *      fails
 *
 * @throws IOException if something goes wrong
 * @throws SQLException if something goes wrong
 */
private void doInsertTest(int[] INSERT, boolean autoCommit, boolean rollbackOnError) throws IOException, SQLException {
    // A few sanity checks.
    assertEquals("Expects 7 rows", 7, INSERT.length);
    rollback();
    Statement stmt = createStatement();
    try {
        stmt.executeUpdate("create table t2017_id (id int unique, c clob)");
    } catch (SQLException sqle) {
        assertSQLState("X0Y32", sqle);
        stmt.executeUpdate("delete from t2017_id");
    }
    commit();
    setAutoCommit(autoCommit);
    PreparedStatement ps = prepareStatement("insert into t2017_id values (?, ?)");
    // Insert the 3 first rows (id is 1-based).
    for (int i = 0; i < 3; i++) {
        ps.setInt(1, i + 1);
        int length = INSERT[i];
        ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
        assertEquals(1, ps.executeUpdate());
    }
    // Insert the 4th row with a stream that's longer than the specified
    // length, then the 5th row that's shorter. Both should fail, and the
    // data shouldn't be inserted into the database.
    Reader r4 = new LoopingAlphabetReader(INSERT[3]);
    ps.setInt(1, 4);
    ps.setCharacterStream(2, r4, INSERT[3] - 5);
    try {
        ps.executeUpdate();
        fail("Insert should have failed, stream too long");
    } catch (SQLException sqle) {
        // The states are different between client and embedded.
        assertSQLState(usingEmbedded() ? "XSDA4" : "XN015", sqle);
        if (rollbackOnError) {
            rollback();
        }
    }
    Reader r5 = new LoopingAlphabetReader(INSERT[4]);
    ps.setInt(1, 5);
    ps.setCharacterStream(2, r5, INSERT[4] + 5);
    try {
        ps.executeUpdate();
        fail("Insert should have failed, stream too short");
    } catch (SQLException sqle) {
        // The states are different between client and embedded.
        assertSQLState(usingEmbedded() ? "XSDA4" : "XN017", sqle);
        if (rollbackOnError) {
            rollback();
        }
    }
    // two rows and make sure the transaction commits.
    for (int i = 5; i < INSERT.length; i++) {
        ps.setInt(1, i + 1);
        int length = INSERT[i];
        ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
        assertEquals(1, ps.executeUpdate());
    }
    if (!autoCommit) {
        commit();
    }
    // Make sure we have the expected number of rows.
    ResultSet rs = stmt.executeQuery("select count(*) from t2017_id");
    rs.next();
    assertEquals((rollbackOnError && !autoCommit ? 2 : 5), rs.getInt(1));
    // Select data in the table, compare to what we expect.
    rs = stmt.executeQuery("select * from t2017_id order by id asc");
    // Check rows 1-4 if rollback on error is false.
    if (autoCommit || !rollbackOnError) {
        for (int i = 0; i < 3; i++) {
            rs.next();
            int id = rs.getInt(1);
            assertTrue(id - 1 == i);
            assertEquals(new LoopingAlphabetReader(INSERT[i]), rs.getCharacterStream(2));
        }
    }
    // Check rows 6 and 7.
    for (int i = 5; i < 7; i++) {
        rs.next();
        int id = rs.getInt(1);
        assertTrue(id - 1 == i);
        assertEquals(new LoopingAlphabetReader(INSERT[i]), rs.getCharacterStream(2));
    }
    assertFalse(rs.next());
    rs.close();
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) 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