提交 90636f66 编写于 作者: T Thomas Risberg

Added support for looking up column values by column label to support...

Added support for looking up column values by column label to support CachedRowSetImpl which doesn't allow for column label use (SPR-7506); added some generics;
上级 c33df597
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -19,10 +19,12 @@ package org.springframework.jdbc.support.rowset; ...@@ -19,10 +19,12 @@ package org.springframework.jdbc.support.rowset;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.Date; import java.sql.Date;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Time; import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.springframework.jdbc.InvalidResultSetAccessException; import org.springframework.jdbc.InvalidResultSetAccessException;
...@@ -33,6 +35,14 @@ import org.springframework.jdbc.InvalidResultSetAccessException; ...@@ -33,6 +35,14 @@ import org.springframework.jdbc.InvalidResultSetAccessException;
* <p>This implementation wraps a <code>javax.sql.ResultSet</code>, * <p>This implementation wraps a <code>javax.sql.ResultSet</code>,
* catching any SQLExceptions and translating them to the * catching any SQLExceptions and translating them to the
* appropriate Spring {@link InvalidResultSetAccessException}. * appropriate Spring {@link InvalidResultSetAccessException}.
*
* <p>Note: Since JDBC 4.0 it has been clarified that any methods using a String to identify the column should
* be using the column label. The column label is assigned using the ALIAS keyword in the SQL query string. When the
* query doesn't use an ALIAS the default label is the column name. Most JDBC ResultSet implementations follow this
* new pattern but there are some exceptions such as the com.sun.rowset.CachedRowSetImpl which only uses the column
* name ignoring any column labels. Since Spring 3.0.5 this class will translate column labels to the correct column
* index to provide better support for the com.sun.rowset.CachedRowSetImpl which is the default implementation used by
* the JdbcTemplate when working with RowSets.
* *
* <p>The passed-in ResultSets should already be disconnected if the SqlRowSet * <p>The passed-in ResultSets should already be disconnected if the SqlRowSet
* is supposed to be usable in a disconnected fashion. This means that * is supposed to be usable in a disconnected fashion. This means that
...@@ -59,6 +69,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -59,6 +69,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
private final ResultSet resultSet; private final ResultSet resultSet;
private final SqlRowSetMetaData rowSetMetaData; private final SqlRowSetMetaData rowSetMetaData;
private final Map<String, Integer> columnLabelMap;
/** /**
...@@ -79,6 +91,22 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -79,6 +91,22 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
catch (SQLException se) { catch (SQLException se) {
throw new InvalidResultSetAccessException(se); throw new InvalidResultSetAccessException(se);
} }
try {
ResultSetMetaData rsmd = resultSet.getMetaData();
if (rsmd != null) {
int columnCount = rsmd.getColumnCount();
this.columnLabelMap = new HashMap<String, Integer>(columnCount);
for (int i = 1; i <= columnCount; i++) {
columnLabelMap.put(rsmd.getColumnLabel(i), Integer.valueOf(i));
}
}
else {
this.columnLabelMap = new HashMap<String, Integer>(0);
}
} catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
...@@ -101,13 +129,17 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -101,13 +129,17 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** /**
* @see java.sql.ResultSet#findColumn(String) * @see java.sql.ResultSet#findColumn(String)
*/ */
public int findColumn(String columnName) throws InvalidResultSetAccessException { public int findColumn(String columnLabel) throws InvalidResultSetAccessException {
Integer columnIndex = columnLabelMap.get(columnLabel);
try { try {
return this.resultSet.findColumn(columnName); if (columnIndex == null) {
columnIndex = this.resultSet.findColumn(columnLabel);
}
} }
catch (SQLException se) { catch (SQLException se) {
throw new InvalidResultSetAccessException(se); throw new InvalidResultSetAccessException(se);
} }
return columnIndex.intValue();
} }
...@@ -128,13 +160,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -128,13 +160,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** /**
* @see java.sql.ResultSet#getBigDecimal(String) * @see java.sql.ResultSet#getBigDecimal(String)
*/ */
public BigDecimal getBigDecimal(String columnName) throws InvalidResultSetAccessException { public BigDecimal getBigDecimal(String columnLabel) throws InvalidResultSetAccessException {
try { return getBigDecimal(findColumn(columnLabel));
return this.resultSet.getBigDecimal(columnName);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
...@@ -152,13 +179,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -152,13 +179,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** /**
* @see java.sql.ResultSet#getBoolean(String) * @see java.sql.ResultSet#getBoolean(String)
*/ */
public boolean getBoolean(String columnName) throws InvalidResultSetAccessException { public boolean getBoolean(String columnLabel) throws InvalidResultSetAccessException {
try { return getBoolean(findColumn(columnLabel));
return this.resultSet.getBoolean(columnName);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
...@@ -176,13 +198,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -176,13 +198,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** /**
* @see java.sql.ResultSet#getByte(String) * @see java.sql.ResultSet#getByte(String)
*/ */
public byte getByte(String columnName) throws InvalidResultSetAccessException { public byte getByte(String columnLabel) throws InvalidResultSetAccessException {
try { return getByte(findColumn(columnLabel));
return this.resultSet.getByte(columnName);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
...@@ -211,25 +228,15 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -211,25 +228,15 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** /**
* @see java.sql.ResultSet#getDate(String, java.util.Calendar) * @see java.sql.ResultSet#getDate(String, java.util.Calendar)
*/ */
public Date getDate(String columnName, Calendar cal) throws InvalidResultSetAccessException { public Date getDate(String columnLabel, Calendar cal) throws InvalidResultSetAccessException {
try { return getDate(findColumn(columnLabel), cal);
return this.resultSet.getDate(columnName, cal);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
* @see java.sql.ResultSet#getDate(String) * @see java.sql.ResultSet#getDate(String)
*/ */
public Date getDate(String columnName) throws InvalidResultSetAccessException { public Date getDate(String columnLabel) throws InvalidResultSetAccessException {
try { return getDate(findColumn(columnLabel));
return this.resultSet.getDate(columnName);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
...@@ -247,13 +254,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -247,13 +254,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** /**
* @see java.sql.ResultSet#getDouble(String) * @see java.sql.ResultSet#getDouble(String)
*/ */
public double getDouble(String columnName) throws InvalidResultSetAccessException { public double getDouble(String columnLabel) throws InvalidResultSetAccessException {
try { return getDouble(findColumn(columnLabel));
return this.resultSet.getDouble(columnName);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
...@@ -271,13 +273,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -271,13 +273,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** /**
* @see java.sql.ResultSet#getFloat(String) * @see java.sql.ResultSet#getFloat(String)
*/ */
public float getFloat(String columnName) throws InvalidResultSetAccessException { public float getFloat(String columnLabel) throws InvalidResultSetAccessException {
try { return getFloat(findColumn(columnLabel));
return this.resultSet.getFloat(columnName);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
* @see java.sql.ResultSet#getInt(int) * @see java.sql.ResultSet#getInt(int)
...@@ -294,13 +291,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -294,13 +291,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** /**
* @see java.sql.ResultSet#getInt(String) * @see java.sql.ResultSet#getInt(String)
*/ */
public int getInt(String columnName) throws InvalidResultSetAccessException { public int getInt(String columnLabel) throws InvalidResultSetAccessException {
try { return getInt(findColumn(columnLabel));
return this.resultSet.getInt(columnName);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
...@@ -318,19 +310,14 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -318,19 +310,14 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** /**
* @see java.sql.ResultSet#getLong(String) * @see java.sql.ResultSet#getLong(String)
*/ */
public long getLong(String columnName) throws InvalidResultSetAccessException { public long getLong(String columnLabel) throws InvalidResultSetAccessException {
try { return getLong(findColumn(columnLabel));
return this.resultSet.getLong(columnName);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
* @see java.sql.ResultSet#getObject(int, java.util.Map) * @see java.sql.ResultSet#getObject(int, java.util.Map)
*/ */
public Object getObject(int i, Map map) throws InvalidResultSetAccessException { public Object getObject(int i, Map<String, Class<?>> map) throws InvalidResultSetAccessException {
try { try {
return this.resultSet.getObject(i, map); return this.resultSet.getObject(i, map);
} }
...@@ -354,25 +341,15 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -354,25 +341,15 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** /**
* @see java.sql.ResultSet#getObject(String, java.util.Map) * @see java.sql.ResultSet#getObject(String, java.util.Map)
*/ */
public Object getObject(String columnName, Map map) throws InvalidResultSetAccessException { public Object getObject(String columnLabel, Map<String, Class<?>> map) throws InvalidResultSetAccessException {
try { return getObject(findColumn(columnLabel), map);
return this.resultSet.getObject(columnName, map);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
* @see java.sql.ResultSet#getObject(String) * @see java.sql.ResultSet#getObject(String)
*/ */
public Object getObject(String columnName) throws InvalidResultSetAccessException { public Object getObject(String columnLabel) throws InvalidResultSetAccessException {
try { return getObject(findColumn(columnLabel));
return this.resultSet.getObject(columnName);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
...@@ -390,13 +367,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -390,13 +367,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** /**
* @see java.sql.ResultSet#getShort(String) * @see java.sql.ResultSet#getShort(String)
*/ */
public short getShort(String columnName) throws InvalidResultSetAccessException { public short getShort(String columnLabel) throws InvalidResultSetAccessException {
try { return getShort(findColumn(columnLabel));
return this.resultSet.getShort(columnName);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
...@@ -414,13 +386,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -414,13 +386,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** /**
* @see java.sql.ResultSet#getString(String) * @see java.sql.ResultSet#getString(String)
*/ */
public String getString(String columnName) throws InvalidResultSetAccessException { public String getString(String columnLabel) throws InvalidResultSetAccessException {
try { return getString(findColumn(columnLabel));
return this.resultSet.getString(columnName);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
...@@ -450,25 +417,15 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -450,25 +417,15 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** /**
* @see java.sql.ResultSet#getTime(String, java.util.Calendar) * @see java.sql.ResultSet#getTime(String, java.util.Calendar)
*/ */
public Time getTime(String columnName, Calendar cal) throws InvalidResultSetAccessException { public Time getTime(String columnLabel, Calendar cal) throws InvalidResultSetAccessException {
try { return getTime(findColumn(columnLabel), cal);
return this.resultSet.getTime(columnName, cal);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
* @see java.sql.ResultSet#getTime(String) * @see java.sql.ResultSet#getTime(String)
*/ */
public Time getTime(String columnName) throws InvalidResultSetAccessException { public Time getTime(String columnLabel) throws InvalidResultSetAccessException {
try { return getTime(findColumn(columnLabel));
return this.resultSet.getTime(columnName);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
...@@ -498,25 +455,15 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet { ...@@ -498,25 +455,15 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** /**
* @see java.sql.ResultSet#getTimestamp(String, java.util.Calendar) * @see java.sql.ResultSet#getTimestamp(String, java.util.Calendar)
*/ */
public Timestamp getTimestamp(String columnName, Calendar cal) throws InvalidResultSetAccessException { public Timestamp getTimestamp(String columnLabel, Calendar cal) throws InvalidResultSetAccessException {
try { return getTimestamp(findColumn(columnLabel), cal);
return this.resultSet.getTimestamp(columnName, cal);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
/** /**
* @see java.sql.ResultSet#getTimestamp(String) * @see java.sql.ResultSet#getTimestamp(String)
*/ */
public Timestamp getTimestamp(String columnName) throws InvalidResultSetAccessException { public Timestamp getTimestamp(String columnLabel) throws InvalidResultSetAccessException {
try { return getTimestamp(findColumn(columnLabel));
return this.resultSet.getTimestamp(columnName);
}
catch (SQLException se) {
throw new InvalidResultSetAccessException(se);
}
} }
......
/* /*
* Copyright 2002-2005 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -59,12 +59,12 @@ public interface SqlRowSet extends Serializable { ...@@ -59,12 +59,12 @@ public interface SqlRowSet extends Serializable {
SqlRowSetMetaData getMetaData(); SqlRowSetMetaData getMetaData();
/** /**
* Maps the given column name to its column index. * Maps the given column label to its column index.
* @param columnName the name of the column * @param columnLabel the name of the column
* @return the column index for the given column name * @return the column index for the given column label
* @see java.sql.ResultSet#findColumn(String) * @see java.sql.ResultSet#findColumn(String)
*/ */
int findColumn(String columnName) throws InvalidResultSetAccessException; int findColumn(String columnLabel) throws InvalidResultSetAccessException;
// RowSet methods for extracting data values // RowSet methods for extracting data values
...@@ -81,11 +81,11 @@ public interface SqlRowSet extends Serializable { ...@@ -81,11 +81,11 @@ public interface SqlRowSet extends Serializable {
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* an BigDecimal object. * an BigDecimal object.
* @param columnName the column name * @param columnLabel the column label
* @return an BigDecimal object representing the column value * @return an BigDecimal object representing the column value
* @see java.sql.ResultSet#getBigDecimal(java.lang.String) * @see java.sql.ResultSet#getBigDecimal(java.lang.String)
*/ */
BigDecimal getBigDecimal(String columnName) throws InvalidResultSetAccessException; BigDecimal getBigDecimal(String columnLabel) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
...@@ -99,11 +99,11 @@ public interface SqlRowSet extends Serializable { ...@@ -99,11 +99,11 @@ public interface SqlRowSet extends Serializable {
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* a boolean. * a boolean.
* @param columnName the column name * @param columnLabel the column label
* @return a boolean representing the column value * @return a boolean representing the column value
* @see java.sql.ResultSet#getBoolean(java.lang.String) * @see java.sql.ResultSet#getBoolean(java.lang.String)
*/ */
boolean getBoolean(String columnName) throws InvalidResultSetAccessException; boolean getBoolean(String columnLabel) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
...@@ -117,11 +117,11 @@ public interface SqlRowSet extends Serializable { ...@@ -117,11 +117,11 @@ public interface SqlRowSet extends Serializable {
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* a byte. * a byte.
* @param columnName the column name * @param columnLabel the column label
* @return a byte representing the column value * @return a byte representing the column value
* @see java.sql.ResultSet#getByte(java.lang.String) * @see java.sql.ResultSet#getByte(java.lang.String)
*/ */
byte getByte(String columnName) throws InvalidResultSetAccessException; byte getByte(String columnLabel) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
...@@ -145,21 +145,21 @@ public interface SqlRowSet extends Serializable { ...@@ -145,21 +145,21 @@ public interface SqlRowSet extends Serializable {
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* a Date object. * a Date object.
* @param columnName the column name * @param columnLabel the column label
* @param cal the Calendar to use in constructing the Date * @param cal the Calendar to use in constructing the Date
* @return a Date object representing the column value * @return a Date object representing the column value
* @see java.sql.ResultSet#getDate(java.lang.String, java.util.Calendar) * @see java.sql.ResultSet#getDate(java.lang.String, java.util.Calendar)
*/ */
Date getDate(String columnName, Calendar cal) throws InvalidResultSetAccessException; Date getDate(String columnLabel, Calendar cal) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* a Date object. * a Date object.
* @param columnName the column name * @param columnLabel the column label
* @return a Date object representing the column value * @return a Date object representing the column value
* @see java.sql.ResultSet#getDate(java.lang.String) * @see java.sql.ResultSet#getDate(java.lang.String)
*/ */
Date getDate(String columnName) throws InvalidResultSetAccessException; Date getDate(String columnLabel) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
...@@ -173,11 +173,11 @@ public interface SqlRowSet extends Serializable { ...@@ -173,11 +173,11 @@ public interface SqlRowSet extends Serializable {
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* a Double object. * a Double object.
* @param columnName the column name * @param columnLabel the column label
* @return a Double object representing the column value * @return a Double object representing the column value
* @see java.sql.ResultSet#getDouble(java.lang.String) * @see java.sql.ResultSet#getDouble(java.lang.String)
*/ */
double getDouble(String columnName) throws InvalidResultSetAccessException; double getDouble(String columnLabel) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
...@@ -191,11 +191,11 @@ public interface SqlRowSet extends Serializable { ...@@ -191,11 +191,11 @@ public interface SqlRowSet extends Serializable {
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* a float. * a float.
* @param columnName the column name * @param columnLabel the column label
* @return a float representing the column value * @return a float representing the column value
* @see java.sql.ResultSet#getFloat(java.lang.String) * @see java.sql.ResultSet#getFloat(java.lang.String)
*/ */
float getFloat(String columnName) throws InvalidResultSetAccessException; float getFloat(String columnLabel) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
...@@ -209,11 +209,11 @@ public interface SqlRowSet extends Serializable { ...@@ -209,11 +209,11 @@ public interface SqlRowSet extends Serializable {
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* an int. * an int.
* @param columnName the column name * @param columnLabel the column label
* @return an int representing the column value * @return an int representing the column value
* @see java.sql.ResultSet#getInt(java.lang.String) * @see java.sql.ResultSet#getInt(java.lang.String)
*/ */
int getInt(String columnName) throws InvalidResultSetAccessException; int getInt(String columnLabel) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
...@@ -227,11 +227,11 @@ public interface SqlRowSet extends Serializable { ...@@ -227,11 +227,11 @@ public interface SqlRowSet extends Serializable {
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* a long. * a long.
* @param columnName the column name * @param columnLabel the column label
* @return a long representing the column value * @return a long representing the column value
* @see java.sql.ResultSet#getLong(java.lang.String) * @see java.sql.ResultSet#getLong(java.lang.String)
*/ */
long getLong(String columnName) throws InvalidResultSetAccessException; long getLong(String columnLabel) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
...@@ -241,7 +241,7 @@ public interface SqlRowSet extends Serializable { ...@@ -241,7 +241,7 @@ public interface SqlRowSet extends Serializable {
* @return a Object representing the column value * @return a Object representing the column value
* @see java.sql.ResultSet#getObject(int, java.util.Map) * @see java.sql.ResultSet#getObject(int, java.util.Map)
*/ */
Object getObject(int columnIndex, Map map) throws InvalidResultSetAccessException; Object getObject(int columnIndex, Map<String, Class<?>> map) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
...@@ -255,21 +255,21 @@ public interface SqlRowSet extends Serializable { ...@@ -255,21 +255,21 @@ public interface SqlRowSet extends Serializable {
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* an Object. * an Object.
* @param columnName the column name * @param columnLabel the column label
* @param map a Map object containing the mapping from SQL types to Java types * @param map a Map object containing the mapping from SQL types to Java types
* @return a Object representing the column value * @return a Object representing the column value
* @see java.sql.ResultSet#getObject(java.lang.String, java.util.Map) * @see java.sql.ResultSet#getObject(java.lang.String, java.util.Map)
*/ */
Object getObject(String columnName, Map map) throws InvalidResultSetAccessException; Object getObject(String columnLabel, Map<String, Class<?>> map) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* an Object. * an Object.
* @param columnName the column name * @param columnLabel the column label
* @return a Object representing the column value * @return a Object representing the column value
* @see java.sql.ResultSet#getObject(java.lang.String) * @see java.sql.ResultSet#getObject(java.lang.String)
*/ */
Object getObject(String columnName) throws InvalidResultSetAccessException; Object getObject(String columnLabel) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
...@@ -283,11 +283,11 @@ public interface SqlRowSet extends Serializable { ...@@ -283,11 +283,11 @@ public interface SqlRowSet extends Serializable {
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* a short. * a short.
* @param columnName the column name * @param columnLabel the column label
* @return a short representing the column value * @return a short representing the column value
* @see java.sql.ResultSet#getShort(java.lang.String) * @see java.sql.ResultSet#getShort(java.lang.String)
*/ */
short getShort(String columnName) throws InvalidResultSetAccessException; short getShort(String columnLabel) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
...@@ -301,11 +301,11 @@ public interface SqlRowSet extends Serializable { ...@@ -301,11 +301,11 @@ public interface SqlRowSet extends Serializable {
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* a String. * a String.
* @param columnName the column name * @param columnLabel the column label
* @return a String representing the column value * @return a String representing the column value
* @see java.sql.ResultSet#getString(java.lang.String) * @see java.sql.ResultSet#getString(java.lang.String)
*/ */
String getString(String columnName) throws InvalidResultSetAccessException; String getString(String columnLabel) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
...@@ -329,21 +329,21 @@ public interface SqlRowSet extends Serializable { ...@@ -329,21 +329,21 @@ public interface SqlRowSet extends Serializable {
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* a Time object. * a Time object.
* @param columnName the column name * @param columnLabel the column label
* @param cal the Calendar to use in constructing the Date * @param cal the Calendar to use in constructing the Date
* @return a Time object representing the column value * @return a Time object representing the column value
* @see java.sql.ResultSet#getTime(java.lang.String, java.util.Calendar) * @see java.sql.ResultSet#getTime(java.lang.String, java.util.Calendar)
*/ */
Time getTime(String columnName, Calendar cal) throws InvalidResultSetAccessException; Time getTime(String columnLabel, Calendar cal) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* a Time object. * a Time object.
* @param columnName the column name * @param columnLabel the column label
* @return a Time object representing the column value * @return a Time object representing the column value
* @see java.sql.ResultSet#getTime(java.lang.String) * @see java.sql.ResultSet#getTime(java.lang.String)
*/ */
Time getTime(String columnName) throws InvalidResultSetAccessException; Time getTime(String columnLabel) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
...@@ -367,21 +367,21 @@ public interface SqlRowSet extends Serializable { ...@@ -367,21 +367,21 @@ public interface SqlRowSet extends Serializable {
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* a Timestamp object. * a Timestamp object.
* @param columnName the column name * @param columnLabel the column label
* @param cal the Calendar to use in constructing the Date * @param cal the Calendar to use in constructing the Date
* @return a Timestamp object representing the column value * @return a Timestamp object representing the column value
* @see java.sql.ResultSet#getTimestamp(java.lang.String, java.util.Calendar) * @see java.sql.ResultSet#getTimestamp(java.lang.String, java.util.Calendar)
*/ */
Timestamp getTimestamp(String columnName, Calendar cal) throws InvalidResultSetAccessException; Timestamp getTimestamp(String columnLabel, Calendar cal) throws InvalidResultSetAccessException;
/** /**
* Retrieves the value of the indicated column in the current row as * Retrieves the value of the indicated column in the current row as
* a Timestamp object. * a Timestamp object.
* @param columnName the column name * @param columnLabel the column label
* @return a Timestamp object representing the column value * @return a Timestamp object representing the column value
* @see java.sql.ResultSet#getTimestamp(java.lang.String) * @see java.sql.ResultSet#getTimestamp(java.lang.String)
*/ */
Timestamp getTimestamp(String columnName) throws InvalidResultSetAccessException; Timestamp getTimestamp(String columnLabel) throws InvalidResultSetAccessException;
// RowSet navigation methods // RowSet navigation methods
......
/* /*
* Copyright 2002-2006 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -44,6 +44,8 @@ public class ResultSetWrappingRowSetTests extends TestCase { ...@@ -44,6 +44,8 @@ public class ResultSetWrappingRowSetTests extends TestCase {
rset = (ResultSet) rsetControl.getMock(); rset = (ResultSet) rsetControl.getMock();
rset.getMetaData(); rset.getMetaData();
rsetControl.setReturnValue(null); rsetControl.setReturnValue(null);
rset.getMetaData();
rsetControl.setReturnValue(null);
} }
public void testGetBigDecimalInt() throws Exception { public void testGetBigDecimalInt() throws Exception {
...@@ -53,7 +55,7 @@ public class ResultSetWrappingRowSetTests extends TestCase { ...@@ -53,7 +55,7 @@ public class ResultSetWrappingRowSetTests extends TestCase {
} }
public void testGetBigDecimalString() throws Exception { public void testGetBigDecimalString() throws Exception {
Method rset = ResultSet.class.getDeclaredMethod("getBigDecimal", new Class[] {String.class}); Method rset = ResultSet.class.getDeclaredMethod("getBigDecimal", new Class[] {int.class});
Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getBigDecimal", new Class[] {String.class}); Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getBigDecimal", new Class[] {String.class});
doTest(rset, rowset, "test", BigDecimal.valueOf(1)); doTest(rset, rowset, "test", BigDecimal.valueOf(1));
} }
...@@ -65,7 +67,7 @@ public class ResultSetWrappingRowSetTests extends TestCase { ...@@ -65,7 +67,7 @@ public class ResultSetWrappingRowSetTests extends TestCase {
} }
public void testGetStringString() throws Exception { public void testGetStringString() throws Exception {
Method rset = ResultSet.class.getDeclaredMethod("getString", new Class[] {String.class}); Method rset = ResultSet.class.getDeclaredMethod("getString", new Class[] {int.class});
Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getString", new Class[] {String.class}); Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getString", new Class[] {String.class});
doTest(rset, rowset, "test", "test"); doTest(rset, rowset, "test", "test");
} }
...@@ -77,7 +79,7 @@ public class ResultSetWrappingRowSetTests extends TestCase { ...@@ -77,7 +79,7 @@ public class ResultSetWrappingRowSetTests extends TestCase {
} }
public void testGetTimestampString() throws Exception { public void testGetTimestampString() throws Exception {
Method rset = ResultSet.class.getDeclaredMethod("getTimestamp", new Class[] {String.class}); Method rset = ResultSet.class.getDeclaredMethod("getTimestamp", new Class[] {int.class});
Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getTimestamp", new Class[] {String.class}); Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getTimestamp", new Class[] {String.class});
doTest(rset, rowset, "test", new Timestamp(1234l)); doTest(rset, rowset, "test", new Timestamp(1234l));
} }
...@@ -89,7 +91,7 @@ public class ResultSetWrappingRowSetTests extends TestCase { ...@@ -89,7 +91,7 @@ public class ResultSetWrappingRowSetTests extends TestCase {
} }
public void testGetDateString() throws Exception { public void testGetDateString() throws Exception {
Method rset = ResultSet.class.getDeclaredMethod("getDate", new Class[] {String.class}); Method rset = ResultSet.class.getDeclaredMethod("getDate", new Class[] {int.class});
Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getDate", new Class[] {String.class}); Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getDate", new Class[] {String.class});
doTest(rset, rowset, "test", new Date(1234l)); doTest(rset, rowset, "test", new Date(1234l));
} }
...@@ -101,7 +103,7 @@ public class ResultSetWrappingRowSetTests extends TestCase { ...@@ -101,7 +103,7 @@ public class ResultSetWrappingRowSetTests extends TestCase {
} }
public void testGetTimeString() throws Exception { public void testGetTimeString() throws Exception {
Method rset = ResultSet.class.getDeclaredMethod("getTime", new Class[] {String.class}); Method rset = ResultSet.class.getDeclaredMethod("getTime", new Class[] {int.class});
Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getTime", new Class[] {String.class}); Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getTime", new Class[] {String.class});
doTest(rset, rowset, "test", new Time(1234l)); doTest(rset, rowset, "test", new Time(1234l));
} }
...@@ -113,7 +115,7 @@ public class ResultSetWrappingRowSetTests extends TestCase { ...@@ -113,7 +115,7 @@ public class ResultSetWrappingRowSetTests extends TestCase {
} }
public void testGetObjectString() throws Exception { public void testGetObjectString() throws Exception {
Method rset = ResultSet.class.getDeclaredMethod("getObject", new Class[] {String.class}); Method rset = ResultSet.class.getDeclaredMethod("getObject", new Class[] {int.class});
Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getObject", new Class[] {String.class}); Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getObject", new Class[] {String.class});
doTest(rset, rowset, "test", new Object()); doTest(rset, rowset, "test", new Object());
} }
...@@ -125,7 +127,7 @@ public class ResultSetWrappingRowSetTests extends TestCase { ...@@ -125,7 +127,7 @@ public class ResultSetWrappingRowSetTests extends TestCase {
} }
public void testGetIntString() throws Exception { public void testGetIntString() throws Exception {
Method rset = ResultSet.class.getDeclaredMethod("getInt", new Class[] {String.class}); Method rset = ResultSet.class.getDeclaredMethod("getInt", new Class[] {int.class});
Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getInt", new Class[] {String.class}); Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getInt", new Class[] {String.class});
doTest(rset, rowset, "test", new Integer(1)); doTest(rset, rowset, "test", new Integer(1));
} }
...@@ -137,7 +139,7 @@ public class ResultSetWrappingRowSetTests extends TestCase { ...@@ -137,7 +139,7 @@ public class ResultSetWrappingRowSetTests extends TestCase {
} }
public void testGetFloatString() throws Exception { public void testGetFloatString() throws Exception {
Method rset = ResultSet.class.getDeclaredMethod("getFloat", new Class[] {String.class}); Method rset = ResultSet.class.getDeclaredMethod("getFloat", new Class[] {int.class});
Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getFloat", new Class[] {String.class}); Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getFloat", new Class[] {String.class});
doTest(rset, rowset, "test", new Float(1)); doTest(rset, rowset, "test", new Float(1));
} }
...@@ -149,7 +151,7 @@ public class ResultSetWrappingRowSetTests extends TestCase { ...@@ -149,7 +151,7 @@ public class ResultSetWrappingRowSetTests extends TestCase {
} }
public void testGetDoubleString() throws Exception { public void testGetDoubleString() throws Exception {
Method rset = ResultSet.class.getDeclaredMethod("getDouble", new Class[] {String.class}); Method rset = ResultSet.class.getDeclaredMethod("getDouble", new Class[] {int.class});
Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getDouble", new Class[] {String.class}); Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getDouble", new Class[] {String.class});
doTest(rset, rowset, "test", new Double(1)); doTest(rset, rowset, "test", new Double(1));
} }
...@@ -161,7 +163,7 @@ public class ResultSetWrappingRowSetTests extends TestCase { ...@@ -161,7 +163,7 @@ public class ResultSetWrappingRowSetTests extends TestCase {
} }
public void testGetLongString() throws Exception { public void testGetLongString() throws Exception {
Method rset = ResultSet.class.getDeclaredMethod("getLong", new Class[] {String.class}); Method rset = ResultSet.class.getDeclaredMethod("getLong", new Class[] {int.class});
Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getLong", new Class[] {String.class}); Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getLong", new Class[] {String.class});
doTest(rset, rowset, "test", new Long(1)); doTest(rset, rowset, "test", new Long(1));
} }
...@@ -173,13 +175,21 @@ public class ResultSetWrappingRowSetTests extends TestCase { ...@@ -173,13 +175,21 @@ public class ResultSetWrappingRowSetTests extends TestCase {
} }
public void testGetBooleanString() throws Exception { public void testGetBooleanString() throws Exception {
Method rset = ResultSet.class.getDeclaredMethod("getBoolean", new Class[] {String.class}); Method rset = ResultSet.class.getDeclaredMethod("getBoolean", new Class[] {int.class});
Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getBoolean", new Class[] {String.class}); Method rowset = ResultSetWrappingSqlRowSet.class.getDeclaredMethod("getBoolean", new Class[] {String.class});
doTest(rset, rowset, "test", new Boolean(true)); doTest(rset, rowset, "test", new Boolean(true));
} }
private void doTest(Method rsetMethod, Method rowsetMethod, Object arg, Object ret) throws Exception { private void doTest(Method rsetMethod, Method rowsetMethod, Object arg, Object ret) throws Exception {
rsetMethod.invoke(rset, new Object[] {arg}); if (arg instanceof String) {
Method findMethod = ResultSet.class.getDeclaredMethod("findColumn", new Class[] {String.class});
findMethod.invoke(rset, new Object[] {arg});
rsetControl.setReturnValue(1);
rsetMethod.invoke(rset, new Object[] {1});
}
else {
rsetMethod.invoke(rset, new Object[] {arg});
}
if (ret instanceof Double) { if (ret instanceof Double) {
rsetControl.setReturnValue(((Double) ret).doubleValue()); rsetControl.setReturnValue(((Double) ret).doubleValue());
} }
...@@ -204,7 +214,16 @@ public class ResultSetWrappingRowSetTests extends TestCase { ...@@ -204,7 +214,16 @@ public class ResultSetWrappingRowSetTests extends TestCase {
else { else {
rsetControl.setReturnValue(ret); rsetControl.setReturnValue(ret);
} }
rsetMethod.invoke(rset, new Object[] {arg});
if (arg instanceof String) {
Method findMethod = ResultSet.class.getDeclaredMethod("findColumn", new Class[] {String.class});
findMethod.invoke(rset, new Object[] {arg});
rsetControl.setReturnValue(1);
rsetMethod.invoke(rset, new Object[] {1});
}
else {
rsetMethod.invoke(rset, new Object[] {arg});
}
rsetControl.setThrowable(new SQLException("test")); rsetControl.setThrowable(new SQLException("test"));
rsetControl.replay(); rsetControl.replay();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册