Search in sources :

Example 6 with LoopingAlphabetStream

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

the class UTF8UtilTest method testEqualityOfModifedUTF8AndASCII.

/**
 * Ensure the assumption that the default looping alphabet stream and the
 * modified UTF-8 encoding is equal.
 * <p>
 * If this assumption is broken, several of the other tests will fail.
 */
public void testEqualityOfModifedUTF8AndASCII() throws IOException {
    final int length = 12706;
    InputStream ascii = new LoopingAlphabetStream(length);
    InputStream modUTF8 = new ReaderToUTF8Stream(new LoopingAlphabetReader(length), length, 0, TYPENAME, new CharStreamHeaderGenerator());
    // Skip encoded length added by ReaderToUTF8Stream.
    modUTF8.skip(HEADER_LENGTH);
    assertEquals(ascii, modUTF8);
}
Also used : DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ReaderToUTF8Stream(org.apache.derby.iapi.types.ReaderToUTF8Stream) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream) CharStreamHeaderGenerator(org.apache.derby.iapi.types.CharStreamHeaderGenerator) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 7 with LoopingAlphabetStream

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

the class PrepareStatementTest method testReadBlobCloseToMaxDssLength.

/**
 * Test fix for DERBY-4088 where an ArrayIndexOutOfBoundsException was
 * thrown by DDMReader.readBytes() when reading a BLOB value whose length
 * was close to the maximum length of a DSS.
 */
public void testReadBlobCloseToMaxDssLength() throws Exception {
    // max DSS length is 32767
    final int length = 32766;
    // Create test data with the requested length
    DataInputStream stream1 = new DataInputStream(new LoopingAlphabetStream(length));
    byte[] bytes = new byte[length];
    stream1.readFully(bytes);
    // See if the test data can be sent to the server and back with
    // no errors.
    PreparedStatement ps = prepareStatement("values cast(? as blob)");
    ps.setBytes(1, bytes);
    ResultSet rs = ps.executeQuery();
    assertTrue("empty result set", rs.next());
    InputStream stream2 = rs.getBinaryStream(1);
    assertEquals(new LoopingAlphabetStream(length), stream2);
    assertFalse("too many rows", rs.next());
    rs.close();
}
Also used : DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream) PreparedStatement(java.sql.PreparedStatement) DataInputStream(java.io.DataInputStream)

Example 8 with LoopingAlphabetStream

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

the class BlobTest method testGetBinaryStreamLongLastByte.

/**
 * Obtains a binary stream and tries to drain it to read the last byte in
 * the Blob.
 * <p>
 * See DERBY-4060.
 *
 * @throws IOException if reading from a stream fails
 * @throws SQLException if something goes wrong
 */
public void testGetBinaryStreamLongLastByte() throws IOException, SQLException {
    int length = 5000;
    // Insert a Blob
    PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, BLOBDATA) values(?,?)");
    int id = BlobClobTestSetup.getID();
    ps.setInt(1, id);
    ps.setBinaryStream(2, new LoopingAlphabetStream(length), length);
    ps.execute();
    ps.close();
    // Get last byte from the source stream.
    InputStream cmpIs = new LoopingAlphabetStream(length);
    cmpIs.skip(length - 1);
    int srcLastByte = cmpIs.read();
    assertTrue(cmpIs.read() == -1);
    // Read everything first.
    int bytesToRead = 5000;
    ps = prepareStatement("select BLOBDATA from BLOBCLOB where ID=?");
    ps.setInt(1, id);
    ResultSet rs = ps.executeQuery();
    rs.next();
    InputStream is = rs.getBlob(1).getBinaryStream(length - bytesToRead + 1, bytesToRead);
    // Drain the stream, and make sure we are able to read the last byte.
    int lastByteRead = getLastByteInStream(is, bytesToRead);
    assertEquals(srcLastByte, lastByteRead);
    is.close();
    rs.close();
    // Read a portion of the stream.
    bytesToRead = 2000;
    rs = ps.executeQuery();
    rs.next();
    is = rs.getBlob(1).getBinaryStream(length - bytesToRead + 1, bytesToRead);
    assertEquals(srcLastByte, lastByteRead);
    is.close();
    rs.close();
    // Read a very small portion of the stream.
    bytesToRead = 1;
    rs = ps.executeQuery();
    rs.next();
    is = rs.getBlob(1).getBinaryStream(length - bytesToRead + 1, bytesToRead);
    assertEquals(srcLastByte, lastByteRead);
    is.close();
    rs.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 9 with LoopingAlphabetStream

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

the class CaseExpressionTest method testLobAsCaseOperand.

/**
 * Test that large objects can be used as case operands.
 */
public void testLobAsCaseOperand() throws SQLException {
    Statement s = createStatement();
    // BLOB and CLOB are allowed in the case operand.
    JDBC.assertSingleValueResultSet(s.executeQuery("values case cast(null as blob) when is null then 'yes' end"), "yes");
    JDBC.assertSingleValueResultSet(s.executeQuery("values case cast(null as clob) when is null then 'yes' end"), "yes");
    // Comparisons between BLOB and BLOB, or between CLOB and CLOB, are
    // not allowed, so expect a compile-time error for these queries.
    assertCompileError("42818", "values case cast(null as blob) " + "when cast(null as blob) then true end");
    assertCompileError("42818", "values case cast(null as clob) " + "when cast(null as clob) then true end");
    // Now create a table with some actual LOBs in them.
    s.execute("create table lobs_for_simple_case(" + "id int generated always as identity, b blob, c clob)");
    PreparedStatement insert = prepareStatement("insert into lobs_for_simple_case(b, c) values (?, ?)");
    // A small one.
    insert.setBytes(1, new byte[] { 1, 2, 3 });
    insert.setString(2, "small");
    insert.executeUpdate();
    // And a big one (larger than 32K means it will be streamed
    // from store, instead of being returned as a materialized value).
    insert.setBinaryStream(1, new LoopingAlphabetStream(40000));
    insert.setCharacterStream(2, new LoopingAlphabetReader(40000));
    insert.executeUpdate();
    // And a NULL.
    insert.setNull(1, Types.BLOB);
    insert.setNull(2, Types.CLOB);
    insert.executeUpdate();
    // IS [NOT] NULL can be used on both BLOB and CLOB. LIKE can be
    // used on CLOB. Those are the only predicates supported on BLOB
    // and CLOB in simple case expressions currently. Test that they
    // all work.
    JDBC.assertUnorderedResultSet(s.executeQuery("select id, case b when is null then 'yes'" + " when is not null then 'no' end, " + "case c when is null then 'yes' when like 'abc' then 'abc'" + " when like 'abc%' then 'abc...' when is not null then 'no'" + " end " + "from lobs_for_simple_case"), new String[][] { { "1", "no", "no" }, { "2", "no", "abc..." }, { "3", "yes", "yes" } });
    s.execute("drop table lobs_for_simple_case");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 10 with LoopingAlphabetStream

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

the class TriggerTest method testBlobInTriggerTable.

/**
 * Create a table with after update trigger on non-lob column.
 * Insert two blobs of size blobSize into table and perform update
 * on str1 column to fire trigger. Helper method called from
 * testBlobInTriggerTable
 *
 * @param blobSize  size of blob to test.
 * @throws SQLException
 * @throws IOException
 */
private void testBlobInTriggerTable(int blobSize) throws SQLException, IOException {
    // Alphabet used when inserting a BLOB.
    ByteAlphabet a1 = ByteAlphabet.singleByte((byte) 8);
    // Alphabet used when updating a BLOB.
    ByteAlphabet a2 = ByteAlphabet.singleByte((byte) 9);
    String trig = " create trigger t_lob1 after update of str1 on lob1 ";
    trig = trig + " REFERENCING OLD AS old NEW AS new FOR EACH ROW MODE DB2SQL ";
    trig = trig + " insert into t_lob1_log(oldvalue, newvalue) values (old.str1, new.str1)";
    Statement s = createStatement();
    s.executeUpdate("create table LOB1 (str1 Varchar(80), b_lob BLOB(50M), b_lob2 BLOB(50M))");
    s.executeUpdate("create table t_lob1_log(oldvalue varchar(80), newvalue varchar(80), chng_time timestamp default current_timestamp)");
    s.executeUpdate(trig);
    commit();
    // --- add a blob
    PreparedStatement ps = prepareStatement("INSERT INTO LOB1 VALUES (?, ?, ?)");
    ps.setString(1, blobSize + "");
    // - set the value of the input parameter to the input stream
    // use a couple blobs so we are sure it works with multiple lobs
    ps.setBinaryStream(2, new LoopingAlphabetStream(blobSize, a1), blobSize);
    ps.setBinaryStream(3, new LoopingAlphabetStream(blobSize, a1), blobSize);
    ps.execute();
    closeStatement(ps);
    commit();
    // Now executing update to fire trigger
    s.executeUpdate("update LOB1 set str1 = str1 || ' '");
    s.executeUpdate("drop table lob1");
    s.executeUpdate("drop table t_lob1_log");
    // now referencing the lob column
    trig = " create trigger t_lob1 after update of b_lob on lob1 ";
    trig = trig + " REFERENCING OLD AS old NEW AS new FOR EACH ROW MODE DB2SQL ";
    trig = trig + " insert into t_lob1_log(oldvalue, newvalue) values (old.b_lob, new.b_lob)";
    s.executeUpdate("create table LOB1 (str1 Varchar(80), b_lob BLOB(50M))");
    s.executeUpdate("create table t_lob1_log(oldvalue BLOB(50M), newvalue  BLOB(50M), chng_time timestamp default current_timestamp)");
    s.executeUpdate(trig);
    commit();
    ps = prepareStatement("INSERT INTO LOB1 VALUES (?, ?)");
    ps.setString(1, blobSize + "");
    // - set the value of the input parameter to the input stream
    ps.setBinaryStream(2, new LoopingAlphabetStream(blobSize, a1), blobSize);
    ps.execute();
    closeStatement(ps);
    commit();
    // Now executing update to fire trigger
    ps = prepareStatement("update LOB1 set b_lob = ?");
    ps.setBinaryStream(1, new LoopingAlphabetStream(blobSize, a2), blobSize);
    ps.execute();
    closeStatement(ps);
    commit();
    s.executeUpdate("drop table lob1");
    s.executeUpdate("drop table t_lob1_log");
    // now referencing the lob column twice
    trig = " create trigger t_lob1 after update of b_lob on lob1 ";
    trig = trig + " REFERENCING OLD AS old NEW AS new FOR EACH ROW MODE DB2SQL ";
    trig = trig + " insert into t_lob1_log(oldvalue, newvalue, oldvalue_again, newvalue_again) values (old.b_lob, new.b_lob, old.b_lob, new.b_lob)";
    s.executeUpdate("create table LOB1 (str1 Varchar(80), b_lob BLOB(50M))");
    s.executeUpdate("create table t_lob1_log(oldvalue BLOB(50M), newvalue  BLOB(50M), oldvalue_again BLOB(50M), newvalue_again BLOB(50M), chng_time timestamp default current_timestamp)");
    s.executeUpdate(trig);
    commit();
    ps = prepareStatement("INSERT INTO LOB1 VALUES (?, ?)");
    ps.setString(1, blobSize + "");
    // - set the value of the input parameter to the input stream
    ps.setBinaryStream(2, new LoopingAlphabetStream(blobSize, a1), blobSize);
    ps.execute();
    closeStatement(ps);
    commit();
    // Now executing update to fire trigger
    ps = prepareStatement("update LOB1 set b_lob = ?");
    ps.setBinaryStream(1, new LoopingAlphabetStream(blobSize, a2), blobSize);
    ps.execute();
    closeStatement(ps);
    commit();
    // check log table.
    ResultSet rs = s.executeQuery("SELECT * from t_lob1_log");
    rs.next();
    assertEquals(new LoopingAlphabetStream(blobSize, a1), rs.getBinaryStream(1));
    assertEquals(new LoopingAlphabetStream(blobSize, a2), rs.getBinaryStream(2));
    assertEquals(new LoopingAlphabetStream(blobSize, a1), rs.getBinaryStream(3));
    assertEquals(new LoopingAlphabetStream(blobSize, a2), rs.getBinaryStream(4));
    rs.close();
    s.executeUpdate("drop table lob1");
    s.executeUpdate("drop table t_lob1_log");
}
Also used : ByteAlphabet(org.apache.derbyTesting.functionTests.util.streams.ByteAlphabet) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Aggregations

LoopingAlphabetStream (org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)61 PreparedStatement (java.sql.PreparedStatement)33 InputStream (java.io.InputStream)31 ResultSet (java.sql.ResultSet)31 Statement (java.sql.Statement)20 Blob (java.sql.Blob)10 SQLException (java.sql.SQLException)9 LoopingAlphabetReader (org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)9 PositionedStoreStream (org.apache.derby.impl.jdbc.PositionedStoreStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Connection (java.sql.Connection)7 OutputStream (java.io.OutputStream)5 Reader (java.io.Reader)5 Clob (java.sql.Clob)3 Timestamp (java.sql.Timestamp)3 DataInputStream (java.io.DataInputStream)2 EOFException (java.io.EOFException)2 CallableStatement (java.sql.CallableStatement)2 Date (java.sql.Date)2 Time (java.sql.Time)2