Search in sources :

Example 16 with LoopingAlphabetReader

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader 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 17 with LoopingAlphabetReader

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

the class ImportExportLobTest method loadData.

/*
     * Insert data to the into the table, whose data will be exported.
     */
private static void loadData(Statement s) throws SQLException {
    s.executeUpdate("insert into books values " + "(1, 'book 1', 'clob 1'," + "cast(X'3743640ADE12337610' as blob))");
    // rows with empty strings.
    s.executeUpdate("insert into books values " + "(2, 'book 2', 'clob 2',  cast (X'' as blob))");
    s.executeUpdate("insert into books values " + "(3, 'book 3', '', cast(X'42' as blob))");
    s.executeUpdate("insert into books values " + "(4, 'book 4', 'clob 4',  " + "cast (X'3233445578990122558820' as blob))");
    // rows with a null
    s.executeUpdate("insert into books values " + "(5, null, 'clob 5'," + "cast(X'3843640ADE12337610' as blob))");
    s.executeUpdate("insert into books values " + "(6,  'book  6', null,  " + "cast(X'3843640ADE12337610' as blob))");
    s.executeUpdate("insert into books values " + "(7,  'book  7',  'clob 7' , null)");
    s.executeUpdate("insert into books values " + "(8, '', null,  cast (X'3843640ADE12' as blob))");
    s.executeUpdate("insert into books values " + "(9, 'book  9', null,  cast (X'' as blob))");
    // insert data that contains some delimiter characters
    // ( "(x22) ,(x2C) %(x25) ;(x3B) , tab(9) LF(A) )
    s.executeUpdate("insert into books values " + "(10, 'book ;10', '%asdadasdasd'," + " cast (X'222C23B90A' as blob))");
    // !(x21) $(24)
    s.executeUpdate("insert into books values " + "(11, '212C3B24', '2422412221', " + "  cast (X'212421222C23B90A2124' as blob))");
    // insert some clob data with default char delimiter inside
    // the data. It should get exported in double-delimiter format
    // when exporting to the main export file.
    s.executeUpdate("insert into books values" + "(12, 'Transaction Processing' , " + "'This books covers \"Transaction\" \"processing\" concepts'" + ",cast (X'144594322143423214ab35f2e54e' as blob))");
    s.executeUpdate("insert into books values" + "(13, 'effective java' ," + "'describes how to write \" quality java \" code', " + "cast (X'124594322143423214ab35f2e34c' as blob))");
    // insert some more randomly genrated data.
    Connection conn = s.getConnection();
    String sql = "insert into books values(? , ? , ? , ?)";
    PreparedStatement ps = conn.prepareStatement(sql);
    int blobSize = 0;
    int id = 14;
    for (int i = 0; i < 17; i++) {
        ps.setInt(1, id++);
        ps.setString(2, "book" + i);
        blobSize += 1024 * i;
        int clobSize = 1024 * i;
        Reader reader = new LoopingAlphabetReader(clobSize);
        ps.setCharacterStream(3, reader, clobSize);
        InputStream stream = new LoopingAlphabetStream(blobSize);
        ps.setBinaryStream(4, stream, blobSize);
        ps.executeUpdate();
        if ((i % 10) == 0)
            conn.commit();
    }
    ps.close();
    conn.commit();
}
Also used : InputStream(java.io.InputStream) Connection(java.sql.Connection) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 18 with LoopingAlphabetReader

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

the class BlobClob4BlobTest method testSetCharacterStream.

/**
 * Tests PreparedStatement.setCharacterStream
 */
public void testSetCharacterStream() throws Exception {
    int clobLength = 5009;
    // insert a streaming column
    PreparedStatement ps = prepareStatement("insert into testClob (a) values(?)");
    Reader streamReader = new LoopingAlphabetReader(clobLength, CharAlphabet.tamil());
    ps.setCharacterStream(1, streamReader, clobLength);
    // DERBY-4312 make sure commit() doesn't interfere
    commit();
    ps.executeUpdate();
    streamReader.close();
    ps.close();
    commit();
    Statement stmt = createStatement();
    ResultSet rs = stmt.executeQuery("SELECT a FROM testClob");
    while (rs.next()) {
        Clob clob = rs.getClob(1);
        assertEquals("FAIL - wrong clob length", clobLength, clob.length());
        Reader clobValue = clob.getCharacterStream();
        Reader origValue = new LoopingAlphabetReader(clobLength, CharAlphabet.tamil());
        assertTrue("New clob value did not match", compareReaders(origValue, clobValue));
        origValue.close();
        clobValue.close();
    }
    rs.close();
    stmt.close();
    commit();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) CharArrayReader(java.io.CharArrayReader) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) StringReader(java.io.StringReader) PreparedStatement(java.sql.PreparedStatement) Clob(java.sql.Clob) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 19 with LoopingAlphabetReader

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

