Search in sources :

Example 6 with LoopingAlphabetReader

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

the class LobRsGetterTest method suite.

/**
 * Returns a suite with all tests running with both embedded and client.
 */
public static Test suite() {
    Test suite = TestConfiguration.defaultSuite(LobRsGetterTest.class, false);
    return new CleanDatabaseTestSetup(suite) {

        protected void decorateSQL(Statement s) throws SQLException {
            Connection con = s.getConnection();
            dropTable(con, TABLE);
            // NOTE: Do not insert a lot of data into this table, many
            // of the tests iterate over all rows in the table.
            s.executeUpdate("create table " + TABLE + "(" + "id INT GENERATED ALWAYS AS IDENTITY, " + "dBlob BLOB, dClob CLOB)");
            // Insert a few rows with different characteristics:
            // multi page LOB, single page LOB, NULL
            PreparedStatement ps = con.prepareStatement("insert into " + TABLE + "(dBlob, dClob) values (?,?)");
            // 173 KB or KChars
            int mpSize = 173 * 1024;
            // 300 B or chars
            int spSize = 300;
            ps.setBinaryStream(1, new LoopingAlphabetStream(mpSize), mpSize);
            ps.setCharacterStream(2, new LoopingAlphabetReader(mpSize), mpSize);
            ps.executeUpdate();
            ps.setBinaryStream(1, new LoopingAlphabetStream(spSize), spSize);
            ps.setCharacterStream(2, new LoopingAlphabetReader(spSize), spSize);
            ps.executeUpdate();
            ps.setNull(1, Types.BLOB);
            ps.setNull(2, Types.CLOB);
            ps.executeUpdate();
            // Make sure there are three rows.
            JDBC.assertDrainResults(s.executeQuery("select * from " + TABLE), 3);
            ps.close();
            s.close();
        }
    };
}
Also used : Test(junit.framework.Test) CleanDatabaseTestSetup(org.apache.derbyTesting.junit.CleanDatabaseTestSetup) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 7 with LoopingAlphabetReader

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

the class ResultSetStreamTest method testSetMaxFieldSizeLarge.

/**
 * Tests that the max field size limit is handled correctly when accessing
 * values as streams. The limit should apply for VARCHAR, but not for CLOB.
 *
 * @throws IOException if something goes wrong
 * @throws SQLException if something goes wrong
 */
