use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class Derby3650Test method initializeClobTables.
private static void initializeClobTables(Statement stmt) throws SQLException, IOException {
// CLOB TEST SETUP...........................................
stmt.executeUpdate("CREATE TABLE testClob (id int, length int, c CLOB(2M))");
Connection conn = stmt.getConnection();
PreparedStatement ps = conn.prepareStatement("INSERT INTO TestClob VALUES(?,?,?)");
// insert 4 rows into "left" table containing clobs of join:
// (1, clob), (1, clob), (2, clob), (2, clob)
ps.setInt(1, 1);
ps.setInt(2, 40000);
ps.setCharacterStream(3, new LoopingAlphabetReader(40000));
ps.executeUpdate();
ps.setInt(1, 1);
ps.setInt(2, 40001);
ps.setCharacterStream(3, new LoopingAlphabetReader(40001));
ps.executeUpdate();
ps.setInt(1, 2);
ps.setInt(2, 40002);
ps.setCharacterStream(3, new LoopingAlphabetReader(40002));
ps.executeUpdate();
ps.setInt(1, 2);
ps.setInt(2, 40003);
ps.setCharacterStream(3, new LoopingAlphabetReader(40003));
ps.executeUpdate();
ps.close();
stmt.executeUpdate("CREATE TABLE testMultipleClob (id int, length int, c CLOB(2M))");
ps = conn.prepareStatement("INSERT INTO testMultipleClob VALUES(?,?,?)");
for (int i = 0; i < 100; i++) {
ps.setInt(1, i);
ps.setInt(2, 40000 + i);
ps.setCharacterStream(3, new LoopingAlphabetReader(40000 + i));
ps.executeUpdate();
}
ps.close();
conn.commit();
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class BasicInMemoryDbTest method getString.
/**
* Generates a string.
*
* @param length length of the string
* @param alphabet the alphabet to use for the content
* @return A string.
* @throws IOException if reading from the source stream fails
*/
public static String getString(int length, CharAlphabet alphabet) throws IOException {
LoopingAlphabetReader reader = new LoopingAlphabetReader(length, alphabet);
char[] strChar = new char[length];
int read = 0;
while (read < length) {
int readNow = reader.read(strChar, read, length - read);
if (readNow < 1) {
fail("Creating string failed, stream returned " + readNow);
}
read += readNow;
}
return String.copyValueOf(strChar);
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class ClobMemTest method testClobLength.
/**
* Insert a clob and test length.
*
* @param lengthless if true use the lengthless setCharacterStream api
*
* @throws SQLException
* @throws IOException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
private void testClobLength(boolean lengthless) throws SQLException, IOException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
setAutoCommit(false);
Statement s = createStatement();
s.executeUpdate("CREATE TABLE CLOBTABLE (K INT CONSTRAINT PK PRIMARY KEY, C CLOB(" + LONG_CLOB_LENGTH + "))");
PreparedStatement ps = prepareStatement("INSERT INTO CLOBTABLE VALUES(?,?)");
// We allocate 16MB for the test so use something bigger than that.
ps.setInt(1, 1);
LoopingAlphabetReader reader = new LoopingAlphabetReader(LONG_CLOB_LENGTH);
if (lengthless) {
Method m = null;
try {
Class<?> c = ps.getClass();
m = c.getMethod("setCharacterStream", new Class[] { Integer.TYPE, InputStream.class });
} catch (NoSuchMethodException e) {
// ignore method not found as method may not be present for
// jdk's lower than 1.6.
println("Skipping lengthless insert because method is not available");
return;
}
m.invoke(ps, new Object[] { 2, reader });
} else
ps.setCharacterStream(2, reader, LONG_CLOB_LENGTH);
ps.executeUpdate();
// insert a zero length clob.
ps.setInt(1, 2);
ps.setString(2, "");
ps.executeUpdate();
// insert a null clob.
ps.setInt(1, 3);
ps.setString(2, null);
ps.executeUpdate();
// insert a short clob
ps.setInt(1, 4);
ps.setString(2, new String(SHORT_CLOB_CHARS));
ps.executeUpdate();
// Currently need to use optimizer override to force use of the index.
// Derby should use sort avoidance and do it automatically, but there
// appears to be a bug.
ResultSet rs = s.executeQuery("SELECT K, LENGTH(C), C FROM CLOBTABLE" + "-- DERBY-PROPERTIES constraint=pk\n ORDER BY K");
rs.next();
assertEquals(LONG_CLOB_LENGTH_STRING, rs.getString(2));
// make sure we can still access the clob after getting length.
// It should be ok because we reset the stream
Reader rsReader = rs.getCharacterStream(3);
int len = 0;
char[] buf = new char[32672];
for (; ; ) {
int size = rsReader.read(buf);
if (size == -1)
break;
len += size;
int expectedValue = ((len - 1) % 26) + 'a';
if (size != 0)
assertEquals(expectedValue, buf[size - 1]);
}
assertEquals(LONG_CLOB_LENGTH, len);
// empty clob
rs.next();
assertEquals("0", rs.getString(2));
String chars = rs.getString(3);
assertEquals(0, chars.length());
// null clob
rs.next();
assertEquals(null, rs.getString(2));
chars = rs.getString(3);
assertEquals(null, chars);
// short clob
rs.next();
assertEquals("" + SHORT_CLOB_CHARS.length, rs.getString(2));
chars = rs.getString(3);
assertTrue(Arrays.equals(chars.toCharArray(), SHORT_CLOB_CHARS));
rs.close();
// Select just length without selecting the clob.
rs = s.executeQuery("SELECT K, LENGTH(C) FROM CLOBTABLE " + "ORDER BY K");
JDBC.assertFullResultSet(rs, new String[][] { { "1", LONG_CLOB_LENGTH_STRING }, { "2", "0" }, { "3", null }, { "4", "6" } });
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class Datatypes method add_one_row.
public static synchronized void add_one_row(Connection conn, int thread_id) throws Exception {
try {
// initialize();
PreparedStatement ps = conn.prepareStatement(" insert into Datatypes (id,t_char,t_blob," + "t_clob," + " t_date, t_decimal, t_decimal_nn, t_double, " + " t_float, t_int, t_longint, t_numeric_large," + " t_real, t_smallint, t_time, t_timestamp," + " t_varchar) values (" + " ?,?, ?,?, ?, ?,?, ?, ?, ?,?, ?, ?, ?, ?, ?,?)", /* autoincrement feature added, so we need to specify the
* column name for prepared statement, otherwise auto increment
* column will think it is trying to update/insert a null value
* to the column.
*/
Statement.RETURN_GENERATED_KEYS);
InputStream streamIn = null;
Reader streamReader = null;
int ind = Rn.nextInt();
double x;
Date dt = new Date(1);
Time tt = new Time(1);
Timestamp ts = new Timestamp(1);
String cs = "asdf qwerqwer 12341234 ZXCVZXCVZXCV !@#$!@#$ asdfasdf 1 q a z asdf ASDF qwerasdfzxcvasdfqwer1234asd#";
ps.setInt(1, ind);
// scramble the string
int i1 = Math.abs(ind % 100);
String cs2 = cs.substring(i1, 99) + cs.substring(0, i1);
int i2 = i1 < 89 ? i1 + 10 : i1;
ps.setString(2, cs2.substring(0, i2));
// "t_blob"
// to create a stream of random length between 0 and 100K
int blobLength = Rn.nextInt(102400 - 0 + 1) + 0;
streamIn = new LoopingAlphabetStream(blobLength);
ps.setBinaryStream(3, streamIn, blobLength);
// "t_clob
// to create a stream of random length between 0 and 100K
int clobLength = Rn.nextInt(102400 - 0 + 1) + 0;
streamReader = new LoopingAlphabetReader(clobLength, CharAlphabet.modernLatinLowercase());
ps.setCharacterStream(4, streamReader, clobLength);
// "t_ndate"
dt.setTime(Math.abs(Rn.nextLong() / 150000));
ps.setDate(5, dt);
// "t_decimal"
x = Math.abs(Rn.nextInt() % 18);
if (x > 5)
x = 5;
ps.setDouble(6, Math.abs(Rn.nextDouble() * Math.pow(10, x)));
// "t_decimal_nn"
ps.setDouble(7, Rn.nextDouble());
// "t_double"
ps.setDouble(8, Rn.nextDouble() * Math.pow(10, Math.abs(Rn.nextInt() % 300)));
// "t_float"
ps.setFloat(9, Rn.nextFloat() * (float) Math.pow(10, Math.abs(Rn.nextInt() % 30)));
// "t_int"
ps.setInt(10, Rn.nextInt());
// "t_longint"
ps.setLong(11, Rn.nextLong());
// "t_numeric_large"
x = Math.abs(Rn.nextInt() % 30);
if (x > 30)
x = 31;
ps.setDouble(12, Math.abs(Rn.nextDouble() * Math.pow(10, x)));
// "t_real"
ps.setFloat(13, Rn.nextFloat() * (float) Math.pow(10, Math.abs(Rn.nextInt() % 7)));
// "t_smallint"
ps.setInt(14, Rn.nextInt() % (256 * 128));
// "t_time"
tt.setTime(Math.abs(Rn.nextInt()));
ps.setTime(15, tt);
// "t_timestamp"
ts.setTime(Math.abs(Rn.nextLong() / 50000));
ps.setTimestamp(16, ts);
// "t_varchar"
ps.setString(17, cs.substring(Math.abs(Rn.nextInt() % 100)));
int rows = ps.executeUpdate();
if (rows == 1) {
ResultSet rs = ps.getGeneratedKeys();
while (rs.next()) {
ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();
}
} else
System.out.println("t" + thread_id + " insert failed");
streamReader.close();
streamIn.close();
} catch (SQLException se) {
if (se.getNextException() == null)
throw se;
String m = se.getNextException().getSQLState();
System.out.println(se.getNextException().getMessage() + " SQLSTATE: " + m);
}
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class BlobClob4BlobTest method testNegativeTestDerby265Clob.
/**
* Test fix for derby-265.
* Test that if getClob is called after the transaction
* in which it was created is committed, a proper user error
* is thrown instead of an NPE.
* Basically per the spec, getClob is valid only for the duration of
* the transaction in it was created in
* Updated for DERBY-1511: The test case wasn't supposed to fail in the
* first place (neither with NPE nor with "proper user error") since none
* of the CLOBs are accessed after the transaction that created them was
* completed.
* @throws Exception
*/
public void testNegativeTestDerby265Clob() throws Exception {
getConnection().setAutoCommit(false);
PreparedStatement ps = prepareStatement("insert into testClob(b, a) values(?,?)");
for (int i = 0; i < 3; i++) {
Reader fis = new LoopingAlphabetReader(300000);
ps.setInt(1, i);
ps.setCharacterStream(2, fis, 300000);
ps.executeUpdate();
fis.close();
}
commit();
getConnection().setAutoCommit(true);
Statement s = createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
s.execute("SELECT b, a FROM testClob");
ResultSet rs1 = s.getResultSet();
Statement s2 = createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
s2.executeQuery("SELECT b, a FROM testClob");
ResultSet rs2 = s2.getResultSet();
rs2.next();
Clob b2 = rs2.getClob(2);
rs1.next();
Clob b1 = rs1.getClob(2);
rs1.close();
// DERBY-1511: Fetching the next CLOB here used to fail because it
// had been pre-fetched before the commit and was closed in the commit.
// Now, we don't pre-fetch CLOBs anymore, so expect to get a working
// object here.
assertTrue(rs2.next());
assertNotNull(rs2.getClob(2));
rs2.close();
}
Aggregations