harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lvj...@apache.org
Subject svn commit: r598576 - in /harmony/enhanced/classlib/trunk/modules/sql/src: main/java/org/apache/harmony/sql/internal/rowset/ test/java/org/apache/harmony/sql/tests/internal/rowset/
Date Tue, 27 Nov 2007 09:55:09 GMT
Author: lvjing
Date: Tue Nov 27 01:55:08 2007
New Revision: 598576

URL: http://svn.apache.org/viewvc?rev=598576&view=rev
Log:
Apply patch for HARMONY-5204 [classlib][sql] implement CachedRowSet.populate method.

Modified:
    harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java
    harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetReader.java
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.java

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java?rev=598576&r1=598575&r2=598576&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java
Tue Nov 27 01:55:08 2007
@@ -335,7 +335,7 @@
             ps.setObject(i + 1, params[i]);
 
         if (ps.execute()) {
-            populate(ps.getResultSet());
+            doPopulate(ps.getResultSet(), true);
         }
     }
 
@@ -388,25 +388,51 @@
         return false;
     }
 
-    public void populate(ResultSet data) throws SQLException {
-        populate(data, -1);
+    public void populate(ResultSet rs) throws SQLException {
+        doPopulate(rs, false);
     }
 
     public void populate(ResultSet rs, int startRow) throws SQLException {
+        if (startRow == 1) {
+            rs.beforeFirst();
+        } else if (startRow <= 0 || !rs.absolute(startRow - 1)) {
+            // rowset.7=Not a valid cursor
+            throw new SQLException(Messages.getString("rowset.7")); //$NON-NLS-1$
+        }
+
+        doPopulate(rs, true);
+    }
+
+    private void doPopulate(ResultSet rs, boolean isPaging) throws SQLException {
         meta = copyMetaData(rs.getMetaData());
 
-        new CachedRowSetReader(rs, startRow).readData(this);
+        /*
+         * this method not support paging, so before readData set pageSize and
+         * maxRowsto 0 and restore previous values after readData
+         */
+        if (!isPaging) {
+            int prePageSize = getPageSize();
+            setPageSize(0);
+            int preMaxRows = getMaxRows();
+            setMaxRows(0);
+            // FIXME use SyncProvider to get RowSetReader
+            new CachedRowSetReader(rs).readData(this);
+            setPageSize(prePageSize);
+            setMaxRows(preMaxRows);
+        } else {
+            // FIXME use SyncProvider to get RowSetReader
+            new CachedRowSetReader(rs).readData(this);
+        }
 
         setTableName(rs.getMetaData().getTableName(1));
 
         originalResultSet = new CachedRowSetImpl();
-        new CachedRowSetReader(this, startRow).readData(originalResultSet);
+        // FIXME use SyncProvider to get RowSetReader
+        new CachedRowSetReader(this).readData(originalResultSet);
         originalResultSet.setMetaData((RowSetMetaData) (getMetaData()));
 
         // recovery the states
-        currentRow = null;
-        currentRowIndex = 0;
-
+        beforeFirst();
     }
 
     // deep copy of ResultSetMetaData

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetReader.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetReader.java?rev=598576&r1=598575&r2=598576&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetReader.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetReader.java
Tue Nov 27 01:55:08 2007
@@ -23,47 +23,47 @@
 
 import javax.sql.RowSetInternal;
 import javax.sql.RowSetReader;
-import javax.sql.rowset.CachedRowSet;
 
 public class CachedRowSetReader implements RowSetReader {
 
     private ResultSet rs;
 
-    private int startRow;
-
     private ResultSetMetaData metadata;
 
-    public CachedRowSetReader(ResultSet rs, int startRow) throws SQLException {
+    public CachedRowSetReader(ResultSet rs) throws SQLException {
         this.rs = rs;
-        this.startRow = startRow;
         this.metadata = rs.getMetaData();
     }
 
+    /**
+     * TODO disable all listeners
+     */
     public void readData(RowSetInternal theCaller) throws SQLException {
-        int pageSize = ((CachedRowSet)theCaller).getPageSize();
+        CachedRowSetImpl cachedRowSet = (CachedRowSetImpl) theCaller;
+        int pageSize = cachedRowSet.getPageSize();
+        int maxRows = cachedRowSet.getMaxRows();
+
         ArrayList<CachedRow> data = new ArrayList<CachedRow>();
-        int columnCount = metadata.getColumnCount();        
-        int tempCursor = 0;
-        
-        if (startRow >= 0) {
-            if (rs.getType() == ResultSet.TYPE_FORWARD_ONLY)
-                throw new SQLException();
-            else {
-                rs.beforeFirst();
-                for (int j = 1; j++ < startRow; rs.next())
-                    ;
-            }
-        }
-        
-        while ((rs.next())) {            
+        int columnCount = metadata.getColumnCount();
+
+        while (rs.next()) {
             Object[] columnData = new Object[columnCount];
             for (int i = 0; i < columnCount; i++) {
-                columnData[i] = rs.getObject(i+1);
+                columnData[i] = rs.getObject(i + 1);
+            }
+
+            data.add(new CachedRow(columnData));
+
+            if (maxRows > 0 && maxRows == data.size()) {
+                break;
+            }
+
+            if (pageSize > 0 && data.size() == pageSize) {
+                break;
             }
-            if((pageSize>0)&&(pageSize<++tempCursor)) break;
-            data.add(new CachedRow(columnData));            
-            
+
         }
-        ((CachedRowSetImpl) theCaller).setRows(data, columnCount);
+
+        cachedRowSet.setRows(data, columnCount);
     }
 }

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.java?rev=598576&r1=598575&r2=598576&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.java
Tue Nov 27 01:55:08 2007
@@ -106,6 +106,16 @@
         crset.setUrl(DERBY_URL);
     }
 
+    private CachedRowSet newNoInitialInstance() throws Exception {
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            return (CachedRowSet) Class.forName(
+                    "org.apache.harmony.sql.internal.rowset.CachedRowSetImpl")
+                    .newInstance();
+        }
+        return (CachedRowSet) Class.forName("com.sun.rowset.CachedRowSetImpl")
+                .newInstance();
+    }
+
     private void reloadCachedRowSet() throws SQLException {
         rs = st.executeQuery("select * from USER_INFO");
         crset.populate(rs);
@@ -372,6 +382,63 @@
         assertEquals("hermit", crset.getString(2));
     }
 
+    public void testExecute3() throws Exception {
+        // insert 15 more rows for test
+        insertMoreData(15);
+
+        rs = st.executeQuery("select * from USER_INFO");
+        noInitialCrset.setUrl(DERBY_URL);
+        noInitialCrset.setPageSize(5);
+        noInitialCrset.setCommand("select * from USER_INFO");
+        noInitialCrset.execute();
+        rs = st.executeQuery("select * from USER_INFO");
+        int cursorIndex = 0;
+        while (noInitialCrset.next() && rs.next()) {
+            cursorIndex++;
+            for (int i = 1; i <= DEFAULT_COLUMN_COUNT; i++) {
+                assertEquals(rs.getObject(i), noInitialCrset.getObject(i));
+            }
+        }
+        // The pageSize works here. CachedRowSet only get 5 rows from ResultSet.
+        assertEquals(5, cursorIndex);
+
+        // change a command
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.setUrl(null);
+        // The pageSize still work here
+        noInitialCrset.setPageSize(5);
+        assertEquals(5, noInitialCrset.getPageSize());
+        noInitialCrset.setCommand("select * from USER_INFO where NAME like ?");
+        noInitialCrset.setString(1, "test%");
+        Connection aConn = DriverManager.getConnection(DERBY_URL);
+        noInitialCrset.execute(aConn);
+        aConn.close();
+        cursorIndex = 1;
+        while (noInitialCrset.next()) {
+            cursorIndex++;
+            assertEquals(cursorIndex, noInitialCrset.getInt(1));
+        }
+        assertEquals(6, cursorIndex);
+
+        noInitialCrset = newNoInitialInstance();
+        rs = st.executeQuery("select * from USER_INFO");
+
+        noInitialCrset.setUrl(DERBY_URL);
+        noInitialCrset.setPageSize(5);
+        noInitialCrset.setMaxRows(2);
+        noInitialCrset.setCommand("select * from USER_INFO");
+
+        noInitialCrset.execute();
+
+        rs = st.executeQuery("select * from USER_INFO");
+        cursorIndex = 0;
+        while (noInitialCrset.next() && rs.next()) {
+            cursorIndex++;
+        }
+        // maxRows works here
+        assertEquals(2, cursorIndex);
+    }
+
     public void testCreateShared() throws Exception {
         crset.setUsername("testUsername");
         crset.setPassword("testPassword");
@@ -1018,26 +1085,199 @@
         }
     }
 
