Class FHIRDbDAOImpl
- java.lang.Object
-
- org.linuxforhealth.fhir.persistence.jdbc.dao.impl.FHIRDbDAOImpl
-
- All Implemented Interfaces:
FHIRDbDAO
- Direct Known Subclasses:
ParameterDAOImpl
,ResourceDAOImpl
public class FHIRDbDAOImpl extends java.lang.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(java.sql.Connection connection, java.lang.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(java.lang.String msg, IssueType issueType)
protected FHIRPersistenceDBConnectException
buildFHIRPersistenceDBConnectException(java.lang.String msg, IssueType issueType)
protected void
cleanup(java.sql.PreparedStatement stmt)
Closes the passed PreparedStatement.protected void
cleanup(java.sql.ResultSet resultSet, java.sql.PreparedStatement stmt)
Closes the passed ResultSet, PreparedStatement.protected Resource
createDTO(java.sql.ResultSet resultSet, boolean hasResourceTypeId)
A method for creating a Data Transfer Object of type T from the contents of the passed ResultSet.protected java.util.List<Resource>
createDTOs(java.sql.ResultSet resultSet)
An method for creating a collection of Data Transfer Objects of type T from the contents of the passed ResultSet.java.sql.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.java.lang.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 usingprotected int
runCountQuery(java.lang.String sql, java.lang.Object... searchArgs)
Creates and executes a PreparedStatement for the passed sql containing a 'SELECT COUNT...'.protected int
runCountQuery(Select countQuery)
Creates and executes a PreparedStatement for the passed sql containing a 'SELECT COUNT...'.protected java.util.List<Resource>
runQuery(java.lang.String sql, java.lang.Object... searchArgs)
Creates and executes a PreparedStatement using the passed parameters that returns a collection of FHIR Data Transfer Objects of type T.protected java.util.List<Resource>
runQuery(Select select)
Retrieve the FHIR objects by executing the givenSelect
statementprotected java.util.List<java.lang.String>
runQuery_STR_VALUES(java.lang.String sql, java.lang.Object... searchArgs)
Creates and executes a PreparedStatement using the passed parameters that returns a collection of String values.protected <XT extends FHIRPersistenceException>
XTsevere(java.util.logging.Logger logger, XT fx, java.lang.String errorMessage, java.lang.Throwable cause)
Log the exception message here along with the cause stack.protected <XT extends FHIRPersistenceException>
XTsevere(java.util.logging.Logger logger, XT fx, java.lang.Throwable cause)
Convenience function to log the cause of an exception about to be thrown.
-
-
-
Constructor Detail
-
FHIRDbDAOImpl
public FHIRDbDAOImpl(java.sql.Connection connection, java.lang.String schemaName, FHIRDbFlavor flavor)
Constructs a DAO instance suitable for acquiring DB connections via JNDI from the app server.
-
-
Method Detail
-
getConnection
public java.sql.Connection getConnection()
Getter for the connection being used by this DAO.- Specified by:
getConnection
in interfaceFHIRDbDAO
- Returns:
-
getSchemaName
public java.lang.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(java.util.logging.Logger logger, XT fx, java.lang.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(java.util.logging.Logger logger, XT fx, java.lang.String errorMessage, java.lang.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(java.sql.PreparedStatement stmt)
Closes the passed PreparedStatement.- Parameters:
stmt
-connection
-
-
cleanup
protected void cleanup(java.sql.ResultSet resultSet, java.sql.PreparedStatement stmt)
Closes the passed ResultSet, PreparedStatement. We are no longer responsible for Connections so don't close them- Parameters:
resultSet
-stmt
-
-
runQuery
protected java.util.List<Resource> runQuery(java.lang.String sql, java.lang.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(java.lang.String sql, java.lang.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 java.util.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 java.util.List<Resource> createDTOs(java.sql.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(java.sql.ResultSet resultSet, boolean hasResourceTypeId) 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
-
buildExceptionWithIssue
protected FHIRPersistenceDataAccessException buildExceptionWithIssue(java.lang.String msg, IssueType issueType) throws FHIRPersistenceDataAccessException
-
buildFHIRPersistenceDBConnectException
protected FHIRPersistenceDBConnectException buildFHIRPersistenceDBConnectException(java.lang.String msg, IssueType issueType) throws FHIRPersistenceDBConnectException
-
runQuery_STR_VALUES
protected java.util.List<java.lang.String> runQuery_STR_VALUES(java.lang.String sql, java.lang.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:
-
-