Class FHIRDbDAOImpl
- java.lang.Object
-
- com.ibm.fhir.persistence.jdbc.dao.impl.FHIRDbDAOImpl
-
- All Implemented Interfaces:
FHIRDbDAO
- Direct Known Subclasses:
ParameterDAOImpl
,ResourceDAOImpl
public class FHIRDbDAOImpl extends Object implements FHIRDbDAO
This class is a root Data Access Object for managing JDBC access to the FHIR database. As of 4.3.0, connection handling is factored out of the DAO and is instead the responsibility of the FHIRPersistenceJDBCImpl. Each DAO therefore requires a connection to be passed to it. This is good for separation of concerns, because the DAO code shouldn't care where the connection comes from. As each DAO is stateless, and lightweight, they can be created on-the-fly and handed a connection in the constructor. This pattern avoids polluting every method call with a connection parameter.
-
-
Constructor Summary
Constructors Constructor Description FHIRDbDAOImpl(Connection connection, String schemaName, FHIRDbFlavor flavor)
Constructs a DAO instance suitable for acquiring DB connections via JNDI from the app server.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected FHIRPersistenceDataAccessException
buildExceptionWithIssue(String msg, IssueType issueType)
protected FHIRPersistenceDBConnectException
buildFHIRPersistenceDBConnectException(String msg, IssueType issueType)
protected void
cleanup(PreparedStatement stmt)
Closes the passed PreparedStatement.protected void
cleanup(ResultSet resultSet, PreparedStatement stmt)
Closes the passed ResultSet, PreparedStatement.protected Resource
createDTO(ResultSet resultSet)
A method for creating a Data Transfer Object of type T from the contents of the passed ResultSet.protected List<Resource>
createDTOs(ResultSet resultSet)
An method for creating a collection of Data Transfer Objects of type T from the contents of the passed ResultSet.Connection
getConnection()
Getter for the connection being used by this DAO.FHIRDbFlavor
getFlavor()
Get the database flavor, which describes the database type and its capabilities (e.g.String
getSchemaName()
Getter for the schema name we've been configured forprotected IDatabaseTranslator
getTranslator()
Get the translator appropriate for the flavor of database we are usingboolean
isDb2Database()
protected int
runCountQuery(Select countQuery)
Creates and executes a PreparedStatement for the passed sql containing a 'SELECT COUNT...'.protected int
runCountQuery(String sql, Object... searchArgs)
Creates and executes a PreparedStatement for the passed sql containing a 'SELECT COUNT...'.protected List<Resource>
runQuery(Select select)
Retrieve the FHIR objects by executing the givenSelect
statementprotected List<Resource>
runQuery(String sql, Object... searchArgs)
Creates and executes a PreparedStatement using the passed parameters that returns a collection of FHIR Data Transfer Objects of type T.protected List<String>
runQuery_STR_VALUES(String sql, Object... searchArgs)
Creates and executes a PreparedStatement using the passed parameters that returns a collection of String values.protected <XT extends FHIRPersistenceException>
XTsevere(Logger logger, XT fx, String errorMessage, Throwable cause)
Log the exception message here along with the cause stack.protected <XT extends FHIRPersistenceException>
XTsevere(Logger logger, XT fx, Throwable cause)
Convenience function to log the cause of an exception about to be thrown.
-
-
-
Constructor Detail
-
FHIRDbDAOImpl
public FHIRDbDAOImpl(Connection connection, String schemaName, FHIRDbFlavor flavor)
Constructs a DAO instance suitable for acquiring DB connections via JNDI from the app server.
-
-
Method Detail
-
getConnection
public Connection getConnection()
Getter for the connection being used by this DAO.- Specified by:
getConnection
in interfaceFHIRDbDAO
- Returns:
-
getSchemaName
public String getSchemaName()
Getter for the schema name we've been configured for- Returns:
-
getFlavor
public FHIRDbFlavor getFlavor()
Description copied from interface:FHIRDbDAO
Get the database flavor, which describes the database type and its capabilities (e.g. is it multi-tenant?)
-
severe
protected <XT extends FHIRPersistenceException> XT severe(Logger logger, XT fx, Throwable cause)
Convenience function to log the cause of an exception about to be thrown. This is useful when avoiding chaining the cause with the persistence exception, which could inadvertently leak sensitive information (details of the schema, for example)- Parameters:
logger
-fx
-cause
-- Returns:
-
severe
protected <XT extends FHIRPersistenceException> XT severe(Logger logger, XT fx, String errorMessage, Throwable cause)
Log the exception message here along with the cause stack. Return the exception fx to the caller so that it can be thrown easily.- Parameters:
logger
-fx
-errorMessage
-cause
-- Returns:
-
cleanup
protected void cleanup(PreparedStatement stmt)
Closes the passed PreparedStatement.- Parameters:
stmt
-connection
-
-
cleanup
protected void cleanup(ResultSet resultSet, PreparedStatement stmt)
Closes the passed ResultSet, PreparedStatement. We are no longer responsible for Connections so don't close them- Parameters:
resultSet
-stmt
-
-
runQuery
protected List<Resource> runQuery(String sql, Object... searchArgs) throws FHIRPersistenceDataAccessException, FHIRPersistenceDBConnectException
Creates and executes a PreparedStatement using the passed parameters that returns a collection of FHIR Data Transfer Objects of type T.- Parameters:
sql
- - The SQL template to execute.searchArgs
- - An array of arguments to be substituted into the SQL template.- Returns:
- List
- A List of FHIR Data Transfer Objects resulting from the executed query. - Throws:
FHIRPersistenceDataAccessException
FHIRPersistenceDBConnectException
-
runCountQuery
protected int runCountQuery(String sql, Object... searchArgs) throws FHIRPersistenceDataAccessException, FHIRPersistenceDBConnectException
Creates and executes a PreparedStatement for the passed sql containing a 'SELECT COUNT...'. The count value is extracted from the ResultSet and returned as an int.- Parameters:
sql
- - The SQL SELECT COUNT template to execute.searchArgs
- - An array of arguments to be substituted into the SQL template.- Returns:
- int - The count of results returned by the SQL query.
- Throws:
FHIRPersistenceDataAccessException
FHIRPersistenceDBConnectException
-
runCountQuery
protected int runCountQuery(Select countQuery) throws FHIRPersistenceDataAccessException, FHIRPersistenceDBConnectException
Creates and executes a PreparedStatement for the passed sql containing a 'SELECT COUNT...'. The count value is extracted from the ResultSet and returned as an int.- Parameters:
countQuery
- - The SQL SELECT COUNT statement and bind variables to execute.- Returns:
- int - The count of results returned by the SQL query.
- Throws:
FHIRPersistenceDataAccessException
FHIRPersistenceDBConnectException
-
runQuery
protected List<Resource> runQuery(Select select) throws FHIRPersistenceDataAccessException, FHIRPersistenceDBConnectException
Retrieve the FHIR objects by executing the givenSelect
statement- Parameters:
select
-- Returns:
- Throws:
FHIRPersistenceDataAccessException
FHIRPersistenceDBConnectException
-
createDTOs
protected List<Resource> createDTOs(ResultSet resultSet) throws FHIRPersistenceDataAccessException
An method for creating a collection of Data Transfer Objects of type T from the contents of the passed ResultSet.- Parameters:
resultSet
- A ResultSet containing FHIR persistent object data.- Returns:
- List
- A collection of FHIR Data Transfer objects of the same type. - Throws:
FHIRPersistenceDataAccessException
-
createDTO
protected Resource createDTO(ResultSet resultSet) throws FHIRPersistenceDataAccessException
A method for creating a Data Transfer Object of type T from the contents of the passed ResultSet.- Parameters:
resultSet
- A ResultSet containing FHIR persistent object data.- Returns:
- T - An instance of type T, which is a FHIR Data Transfer Object.
- Throws:
FHIRPersistenceDataAccessException
-
isDb2Database
public boolean isDb2Database()
- Specified by:
isDb2Database
in interfaceFHIRDbDAO
- Returns:
- true if this DAO is connected to a DB2 database.
-
buildExceptionWithIssue
protected FHIRPersistenceDataAccessException buildExceptionWithIssue(String msg, IssueType issueType) throws FHIRPersistenceDataAccessException
-
buildFHIRPersistenceDBConnectException
protected FHIRPersistenceDBConnectException buildFHIRPersistenceDBConnectException(String msg, IssueType issueType) throws FHIRPersistenceDBConnectException
-
runQuery_STR_VALUES
protected List<String> runQuery_STR_VALUES(String sql, Object... searchArgs) throws FHIRPersistenceDataAccessException, FHIRPersistenceDBConnectException
Creates and executes a PreparedStatement using the passed parameters that returns a collection of String values.- Parameters:
sql
- - The SQL template to execute.searchArgs
- - An array of arguments to be substituted into the SQL template.- Returns:
- List
- A List of strings resulting from the executed query. - Throws:
FHIRPersistenceDataAccessException
FHIRPersistenceDBConnectException
-
getTranslator
protected IDatabaseTranslator getTranslator()
Get the translator appropriate for the flavor of database we are using- Returns:
-
-