-    public void testPopulate() throws Exception {
-        CachedRowSet cc = crset.createCopy();
+    public void testPopulate_LResultSet() throws Exception {
+        // insert 15 more rows for test
+        insertMoreData(15);
+
+        rs = st.executeQuery("select * from USER_INFO");
+        noInitialCrset.setMaxRows(15);
+        assertEquals(15, noInitialCrset.getMaxRows());
+
+        noInitialCrset.populate(rs);
+
+        assertTrue(noInitialCrset.isBeforeFirst());
+        int cursorIndex = 0;
+        while (noInitialCrset.next()) {
+            cursorIndex++;
+        }
+        // setMaxRows no effect, we follow ri
+        assertEquals(20, cursorIndex);
+
+        /*
+         * The pageSize won't work when call method populate(ResultSet) without
+         * second parameter
+         */
+        noInitialCrset = newNoInitialInstance();
+        rs = st.executeQuery("select * from USER_INFO");
 
+        noInitialCrset.setMaxRows(15);
+        assertEquals(15, noInitialCrset.getMaxRows());
+
+        noInitialCrset.setPageSize(5);
+        assertEquals(5, noInitialCrset.getPageSize());
+
+        noInitialCrset.populate(rs);
+
+        assertTrue(noInitialCrset.isBeforeFirst());
+        rs = st.executeQuery("select * from USER_INFO");
+        cursorIndex = 0;
+        while (noInitialCrset.next() && rs.next()) {
+            cursorIndex++;
+        }
+        /*
+         * It's supposed to only get five rows in CachedRowSet as the
+         * CachedRowSet's pageSize is 5. However, the pageSize doesn't work in
+         * RI. The CachedRowSet gets all the data from ResultSet. We follow ri.
+         */
+        assertEquals(20, cursorIndex);
+
+        noInitialCrset = newNoInitialInstance();
+        rs = st.executeQuery("select * from USER_INFO");
+        // cursor move two rows
+        rs.next();
+        rs.next();
+
+        noInitialCrset.populate(rs);
+        assertTrue(noInitialCrset.isBeforeFirst());
+        cursorIndex = 0;
+        while (noInitialCrset.next()) {
+            cursorIndex++;
+        }
+        assertEquals(18, cursorIndex);
+    }
+
+    public void testPopulate_LResultSet_I() throws Exception {
+        // insert 15 more rows for test
+        insertMoreData(15);
+
+        rs = st.executeQuery("select * from USER_INFO");
+        noInitialCrset.setPageSize(5);
         try {
-            crset.populate(rs, 0);
-            fail("should throw exception");
-        } catch (Exception e) {
+            noInitialCrset.populate(rs, 1);
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
             // expected
         }
+
+        // create a scrollable and updatable ResultSet
+        st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
+                ResultSet.CONCUR_UPDATABLE);
+        rs = st.executeQuery("select * from USER_INFO");
+
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.setPageSize(6);
+        noInitialCrset.populate(rs, 6);
+        int cursorIndex = 5;
+        while (noInitialCrset.next()) {
+            cursorIndex++;
+            assertEquals(cursorIndex, noInitialCrset.getInt(1));
+        }
+        assertEquals(11, cursorIndex);
+
+        st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
+                ResultSet.CONCUR_UPDATABLE);
+        rs = st.executeQuery("select * from USER_INFO");
+
+        noInitialCrset = newNoInitialInstance();
+
+        noInitialCrset.setPageSize(6);
+        noInitialCrset.setMaxRows(5);
+
+        noInitialCrset.populate(rs, 6);
+        cursorIndex = 0;
+        while (noInitialCrset.next()) {
+            cursorIndex++;
+            assertEquals(cursorIndex + 5, noInitialCrset.getInt(1));
+        }
+        // only get MaxRows
+        assertEquals(5, cursorIndex);
+
+        st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
+                ResultSet.CONCUR_UPDATABLE);
+        rs = st.executeQuery("select * from USER_INFO");
+
+        noInitialCrset = newNoInitialInstance();
+
+        noInitialCrset.setMaxRows(5);
+        try {
+            noInitialCrset.setPageSize(6);
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
+            // expected, Page size cannot be greater than maxRows
+        }
+
+    }
+
+    public void testPopulate_use_copy() throws Exception {
+        // insert 15 more rows for test
+        insertMoreData(15);
+
+        rs = st.executeQuery("select * from USER_INFO");
+        crset.close();
         crset.populate(rs);
-        crset.first();
-        assertEquals("hermit", crset.getString(2));
 
-        crset.populate(cc, 2);
-        crset.first();
-        assertEquals("test", crset.getString(2));
+        CachedRowSet crsetCopy = crset.createCopy();
+        assertEquals(0, crsetCopy.getPageSize());
+        noInitialCrset.setPageSize(5);
+        // if it doesn't specify the startRow for method populate(), then the
+        // pageSize wouldn't work.
+        assertTrue(crsetCopy.isBeforeFirst());
+        noInitialCrset.populate(crsetCopy);
+        assertTrue(crsetCopy.isAfterLast());
+        int cursorIndex = 0;
+        while (noInitialCrset.next()) {
+            cursorIndex++;
+            for (int i = 1; i <= DEFAULT_COLUMN_COUNT; i++) {
+                assertEquals(cursorIndex, noInitialCrset.getInt(1));
+            }
+        }
+        assertEquals(20, cursorIndex);
 
-        crset.populate(cc, 1);
-        crset.first();
-        assertEquals("hermit", crset.getString(2));
+        try {
+            noInitialCrset.populate(crsetCopy, 0);
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
+            // invalid cursor position
+        }
+
+        try {
+            noInitialCrset.populate(crsetCopy, -1);
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
+            // invalid cursor position
+        }
+
+        try {
+            noInitialCrset.populate(crsetCopy, 100);
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
+            // invalid cursor position
+        }
+
+        // specify the startRow, then the noInitialCrset will get only 5 rows
+        noInitialCrset.populate(crsetCopy, 1);
+        assertEquals(5, noInitialCrset.getPageSize());
+        assertTrue(noInitialCrset.isBeforeFirst());
+        cursorIndex = 0;
+        rs = st.executeQuery("select * from USER_INFO");
+        while (noInitialCrset.next() && rs.next()) {
+            cursorIndex++;
+            for (int i = 1; i <= DEFAULT_COLUMN_COUNT; i++) {
+                assertEquals(cursorIndex, noInitialCrset.getInt(1));
+                assertEquals(rs.getObject(i), noInitialCrset.getObject(i));
+            }
+        }
+        // the pageSize works here
+        assertEquals(5, cursorIndex);
+
+        // the noInitialCrset would fetch data from the eleventh row
+        noInitialCrset.populate(crsetCopy, 11);
+        cursorIndex = 10;
+        while (noInitialCrset.next()) {
+            cursorIndex++;
+            assertEquals(cursorIndex, noInitialCrset.getInt(1));
+        }
+        assertEquals(15, cursorIndex);
     }
 
     private void insertData() throws Exception {
@@ -1087,6 +1327,35 @@
         }
     }
 
