001package ca.uhn.fhir.jpa.migrate.taskdef;
002
003/*-
004 * #%L
005 * HAPI FHIR Server - SQL Migration
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
024import ca.uhn.fhir.jpa.migrate.JdbcUtils;
025import org.apache.commons.lang3.Validate;
026import org.apache.commons.lang3.builder.EqualsBuilder;
027import org.apache.commons.lang3.builder.HashCodeBuilder;
028import org.intellij.lang.annotations.Language;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032import java.sql.SQLException;
033import java.util.ArrayList;
034import java.util.HashMap;
035import java.util.List;
036import java.util.Map;
037import java.util.Set;
038
039public class AddTableRawSqlTask extends BaseTableTask {
040
041        private static final Logger ourLog = LoggerFactory.getLogger(AddTableRawSqlTask.class);
042        private Map<DriverTypeEnum, List<String>> myDriverToSqls = new HashMap<>();
043        private List<String> myDriverNeutralSqls = new ArrayList<>();
044
045        public AddTableRawSqlTask(String theProductVersion, String theSchemaVersion) {
046                super(theProductVersion, theSchemaVersion);
047        }
048
049        @Override
050        public void validate() {
051                super.validate();
052                setDescription("Add table using raw sql");
053        }
054
055        public void addSql(DriverTypeEnum theDriverType, @Language("SQL") String theSql) {
056                Validate.notNull(theDriverType);
057                Validate.notBlank(theSql);
058
059                List<String> list = myDriverToSqls.computeIfAbsent(theDriverType, t -> new ArrayList<>());
060                list.add(theSql);
061        }
062
063        @Override
064        public void doExecute() throws SQLException {
065                Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties());
066                if (tableNames.contains(getTableName())) {
067                        logInfo(ourLog, "Table {} already exists - No action performed", getTableName());
068                        return;
069                }
070
071                List<String> sqlStatements = myDriverToSqls.computeIfAbsent(getDriverType(), t -> new ArrayList<>());
072                sqlStatements.addAll(myDriverNeutralSqls);
073
074                logInfo(ourLog, "Going to create table {} using {} SQL statements", getTableName(), sqlStatements.size());
075                executeSqlListInTransaction(getTableName(), sqlStatements);
076        }
077
078        public void addSql(String theSql) {
079                Validate.notBlank("theSql must not be null", theSql);
080                myDriverNeutralSqls.add(theSql);
081        }
082
083        @Override
084        protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) {
085                super.generateEquals(theBuilder, theOtherObject);
086                AddTableRawSqlTask otherObject = (AddTableRawSqlTask) theOtherObject;
087                theBuilder.append(myDriverNeutralSqls, otherObject.myDriverNeutralSqls);
088        }
089
090        @Override
091        protected void generateHashCode(HashCodeBuilder theBuilder) {
092                super.generateHashCode(theBuilder);
093                theBuilder.append(myDriverNeutralSqls);
094        }
095}