use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class ClobAccessTest method fetchPieceByPiece.
/**
* Fetches a "large" Clob piece by piece using getSubString.
*
* @param modifyClob whether to modify the Clob before fetching it
* (determines the internal Derby Clob representation)
*/
private void fetchPieceByPiece(final boolean modifyClob) throws IOException, SQLException {
// Select just one Clob.
PreparedStatement ps = prepareStatement("select dClob, length from largeClobs where id = 4");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Clob clob = rs.getClob(1);
int remaining = rs.getInt(2);
Reader myReader = new LoopingAlphabetReader(remaining);
if (modifyClob) {
// Modify the Clob to create a temporary copy in memory or on
// disk (depends on the Clob size).
long modifyStart = System.currentTimeMillis();
clob.setString(++remaining, "X");
println("Clob modification duration: " + (System.currentTimeMillis() - modifyStart) + " ms");
}
long pos = 1;
while (remaining > 0) {
String str = clob.getSubString(pos, Math.min(MAX_BSIZE, remaining));
myReader.skip(Math.min(MAX_BSIZE, remaining) - 1);
pos += str.length();
remaining -= str.length();
// Avoid failure on the last char when Clob is modified.
if (!modifyClob || remaining != 0) {
assertEquals(myReader.read(), str.charAt(str.length() - 1));
}
}
}
rs.close();
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class ReaderToUTF8StreamTest method getStream.
/**
* Returns a stream to test, loaded with the repeating modern latin
* lowercase alphabet.
*
* @param length the length of the stream in characters
* @return A stream serving bytes.
*/
private InputStream getStream(int length) {
Reader src = new LoopingAlphabetReader(length, CharAlphabet.modernLatinLowercase());
InputStream is = new ReaderToUTF8Stream(src, length, 0, "CLOB", new ClobStreamHeaderGenerator(false));
assertTrue("The stream doesn't support mark/reset", is.markSupported());
return is;
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class UTF8UtilTest method testSkipFullyOnInvalidStreamCJK.
/**
* Tests that <code>skipFully</code> throws exception if there is a UTF-8
* encoding error in the stream
*
* @throws IOException if the test fails for some unexpected reason
*/
public void testSkipFullyOnInvalidStreamCJK() throws IOException {
final int charLength = 10;
InputStream in = new ReaderToUTF8Stream(new LoopingAlphabetReader(charLength, CharAlphabet.cjkSubset()), charLength, 0, TYPENAME, new CharStreamHeaderGenerator());
// Skip encoded length added by ReaderToUTF8Stream.
in.skip(HEADER_LENGTH);
// Skip one more byte to trigger a UTF error.
in.skip(1L);
try {
UTF8Util.skipFully(in, charLength);
fail("Should have failed because of UTF error.");
} catch (UTFDataFormatException udfe) {
// As expected, do nothing.
}
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class StreamTest method insertClobData.
private void insertClobData(int clobSize) throws SQLException {
Statement stmt = createStatement();
stmt.executeUpdate("CREATE TABLE testLob " + " (c clob(" + clobSize + "))");
stmt.close();
PreparedStatement ps = prepareStatement("insert into testLob values(?)");
Reader reader = new LoopingAlphabetReader(clobSize);
ps.setCharacterStream(1, reader, clobSize);
ps.executeUpdate();
ps.close();
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class TriggerTest method testUpdateTriggerOnClobColumn.
/*
* Test an update trigger on a Clob column
*
*/
public void testUpdateTriggerOnClobColumn() throws SQLException, IOException {
// Alphabet used when inserting a CLOB.
CharAlphabet a1 = CharAlphabet.singleChar('a');
// Alphabet used when updating a CLOB.
CharAlphabet a2 = CharAlphabet.singleChar('b');
Connection conn = getConnection();
Statement s = createStatement();
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)";
s.executeUpdate("create table LOB1 (str1 Varchar(80), C_lob CLOB(50M))");
s.executeUpdate("create table t_lob1_log(oldvalue varchar(80), newvalue varchar(80), chng_time timestamp default current_timestamp)");
s.executeUpdate(trig);
conn.commit();
PreparedStatement ps = prepareStatement("INSERT INTO LOB1 VALUES (?, ?)");
int clobSize = 1024 * 64 + 1;
ps.setString(1, clobSize + "");
// - set the value of the input parameter to the input stream
ps.setCharacterStream(2, new LoopingAlphabetReader(clobSize, a1), clobSize);
ps.execute();
conn.commit();
PreparedStatement ps2 = prepareStatement("update LOB1 set c_lob = ? where str1 = '" + clobSize + "'");
ps2.setCharacterStream(1, new LoopingAlphabetReader(clobSize, a2), clobSize);
ps2.executeUpdate();
conn.commit();
// --- reading the clob make sure it was updated
ResultSet rs = s.executeQuery("SELECT * FROM LOB1 where str1 = '" + clobSize + "'");
rs.next();
assertEquals(new LoopingAlphabetReader(clobSize, a2), rs.getCharacterStream(2));
rs.close();
s.executeUpdate("drop table lob1");
s.executeUpdate("drop table t_lob1_log");
}
Aggregations