You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by to...@apache.org on 2008/03/03 09:16:08 UTC
svn commit: r632978 - in /harmony/enhanced/classlib/trunk/modules/sql/src:
main/java/org/apache/harmony/sql/internal/nls/
main/java/org/apache/harmony/sql/internal/rowset/
test/java/org/apache/harmony/sql/tests/internal/rowset/
Author: tonywu
Date: Mon Mar 3 00:16:06 2008
New Revision: 632978
URL: http://svn.apache.org/viewvc?rev=632978&view=rev
Log:
Apply patch for HARMONY-5561 (implements all update methods in CachedRowSet)
Added:
harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java (with props)
Modified:
harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties
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/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/nls/messages.properties
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties?rev=632978&r1=632977&r2=632978&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties (original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties Mon Mar 3 00:16:06 2008
@@ -68,5 +68,6 @@
rowset.5=There are conflicts between rowset and data source
rowset.6=Errors in the process of Writing Back RowData
rowset.7=Not a valid cursor
-rowset.8=The Result Set Type is TYPE_FORWARD_ONLY
-rowset.9=PageSize can not larger than MaxRows
+rowset.8=The ResultSet type is TYPE_FORWARD_ONLY
+rowset.9=PageSize can not be larger than MaxRows
+rowset.10=Data type mismatch
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=632978&r1=632977&r2=632978&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 3 00:16:06 2008
@@ -122,6 +122,14 @@
return new CachedRow(originalColumnData);
}
+ public void setOriginal() {
+ isUpdate = false;
+ isDelete = false;
+ isInsert = false;
+ mask.flip(0, columnData.length);
+ originalColumnData = columnData.clone();
+ }
+
public Object getObject(int columnIndex) {
return columnData[columnIndex - 1];
}
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=632978&r1=632977&r2=632978&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 3 00:16:06 2008
@@ -34,8 +34,10 @@
import java.sql.SQLWarning;
import java.sql.Savepoint;
import java.sql.Statement;
+import java.sql.Struct;
import java.sql.Time;
import java.sql.Timestamp;
+import java.sql.Types;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
@@ -96,15 +98,50 @@
// TODO where is it initialized
private CachedRowSetImpl originalResultSet;
- private Object currentColumn;
-
private SQLWarning sqlwarn;
+ private Class[] columnTypes;
+
+ private static Map<Integer, Class> TYPE_MAPPING = initialTypeMapping();
+
+ public static final String PROVIDER_ID = "Apache Harmony HYOptimisticProvider"; //$NON-NLS-1$
+
public CachedRowSetImpl(String providerID) throws SyncFactoryException {
syncProvider = SyncFactory.getInstance(providerID);
initialProperties();
}
+ private static Map<Integer, Class> initialTypeMapping() {
+ HashMap<Integer, Class> map = new HashMap<Integer, Class>();
+ map.put(Integer.valueOf(Types.ARRAY), Array.class);
+ map.put(Integer.valueOf(Types.BIGINT), Long.class);
+ map.put(Integer.valueOf(Types.BINARY), byte[].class);
+ map.put(Integer.valueOf(Types.BIT), Boolean.class);
+ map.put(Integer.valueOf(Types.BLOB), Blob.class);
+ map.put(Integer.valueOf(Types.BOOLEAN), Boolean.class);
+ map.put(Integer.valueOf(Types.CHAR), String.class);
+ map.put(Integer.valueOf(Types.CLOB), Clob.class);
+ map.put(Integer.valueOf(Types.DATE), Date.class);
+ map.put(Integer.valueOf(Types.DECIMAL), BigDecimal.class);
+ map.put(Integer.valueOf(Types.DOUBLE), Double.class);
+ map.put(Integer.valueOf(Types.FLOAT), Double.class);
+ map.put(Integer.valueOf(Types.INTEGER), Integer.class);
+ map.put(Integer.valueOf(Types.LONGVARBINARY), byte[].class);
+ map.put(Integer.valueOf(Types.LONGVARCHAR), String.class);
+ map.put(Integer.valueOf(Types.NUMERIC), BigDecimal.class);
+ map.put(Integer.valueOf(Types.REAL), Float.class);
+ map.put(Integer.valueOf(Types.REF), Ref.class);
+ map.put(Integer.valueOf(Types.SMALLINT), Short.class);
+ map.put(Integer.valueOf(Types.STRUCT), Struct.class);
+ map.put(Integer.valueOf(Types.TIME), Time.class);
+ map.put(Integer.valueOf(Types.TIMESTAMP), Timestamp.class);
+ map.put(Integer.valueOf(Types.TINYINT), Byte.class);
+ map.put(Integer.valueOf(Types.VARBINARY), byte[].class);
+ map.put(Integer.valueOf(Types.VARCHAR), String.class);
+
+ return map;
+ }
+
private void initialProperties() {
try {
setEscapeProcessing(true);
@@ -126,7 +163,7 @@
}
public CachedRowSetImpl() throws SyncFactoryException {
- this("Apache Harmony HYOptimisticProvider");
+ this(PROVIDER_ID);
}
public void setRows(ArrayList<CachedRow> data, int cloumnCount) {
@@ -176,7 +213,7 @@
public boolean columnUpdated(int idx) throws SQLException {
if (currentRow == null || idx > meta.getColumnCount()) {
// rowset.0 = Not a valid position
- throw new SQLException(Messages.getString("rowset.0"));
+ throw new SQLException(Messages.getString("rowset.0")); //$NON-NLS-1$
}
return currentRow.getUpdateMask(idx - 1);
}
@@ -192,7 +229,7 @@
}
}
// rowset.1=Not a valid column name
- throw new SQLException(Messages.getString("rowset.1"));
+ throw new SQLException(Messages.getString("rowset.1")); //$NON-NLS-1$
}
public void commit() throws SQLException {
@@ -206,7 +243,7 @@
* the attribute of BaseRowSet which are needed to deep copy
*/
// BaseRowSet.params <Hashtable>
- Object[] paramsObjArray = super.getParams().clone();
+ Object[] paramsObjArray = super.getParams();
Hashtable<Object, Object> paramsHashtable = new Hashtable<Object, Object>();
for (int i = 0; i < paramsObjArray.length; i++) {
paramsHashtable.put(Integer.valueOf(i), paramsObjArray[i]);
@@ -298,7 +335,6 @@
CachedRowSetImpl output = (CachedRowSetImpl) createCopy();
// clean up rows data
- output.currentColumn = null;
output.currentRow = null;
output.currentRowIndex = 0;
output.insertRow = null;
@@ -408,6 +444,14 @@
private void doPopulate(ResultSet rs, boolean isPaging) throws SQLException {
meta = copyMetaData(rs.getMetaData());
+ columnCount = meta.getColumnCount();
+ // initial columnTypes
+ columnTypes = new Class[columnCount];
+ for (int i = 1; i <= columnTypes.length; ++i) {
+ columnTypes[i - 1] = TYPE_MAPPING.get(Integer.valueOf(meta
+ .getColumnType(i)));
+ }
+
/*
* this method not support paging, so before readData set pageSize and
* maxRowsto 0 and restore previous values after readData
@@ -526,11 +570,11 @@
public void setPageSize(int size) throws SQLException {
if (size < 0) {
// rowset.2=Negative page size
- throw new SQLException(Messages.getString("rowset.2"));
+ throw new SQLException(Messages.getString("rowset.2")); //$NON-NLS-1$
}
if ((getMaxRows() != 0) && (getMaxRows() < size)) {
// rowset.9=PageSize can not larger than MaxRows
- throw new SQLException(Messages.getString("rowset.9"));
+ throw new SQLException(Messages.getString("rowset.9")); //$NON-NLS-1$
}
pageSize = size;
}
@@ -542,7 +586,7 @@
public void setTableName(String tabName) throws SQLException {
if (tabName == null) {
// rowset.3=Table name should not be null
- throw new SQLException("rowset.3");
+ throw new SQLException("rowset.3"); //$NON-NLS-1$
}
tableName = tabName;
}
@@ -645,7 +689,7 @@
if (checkType && getType() == ResultSet.TYPE_FORWARD_ONLY) {
// rowset.8=The Result Set Type is TYPE_FORWARD_ONLY
- throw new SQLException(Messages.getString("rowset.8"));
+ throw new SQLException(Messages.getString("rowset.8")); //$NON-NLS-1$
}
if (row < 0) {
@@ -738,7 +782,8 @@
}
public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
- throw new NotImplementedException();
+ // TODO re-implement it
+ return (BigDecimal) getObject(columnIndex);
}
public BigDecimal getBigDecimal(int columnIndex, int scale)
@@ -893,6 +938,7 @@
}
public Object getObject(int columnIndex) throws SQLException {
+ // TODO re-implement it
return currentRow.getObject(columnIndex);
}
@@ -949,12 +995,19 @@
return (String) value;
}
- private boolean checkCursorValid() throws SQLException {
+ private void checkColumnValid(int columnIndex) throws SQLException {
+ if (columnIndex <= 0 || columnIndex > meta.getColumnCount()) {
+ // sql.27=Invalid column index :{0}
+ throw new SQLException(Messages.getString("sql.27", Integer //$NON-NLS-1$
+ .valueOf(columnIndex)));
+ }
+ }
+
+ private void checkCursorValid() throws SQLException {
if ((currentRowIndex <= 0) || (currentRowIndex > rows.size())) {
// rowset.7=Not a valid cursor
throw new SQLException(Messages.getString("rowset.7")); //$NON-NLS-1$
}
- return false;
}
public String getString(String columnName) throws SQLException {
@@ -1019,7 +1072,7 @@
checkValidRow();
if (currentRow != insertRow) {
// rowset.4=Not an insert row
- throw new SQLException(Messages.getString("rowset.4"));
+ throw new SQLException(Messages.getString("rowset.4")); //$NON-NLS-1$
}
insertRow.setInsert();
rows.add(insertRow);
@@ -1131,21 +1184,21 @@
public boolean rowUpdated() throws SQLException {
if (!currentRow.isUpdate()) {
return false;
- } else {
- boolean sign = false;
- for (int i = 0; i < meta.getColumnCount(); ++i) {
- sign = currentRow.getUpdateMask(i) | sign;
- }
- return sign;
}
+
+ boolean sign = false;
+ for (int i = 0; i < meta.getColumnCount(); ++i) {
+ sign = currentRow.getUpdateMask(i) | sign;
+ }
+ return sign;
}
public void updateArray(int columnIndex, Array x) throws SQLException {
- throw new NotImplementedException();
+ updateByType(columnIndex, x);
}
public void updateArray(String columnName, Array x) throws SQLException {
- throw new NotImplementedException();
+ updateArray(getIndexByName(columnName), x);
}
public void updateAsciiStream(int columnIndex, InputStream x, int length)
@@ -1155,12 +1208,16 @@
public void updateAsciiStream(String columnName, InputStream x, int length)
throws SQLException {
- throw new NotImplementedException();
+ updateAsciiStream(getIndexByName(columnName), x, length);
}
public void updateBigDecimal(int columnIndex, BigDecimal x)
throws SQLException {
- currentRow.updateObject(columnIndex, x);
+ if (x == null) {
+ throw new NullPointerException();
+ }
+
+ updateByType(columnIndex, x);
}
public void updateBigDecimal(String columnName, BigDecimal x)
@@ -1175,39 +1232,39 @@
public void updateBinaryStream(String columnName, InputStream x, int length)
throws SQLException {
- throw new NotImplementedException();
+ updateBinaryStream(getIndexByName(columnName), x, length);
}
public void updateBlob(int columnIndex, Blob x) throws SQLException {
- throw new NotImplementedException();
+ updateByType(columnIndex, x);
}
public void updateBlob(String columnName, Blob x) throws SQLException {
- throw new NotImplementedException();
+ updateBlob(getIndexByName(columnName), x);
}
public void updateBoolean(int columnIndex, boolean x) throws SQLException {
- throw new NotImplementedException();
+ updateByType(columnIndex, Boolean.valueOf(x));
}
public void updateBoolean(String columnName, boolean x) throws SQLException {
- throw new NotImplementedException();
+ updateBoolean(getIndexByName(columnName), x);
}
public void updateByte(int columnIndex, byte x) throws SQLException {
- throw new NotImplementedException();
+ updateByType(columnIndex, Byte.valueOf(x));
}
public void updateByte(String columnName, byte x) throws SQLException {
- throw new NotImplementedException();
+ updateByte(getIndexByName(columnName), x);
}
public void updateBytes(int columnIndex, byte[] x) throws SQLException {
- throw new NotImplementedException();
+ updateByType(columnIndex, x);
}
public void updateBytes(String columnName, byte[] x) throws SQLException {
- throw new NotImplementedException();
+ updateBytes(getIndexByName(columnName), x);
}
public void updateCharacterStream(int columnIndex, Reader x, int length)
@@ -1217,19 +1274,19 @@
public void updateCharacterStream(String columnName, Reader reader,
int length) throws SQLException {
- throw new NotImplementedException();
+ updateCharacterStream(getIndexByName(columnName), reader, length);
}
public void updateClob(int columnIndex, Clob x) throws SQLException {
- throw new NotImplementedException();
+ updateByType(columnIndex, x);
}
public void updateClob(String columnName, Clob x) throws SQLException {
- throw new NotImplementedException();
+ updateClob(getIndexByName(columnName), x);
}
public void updateDate(int columnIndex, Date x) throws SQLException {
- currentRow.updateObject(columnIndex, x);
+ updateByType(columnIndex, x);
}
public void updateDate(String columnName, Date x) throws SQLException {
@@ -1237,7 +1294,7 @@
}
public void updateDouble(int columnIndex, double x) throws SQLException {
- currentRow.updateObject(columnIndex, x);
+ updateByType(columnIndex, Double.valueOf(x));
}
public void updateDouble(String columnName, double x) throws SQLException {
@@ -1245,7 +1302,7 @@
}
public void updateFloat(int columnIndex, float x) throws SQLException {
- currentRow.updateObject(columnIndex, x);
+ updateByType(columnIndex, Float.valueOf(x));
}
public void updateFloat(String columnName, float x) throws SQLException {
@@ -1253,7 +1310,7 @@
}
public void updateInt(int columnIndex, int x) throws SQLException {
- currentRow.updateObject(columnIndex, x);
+ updateByType(columnIndex, Integer.valueOf(x));
}
public void updateInt(String columnName, int x) throws SQLException {
@@ -1261,7 +1318,7 @@
}
public void updateLong(int columnIndex, long x) throws SQLException {
- currentRow.updateObject(columnIndex, x);
+ updateByType(columnIndex, Long.valueOf(x));
}
public void updateLong(String columnName, long x) throws SQLException {
@@ -1269,37 +1326,65 @@
}
public void updateNull(int columnIndex) throws SQLException {
- throw new NotImplementedException();
+ checkCursorValid();
+ checkColumnValid(columnIndex);
+ currentRow.updateObject(columnIndex, null);
}
public void updateNull(String columnName) throws SQLException {
- throw new NotImplementedException();
+ updateNull(getIndexByName(columnName));
}
+ /**
+ * note check type compatibility
+ */
public void updateObject(int columnIndex, Object x) throws SQLException {
- throw new NotImplementedException();
+ checkValidRow();
+ checkColumnValid(columnIndex);
+ currentRow.updateObject(columnIndex, x);
}
public void updateObject(int columnIndex, Object x, int scale)
throws SQLException {
- throw new NotImplementedException();
+ checkValidRow();
+ checkColumnValid(columnIndex);
+ Class type = columnTypes[columnIndex - 1];
+ // ava.sql.Types.DECIMA or java.sql.Types.NUMERIC types
+ if (type.equals(BigDecimal.class)) {
+ /*
+ * TODO ri doesn't check type of x and only support BigDecimal,
+ * should we follow ri here? If not, uncomment below fragment of
+ * code
+ */
+ // if (x instanceof BigDecimal) {
+ x = ((BigDecimal) x).setScale(scale);
+ // } else if (x instanceof Double) {
+ // x = new BigDecimal(((Double) x).doubleValue());
+ // x = ((BigDecimal) x).setScale(scale);
+ // } else if (x instanceof Float) {
+ // x = new BigDecimal(((Float) x).doubleValue());
+ // x = ((BigDecimal) x).setScale(scale);
+ // }
+ }
+
+ currentRow.updateObject(columnIndex, x);
}
public void updateObject(String columnName, Object x) throws SQLException {
- throw new NotImplementedException();
+ updateObject(getIndexByName(columnName), x);
}
public void updateObject(String columnName, Object x, int scale)
throws SQLException {
- throw new NotImplementedException();
+ updateObject(getIndexByName(columnName), x, scale);
}
public void updateRef(int columnIndex, Ref x) throws SQLException {
- throw new NotImplementedException();
+ updateByType(columnIndex, x);
}
public void updateRef(String columnName, Ref x) throws SQLException {
- throw new NotImplementedException();
+ updateRef(getIndexByName(columnName), x);
}
public void updateRow() throws SQLException {
@@ -1313,15 +1398,199 @@
}
public void updateShort(int columnIndex, short x) throws SQLException {
- throw new NotImplementedException();
+ updateByType(columnIndex, Short.valueOf(x));
}
public void updateShort(String columnName, short x) throws SQLException {
- throw new NotImplementedException();
+ updateShort(getIndexByName(columnName), x);
}
public void updateString(int columnIndex, String x) throws SQLException {
- currentRow.updateObject(columnIndex, x);
+ updateByType(columnIndex, x);
+ }
+
+ /**
+ * Check type compatibility and update value
+ *
+ * @param columnIndex
+ * @param value
+ * @throws SQLException
+ */
+ private void updateByType(int columnIndex, Object value)
+ throws SQLException {
+ checkValidRow();
+ checkColumnValid(columnIndex);
+ currentRow.updateObject(columnIndex, convertUpdateValue(columnIndex,
+ value));
+ }
+
+ /**
+ * Convert <code>value</code> to the JDBC type of the
+ * <code>columnIndex</code>. The columnIndex is not checked in this
+ * method, so caller must be sure the <code>columnIndex</code> is valid,
+ * or invoke <code>checkColumnValid</code> before invoke this method.
+ *
+ * TODO any better ways to do this?
+ *
+ * @param columnIndex
+ * index of column to be updated
+ * @param value
+ * the new value to be updated
+ */
+ private Object convertUpdateValue(int columnIndex, Object value)
+ throws SQLException {
+
+ if (value == null) {
+ return value;
+ }
+
+ Class type = columnTypes[columnIndex - 1];
+
+ /*
+ * TODO if type == null, the type mapping is not supported by Harmony
+ * now, leave this type check to JDBC driver
+ */
+
+ if (type == null || type.isInstance(value)) {
+ return value;
+ }
+
+ if (type.equals(Integer.class)) {
+ if (value instanceof Integer || value instanceof Short
+ || value instanceof Byte) {
+ return value;
+ }
+
+ if (value instanceof Long) {
+ long l = ((Long) value).longValue();
+ if (l >= Integer.MIN_VALUE && l <= Integer.MAX_VALUE) {
+ return (int) l;
+ }
+ }
+
+ if (value instanceof BigDecimal) {
+ BigDecimal bigDecimal = (BigDecimal) value;
+ try {
+ return bigDecimal.intValueExact();
+
+ } catch (ArithmeticException e) {
+ // TODO load from resource file
+ throw new SQLException("Data Type Mismatch"); //$NON-NLS-1$
+ }
+ }
+
+ if (value instanceof String) {
+ return value;
+ }
+ }
+
+ if (type.equals(Short.class)) {
+ if (value instanceof Short || value instanceof Byte) {
+ return value;
+ }
+ if (value instanceof Long) {
+ long l = ((Long) value).longValue();
+ if (l >= Short.MIN_VALUE && l <= Short.MAX_VALUE) {
+ return (short) l;
+ }
+ }
+ if (value instanceof Integer) {
+ int i = ((Integer) value).intValue();
+ if (i >= Short.MIN_VALUE && i <= Short.MAX_VALUE) {
+ return (short) i;
+ }
+ }
+ if (value instanceof BigDecimal) {
+ BigDecimal bigDecimal = (BigDecimal) value;
+ try {
+ return bigDecimal.intValueExact();
+
+ } catch (ArithmeticException e) {
+ // TODO load from resource file
+ throw new SQLException("Data Type Mismatch"); //$NON-NLS-1$
+ }
+ }
+ if (value instanceof String) {
+ return value;
+ }
+ }
+
+ if (type.equals(Byte.class)) {
+ if (value instanceof Byte) {
+ return value;
+ }
+ if (value instanceof Long) {
+ long l = ((Long) value).longValue();
+ if (l >= Byte.MIN_VALUE && l <= Byte.MAX_VALUE) {
+ return (byte) l;
+ }
+ }
+ if (value instanceof Integer) {
+ int i = ((Integer) value).intValue();
+ if (i >= Byte.MIN_VALUE && i <= Byte.MAX_VALUE) {
+ return (byte) i;
+ }
+ }
+ if (value instanceof Short) {
+ int i = ((Short) value).shortValue();
+ if (i >= Byte.MIN_VALUE && i <= Byte.MAX_VALUE) {
+ return (byte) i;
+ }
+ }
+ if (value instanceof BigDecimal) {
+ BigDecimal bigDecimal = (BigDecimal) value;
+ try {
+ return bigDecimal.byteValueExact();
+
+ } catch (ArithmeticException e) {
+ // TODO load from resource file
+ throw new SQLException("Data Type Mismatch"); //$NON-NLS-1$
+ }
+ }
+ if (value instanceof String) {
+ return value;
+ }
+ }
+
+ if (type.equals(Long.class)) {
+ if (value instanceof Integer || value instanceof Short
+ || value instanceof Byte || value instanceof Long) {
+ return value;
+ }
+ if (value instanceof BigDecimal) {
+ BigDecimal bigDecimal = (BigDecimal) value;
+ try {
+ return bigDecimal.longValueExact();
+
+ } catch (ArithmeticException e) {
+ // rowset.10=Data Type Mismatch
+ throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
+ }
+ }
+ if (value instanceof String) {
+ return value;
+ }
+ }
+
+ if (type.equals(Float.class) || type.equals(Double.class)) {
+ if (value instanceof Float || value instanceof Double
+ || value instanceof BigDecimal) {
+ return value;
+ }
+ if (value instanceof Number) {
+ return ((Number) value).longValue();
+ }
+ if (value instanceof String) {
+ return value;
+ }
+ }
+
+ if (type.equals(BigDecimal.class)) {
+ return value;
+ }
+
+ // rowset.10=Data Type Mismatch
+ throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
}
public void updateString(String columnName, String x) throws SQLException {
@@ -1329,7 +1598,7 @@
}
public void updateTime(int columnIndex, Time x) throws SQLException {
- currentRow.updateObject(columnIndex, x);
+ updateByType(columnIndex, x);
}
public void updateTime(String columnName, Time x) throws SQLException {
@@ -1338,7 +1607,7 @@
public void updateTimestamp(int columnIndex, Timestamp x)
throws SQLException {
- currentRow.updateObject(columnIndex, x);
+ updateByType(columnIndex, x);
}
public void updateTimestamp(String columnName, Timestamp x)
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=632978&r1=632977&r2=632978&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 3 00:16:06 2008
@@ -443,12 +443,13 @@
assertEquals(crset.getFetchDirection(), copy.getFetchDirection());
assertEquals(crset.getPageSize(), copy.getPageSize());
+ assertEquals(crset.isBeforeFirst(), copy.isBeforeFirst());
+ assertEquals(crset.isAfterLast(), copy.isAfterLast());
+ assertEquals(crset.isFirst(), copy.isFirst());
+ assertEquals(crset.isLast(), copy.isLast());
+ assertEquals(crset.getRow(), copy.getRow());
+
// TODO uncomment them after implemented
- // assertEquals(crset.isBeforeFirst(), crsetCopy.isBeforeFirst());
- // assertEquals(crset.isAfterLast(), crsetCopy.isAfterLast());
- // assertEquals(crset.isFirst(), crsetCopy.isFirst());
- // assertEquals(crset.isLast(), crsetCopy.isLast());
- // assertEquals(crset.getRow(), copy.getRow());
// assertNotSame(crset.getWarnings(), copy.getWarnings());
// assertEquals(crset.getStatement(), copy.getStatement());
// try {
@@ -720,8 +721,8 @@
rs.next();
rs.next();
rs.next();
- // TODO: Uncomment it when Writer is implemented fully.
- // assertEquals("copyTest3", rs.getString(2));
+
+ assertEquals("copyTest3", rs.getString(2));
reloadCachedRowSet();
crset.absolute(2);
@@ -729,14 +730,10 @@
crsetCopy = crset.createCopy();
assertEquals(crset.isReadOnly(), crsetCopy.isReadOnly());
- // TODO uncomment when isBeforeFirst is implemented
- // assertEquals(crset.isBeforeFirst(), crsetCopy.isBeforeFirst());
- // TODO uncomment when isAfterLast is implemented
- // assertEquals(crset.isAfterLast(), crsetCopy.isAfterLast());
- // TODO uncomment when isFirst is implemented
- // assertEquals(crset.isFirst(), crsetCopy.isFirst());
- // TODO uncomment when isLast is implemented
- // assertEquals(crset.isLast(), crsetCopy.isLast());
+ assertEquals(crset.isBeforeFirst(), crsetCopy.isBeforeFirst());
+ assertEquals(crset.isAfterLast(), crsetCopy.isAfterLast());
+ assertEquals(crset.isFirst(), crsetCopy.isFirst());
+ assertEquals(crset.isLast(), crsetCopy.isLast());
assertEquals(crset.size(), crsetCopy.size());
// different metaData object
@@ -848,10 +845,9 @@
* it, and all resource would be released after connection closed.
*/
crset.acceptChanges(conn);
- // TODO: wait the implementation of Writer
- // fail("Should throw SyncProviderException");
+ fail("Should throw SyncProviderException");
} catch (SyncProviderException e) {
- // expected
+ // expected, TODO test SyncProviderException
}
assertEquals("updated", copy.getString(2));
@@ -1150,6 +1146,8 @@
assertNull(noInitialCrset.getCommand());
assertEquals(ResultSet.CONCUR_UPDATABLE, noInitialCrset
.getConcurrency());
+ assertEquals(0, crset.getRow());
+
// TODO uncomment after impelemented
// try {
// crset.getCursorName();
@@ -1169,7 +1167,6 @@
// } catch (SQLException e) {
// // expected
// }
- // assertEquals(0, crset.getRow());
// assertNull(crset.getStatement());
assertEquals(true, noInitialCrset.getEscapeProcessing());
Added: 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=632978&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java Mon Mar 3 00:16:06 2008
@@ -0,0 +1,476 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.sql.tests.internal.rowset;
+
+import java.math.BigDecimal;
+import java.sql.Date;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+
+import javax.sql.rowset.spi.SyncProviderException;
+
+public class CachedRowSetUpdateTest extends CachedRowSetTestCase {
+ public void testUpdateValue() throws Exception {
+
+ try {
+ crset.updateString(1, "");
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Invalid cursor position
+ }
+
+ try {
+ crset.updateString(-1, "");
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Invalid column index
+ }
+
+ crset.moveToInsertRow();
+
+ try {
+ crset.updateDate(1, new Date(10000));
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ crset.updateInt(1, 100);
+ crset.updateString(2, "test8");
+
+ try {
+ crset.updateBytes(2, "hello".getBytes());
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ crset.updateLong(3, 444423L);
+ crset.updateBigDecimal(4, new BigDecimal(12));
+ crset.updateBigDecimal(5, new BigDecimal(23));
+ crset.updateInt(6, 41);
+ 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));
+
+ crset.insertRow();
+ crset.moveToCurrentRow();
+ crset.acceptChanges(conn);
+
+ }
+
+ public void testUpdateLong() throws SQLException {
+ crset.moveToInsertRow();
+
+ crset.updateInt(1, 100);
+ crset.updateString(2, "update");
+
+ crset.updateLong(3, 444423L);
+ crset.updateInt(3, 100);
+ crset.updateShort(3, (short) 100);
+ crset.updateByte(3, (byte) 10);
+ crset.updateBigDecimal(3, new BigDecimal(Long.MAX_VALUE));
+ crset.updateString(3, "1000");
+
+ try {
+ crset.updateBigDecimal(3, new BigDecimal(Double.MAX_VALUE));
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ try {
+ crset.updateFloat(3, 80.98F);
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ try {
+ crset.updateDouble(3, 80.98);
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ try {
+ crset.updateBoolean(3, false);
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ crset.updateBigDecimal(4, new BigDecimal(12));
+ crset.updateBigDecimal(5, new BigDecimal(23));
+ crset.updateInt(6, 41);
+ 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));
+ crset.insertRow();
+ crset.moveToCurrentRow();
+ crset.acceptChanges(conn);
+
+ }
+
+ public void testUpdateInt() throws SQLException {
+ crset.moveToInsertRow();
+
+ crset.updateInt(1, 50);
+ crset.updateByte(1, (byte) 10);
+ crset.updateShort(1, (short) 60);
+ crset.updateLong(1, 100L);
+ crset.updateBigDecimal(1, new BigDecimal(50));
+ crset.updateString(1, "100");
+
+ try {
+ crset.updateBigDecimal(1, new BigDecimal(Double.MAX_VALUE));
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ try {
+ crset.updateLong(1, 100000000000L);
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ try {
+ crset.updateFloat(1, 80.98F);
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ try {
+ crset.updateDouble(1, 80.98);
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ try {
+ crset.updateBoolean(1, false);
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ crset.updateString(2, "test100");
+ crset.updateLong(3, 444423L);
+ crset.updateBigDecimal(4, new BigDecimal(12));
+ crset.updateBigDecimal(5, new BigDecimal(23));
+ crset.updateInt(6, 41);
+ 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));
+ crset.insertRow();
+ crset.moveToCurrentRow();
+ crset.acceptChanges(conn);
+
+ ResultSet rs = st
+ .executeQuery("SELECT * FROM USER_INFO WHERE ID = 100");
+ assertTrue(rs.next());
+ }
+
+ public void testUpdateShort() throws SQLException {
+ crset.moveToInsertRow();
+
+ crset.updateInt(1, 50);
+ crset.updateString(2, "test100");
+ crset.updateLong(3, 444423L);
+ crset.updateBigDecimal(4, new BigDecimal(12));
+ crset.updateBigDecimal(5, new BigDecimal(23));
+
+ crset.updateShort(6, (short) 10);
+ crset.updateByte(6, (byte) 10);
+ crset.updateInt(6, 41);
+ crset.updateLong(6, 33);
+ crset.updateBigDecimal(6, new BigDecimal(23));
+
+ try {
+ crset.updateBigDecimal(6, new BigDecimal(Double.MAX_VALUE));
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ try {
+ crset.updateLong(6, 100000000000L);
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ try {
+ crset.updateInt(6, Integer.MAX_VALUE);
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ try {
+ crset.updateFloat(6, 80.98F);
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ try {
+ crset.updateDouble(6, 80.98);
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ try {
+ crset.updateBoolean(6, false);
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ 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));
+ crset.insertRow();
+ crset.moveToCurrentRow();
+ crset.acceptChanges(conn);
+ }
+
+ public void testUpdateFloat() throws SQLException {
+ crset.moveToInsertRow();
+
+ crset.updateInt(1, 50);
+ crset.updateString(2, "test100");
+ crset.updateLong(3, 444423L);
+ crset.updateBigDecimal(4, new BigDecimal(12));
+ crset.updateBigDecimal(5, new BigDecimal(23));
+ crset.updateInt(6, 41);
+
+ crset.updateFloat(7, 4.8F);
+ crset.updateDouble(7, 80.98);
+
+ crset.updateInt(7, 10000);
+ crset.updateLong(7, 10000);
+ crset.updateShort(7, (short) 10);
+ crset.updateByte(7, (byte) 10);
+ crset.updateBigDecimal(7, new BigDecimal(100));
+ crset.updateDouble(7, Float.MAX_VALUE);
+
+ // throw exception when acceptChange
+ // crset.updateDouble(7, Double.MAX_VALUE);
+ // crset.updateBigDecimal(7, new BigDecimal(Double.MAX_VALUE));
+
+ try {
+ crset.updateBoolean(1, false);
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ 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));
+ crset.insertRow();
+ crset.moveToCurrentRow();
+ crset.acceptChanges(conn);
+ }
+
+ public void testUpdateFloat_Exception() throws SQLException {
+ crset.moveToInsertRow();
+ crset.updateInt(1, 50);
+ crset.updateString(2, "test100");
+ crset.updateLong(3, 444423L);
+ crset.updateBigDecimal(4, new BigDecimal(12));
+ crset.updateBigDecimal(5, new BigDecimal(23));
+ crset.updateInt(6, 41);
+
+ // over range
+ crset.updateDouble(7, Double.MAX_VALUE);
+
+ 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));
+ crset.insertRow();
+ crset.moveToCurrentRow();
+
+ try {
+ crset.acceptChanges(conn);
+ fail("Should throw SyncProviderException");
+ } catch (SyncProviderException e) {
+ // expected
+ }
+ }
+
+ public void testUpdateBigDecimal() throws SQLException {
+ crset.moveToInsertRow();
+ crset.updateInt(1, 50);
+ crset.updateString(2, "test100");
+ crset.updateLong(3, 444423L);
+
+ crset.updateBigDecimal(4, new BigDecimal(12));
+ crset.updateByte(4, (byte) 10);
+ crset.updateShort(4, (short) 10);
+ crset.updateInt(4, 300);
+ crset.updateLong(4, 2000);
+ crset.updateFloat(4, 3.59F);
+ crset.updateDouble(4, 3.4994);
+
+ crset.updateBigDecimal(5, new BigDecimal(23));
+ crset.updateInt(6, 41);
+ crset.updateDouble(7, Float.MAX_VALUE);
+ 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));
+
+ crset.insertRow();
+ crset.moveToCurrentRow();
+
+ crset.acceptChanges(conn);
+ }
+
+ public void testUpdateObject() throws SQLException {
+ crset.moveToInsertRow();
+
+ // updateObject doesn't check type compatibility
+ crset.updateObject(1, new Date(1000));
+ crset.updateObject(2, new ArrayList<Object>());
+
+ try {
+ crset.updateDate(1, new Date(1000));
+ fail("Should throw SQLException");
+ } catch (SQLException e) {
+ // expected, Data Type Mismatch
+ }
+
+ crset.updateObject(1, null);
+
+ try {
+ crset.updateBigDecimal(7, null);
+ fail("Should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ crset.updateString(2, null);
+
+ // TODO test Array, Blob, Clob
+ // crset.updateArray(3, null);
+ }
+
+ public void testUpdateObject_I_LObject_I() throws Exception {
+ crset.moveToInsertRow();
+
+ crset.updateInt(1, 50);
+ crset.updateString(2, "test100");
+ crset.updateLong(3, 444423L);
+ crset.updateBigDecimal(4, new BigDecimal(12));
+
+ crset.updateObject(5, new BigDecimal("3.1200"), 3);
+
+ BigDecimal bigDecimal = new BigDecimal("3.1200");
+ BigDecimal scaled = bigDecimal.setScale(3);
+
+ if ("true".equals(System.getProperty("Testing Harmony"))) {
+ assertEquals(scaled, crset.getBigDecimal(5));
+ assertEquals(scaled, crset.getObject(5));
+ } else {
+ /*
+ * seems ri doesn't do scale
+ */
+ assertEquals(bigDecimal, crset.getBigDecimal(5));
+ assertEquals(bigDecimal, crset.getObject(5));
+ }
+
+ try {
+ crset.updateObject(5, new BigDecimal("3.1200"), 1);
+ fail("Should throw ArithmeticException");
+ } catch (ArithmeticException e) {
+ // expected
+ }
+
+ try {
+ crset.updateObject(5, null, 1);
+ fail("Should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ try {
+ crset.updateObject(5, Double.valueOf(3.0000), 1);
+ fail("Should throw ClassCastException");
+ } catch (ClassCastException e) {
+ // ri throw ClassCastException, we follow ri
+ }
+
+ try {
+ crset.updateObject(5, Float.valueOf(3.0000F), 3);
+ fail("Should throw ClassCastException");
+ } catch (ClassCastException e) {
+ // ri throw ClassCastException, we follow ri
+ }
+
+ try {
+ crset.updateObject(5, Integer.valueOf(3), 3);
+ fail("Should throw ClassCastException");
+ } catch (ClassCastException e) {
+ // ri throw ClassCastException, we follow ri
+ }
+
+ try {
+ crset.updateObject(5, new ArrayList<Object>(), 3);
+ fail("Should throw ClassCastException");
+ } catch (ClassCastException e) {
+ // ri throw ClassCastException, we follow ri
+ }
+
+ crset.updateInt(6, 41);
+ 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));
+ crset.insertRow();
+ crset.moveToCurrentRow();
+ crset.acceptChanges(conn);
+ }
+}
Propchange: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java
------------------------------------------------------------------------------
svn:eol-style = native