Class AbstractNamedParameterStatement<StatementType extends Statement>
- java.lang.Object
-
- com.identityworksllc.iiq.common.query.AbstractNamedParameterStatement<StatementType>
-
- Type Parameters:
StatementType- The type of statement,PreparedStatementorCallableStatement
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
NamedParameterStatement
public abstract class AbstractNamedParameterStatement<StatementType extends Statement> extends Object implements AutoCloseable
The abstract superclass for the named parameter statement types.
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanallowMissingAttributesIf true, invoking one of the setXXX methods on a non-existent parameter will be silently ignored.protected ConnectionconnectionThe connectionprotected Map<String,int[]>indexMapMaps parameter names to arrays of ints which are the parameter indices.protected StatementTypestatementThe statement this object is wrapping.
-
Constructor Summary
Constructors Constructor Description AbstractNamedParameterStatement()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract voidaddBatch()Adds an item to the current batch.voidcancel()Cancels this Statement object if both the DBMS and driver support aborting an SQL statement.voidclose()Closes the statement.int[]executeBatch()Executes all of the batched statements.ConnectiongetConnection()Returns the connection this statement is using.ResultSetgetGeneratedKeys()Returns any keys generated by the SQL statement.protected int[]getIndexes(String name)Returns the indexes for a parameter.booleangetMoreResults()Moves to this Statement object’s next result, returns true if it is a ResultSet object, and implicitly closes any current ResultSet object(s) obtained with the method getResultSet.booleangetMoreResults(int current)Moves to this Statement object’s next result, deals with any current ResultSet object(s) according to the instructions specified by the given flag, and returns true if the next result is a ResultSet object.Set<String>getParameterNames()Returns the set of parameter names.ResultSetgetResultSet()Returns the result set of the statement.StatementTypegetStatement()Returns the underlying statement.booleanisClosed()Returns true if the wrapped statement is closedstatic Stringparse(String query, Map<String,int[]> indexMap)Parses a query with named parameters.voidsetAllowMissingAttributes(boolean allowMissingAttributes)Sets the flag to allow an attempt to set missing attributes without throwing an exception.voidsetFetchDirection(int i)voidsetFetchSize(int i)voidsetMaxRows(int i)Set the max rows in the result setabstract voidsetObject(String name, Object value)Abstract method for use by subclasses to register their own specific value typesvoidsetParameters(Map<String,Object> parameters)Sets all parameters to this named parameter statement in bulk from the given MapvoidsetQueryTimeout(int seconds)Sets the query timeout
-
-
-
Field Detail
-
allowMissingAttributes
protected boolean allowMissingAttributes
If true, invoking one of the setXXX methods on a non-existent parameter will be silently ignored.The default is to throw an exception.
-
connection
protected Connection connection
The connection
-
indexMap
protected Map<String,int[]> indexMap
Maps parameter names to arrays of ints which are the parameter indices.
-
statement
protected StatementType extends Statement statement
The statement this object is wrapping.
-
-
Constructor Detail
-
AbstractNamedParameterStatement
public AbstractNamedParameterStatement()
-
-
Method Detail
-
parse
public static String parse(String query, Map<String,int[]> indexMap)
Parses a query with named parameters.The parameter-index mappings are put into the map, and the parsed query is returned.
- Parameters:
query- query to parseindexMap- map to hold parameter-index mappings- Returns:
- the parsed query
-
addBatch
public abstract void addBatch() throws SQLException
Adds an item to the current batch.Oddly this is not implemented in
Statement, but it is implemented by both sub-types of statement.- Throws:
SQLException- if an error occurred- See Also:
PreparedStatement.addBatch(),PreparedStatement.addBatch()
-
cancel
public void cancel() throws SQLException
Cancels this Statement object if both the DBMS and driver support aborting an SQL statement.- Throws:
SQLException- if an error occurred- See Also:
Statement.cancel()
-
close
public void close() throws SQLException
Closes the statement.- Specified by:
closein interfaceAutoCloseable- Throws:
SQLException- if an error occurred- See Also:
Statement.close()
-
executeBatch
public int[] executeBatch() throws SQLException
Executes all of the batched statements.See
Statement.executeBatch()andaddBatch()for details.- Returns:
- update counts for each statement
- Throws:
SQLException- if something went wrong- See Also:
Statement.executeBatch()
-
getConnection
public Connection getConnection() throws SQLException
Returns the connection this statement is using.- Returns:
- the connection
- Throws:
SQLException- if an error occurs while retrieving the connection- See Also:
Statement.getConnection()
-
getIndexes
protected int[] getIndexes(String name)
Returns the indexes for a parameter.- Parameters:
name- parameter name- Returns:
- parameter indexes
- Throws:
IllegalArgumentException- if the parameter does not exist
-
getGeneratedKeys
public ResultSet getGeneratedKeys() throws SQLException
Returns any keys generated by the SQL statement.- Returns:
- A ResultSet containing the generated keys
- Throws:
SQLException- if an error occurs while retrieving the keys- See Also:
Statement.getGeneratedKeys()
-
getMoreResults
public boolean getMoreResults() throws SQLException
Moves to this Statement object’s next result, returns true if it is a ResultSet object, and implicitly closes any current ResultSet object(s) obtained with the method getResultSet.- Returns:
- True if the next result is a ResultSet object, false if there are no more results
- Throws:
SQLException- if an error occurs while moving to the next result
-
getMoreResults
public boolean getMoreResults(int current) throws SQLException
Moves to this Statement object’s next result, deals with any current ResultSet object(s) according to the instructions specified by the given flag, and returns true if the next result is a ResultSet object.- Parameters:
current- What to do with the current result set, a constant fromStatement- Returns:
- True if the next result is a ResultSet object, false if there are no more results
- Throws:
SQLException- if an error occurs while moving to the next result- See Also:
Statement.getMoreResults(int),Statement.CLOSE_CURRENT_RESULT,Statement.KEEP_CURRENT_RESULT,Statement.CLOSE_ALL_RESULTS
-
getParameterNames
public Set<String> getParameterNames()
Returns the set of parameter names.- Returns:
- An unmodifiable set of parameter names
-
getResultSet
public ResultSet getResultSet() throws SQLException
Returns the result set of the statement.- Returns:
- the result set
- Throws:
SQLException- if an error occurs while retrieving the result set- See Also:
Statement.getResultSet()
-
getStatement
public StatementType getStatement()
Returns the underlying statement.- Returns:
- the statement
-
isClosed
public boolean isClosed() throws SQLException
Returns true if the wrapped statement is closed- Returns:
- True if the wrapped statement is closed
- Throws:
SQLException- See Also:
Statement.isClosed()
-
setAllowMissingAttributes
public void setAllowMissingAttributes(boolean allowMissingAttributes)
Sets the flag to allow an attempt to set missing attributes without throwing an exception.With this flag set to false, any attempt to invoke setString, or any other setXYZ method, will result in an exception.
- Parameters:
allowMissingAttributes-trueif we should not throw an exception when a parameter is unused
-
setFetchDirection
public void setFetchDirection(int i) throws SQLException
- Throws:
SQLException- See Also:
Statement.setFetchDirection(int)
-
setFetchSize
public void setFetchSize(int i) throws SQLException
- Throws:
SQLException- See Also:
Statement.setFetchSize(int)
-
setMaxRows
public void setMaxRows(int i) throws SQLException
Set the max rows in the result set- Parameters:
i- The result row size- Throws:
SQLException- on errors
-
setObject
public abstract void setObject(String name, Object value) throws SQLException
Abstract method for use by subclasses to register their own specific value types- Parameters:
name- The name of the argument to setvalue- The value to set- Throws:
SQLException- if any errors occur
-
setParameters
public final void setParameters(Map<String,Object> parameters) throws SQLException
Sets all parameters to this named parameter statement in bulk from the given Map- Parameters:
parameters- The parameters in question- Throws:
SQLException- if any failures occur
-
setQueryTimeout
public void setQueryTimeout(int seconds) throws SQLException
Sets the query timeout- Parameters:
seconds- The timeout for this query in seconds- Throws:
SQLException- if any errors occur- See Also:
Statement.setQueryTimeout(int)
-
-