Search in sources :

Example 16 with LoopingAlphabetStream

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

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

the class BlobClob4BlobTest method insertLoopingAlphabetStreamData.

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

Example 18 with LoopingAlphabetStream

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

the class BlobClob4BlobTest method testLockingWithLongRowBlob.

/**
 * test locking with a long row + long column
 */
public void testLockingWithLongRowBlob() throws Exception {
    ResultSet rs;
    Statement stmt, stmt2;
    // for blob lock to remain, autocommit must be false.
    assertFalse(getConnection().getAutoCommit());
    stmt = createStatement();
    // test depends on small page size, make sure size is 4k
    checkSmallPageSize(stmt, "TESTBLOB");
    stmt.execute("alter table testBlob add column al varchar(2000)");
    stmt.execute("alter table testBlob add column bl varchar(3000)");
    stmt.execute("alter table testBlob add column cl varchar(2000)");
    stmt.execute("alter table testBlob add column dl varchar(3000)");
    stmt.execute("alter table testBlob add column el BLOB(400k)");
    PreparedStatement ps = prepareStatement("insert into testBlob (al, bl, cl, dl, el, b) values(?,?,?,?,?,?)");
    ps.setString(1, Formatters.padString("blaaa", 2000));
    ps.setString(2, Formatters.padString("tralaaaa", 3000));
    ps.setString(3, Formatters.padString("foodar", 2000));
    ps.setString(4, Formatters.padString("moped", 3000));
    InputStream streamIn = new LoopingAlphabetStream(10000);
    ps.setBinaryStream(5, streamIn, 10000);
    ps.setInt(6, 1);
    ps.executeUpdate();
    streamIn.close();
    ps.close();
    commit();
    stmt = createStatement();
    rs = stmt.executeQuery("select el from testBlob");
    // fetch row back, get the column as a clob.
    Blob blob = null;
    assertTrue("FAIL - row not found", rs.next());
    blob = rs.getBlob(1);
    assertTrue("FAIL - blob is null", blob != null);
    rs.close();
    stmt.close();
    Connection conn2 = openDefaultConnection();
    // turn off autocommit, otherwise blobs/clobs cannot hang around
    // until end of transaction
    conn2.setAutoCommit(false);
    // the following should timeout
    stmt2 = conn2.createStatement();
    try {
        stmt2.executeUpdate("update testBlob set el = null where b = 1");
        stmt2.close();
        stmt.close();
        conn2.rollback();
        conn2.close();
        fail("FAIL - statement should timeout");
    } catch (SQLException se) {
        checkException(LOCK_TIMEOUT, se);
    }
    // Test that update goes through after the transaction is committed
    commit();
    // DERBY-3740, add a reference to the retrieved blobs that is used
    // after the expected lock timeout in conn2.  Before this change
    // this test would intermittently fail.  I believe that a smart
    // JVM/JIT recognized that the blob reference was no longer used
    // after the above while loop, and allowed gc on it before the routine
    // could get to the expected conflicting lock.  Upon GC the blob's
    // finalize code closes the internal stream and releases the lock in
    // read committed mode.
    assertTrue("FAIL - blob is null after expected lock timeout", blob != null);
    stmt2.executeUpdate("update testBlob set el = null where b = 1");
    stmt2.close();
    conn2.commit();
    stmt.close();
    conn2.close();
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 19 with LoopingAlphabetStream

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

the class BlobUpdatableStreamTest method testUpdatableBlob.

public void testUpdatableBlob() throws Exception {
    getConnection().setAutoCommit(false);
    PreparedStatement ps = prepareStatement("insert into testblob " + "values (?)");
    // insert a large blob to ensure dvd gives out a stream and not
    // a byte array
    ps.setBinaryStream(1, new LoopingAlphabetStream(1024 * 1024), 1024 * 1024);
    ps.executeUpdate();
    ps.close();
    Statement stmt = createStatement();
    ResultSet rs = stmt.executeQuery("select * from testblob");
    rs.next();
    Blob blob = rs.getBlob(1);
    InputStream is = blob.getBinaryStream();
    long l = is.skip(20);
    // truncate blob after accessing original stream
    blob.truncate(l);
    int ret = is.read();
    // should not be able to read after truncated value
    assertEquals("stream update failed", -1, ret);
    byte[] buffer = new byte[1024];
    for (int i = 0; i < buffer.length; i++) buffer[i] = (byte) (i % 255);
    blob.setBytes(blob.length() + 1, buffer);
    byte[] buff = new byte[1024];
    int toRead = 1024;
    while (toRead != 0) {
        long read = is.read(buff, 1024 - toRead, toRead);
        if (read < 0)
            fail("couldn't retrieve updated value");
        toRead -= read;
    }
    for (int i = 0; i < buffer.length; i++) {
        assertEquals("value retrieved is not same as updated value", buffer[i], buff[i]);
    }
    blob = null;
    rs.close();
    stmt.close();
    commit();
}
Also used : Blob(java.sql.Blob) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 20 with LoopingAlphabetStream

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

the class ClobTest method insertDataWithToken.

/**
 * Inserts data into the test Clob, referenced by {@code this.clob}.
 *
 * @param token a token to insert into the Clob, cannot be {@code null} but
 *      the empty string is accepted
 * @param pre number of characters to insert before the token, using the
 *      repeating alphabet stream (latin lower-case)
 * @param post number of characters to insert after the token, using the
 *      repeating alphabet stream (latin lower-case)
 * @param mode insertion mode; SET_STRING, SET_ASCII_STREAM or
 *      SET_CHARACTER_STREAM
 * @throws IOException if inserting data fails for some reason
 * @throws SQLException if inserting data fails for some reason
 */
private void insertDataWithToken(String token, long pre, long post, int mode) throws IOException, SQLException {
    long total = 0;
    switch(mode) {
        case SET_STRING:
            {
                Reader charIn = new LoopingAlphabetReader(pre);
                total += transferData(charIn, TRANSFER_BUFFER_SIZE);
                this.clob.setString(pre + 1, token);
                total += token.length();
                charIn = new LoopingAlphabetReader(post);
                total += transferData(charIn, TRANSFER_BUFFER_SIZE);
                break;
            }
        case SET_ASCII_STREAM:
            {
                OutputStream asciiOut = this.clob.setAsciiStream(1L);
                InputStream asciiIn = new LoopingAlphabetStream(pre);
                total += transferData(asciiIn, asciiOut, TRANSFER_BUFFER_SIZE);
                byte[] tokenBytes = token.getBytes("ISO-8859-1");
                asciiOut.write(tokenBytes, 0, tokenBytes.length);
                total += tokenBytes.length;
                asciiIn = new LoopingAlphabetStream(post);
                total += transferData(asciiIn, asciiOut, TRANSFER_BUFFER_SIZE);
                break;
            }
        case SET_CHARACTER_STREAM:
            {
                Writer charOut = this.clob.setCharacterStream(1L);
                Reader charIn = new LoopingAlphabetReader(pre);
                total += transferData(charIn, charOut, TRANSFER_BUFFER_SIZE);
                charOut.write(token);
                total += token.length();
                charIn = new LoopingAlphabetReader(post);
                total += transferData(charIn, charOut, TRANSFER_BUFFER_SIZE);
                break;
            }
        default:
            throw new IllegalArgumentException("Unknown insertion mode: " + mode);
    }
    assertEquals("Invalid length after insertion", pre + post + token.length(), this.clob.length());
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream) BufferedWriter(java.io.BufferedWriter) CharArrayWriter(java.io.CharArrayWriter) Writer(java.io.Writer) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

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