harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r638239 - 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, 18 Mar 2008 06:47:58 GMT
Author: tonywu
Date: Mon Mar 17 23:47:55 2008
New Revision: 638239

URL: http://svn.apache.org/viewvc?rev=638239&view=rev
Log:
Apply patch for HARMONY-5614 ([classlib][sql] implements get/update stream related methods
in CachedRowSetImpl)

Modified:
    harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRow.java
    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
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetTestCase.java
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRow.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRow.java?rev=638239&r1=638238&r2=638239&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRow.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRow.java
Mon Mar 17 23:47:55 2008
@@ -17,6 +17,7 @@
 package org.apache.harmony.sql.internal.rowset;
 
 import java.sql.SQLException;
+import java.sql.SQLWarning;
 import java.util.BitSet;
 
 public class CachedRow implements Cloneable {
@@ -33,6 +34,16 @@
     private boolean isUpdate;
 
     private boolean nonUpdateable = false;
+
+    private SQLWarning sqlWarning = null;
+
+    public SQLWarning getSqlWarning() {
+        return sqlWarning;
+    }
+
+    public void setSqlWarning(SQLWarning sqlWarning) {
+        this.sqlWarning = sqlWarning;
+    }
 
     public CachedRow(Object[] columnData) {
         this.columnData = columnData.clone();

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=638239&r1=638238&r2=638239&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
Mon Mar 17 23:47:55 2008
@@ -16,8 +16,15 @@
  */
 package org.apache.harmony.sql.internal.rowset;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.Reader;
+import java.io.StringBufferInputStream;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Field;
 import java.math.BigDecimal;
 import java.sql.Array;
@@ -120,7 +127,9 @@
 
     private CachedRowSetImpl originalResultSet;
 
-    private SQLWarning sqlwarn;
+    private SQLWarning sqlwarn = new SQLWarning();
+
+    private RowSetWarning rowSetWarning = new RowSetWarning();
 
     private Class[] columnTypes;
 
@@ -535,7 +544,7 @@
     }
 
     public RowSetWarning getRowSetWarnings() throws SQLException {
-        throw new NotImplementedException();
+        return rowSetWarning;
     }
 
     public SyncProvider getSyncProvider() throws SQLException {
@@ -1263,7 +1272,7 @@
     }
 
     public void clearWarnings() throws SQLException {
-        throw new NotImplementedException();
+        sqlwarn = null;
     }
 
     public void close() throws SQLException {
@@ -1400,11 +1409,38 @@
     }
 
     public InputStream getAsciiStream(int columnIndex) throws SQLException {
-        throw new NotImplementedException();
+        Object obj = getObject(columnIndex);
+        if (obj == null) {
+            isLastColNull = true;
+            return null;
+        }
+
+        isLastColNull = false;
+        if (obj instanceof String) {
+            obj = ((String) obj).toCharArray();
+        }
+
+        if (obj instanceof char[]) {
+            char[] cs = (char[]) obj;
+            byte[] bs = new byte[cs.length];
+
+            for (int i = 0; i < cs.length; i++) {
+                // if out of range, convert to unknown char ox3F
+                if (cs[i] > Byte.MAX_VALUE || cs[i] < Byte.MIN_VALUE) {
+                    bs[i] = 63;
+                } else {
+                    bs[i] = (byte) cs[i];
+                }
+            }
+
+            return new ByteArrayInputStream(bs);
+        }
+        // rowset.10=Data Type Mismatch
+        throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
     }
 
     public InputStream getAsciiStream(String columnName) throws SQLException {
-        throw new NotImplementedException();
+        return getAsciiStream(getIndexByName(columnName));
     }
 
     public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
@@ -1437,11 +1473,23 @@
     }
 
     public InputStream getBinaryStream(int columnIndex) throws SQLException {
-        throw new NotImplementedException();
+        Object obj = getObject(columnIndex);
+        if (obj == null) {
+            isLastColNull = true;
+            return null;
+        }
+
+        isLastColNull = false;
+        if (obj instanceof byte[]) {
+            return new ByteArrayInputStream((byte[]) obj);
+        }
+
+        // rowset.10=Data Type Mismatch
+        throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
     }
 
     public InputStream getBinaryStream(String columnName) throws SQLException {
-        throw new NotImplementedException();
+        return getBinaryStream(getIndexByName(columnName));
     }
 
     public Blob getBlob(int columnIndex) throws SQLException {
@@ -1521,11 +1569,29 @@
     }
 
     public Reader getCharacterStream(int columnIndex) throws SQLException {
-        throw new NotImplementedException();
+        Object obj = getObject(columnIndex);
+        if (obj == null) {
+            isLastColNull = true;
+            return null;
+        }
+        isLastColNull = false;
+        if (obj instanceof String) {
+            return new StringReader((String) obj);
+        }
+
+        if (obj instanceof byte[]) {
+            return new StringReader(new String((byte[]) obj));
+        }
+
+        if (obj instanceof char[]) {
+            return new StringReader(new String((char[]) obj));
+        }
+        // rowset.10=Data Type Mismatch
+        throw new SQLException(Messages.getString("rowset.10"));
     }
 
     public Reader getCharacterStream(String columnName) throws SQLException {
-        throw new NotImplementedException();
+        return getCharacterStream(getIndexByName(columnName));
     }
 
     public Clob getClob(int columnIndex) throws SQLException {
@@ -1885,15 +1951,42 @@
     }
 
     public InputStream getUnicodeStream(int columnIndex) throws SQLException {
-        throw new NotImplementedException();
+        Object obj = getObject(columnIndex);
+        if (obj == null) {
+            isLastColNull = true;
+            return null;
+        }
+
+        isLastColNull = false;
+
+        if (obj instanceof byte[]) {
+            return new StringBufferInputStream(new String((byte[]) obj));
+        }
+
+        if (obj instanceof String) {
+            return new StringBufferInputStream((String) obj);
+        }
+
+        if (obj instanceof char[]) {
+            return new StringBufferInputStream(new String((char[]) obj));
+        }
+
+        // rowset.10=Data Type Mismatch
+        throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
     }
 
     public InputStream getUnicodeStream(String columnName) throws SQLException {
-        throw new NotImplementedException();
+        return getUnicodeStream(getIndexByName(columnName));
     }
 
     public SQLWarning getWarnings() throws SQLException {
-        throw new NotImplementedException();
+        if (sqlwarn == null) {
+            return null;
+        }
+        if (currentRow != null && currentRow.getSqlWarning() != null) {
+            return currentRow.getSqlWarning();
+        }
+        return sqlwarn;
     }
 
     public void insertRow() throws SQLException {
@@ -2126,9 +2219,37 @@
         updateArray(getIndexByName(columnName), x);
     }
 