public void testSetMaxFieldSizeLarge() throws IOException, SQLException {
    // Insert test data.
    int id = 1;
    // 2 MB
    int clobSize = 2 * 1024 * 1024;
    int vcSize = 32672;
    int limit = 10;
    PreparedStatement ps = prepareStatement("insert into setMaxFieldSize values (?,?,?)");
    ps.setInt(1, id);
    ps.setCharacterStream(2, new LoopingAlphabetReader(vcSize), vcSize);
    ps.setCharacterStream(3, new LoopingAlphabetReader(clobSize), clobSize);
    ps.executeUpdate();
    // Fetch data back with a limit.
    Statement stmt = createStatement();
    stmt.setMaxFieldSize(limit);
    ResultSet rs = stmt.executeQuery("select dVarchar, dClob from " + "setMaxFieldSize where id = " + id);
    assertTrue(rs.next());
    String vcStr = drainStringFromSource(rs.getCharacterStream(1));
    // Limit should apply to VARCHAR.
    assertEquals(limit, vcStr.length());
    // Limit should *not* apply to CLOB.
    String vsClob = drainStringFromSource(rs.getCharacterStream(2));
    assertEquals(clobSize, vsClob.length());
    rs.close();
    // Again, but without a limit.
    stmt = createStatement();
    rs = stmt.executeQuery("select dVarchar, dClob from " + "setMaxFieldSize where id = " + id);
    assertTrue(rs.next());
    vcStr = drainStringFromSource(rs.getCharacterStream(1));
    assertEquals(vcSize, vcStr.length());
    vsClob = drainStringFromSource(rs.getCharacterStream(2));
    assertEquals(clobSize, vsClob.length());
    rs.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 8 with LoopingAlphabetReader

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

the class ClobAccessTest method initializeClobData.

/**
 * Generates test data.
 */
private static void initializeClobData(Statement stmt) throws SQLException {
    Connection con = stmt.getConnection();
    con.setAutoCommit(false);
    if (!disableSmallClobs) {
        println("Generating small Clobs test data.");
        // Insert small Clob data.
        try {
            stmt.executeUpdate("drop table smallClobs");
        } catch (SQLException sqle) {
            assertSQLState("42Y55", sqle);
        }
        stmt.executeUpdate("create table smallClobs (dClob clob, length int)");
        PreparedStatement smallClobInsert = con.prepareStatement("insert into smallClobs values (?,?)");
        // Insert 15 000 small clobs.
        for (int clobCounter = 1; clobCounter < 15001; clobCounter++) {
            String content = Integer.toString(clobCounter);
            smallClobInsert.setString(1, content);
            smallClobInsert.setInt(2, content.length());
            smallClobInsert.executeUpdate();
            if (clobCounter % 1000 == 0) {
                con.commit();
            }
        }
        con.commit();
    }
    if (!disableLargeClobs) {
        println("Generating large Clobs test data.");
        // Insert large Clob data.
        try {
            stmt.executeUpdate("drop table largeClobs");
        } catch (SQLException sqle) {
            assertSQLState("42Y55", sqle);
        }
        stmt.executeUpdate("create table largeClobs (" + "id int unique not null, dClob clob, length int)");
        PreparedStatement largeClobInsert = con.prepareStatement("insert into largeClobs values (?,?,?)");
        // Insert some large Clobs.
        // 15 MB default
        final int size = largeClobSizeMB * 1024 * 1024;
        for (int clobCounter = 1; clobCounter < 11; clobCounter++) {
            largeClobInsert.setInt(1, clobCounter);
            largeClobInsert.setCharacterStream(2, new LoopingAlphabetReader(size), size);
            largeClobInsert.setInt(3, size);
            largeClobInsert.executeUpdate();
            println("Inserted large Clob #" + (clobCounter - 1));
        }
        con.commit();
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 9 with LoopingAlphabetReader

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

the class DbTasks method insertMail.

public void insertMail(Connection conn, String thread_name) throws Exception {
    // Inserting rows to the inbox table.
    // inbox table would have random attachments - (attach table)
    // The random attachment depends on no of rows id in inbox
    InputStream streamIn = null;
    Reader streamReader = null;
    boolean saveAutoCommit = conn.getAutoCommit();
    int saveIsolation = conn.getTransactionIsolation();
    try {
        conn.setAutoCommit(false);
        PreparedStatement insertFirst = conn.prepareStatement(Statements.insertStr, Statement.RETURN_GENERATED_KEYS);
        String name = new String("ABCD");
        String l_name = new String("WXYZ");
        long total_ins_inb = 0;
        long total_ins_att = 0;
        int row_count = 0;
        int num = Rn.nextInt(10 - 1);
        if (num == 0)
            num = 1;
        for (int i = 0; i < num; i++) {
            long s_insert = System.currentTimeMillis();
            String new_name = new String(increment(name, 60));
            String new_lname = new String(decrement(l_name, 60));
            insertFirst.setString(1, new_name);
            insertFirst.setString(2, new_lname);
            insertFirst.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
            name = new_name;
            l_name = new_lname;
            try {
                // to create a stream of random length between 200 bytes and 3MB
                int clobLength = Rn.nextInt(3078000 - 200 + 1) + 200;
                streamReader = new LoopingAlphabetReader(clobLength, CharAlphabet.modernLatinLowercase());
                insertFirst.setCharacterStream(4, streamReader, clobLength);
            } catch (Exception e) {
                MailJdbc.logAct.logMsg(LogFile.ERROR + thread_name + " : " + "File not found Exception : " + e.getMessage());
                errorPrint(e);
                throw e;
            }
            int result = insertFirst.executeUpdate();
            if (result != 0) {
                insert_count = insert_count + 1;
            }
            streamReader.close();
            long e_insert = System.currentTimeMillis();
            total_ins_inb = total_ins_inb + (e_insert - s_insert);
            PreparedStatement insertAttach = conn.prepareStatement(Statements.insertStrAttach);
            Statement stmt1 = conn.createStatement();
            ResultSet rs = insertFirst.getGeneratedKeys();
            // 10/1 chance to have attactment
            int numa = Rn.nextInt(10 - 1);
            if (numa == 0)
                numa = 1;
            if (i == numa) {
                int attachid = 0;
                while (rs.next()) {
                    attachid = rs.getInt(1);
                }
                // insert from 1 to 5 attachments
                int num_attach = Rn.nextInt(5 - 1);
                if (num_attach == 0)
                    num_attach = 1;
                for (int j = 0; j < num_attach; j++) {
                    long a_start = System.currentTimeMillis();
                    insertAttach.setInt(1, attachid);
                    // attach_id should be automatically generated
                    try {
                        // to create a stream of random length between 0 and 5M
                        int blobLength = Rn.nextInt(5130000 - 0 + 1) + 0;
                        streamIn = new LoopingAlphabetStream(blobLength);
                        insertAttach.setBinaryStream(2, streamIn, blobLength);
                    } catch (Exception e) {
                        MailJdbc.logAct.logMsg(LogFile.ERROR + thread_name + " : " + "Exception : " + e.getMessage());
                        errorPrint(e);
                        throw e;
                    }
                    int result_attach = insertAttach.executeUpdate();
                    streamIn.close();
                    if (result_attach != 0) {
                        blob_count = blob_count + 1;
                        row_count++;
                    }
                    long a_end = System.currentTimeMillis();
                    total_ins_att = total_ins_att + (a_end - a_start);
                }
            }
            id_count++;
            rs.close();
            stmt1.close();
            insertAttach.close();
        }
        log.logMsg(LogFile.INFO + thread_name + " : " + "Time taken to insert " + num + " rows to REFRESH.INBOX :" + PerfTime.readableTime(total_ins_inb));
        log.logMsg(LogFile.INFO + thread_name + " : " + "Time taken to insert " + row_count + " rows to REFRESH.ATTACH :" + PerfTime.readableTime(total_ins_att));
        insertFirst.close();
        conn.commit();
    } catch (SQLException sqe) {
        MailJdbc.logAct.logMsg(LogFile.ERROR + thread_name + " : " + "Error while inserting REFRESH.ATTACH:" + sqe.getMessage());
        sqe.printStackTrace();
        errorPrint(sqe);
        conn.rollback();
        throw sqe;
    } finally {
        conn.setTransactionIsolation(saveIsolation);
        conn.setAutoCommit(saveAutoCommit);
    }
}
Also used : SQLException(java.sql.SQLException) InputStream(java.io.InputStream) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) SQLException(java.sql.SQLException) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) ResultSet(java.sql.ResultSet) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 10 with LoopingAlphabetReader

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

the class UTF8UtilTest method testSkipFullyOnValidLongStreamCJK.

/**
 * Tests that <code>skipFully</code> successfully skips the requested
 * characters and returns the correct number of bytes skipped.
 *
 * @throws IOException if the test fails for some unexpected reason
 */
public void testSkipFullyOnValidLongStreamCJK() throws IOException {
    final int charLength = 161019;
    InputStream in = new ReaderToUTF8Stream(new LoopingAlphabetReader(charLength, CharAlphabet.cjkSubset()), charLength, 0, TYPENAME, new CharStreamHeaderGenerator());
    // Skip encoded length added by ReaderToUTF8Stream.
    in.skip(HEADER_LENGTH);
    // Returns count in bytes, we are using CJK chars so multiply length
    // with 3 to get expected number of bytes.
    assertEquals(charLength * 3, UTF8Util.skipFully(in, charLength));
}
Also used : DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ReaderToUTF8Stream(org.apache.derby.iapi.types.ReaderToUTF8Stream) CharStreamHeaderGenerator(org.apache.derby.iapi.types.CharStreamHeaderGenerator) 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