com.ibm.fhir.persistence.jdbc.connection.CreateTempTablesAction Maven / Gradle / Ivy
/*
* (C) Copyright IBM Corp. 2020
*
* SPDX-License-Identifier: Apache-2.0
*/
package com.ibm.fhir.persistence.jdbc.connection;
import java.sql.Connection;
import java.util.logging.Logger;
import com.ibm.fhir.database.utils.api.IDatabaseStatement;
import com.ibm.fhir.database.utils.common.JdbcTarget;
import com.ibm.fhir.database.utils.derby.DerbyAdapter;
import com.ibm.fhir.database.utils.model.DbType;
import com.ibm.fhir.persistence.jdbc.derby.CreateCodeSystemsTmp;
import com.ibm.fhir.persistence.jdbc.derby.CreateCommonTokenValuesTmp;
import com.ibm.fhir.persistence.jdbc.exception.FHIRPersistenceDBConnectException;
/**
* Creates the declared global temp table used in Derby for handling
* upserts into common_token_values and code_systems, avoiding
* huge VALUES() statements which cause the Derby SQL parser to
* generate a stack overflow.
*/
public class CreateTempTablesAction extends ChainedAction {
private static final Logger log = Logger.getLogger(CreateTempTablesAction.class.getName());
/**
* Public constructor. No next action, so this will be the last action applied
*/
public CreateTempTablesAction() {
super();
}
/**
* Public constructor
* @param next the next action in the chain
*/
public CreateTempTablesAction(Action next) {
super(next);
}
@Override
public void performOn(FHIRDbFlavor flavor, Connection connection) throws FHIRPersistenceDBConnectException {
if (flavor.getType() == DbType.DERBY) {
// This is only used for Derby databases
log.fine("Adding declared global temp tables to this session");
JdbcTarget target = new JdbcTarget(connection);
DerbyAdapter adapter = new DerbyAdapter(target);
createCodeSystemsTmp(adapter);
createCommonTokenValuesTmp(adapter);
}
// perform next action in the chain
super.performOn(flavor, connection);
}
/**
* Create the declared global temporary table COMMON_TOKEN_VALUES_TMP
* @param connection
* @throws FHIRPersistenceDBConnectException
*/
public void createCommonTokenValuesTmp(DerbyAdapter adapter) throws FHIRPersistenceDBConnectException {
IDatabaseStatement cmd = new CreateCommonTokenValuesTmp();
adapter.runStatement(cmd);
}
/**
* Create the declared global temporary table CODE_SYSTEMS_TMP
* @param connection
* @throws FHIRPersistenceDBConnectException
*/
public void createCodeSystemsTmp(DerbyAdapter adapter) throws FHIRPersistenceDBConnectException {
IDatabaseStatement cmd = new CreateCodeSystemsTmp();
adapter.runStatement(cmd);
}
}