|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.dbutils.BasicRowProcessor
Basic implementation of the RowProcessor
interface.
This class is a thread-safe Singleton.
RowProcessor
Nested Class Summary | |
private static class |
BasicRowProcessor.CaseInsensitiveHashMap
A Map that converts all keys to lowercase Strings for case insensitive lookups. |
Field Summary | |
private static BasicRowProcessor |
instance
The Singleton instance of this class. |
private static java.util.Map |
primitiveDefaults
Set a bean's primitive properties to these defaults when SQL NULL is returned. |
private static int |
PROPERTY_NOT_FOUND
Special array index that indicates there is no bean property that matches a column from a ResultSet. |
Constructor Summary | |
protected |
BasicRowProcessor()
Protected constructor for BasicRowProcessor subclasses only. |
Method Summary | |
private void |
callSetter(java.lang.Object target,
java.beans.PropertyDescriptor prop,
java.lang.Object value)
Calls the setter method on the target object for the given property. |
private java.lang.Object |
createBean(java.sql.ResultSet rs,
java.lang.Class type,
java.beans.PropertyDescriptor[] props,
int[] columnToProperty,
int cols)
Creates a new object and initializes its fields from the ResultSet. |
static BasicRowProcessor |
instance()
Returns the Singleton instance of this class. |
private boolean |
isCompatibleType(java.lang.Object value,
java.lang.Class type)
ResultSet.getObject() returns an Integer object for an INT column. |
private int[] |
mapColumnsToProperties(java.sql.ResultSetMetaData rsmd,
java.beans.PropertyDescriptor[] props)
The positions in the returned array represent column numbers. |
private java.lang.Object |
newInstance(java.lang.Class c)
Returns a new instance of the given Class. |
private java.beans.PropertyDescriptor[] |
propertyDescriptors(java.lang.Class c)
Returns a PropertyDescriptor[] for the given Class. |
java.lang.Object[] |
toArray(java.sql.ResultSet rs)
Convert a ResultSet row into an Object[] . |
java.lang.Object |
toBean(java.sql.ResultSet rs,
java.lang.Class type)
Convert a ResultSet row into a JavaBean. |
java.util.List |
toBeanList(java.sql.ResultSet rs,
java.lang.Class type)
Convert a ResultSet into a List of JavaBeans. |
java.util.Map |
toMap(java.sql.ResultSet rs)
Convert a ResultSet row into a Map . |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private static final java.util.Map primitiveDefaults
private static final int PROPERTY_NOT_FOUND
private static final BasicRowProcessor instance
Constructor Detail |
protected BasicRowProcessor()
Method Detail |
public static BasicRowProcessor instance()
public java.lang.Object[] toArray(java.sql.ResultSet rs) throws java.sql.SQLException
ResultSet
row into an Object[]
.
This implementation copies column values into the array in the same
order they're returned from the ResultSet
. Array elements
will be set to null
if the column was SQL NULL.
toArray
in interface RowProcessor
java.sql.SQLException
RowProcessor.toArray(java.sql.ResultSet)
public java.lang.Object toBean(java.sql.ResultSet rs, java.lang.Class type) throws java.sql.SQLException
ResultSet
row into a JavaBean. This
implementation uses reflection and BeanInfo
classes to
match column names to bean property names. Properties are matched to
columns based on several factors:
Primitive bean properties are set to their defaults when SQL NULL is
returned from the ResultSet
. Numeric fields are set to 0
and booleans are set to false. Object bean properties are set to
null
when SQL NULL is returned. This is the same behavior
as the ResultSet
get* methods.
toBean
in interface RowProcessor
java.sql.SQLException
RowProcessor.toBean(java.sql.ResultSet, java.lang.Class)
public java.util.List toBeanList(java.sql.ResultSet rs, java.lang.Class type) throws java.sql.SQLException
ResultSet
into a List
of JavaBeans.
This implementation uses reflection and BeanInfo
classes to
match column names to bean property names. Properties are matched to
columns based on several factors:
Primitive bean properties are set to their defaults when SQL NULL is
returned from the ResultSet
. Numeric fields are set to 0
and booleans are set to false. Object bean properties are set to
null
when SQL NULL is returned. This is the same behavior
as the ResultSet
get* methods.
toBeanList
in interface RowProcessor
List
of beans with the given type in the order
they were returned by the ResultSet
.
java.sql.SQLException
RowProcessor.toBeanList(java.sql.ResultSet, java.lang.Class)
private java.lang.Object createBean(java.sql.ResultSet rs, java.lang.Class type, java.beans.PropertyDescriptor[] props, int[] columnToProperty, int cols) throws java.sql.SQLException
rs
- The result settype
- The bean type (the return type of the object)props
- The property descriptorscolumnToProperty
- The column indices in the result setcols
- The number of columns
java.sql.SQLException
- If a database error occursprivate int[] mapColumnsToProperties(java.sql.ResultSetMetaData rsmd, java.beans.PropertyDescriptor[] props) throws java.sql.SQLException
rsmd
- The result set meta data containing column informationprops
- The bean property descriptors
java.sql.SQLException
- If a database error occurspublic java.util.Map toMap(java.sql.ResultSet rs) throws java.sql.SQLException
ResultSet
row into a Map
. This
implementation returns a Map
with case insensitive column
names as keys. Calls to map.get("COL")
and
map.get("col")
return the same value.
toMap
in interface RowProcessor
java.sql.SQLException
RowProcessor.toMap(java.sql.ResultSet)
private void callSetter(java.lang.Object target, java.beans.PropertyDescriptor prop, java.lang.Object value) throws java.sql.SQLException
target
- The object to set the property on.prop
- The property to set.value
- The value to pass into the setter.
java.sql.SQLException
- if an error occurs setting the property.private boolean isCompatibleType(java.lang.Object value, java.lang.Class type)
value
- The value to be passed into the setter method.type
- The setter's parameter type.
private java.lang.Object newInstance(java.lang.Class c) throws java.sql.SQLException
c
- The Class to create an object from.
java.sql.SQLException
- if creation failed.private java.beans.PropertyDescriptor[] propertyDescriptors(java.lang.Class c) throws java.sql.SQLException
c
- The Class to retrieve PropertyDescriptors for.
java.sql.SQLException
- if introspection failed.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |