Search in sources :

Example 36 with LoopingAlphabetReader

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

the class Derby3650Test method runQueryClob.

/**
 * Test select of multiple rows containing single clob column.
 * <p>
 * Expects input query to return 3 column's per row, which should be:
 * (id, length of clob, clob)
 *
 * Will verify clob using verifyClob().
 * <p>
 *
 * @param query                 query to run.
 * @param freelob               true if we should free the lob after it has
 *                              been retrieved and verified.
 * @param commitAfterLobVerify  true if we should commit after the lob has
 *                              been retrieved and verified.
 *
 * @exception  StandardException  Standard exception policy.
 */
private void runQueryClob(String query, boolean freelob, boolean commitAfterLobVerify) throws SQLException, IOException {
    PreparedStatement ps = prepareStatement(query);
    ResultSet rs = ps.executeQuery();
    while (rs.next()) {
        int id = rs.getInt(1);
        int length = rs.getInt(2);
        Clob clob = rs.getClob(3);
        // verify that stream can be read and is right.
        verifyClob(clob.getCharacterStream(), length, new LoopingAlphabetReader(length));
        if (freelob)
            clob.free();
        if (commitAfterLobVerify)
            commit();
    }
    rs.close();
    commit();
    rs = ps.executeQuery();
    while (rs.next()) {
        int id = rs.getInt(1);
        int length = rs.getInt(2);
        // verify that stream can be read and is right.
        verifyClob(rs.getCharacterStream(3), length, new LoopingAlphabetReader(length));
        if (commitAfterLobVerify)
            commit();
    }
    rs.close();
    commit();
    ps.close();
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Clob(java.sql.Clob) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 37 with LoopingAlphabetReader

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

the class StreamTruncationTest method insertSmall.

/**
 * Inserts a small (smaller than internal conversion buffer) string value.
 *
 * @param colIndex column to insert into (see constants)
 * @param lengthless whether the length of the stream should be specified
 *      or not on insertion
 * @param totalLength the total character length of the stream to insert
 * @param blanks number of trailing blanks in the stream
 * @return The id of the row inserted.
 *
 * @throws IOException if reading from the source stream fails
 * @throws SQLException if something goes wrong, or the test fails
 */
private int insertSmall(int colIndex, boolean lengthless, int totalLength, int blanks) throws IOException, SQLException {
    int id = ID.getAndAdd(1);
    PreparedStatement ps = prepareStatement("insert into " + TABLE_SMALL + " values (?,?,?,?,?)");
    ps.setInt(1, id);
    ps.setNull(2, Types.CLOB);
    ps.setNull(3, Types.VARCHAR);
    ps.setNull(4, Types.LONGVARCHAR);
    ps.setNull(5, Types.CHAR);
    int colWidth = SMALL_SIZE;
    if (colIndex == LONGVARCHAR) {
        colWidth = 32700;
    }
    int expectedLength = Math.min(totalLength, colWidth);
    // Length of CHAR is always the defined length due to padding.
    if (colIndex == CHAR) {
        colWidth = expectedLength = CHAR_SIZE;
    }
    println("totalLength=" + totalLength + ", blanks=" + blanks + ", colWidth=" + colWidth + ", expectedLength=" + expectedLength);
    Reader source = new LoopingAlphabetReader(totalLength, CharAlphabet.modernLatinLowercase(), blanks);
    // Now set what we are going to test.
    if (lengthless) {
        ps.setCharacterStream(colIndex, source);
    } else {
        ps.setCharacterStream(colIndex, source, totalLength);
    }
    try {
        // Exceute the insert.
        assertEquals(1, ps.executeUpdate());
        if (totalLength > expectedLength) {
            assertTrue(totalLength - blanks <= expectedLength);
        }
        // Fetch the value.
        assertEquals(expectedLength, getStreamLength(TABLE_SMALL, colIndex, id));
    } catch (SQLException sqle) {
        // Sanity check of the length.
        if (colIndex == LONGVARCHAR) {
            // Truncation is not allowed.
            assertTrue(totalLength > expectedLength);
        } else {
            // Total length minus blanks must still be larger then the
            // expected length.
            assertTrue(totalLength - blanks > expectedLength);
        }
        // The error handling here is very fuzzy...
        // This will hopefully be fixed, such that the exception thrown
        // will always be 22001. Today this is currently wrapped by several
        // other exceptions.
        String expectedState = "XSDA4";
        if (colIndex == CHAR || colIndex == VARCHAR) {
            if (lengthless) {
                expectedState = "XJ001";
            } else {
                if (!usingEmbedded()) {
                    expectedState = "XJ001";
                } else {
                    expectedState = "22001";
                }
            }
        }
        assertSQLState(expectedState, sqle);
    }
    return id;
}
Also used : SQLException(java.sql.SQLException) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 38 with LoopingAlphabetReader

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

the class StreamTruncationTest method insertLarge.

/**
 * Inserts a large (largerer than internal conversion buffer) string value.
 *
 * @param colIndex column to insert into (see constants)
 * @param lengthless whether the length of the stream should be specified
 *      or not on insertion
 * @param totalLength the total character length of the stream to insert
 * @param blanks number of trailing blanks in the stream
 * @return The id of the row inserted.
 *
 * @throws IOException if reading from the source stream fails
 * @throws SQLException if something goes wrong, or the test fails
 */
private int insertLarge(int colIndex, boolean lengthless, int totalLength, int blanks) throws IOException, SQLException {
    // Not used here, see insertSmall.
    assertTrue(colIndex != CHAR && colIndex != LONGVARCHAR);
    int id = ID.getAndAdd(1);
    PreparedStatement ps = prepareStatement("insert into " + TABLE_LARGE + " values (?,?,?)");
    ps.setInt(1, id);
    ps.setNull(2, Types.CLOB);
    ps.setNull(3, Types.VARCHAR);
    int colWidth = (colIndex == VARCHAR ? LARGE_VARCHAR_SIZE : LARGE_SIZE);
    int expectedLength = Math.min(totalLength, colWidth);
    println("totalLength=" + totalLength + ", blanks=" + blanks + ", colWidth=" + colWidth + ", expectedLength=" + expectedLength);
    Reader source = new LoopingAlphabetReader(totalLength, CharAlphabet.modernLatinLowercase(), blanks);
    // Now set what we are going to test.
    if (lengthless) {
        ps.setCharacterStream(colIndex, source);
    } else {
        ps.setCharacterStream(colIndex, source, totalLength);
    }
    try {
        // Exceute the insert.
        assertEquals(1, ps.executeUpdate());
        if (totalLength > expectedLength) {
            assertTrue(totalLength - blanks <= expectedLength);
        }
        // Fetch the value.
        assertEquals(expectedLength, getStreamLength(TABLE_LARGE, colIndex, id));
    } catch (SQLException sqle) {
        // Sanity check of the length.
        // Total length minus blanks must still be larger then the
        // expected length.
        assertTrue(totalLength - blanks > expectedLength);
        // The error handling here is very fuzzy...
        // This will hopefully be fixed, such that the exception thrown
        // will always be 22001. Today this is currently wrapped by several
        // other exceptions.
        String expectedState = "XSDA4";
        if (colIndex == VARCHAR) {
            if (lengthless) {
                expectedState = "XJ001";
            } else {
                if (!usingEmbedded()) {
                    expectedState = "XJ001";
                } else {
                    expectedState = "22001";
                }
            }
        }
        assertSQLState(expectedState, sqle);
    }
    return id;
}
Also used : SQLException(java.sql.SQLException) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 39 with LoopingAlphabetReader

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

the class RestrictiveFilePermissionsTest method testExportedFiles.

public void testExportedFiles() throws Exception {
    Statement s = createStatement();
    // Simple exported table file
    s.executeUpdate("call SYSCS_UTIL.SYSCS_EXPORT_TABLE(" + "    'SYS'," + "    'SYSTABLES'," + "    '" + home + "/" + dbName + "/" + exportFileName + "'," + // column delimiter
    "    NULL," + // character delimiter
    "    NULL," + // code set
    "    NULL)");
    File exp = new File(home, dbName + "/" + exportFileName);
    checkAccessToOwner(exp, POSITIVE);
    // Make a lob table and insert one row
    s.executeUpdate("create table lobtable(i int, c clob)");
    PreparedStatement ps = prepareStatement("insert into lobtable values (1,?)");
    ps.setCharacterStream(1, new LoopingAlphabetReader(1000), 1000);
    ps.executeUpdate();
    // Export the lob table
    s.executeUpdate("call SYSCS_UTIL.SYSCS_EXPORT_TABLE_LOBS_TO_EXTFILE(" + "    'SYS'," + "    'SYSTABLES'," + "    '" + home + "/" + dbName + "/" + exportFileName2 + "'," + // column delimiter
    "    NULL," + // character delimiter
    "    NULL," + // code set
    "    NULL," + "    '" + home + "/" + dbName + "/" + exportLobFileName + "')");
    File exp2 = new File(home, dbName + "/" + exportFileName2);
    File expLob = new File(home, dbName + "/" + exportLobFileName);
    // Check resuling exported files
    checkAccessToOwner(exp2, POSITIVE);
    checkAccessToOwner(expLob, POSITIVE);
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) PreparedStatement(java.sql.PreparedStatement) File(java.io.File) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 40 with LoopingAlphabetReader

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

the class Derby2017LayerATest method cs_StreamInsertCharBufferBoundary.

/**
 * Tests inserts around some selected buffer boundaries. This test verifies
 * that the client and server can sucessfully insert values of various
 * lengths. It will work also before the fix for DERBY-2017, but will fail
 * if an incorrect fix is applied.
 */
public void cs_StreamInsertCharBufferBoundary() throws IOException, SQLException {
    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);
    // Note that when using layer A streaming the data is converted to
    // UTF-16 on the wire.
    PreparedStatement ps = prepareStatement("insert into t2017_len values (?,?)");
    // Test small values.
    for (int i = 0; i < 512; i++) {
        ps.setInt(1, i);
        ps.setCharacterStream(2, new LoopingAlphabetReader(i), i);
        ps.executeUpdate();
    }
    commit();
    // transmit buffer.
    for (int i = 16000; i < 18000; i++) {
        ps.setInt(1, i);
        ps.setCharacterStream(2, new LoopingAlphabetReader(i), i);
        ps.executeUpdate();
        // Commit periodically.
        if (i % 1000 == 0) {
            commit();
        }
    }
    commit();
    for (int i = 32500; i < 33000; i++) {
        ps.setInt(1, i);
        ps.setCharacterStream(2, new LoopingAlphabetReader(i), i);
        ps.executeUpdate();
    }
    commit();
    // 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++;
        assertEquals(new LoopingAlphabetReader(rs.getInt(1)), rs.getCharacterStream(2));
    }
}
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