-    public void updateAsciiStream(int columnIndex, InputStream x, int length)
+    public void updateAsciiStream(int columnIndex, InputStream in, int length)
             throws SQLException {
-        throw new NotImplementedException();
+        checkValidRow();
+        checkColumnValid(columnIndex);
+        if (isCursorOnInsert && insertRow == null) {
+            insertRow = new CachedRow(new Object[columnCount]);
+            currentRow = insertRow;
+        }
+
+        Class type = columnTypes[columnIndex - 1];
+        if (type != null && !type.equals(String.class)
+                && !type.equals(byte[].class)) {
+            // rowset.10=Data Type Mismatch
+            throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
+        }
+
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        try {
+            for (int i = 0; i < length; ++i) {
+                int value = in.read();
+                if (value == -1) {
+                    throw new IndexOutOfBoundsException();
+                }
+                out.write(value);
+            }
+        } catch (IOException e) {
+            SQLException ex = new SQLException();
+            ex.initCause(e);
+            throw ex;
+        }
+        updateString(columnIndex, new String(out.toByteArray()));
     }
 
     public void updateAsciiStream(String columnName, InputStream x, int length)
@@ -2150,9 +2271,40 @@
         updateBigDecimal(getIndexByName(columnName), x);
     }
 
-    public void updateBinaryStream(int columnIndex, InputStream x, int length)
+    public void updateBinaryStream(int columnIndex, InputStream in, int length)
             throws SQLException {
-        throw new NotImplementedException();
+        checkValidRow();
+        checkColumnValid(columnIndex);
+        if (isCursorOnInsert && insertRow == null) {
+            insertRow = new CachedRow(new Object[columnCount]);
+            currentRow = insertRow;
+        }
+
+        Class type = columnTypes[columnIndex - 1];
+        if (type != null && !type.equals(byte[].class)) {
+            // rowset.10=Data Type Mismatch
+            throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
+        }
+
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        try {
+            int i = 0;
+            for (; i < length; ++i) {
+                int value = in.read();
+                if (value == -1) {
+                    out.write(0);
+                } else {
+                    out.write(value);
+                }
+            }
+
+        } catch (IOException e) {
+            SQLException ex = new SQLException();
+            ex.initCause(e);
+            throw ex;
+        }
+
+        updateBytes(columnIndex, out.toByteArray());
     }
 
     public void updateBinaryStream(String columnName, InputStream x, int length)
@@ -2192,9 +2344,38 @@
         updateBytes(getIndexByName(columnName), x);
     }
 
