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