Class ResultSetIterator
- java.lang.Object
-
- com.identityworksllc.iiq.common.iterators.ResultSetIterator
-
- All Implemented Interfaces:
AutoCloseable
,Iterator<Object[]>
public final class ResultSetIterator extends Object implements Iterator<Object[]>, AutoCloseable
An adapter that translates aResultSet
to IIQ Reporting’s preferredIterator
<Object[]>.This class also exposes several utility methods that can be used to implement standard ResultSet behavior outside of this iterator context.
The constructor requires that you provide a list of column detail objects. These can be:
-
A String, which will be parsed as a
ColumnToken
(if it contains a ‘:’) or as a simple column name. -
An instance of
ColumnConfig
, which can be used to specify column data in greater detail. -
An instance of Sailpoint’s
ReportColumnConfig
class, which contains vastly more options for reporting purposes than are used here.
Column tokens are case-sensitive. Column names are not.
After obtaining an instance of this class, the query itself will be invoked on the first call to
hasNext()
.You may obtain the next row of results as an
Object[]
usingnext()
or as aMap
(where the key is the column’s field name) usingnextRow()
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ResultSetIterator.ColumnOutput
The output ofextractColumnValue(ResultSet, String, Integer)
, containing details about the column used as well as the actual value.static interface
ResultSetIterator.CustomTypeHandler
An interface to use for custom type handlers
-
Constructor Summary
Constructors Constructor Description ResultSetIterator(ResultSet resultSet, List<?> columns, sailpoint.api.SailPointContext context)
Adapts a ResultSet into Iterator<Object[]> form.ResultSetIterator(ResultSet resultSet, sailpoint.api.SailPointContext context)
Constructs an iterator over the ResultSet, inferring the column names from the ResultSet’s metadata object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
addTypeHandler(int type, ResultSetIterator.CustomTypeHandler handler)
API method to add a new type handler, where the type is not handled out of box.void
close()
Closes the result set; this can be called via an AutoCloseablestatic Object
deriveTypedValue(sailpoint.api.SailPointContext context, Object input, ColumnToken token)
TODO invert this so we can use multiple argumentsA variant of deriveTypedValue that takes in a ColumnToken object rather than a String.
static Object
deriveTypedValue(sailpoint.api.SailPointContext context, Object input, String inputDerivedType)
Static implementation of deriveTypedValue so it can be reused elsewhere.static ResultSetIterator.ColumnOutput
extractColumnValue(ResultSet resultSet, String columnToken, Integer type)
Processes the resulting column by extracting an appropriate object from the result set, returning the combination of the value and a derived type which can be passed to deriveTypedValue.Map<String,String>
getFieldHeaderMap()
Gets the map from field name to header.boolean
hasNext()
Returns true if the ResultSet has another row, advancing the ResultSet in the process.Object[]
next()
Retrieves the next row from the result set as an Object[], given the column configs.Map<String,Object>
nextRow()
To be used in place ofnext()
, returns a Map structure with the column ‘field’ names instead of an Object[].-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Iterator
forEachRemaining, remove
-
-
-
-
Constructor Detail
-
ResultSetIterator
public ResultSetIterator(ResultSet resultSet, sailpoint.api.SailPointContext context) throws SQLException
Constructs an iterator over the ResultSet, inferring the column names from the ResultSet’s metadata object.Columns will be returned in the order specified by the ResultSet.
No special parsing or object conversion options are available via this constructor.
- Parameters:
resultSet
- The result set to iterate overcontext
- The sailopint context- Throws:
SQLException
- if anything goes wrong parsing the column names
-
ResultSetIterator
public ResultSetIterator(ResultSet resultSet, List<?> columns, sailpoint.api.SailPointContext context) throws SQLException
Adapts a ResultSet into Iterator<Object[]> form.The columns argument must be a list of objects that can be passed to
ColumnConfig(Object)
.- Parameters:
resultSet
- The result set to adaptcolumns
- The ordered list of columns to include in the resultscontext
- The IIQ context- Throws:
SQLException
- if something goes wrong with checking column names
-
-
Method Detail
-
addTypeHandler
public static void addTypeHandler(int type, ResultSetIterator.CustomTypeHandler handler)
API method to add a new type handler, where the type is not handled out of box.- Parameters:
type
- The type integer (from, e,g.Types
.handler
- The handler to execute
-
deriveTypedValue
public static Object deriveTypedValue(sailpoint.api.SailPointContext context, Object input, ColumnToken token) throws sailpoint.tools.GeneralException
TODO invert this so we can use multiple argumentsA variant of deriveTypedValue that takes in a ColumnToken object rather than a String.
This prevents us from having to repeatedly parse the string.
- Parameters:
context
- The current Sailpoint context contextinput
- The input from which to derive a proper valuetoken
- The parsed column token- Returns:
- The resulting derived value as described in
deriveTypedValue(SailPointContext, Object, String)
- Throws:
sailpoint.tools.GeneralException
- if any failures occur
-
deriveTypedValue
public static Object deriveTypedValue(sailpoint.api.SailPointContext context, Object input, String inputDerivedType) throws sailpoint.tools.GeneralException
Static implementation of deriveTypedValue so it can be reused elsewhere.Derived types take the form [type]:[optional argument], similar to AngularJS filters. The valid types are ‘xml’, ‘timestamp’, boolean’, ‘object’, or any SailPointObject subclass name (e.g., Identity, Link).
Examples:
‘col1’: Returns the value of result set field ‘col1’ as is
‘col1:xml’: Parses the string value of result set column ‘col1’ as SailPoint XML and returns the resulting object.
‘col1:xml:firstname’: Parses the string value of result set column ‘col1’ as SailPoint XML, then (if not null) returns the value of the property ‘firstname’ on that object.
Types and arguments indicate:
‘xml’: The value will be parsed as an XML string. If an argument is present, Utilities.getProperty() will be used to dot-walk to that property of the parsed object. Otherwise, the parsed object itself will be returned.
‘timestamp’: The value will be parsed as a Long timestamp. The output will be a Date object. If an argument is present, it will be interpreted as a SimpleDateFormat format string and the output will be the formatted String.
‘boolean’: If the value is a String, it will be considered true if it is equal to the strings ‘true’, ‘1’, or ‘yes’. If the value is a Number, it will be considered true if it is non-zero. All other values are false.
‘object’: The value will be handled as a Java object of an arbitrary type. The argument will be used via Utilities.getProperty() to dot-walk to any arbitrary property.
‘script’: The value will be passed to the script provided as an argument, and the output of the Script will become the new value.
‘rule’: The value will be passed to the Rule specified by name in the argument, and the output of the Rule will become the new value. This is more efficient than the ‘script’ version because the parsed Beanshell can be cached by the RuleRunner.
Any
SailPointObject
type: If the value is a String, it will be used as an ID to look up the actual object in the current context. The argument’s function is the same as the ‘object’ type.- Parameters:
context
- The sailpoint contextinput
- The input objectinputDerivedType
- The derived type to use- Returns:
- The output object, which may be the same as the input
- Throws:
sailpoint.tools.GeneralException
- if any derivation failures occur
-
extractColumnValue
public static ResultSetIterator.ColumnOutput extractColumnValue(ResultSet resultSet, String columnToken, Integer type) throws SQLException, sailpoint.tools.GeneralException
Processes the resulting column by extracting an appropriate object from the result set, returning the combination of the value and a derived type which can be passed to deriveTypedValue.- Parameters:
resultSet
- Result set to read the column fromcolumnToken
- The column token, potentially including derived typestype
- The type of the column- Returns:
- The derived type of the result, if any
- Throws:
SQLException
- on any database issuessailpoint.tools.GeneralException
- on any Sailpoint issues
-
close
public void close() throws SQLException
Closes the result set; this can be called via an AutoCloseable- Specified by:
close
in interfaceAutoCloseable
- Throws:
SQLException
- if the closure fails
-
getFieldHeaderMap
public Map<String,String> getFieldHeaderMap()
Gets the map from field name to header.In many basic configurations, the field name and header will be identical, and both will be the same as the property name.
This is a
ListOrderedMap
and the keys will be in order.- Returns:
- The map from field name to header
-
hasNext
public boolean hasNext()
Returns true if the ResultSet has another row, advancing the ResultSet in the process.
-
next
public Object[] next()
Retrieves the next row from the result set as an Object[], given the column configs.This method also populates the
lastRow
object returned bynextRow()
.- Specified by:
next
in interfaceIterator<Object[]>
- Returns:
- The columns in the current row in order
- Throws:
NoSuchElementException
- if this class has been closed or if a previous call tohasNext()
returned false
-
nextRow
public Map<String,Object> nextRow()
To be used in place ofnext()
, returns a Map structure with the column ‘field’ names instead of an Object[].See
ColumnConfig.getField()
for detail on which keys will be used.This method advances the iterator by internally invoking
next()
.- Returns:
- A Map representing the next row read by
next()
-
-