-    public void updateCharacterStream(int columnIndex, Reader x, int length)
+    public void updateCharacterStream(int columnIndex, Reader in, int length)
             throws SQLException {
-        throw new NotImplementedException();
+        checkValidRow();
+        checkColumnValid(columnIndex);
+        if (isCursorOnInsert && insertRow == null) {
+            insertRow = new CachedRow(new Object[columnCount]);
+            currentRow = insertRow;
+        }
+
+        Class type = columnTypes[columnIndex - 1];
+        if (type != null && !type.equals(String.class)
+                && !type.equals(byte[].class)) {
+            // rowset.10=Data Type Mismatch
+            throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
+        }
+
+        StringWriter out = new StringWriter();
+        try {
+            for (int i = 0; i < length; ++i) {
+                int value = in.read();
+                if (value == -1) {
+                    throw new IndexOutOfBoundsException();
+                }
+                out.write(value);
+            }
+        } catch (IOException e) {
+            SQLException ex = new SQLException();
+            ex.initCause(e);
+            throw ex;
+        }
+
+        updateString(columnIndex, out.toString());
     }
 
     public void updateCharacterStream(String columnName, Reader reader,
@@ -2389,6 +2570,16 @@
 
         if (type == null || type.isInstance(value)) {
             return value;
+        }
+
+        if (type.equals(byte[].class)) {
+            return value;
+        }
+
+        if (type.equals(String.class)) {
+            if (!(value instanceof Array) && !(value instanceof byte[])) {
+                return value.toString();
+            }
         }
 
         if (type.equals(Integer.class)) {

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=638239&r1=638238&r2=638239&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
Mon Mar 17 23:47:55 2008
@@ -52,7 +52,9 @@
                 columnData[i] = rs.getObject(i + 1);
             }
 
-            data.add(new CachedRow(columnData));
+            CachedRow currentRow = new CachedRow(columnData);
+            currentRow.setSqlWarning(rs.getWarnings());
+            data.add(currentRow);
 
             if (maxRows > 0 && maxRows == data.size()) {
                 break;

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=638239&r1=638238&r2=638239&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
Mon Mar 17 23:47:55 2008
@@ -846,14 +846,14 @@
         crset.setInt(1, 3);
         crset.execute();
 
-        assertEquals(12, crset.getMetaData().getColumnCount());
+        assertEquals(DEFAULT_COLUMN_COUNT, crset.getMetaData().getColumnCount());
         assertTrue(crset.next());
         assertEquals("test3", crset.getString(2));
         assertFalse(crset.next());
 
         CachedRowSet crsetCopy = crset.createCopy();
         crsetCopy.execute();
-        assertEquals(12, crsetCopy.getMetaData().getColumnCount());
+        assertEquals(DEFAULT_COLUMN_COUNT, crsetCopy.getMetaData().getColumnCount());
         assertTrue(crsetCopy.next());
         assertEquals("test3", crsetCopy.getString(2));
         assertFalse(crsetCopy.next());
@@ -2415,7 +2415,7 @@
         }
 
         try {
-            noInitialCrset.toCollection(13);
+            noInitialCrset.toCollection(DEFAULT_COLUMN_COUNT + 1);
             fail("should throw SQLException");
         } catch (SQLException e) {
             // expected

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetTestCase.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetTestCase.java?rev=638239&r1=638238&r2=638239&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetTestCase.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetTestCase.java
Mon Mar 17 23:47:55 2008
@@ -131,7 +131,7 @@
 
         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);
         // third row
         preStmt.setInt(1, 3);
@@ -142,7 +142,7 @@
         preStmt.setInt(6, 13);
         preStmt.setFloat(7, 3.7F);
         preStmt.setFloat(8, 3.888F);
-        preStmt.setDouble(9, 3.9999);
+        preStmt.setDouble(9, 3.99999999);
         preStmt.setDate(10, new Date(52365412356663L));
         preStmt.setTime(11, new Time(96655422555551L));
         preStmt.setTimestamp(12, new Timestamp(52365412356663L));
@@ -170,7 +170,7 @@
     protected 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

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java?rev=638239&r1=638238&r2=638239&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java
Mon Mar 17 23:47:55 2008
@@ -814,4 +814,43 @@
         crset.moveToCurrentRow();
         crset.acceptChanges(conn);
     }
+
+    public void testUpdateString() throws Exception {
+        crset.moveToInsertRow();
+
+        crset.updateInt(1, 50);
+        crset.updateString(2, "test100");
+        crset.updateInt(2, 2);
+        assertEquals("2", crset.getObject(2));
+
+        try {
+            crset.updateBytes(2, new byte[] { 1, 2 });
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
+            // expected, Data Type Mismatch
+        }
+
+        crset.updateDate(2, new Date(965324512));
+        assertEquals(new Date(965324512).toString(), crset.getObject(2));
+
+        crset.updateTime(2, new Time(452368512));
+        assertEquals(new Time(452368512).toString(), crset.getObject(2));
+
+        crset.updateTimestamp(2, new Timestamp(874532105));
+        assertEquals(new Timestamp(874532105).toString(), crset.getObject(2));
+
+        crset.updateBigDecimal(2, new BigDecimal(12));
+        assertEquals(new BigDecimal(12).toString(), crset.getObject(2));
+        
+        crset.updateLong(3, 444423L);
+        crset.updateBigDecimal(4, new BigDecimal(12));
+        crset.updateBigDecimal(5, new BigDecimal(23));
+        crset.updateLong(6, 33);
+        crset.updateFloat(7, 4.8F);
+        crset.updateFloat(8, 4.888F);
+        crset.updateDouble(9, 4.9999);
+        crset.updateDate(10, new Date(965324512));
+        crset.updateTime(11, new Time(452368512));
+        crset.updateTimestamp(12, new Timestamp(874532105));
+    }
 }



Mime
View raw message