Class SingleColumnRowMapper<T>
- All Implemented Interfaces:
RowMapper<T>
RowMapper
implementation that converts a single column into a single result value per row. Expects to operate
on a Row
that just contains a single column.
The type of the result value for each row can be specified. The value for the single column will be extracted from a
Row
and converted into the specified target type.
- Since:
- 2.0
- Author:
- Mark Paluch
- See Also:
-
Constructor Summary
ConstructorDescriptionCreate a newSingleColumnRowMapper
for bean-style configuration.SingleColumnRowMapper
(Class<T> requiredType) Create a newSingleColumnRowMapper
. -
Method Summary
Modifier and TypeMethodDescriptionprotected Object
convertValueToRequiredType
(Object value, Class<?> requiredType) Convert the given column value to the specified required type.protected Object
getColumnValue
(com.datastax.oss.driver.api.core.cql.Row row, int index) Retrieve a object value for the specified column, using the most appropriate value type.protected Object
getColumnValue
(com.datastax.oss.driver.api.core.cql.Row row, int index, Class<?> requiredType) Retrieve a CQL object value for the specified column.mapRow
(com.datastax.oss.driver.api.core.cql.Row row, int rowNum) Extract a value for the single column in the current row.static <T> SingleColumnRowMapper<T>
newInstance
(Class<T> requiredType) Static factory method to create a newSingleColumnRowMapper
(with the required type specified only once).void
setRequiredType
(Class<T> requiredType) Set the type that each result object is expected to match.
-
Constructor Details
-
SingleColumnRowMapper
public SingleColumnRowMapper()Create a newSingleColumnRowMapper
for bean-style configuration.- See Also:
-
SingleColumnRowMapper
Create a newSingleColumnRowMapper
.Consider using the
newInstance(java.lang.Class<T>)
factory method instead, which allows for specifying the required type once only.- Parameters:
requiredType
- the type that each result object is expected to match
-
-
Method Details
-
setRequiredType
Set the type that each result object is expected to match.If not specified, the column value will be exposed as returned by the
Row
. -
mapRow
public T mapRow(com.datastax.oss.driver.api.core.cql.Row row, int rowNum) throws com.datastax.oss.driver.api.core.DriverException Extract a value for the single column in the current row.Validates that there is only one column selected, then delegates to
getColumnValue()
and alsoconvertValueToRequiredType
, if necessary.- Specified by:
mapRow
in interfaceRowMapper<T>
- Parameters:
row
- theRow
to map, must not be null.rowNum
- the number of the current row.- Returns:
- the result object for the current row.
- Throws:
com.datastax.oss.driver.api.core.DriverException
- if aDriverException
is encountered getting column values (that is, there's no need to catchDriverException
)- See Also:
-
ColumnDefinitions.size()
getColumnValue(Row, int, Class)
convertValueToRequiredType(Object, Class)
-
getColumnValue
@Nullable protected Object getColumnValue(com.datastax.oss.driver.api.core.cql.Row row, int index, @Nullable Class<?> requiredType) throws com.datastax.oss.driver.api.core.DriverException Retrieve a CQL object value for the specified column.The default implementation calls
RowUtils.getRowValue(Row, int, Class)
. If no required type has been specified, this method delegates togetColumnValue(rs, index)
, which basically callsGettableByIndex.getObject(int)
but applies some additional default conversion to appropriate value types.- Parameters:
row
- is theRow
holding the data, must not be null.index
- is the column indexrequiredType
- the type that each result object is expected to match (or null if none specified).- Returns:
- the Object value.
- Throws:
com.datastax.oss.driver.api.core.DriverException
- in case of extraction failure- See Also:
-
getColumnValue
Retrieve a object value for the specified column, using the most appropriate value type. Called if no required type has been specified.The default implementation delegates to
RowUtils.getRowValue(Row, int, Class)
, which uses theGettableByIndex.getObject(int)
method.- Parameters:
row
- is theRow
holding the data, must not be null.index
- is the column index- Returns:
- the Object value.
- Throws:
com.datastax.oss.driver.api.core.DriverException
- in case of extraction failure.- See Also:
-
convertValueToRequiredType
Convert the given column value to the specified required type. Only called if the extracted column value does not match already.If the required type is String, the value will simply get stringified via
toString()
. In case of a Number, the value will be converted into a Number, either through number conversion or through String parsing (depending on the value type).- Parameters:
value
- the column value as extracted fromgetColumnValue()
(never null)requiredType
- the type that each result object is expected to match (never null)- Returns:
- the converted value
- See Also:
-
newInstance
Static factory method to create a newSingleColumnRowMapper
(with the required type specified only once).- Parameters:
requiredType
- the type that each result object is expected to match
-