the class BlobClob4BlobTest method testGetCharacterStreamWithUnicode.

/**
 * test of getCharacterStream on a table containing unicode characters
 */
public void testGetCharacterStreamWithUnicode() throws Exception {
    String[] unicodeStrings = { "\u0061\u0062\u0063", "\u0370\u0371\u0372", "\u05d0\u05d1\u05d2" };
    insertUnicodeData(unicodeStrings);
    Statement stmt = createStatement();
    ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM testClob");
    int clobLength = 0, arrayIndex = 0;
    while (rs.next()) {
        clobLength = rs.getInt(2);
        arrayIndex = rs.getInt(3);
        Clob clob = rs.getClob(1);
        if (clob != null) {
            assertEquals("FAIL - wrong clob.length()", clobLength, clob.length());
            Reader clobValue = clob.getCharacterStream();
            if (arrayIndex > 0) {
                char[] buff = new char[3];
                clobValue.read(buff);
                assertEquals("Clob value does not match unicodeString", unicodeStrings[arrayIndex], new String(buff));
                assertEquals("Expected end of stream", -1, clobValue.read());
            } else {
                Reader origValue = new LoopingAlphabetReader(clobLength, CharAlphabet.tamil());
                compareReaders(origValue, clobValue);
            }
        } else {
            assertTrue("Clob was null but length was not 0", (clobLength == 0));
        }
    }
    rs.close();
    stmt.close();
    commit();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) CharArrayReader(java.io.CharArrayReader) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) StringReader(java.io.StringReader) Clob(java.sql.Clob) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 20 with LoopingAlphabetReader

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

the class ClobTest method testInsertCharacter_ReadOnlyToTemporary.

/**
 * Tests that Derby specific end-of-stream markers aren't passed over to
 * the temporary Clob, which doesn't use such markers.
 * <p>
 * Passing the marker over will normally result in a UTF encoding exception.
 * <p>
 * ID USAGE: reads id 2, writes id 10002
 */
public void testInsertCharacter_ReadOnlyToTemporary() throws IOException, SQLException {
    setAutoCommit(false);
    // Insert data, a medium sized Clob to store it as a stream.
    PreparedStatement ps = prepareStatement("insert into ClobTestData values (?,?)");
    int initalSize = 128 * 1024;
    ps.setInt(1, 2);
    ps.setCharacterStream(2, new LoopingAlphabetReader(initalSize), initalSize);
    ps.executeUpdate();
    // Select the Clob, and change one character.
    PreparedStatement psSelect = prepareStatement("select dClob from ClobTestData where id = ?");
    psSelect.setInt(1, 2);
    ResultSet lRs = psSelect.executeQuery();
    lRs.next();
    Clob lClob = lRs.getClob(1);
    lClob.setString(1, "K");
    Reader r = lClob.getCharacterStream();
    assertEquals('K', r.read());
    long length = 1;
    while (true) {
        // Since we're skipping characters, the bytes have to be decoded
        // and we will detect any encoding errors.
        long skipped = r.skip(4096);
        if (skipped > 0) {
            length += skipped;
        } else {
            break;
        }
    }
    lRs.close();
    assertEquals("Wrong length!", initalSize, length);
    // Reports the correct length, now try to insert it.
    ps.setInt(1, 10003);
    ps.setClob(2, lClob);
    ps.executeUpdate();
    // Fetch it back.
    psSelect.setInt(1, 10003);
    lRs = psSelect.executeQuery();
    lRs.next();
    Clob lClob2 = lRs.getClob(1);
    assertEquals(lClob.getCharacterStream(), lClob2.getCharacterStream());
    assertEquals(initalSize, lClob2.length());
}
Also used : ResultSet(java.sql.ResultSet) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) PreparedStatement(java.sql.PreparedStatement) Clob(java.sql.Clob) HarmonySerialClob(org.apache.derby.iapi.types.HarmonySerialClob) 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