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 org.apache.commons.lang3.Validate;
025import org.apache.commons.lang3.builder.EqualsBuilder;
026import org.apache.commons.lang3.builder.HashCodeBuilder;
027import org.intellij.lang.annotations.Language;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031import java.util.ArrayList;
032import java.util.HashMap;
033import java.util.List;
034import java.util.Map;
035
036public class ExecuteRawSqlTask extends BaseTask {
037
038        private static final Logger ourLog = LoggerFactory.getLogger(ExecuteRawSqlTask.class);
039        private Map<DriverTypeEnum, List<String>> myDriverToSqls = new HashMap<>();
040        private List<String> myDriverNeutralSqls = new ArrayList<>();
041
042        public ExecuteRawSqlTask(String theProductVersion, String theSchemaVersion) {
043                super(theProductVersion, theSchemaVersion);
044                setDescription("Execute raw sql");
045        }
046
047        public ExecuteRawSqlTask addSql(DriverTypeEnum theDriverType, @Language("SQL") String theSql) {
048                Validate.notNull(theDriverType);
049                Validate.notBlank(theSql);
050
051                List<String> list = myDriverToSqls.computeIfAbsent(theDriverType, t -> new ArrayList<>());
052                list.add(theSql);
053
054                return this;
055        }
056
057        public ExecuteRawSqlTask addSql(String theSql) {
058                Validate.notBlank("theSql must not be null", theSql);
059                myDriverNeutralSqls.add(theSql);
060
061                return this;
062        }
063
064        @Override
065        public void validate() {
066                // nothing
067        }
068
069        @Override
070        public void doExecute() {
071                List<String> sqlStatements = myDriverToSqls.computeIfAbsent(getDriverType(), t -> new ArrayList<>());
072                sqlStatements.addAll(myDriverNeutralSqls);
073
074                logInfo(ourLog, "Going to execute {} SQL statements", sqlStatements.size());
075
076                for (String nextSql : sqlStatements) {
077                        executeSql(null, nextSql);
078                }
079
080        }
081
082        @Override
083        protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) {
084                ExecuteRawSqlTask otherObject = (ExecuteRawSqlTask) theOtherObject;
085                theBuilder.append(myDriverNeutralSqls, otherObject.myDriverNeutralSqls);
086                theBuilder.append(myDriverToSqls, otherObject.myDriverToSqls);
087        }
088
089        @Override
090        protected void generateHashCode(HashCodeBuilder theBuilder) {
091                theBuilder.append(myDriverNeutralSqls);
092                theBuilder.append(myDriverToSqls);
093        }
094}