use of org.apache.derbyTesting.functionTests.util.streams.CharAlphabet in project derby by apache.
the class ClobTest method testGetCharacterStreamLongLastCharLatin.
/**
* Obtains streams from the Clob reading portions of the content, always
* including the last character in the Clob.
* <p>
* This case fills the Clob with latin lowercase characters.
*/
public void testGetCharacterStreamLongLastCharLatin() throws IOException, SQLException {
CharAlphabet alphabet = CharAlphabet.modernLatinLowercase();
// Insert a Clob
int length = 5000;
PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)");
int id = BlobClobTestSetup.getID();
ps.setInt(1, id);
ps.setCharacterStream(2, new LoopingAlphabetReader(length, alphabet), length);
ps.execute();
ps.close();
// Perform the actual test.
getCharacterStreamLongLastChar(id, length, alphabet);
}
use of org.apache.derbyTesting.functionTests.util.streams.CharAlphabet in project derby by apache.
the class LobSortTest method suite.
public static Test suite() {
Properties props = new Properties();
// Adjust sort buffer size to trigger the bug situation with less data.
props.setProperty("derby.storage.sortBufferMax", "4");
BaseTestSuite suite = new BaseTestSuite(LobSortTest.class, "LobSortTestEmbedded");
return new CleanDatabaseTestSetup(new SystemPropertyTestSetup(suite, props, true)) {
/**
* Generates a table with Blob and Clobs of mixed size.
*/
protected void decorateSQL(Statement s) throws SQLException {
Random rnd = new Random(SEED);
Connection con = s.getConnection();
con.setAutoCommit(false);
s.executeUpdate("create table MIXED_LOBS (" + "c clob, clen int, b blob, blen int, rnd int)");
PreparedStatement ps = con.prepareStatement("insert into MIXED_LOBS values (?,?,?,?,?)");
// Make sure we get at least one zero-length CLOB and BLOB.
ps.setString(1, "");
ps.setInt(2, 0);
ps.setBytes(3, new byte[0]);
ps.setInt(4, 0);
ps.setInt(5, rnd.nextInt());
ps.executeUpdate();
for (int i = 0; i < 100; i++) {
CharAlphabet ca = getCharAlphabet(1 + rnd.nextInt(3));
int length = (int) (rnd.nextDouble() * 64.0 * 1024.0);
if (rnd.nextInt(1000) < 500) {
// Specify the length.
ps.setCharacterStream(1, new LoopingAlphabetReader(length, ca), length);
} else {
// Don't specify the length.
ps.setCharacterStream(1, new LoopingAlphabetReader(length, ca));
}
ps.setInt(2, length);
length = (int) (rnd.nextDouble() * 64.0 * 1024.0);
if (rnd.nextInt(1000) < 500) {
// Specify the length.
ps.setBinaryStream(3, new LoopingAlphabetStream(length), length);
} else {
// Don't specify the length.
ps.setBinaryStream(3, new LoopingAlphabetStream(length));
}
ps.setInt(4, length);
ps.setInt(5, rnd.nextInt());
ps.executeUpdate();
}
con.commit();
ps.close();
}
/**
* Returns a character alphabet.
*/
private CharAlphabet getCharAlphabet(int i) {
switch(i) {
case 1:
return CharAlphabet.modernLatinLowercase();
case 2:
return CharAlphabet.tamil();
case 3:
return CharAlphabet.cjkSubset();
default:
fail("Unknown alphabet identifier: " + i);
}
// Will never be reached.
return null;
}
};
}
use of org.apache.derbyTesting.functionTests.util.streams.CharAlphabet in project derby by apache.
the class ClobTest method testGetCharacterStreamLongLastCharCJK.
/**
* Obtains streams from the Clob reading portions of the content, always
* including the last character in the Clob.
* <p>
* This case fills the Clob with Chinese/Japanese/Korean characters.
*/
public void testGetCharacterStreamLongLastCharCJK() throws IOException, SQLException {
CharAlphabet alphabet = CharAlphabet.cjkSubset();
// Insert a Clob
int length = 9001;
PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)");
int id = BlobClobTestSetup.getID();
ps.setInt(1, id);
ps.setCharacterStream(2, new LoopingAlphabetReader(length, alphabet), length);
ps.execute();
ps.close();
// Perform the actual test.
getCharacterStreamLongLastChar(id, length, alphabet);
}
use of org.apache.derbyTesting.functionTests.util.streams.CharAlphabet in project derby by apache.
the class ParameterMappingTest method helperTestDerby6237.
// numberOfRowsToUpdate - value 1 or 2
// testVariation - if 1 then update CLOB/VARCHAR with short data
// if 2 then update CLOB/VARCHAR with large data
// testCLOB - true means test setCharacterStream on CLOB
// - false means test setCharacterStream on VARCHAR
private void helperTestDerby6237(int numberOfRowsToUpdate, int testVariation, boolean testCLOB) throws Exception {
CharAlphabet a1 = CharAlphabet.singleChar('a');
// Following will update one or 2 rows depending on the 1st param
// Following will update CLOB column or VARCHAR column with short
// or large data depending on param 2
// Following will update CLOB column or VARCHAR column depending
// on 3rd param
PreparedStatement ps = prepareStatement("UPDATE TestUpdateCharStream SET " + (testCLOB == true ? "c3" : "c4") + " = ?, " + "c2 = c2 + 1 WHERE c1 IN (?, ?)");
switch(testVariation) {
case 1:
// test short data
ps.setCharacterStream(1, new LoopingAlphabetReader(50, a1), 50);
break;
case 2:
// test large data
if (testCLOB) {
// for CLOB column, use 50K data
ps.setCharacterStream(1, new LoopingAlphabetReader(50000, a1), 50000);
} else {
// for VARCHAR column, use 32K data
ps.setCharacterStream(1, new LoopingAlphabetReader(32000, a1), 32000);
}
break;
}
// First value in IN clause is getting set to 'AAAAA'
// Using setCharacterStream on VARCHAR to set the value
ps.setCharacterStream(2, new CharArrayReader("AAAAA".toCharArray()), 5);
if (numberOfRowsToUpdate == 1) {
// Second value in IN clause is also getting set to 'AAAAA', which
// means prepared statement will update only one row
ps.setObject(3, "AAAAA", Types.VARCHAR);
} else {
// Second value in IN clause is also getting set to 'EEEEE', which
// means prepared statement will update two rows
ps.setObject(3, "EEEEE", Types.VARCHAR);
}
ps.execute();
// verify updated data. Update happened to either CLOB column or VARCHAR
// column. It is decided by param 3
ResultSet rs;
ps = prepareStatement("select " + (testCLOB == true ? "c3 " : "c4 ") + "from TestUpdateCharStream " + "WHERE c1 IN (?, ?)");
ps.setCharacterStream(1, new CharArrayReader("AAAAA".toCharArray()), 5);
if (numberOfRowsToUpdate == 1) {
ps.setObject(2, "AAAAA", Types.VARCHAR);
} else {
ps.setObject(2, "EEEEE", Types.VARCHAR);
}
rs = ps.executeQuery();
char[] c;
if (testVariation == 1) {
// we are here to test short data
c = new char[50];
} else {
// we are here to test large data
if (testCLOB)
c = new char[50000];
else
c = new char[32000];
}
Arrays.fill(c, 'a');
for (int i = 0; i < numberOfRowsToUpdate; i++) {
rs.next();
if (!compareClobReader2CharArray(c, rs.getCharacterStream(1))) {
System.out.println("FAIL: " + (testCLOB ? "CLOB " : "VARCHAR ") + "data should have matched");
rs.close();
ps.close();
return;
}
}
rs.close();
ps.close();
}
use of org.apache.derbyTesting.functionTests.util.streams.CharAlphabet 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