FHIR icon indicating copy to clipboard operation
FHIR copied to clipboard

Allow FHIROperation implementations to execute arbitrary SQL

Open punktilious opened this issue 3 years ago • 0 comments

Is your feature request related to a problem? Please describe. Currently FHIROperation implementations can access the FHIRPersistence interface:

FHIRPersistence pl =
        (FHIRPersistence) operationContext.getProperty(FHIROperationContext.PROPNAME_PERSISTENCE_IMPL);

Using this interface, the implementations can perform any of the interactions advertised by the FHIRPersistence interface, but this interface does not provide any way to execute arbitrary operations on the underlying database.

The implementer is able to obtain the FHIRPersistenceJDBCImpl instance by downcasting the FHIRPersistence variable shown above. However, this still does not provide access to the correct connection because the openConnection() call is currently private.

Describe the solution you'd like Either:

  1. make openConnection() public and trust that the implementer will close the connection using a pattern such as:
  try (Connection c = persistenceImpl.openConnection()) {
      ...
  }

or

  1. implement a generic method which will execute a command object, hiding the open/close of the connection. Similar to:
    public <T> T runStatement(IDatabaseSupplier<T> supplier);

where:

/**
 * Represents a statement runnable by {@link IDatabaseAdapter} returning a result
 * of type T.
 */
public interface IDatabaseSupplier<T> {

    /**
     * Execute the statement using the connection and return the value
     * @param translator to translate any exceptions
     * @param c
     */
    public T run(IDatabaseTranslator translator, Connection c);
}

Note that in all cases, the implementer is expected to start and end the transaction. See HealthcheckOperation for a good example how to do this.

Describe alternatives you've considered An implementer could attempt to access the configured datasource directly from JNDI, but this is non-trivial as it requires replicating the logic to look up the correct datasource JNDI name from the FHIR server configuration.

Acceptance Criteria

  1. GIVEN [a precondition] AND [another precondition] WHEN [test step] AND [test step] THEN [verification step] AND [verification step]

Additional context Add any other context or screenshots about the feature request here.

punktilious avatar Aug 03 '22 15:08 punktilious