+    private void insertMoreData(int rows) throws Exception {
+        String insertSQL = "INSERT INTO USER_INFO(ID, NAME, BIGINT_T, NUMERIC_T, DECIMAL_T,
SMALLINT_T, "
+                + "FLOAT_T, REAL_T, DOUBLE_T, DATE_T, TIME_T, TIMESTAMP_T) VALUES(?, ?, ?,
?, ?, ?,"
+                + "?, ?, ?, ?, ?, ? )";
+        PreparedStatement preStmt = conn.prepareStatement(insertSQL);
+
+        // insert 15 rows
+        for (int i = DEFAULT_ROW_COUNT + 1; i <= DEFAULT_ROW_COUNT + rows
+                + 1; i++) {
+            preStmt.setInt(1, i);
+            preStmt.setString(2, "test" + i);
+            preStmt.setLong(3, 444423L);
+            preStmt.setBigDecimal(4, new BigDecimal(12));
+            preStmt.setBigDecimal(5, new BigDecimal(23));
+            preStmt.setInt(6, 41);
+            preStmt.setFloat(7, 4.8F);
+            preStmt.setFloat(8, 4.888F);
+            preStmt.setDouble(9, 4.9999);
+            preStmt.setDate(10, new Date(965324512));
+            preStmt.setTime(11, new Time(452368512));
+            preStmt.setTimestamp(12, new Timestamp(874532105));
+            preStmt.executeUpdate();
+        }
+
+        if (preStmt != null) {
+            preStmt.close();
+        }
+    }
+
     public void testConstructor() throws Exception {
 
         assertTrue(noInitialCrset.isReadOnly());
@@ -1193,7 +1462,7 @@
         if ("true".equals(System.getProperty("Testing Harmony"))) {
             assertFalse(crset.absolute(0));
         }
-        
+
         assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, crset.getType());
         assertTrue(crset.absolute(1));
         assertEquals(1, crset.getInt(1));
@@ -1318,7 +1587,6 @@
         } catch (SQLException e) {
             // expected
         }
-
 
         try {
             crset.first();



Mime
View raw message