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 ca.uhn.fhir.jpa.migrate.JdbcUtils;
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.sql.SQLException;
032import java.util.ArrayList;
033import java.util.HashMap;
034import java.util.List;
035import java.util.Map;
036import java.util.Set;
037
038public class AddTableRawSqlTask extends BaseTableTask {
039
040        private static final Logger ourLog = LoggerFactory.getLogger(AddTableRawSqlTask.class);
041        private Map<DriverTypeEnum, List<String>> myDriverToSqls = new HashMap<>();
042        private List<String> myDriverNeutralSqls = new ArrayList<>();
043
044        public AddTableRawSqlTask(String theProductVersion, String theSchemaVersion) {
045                super(theProductVersion, theSchemaVersion);
046        }
047
048        @Override
049        public void validate() {
050                super.validate();
051                setDescription("Add table using raw sql");
052        }
053
054        public void addSql(DriverTypeEnum theDriverType, @Language("SQL") String theSql) {
055                Validate.notNull(theDriverType);
056                Validate.notBlank(theSql);
057
058                List<String> list = myDriverToSqls.computeIfAbsent(theDriverType, t -> new ArrayList<>());
059                list.add(theSql);
060        }
061
062        @Override
063        public void doExecute() throws SQLException {
064                Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties());
065                if (tableNames.contains(getTableName())) {
066                        logInfo(ourLog, "Table {} already exists - No action performed", getTableName());
067                        return;
068                }
069
070                List<String> sqlStatements = myDriverToSqls.computeIfAbsent(getDriverType(), t -> new ArrayList<>());
071                sqlStatements.addAll(myDriverNeutralSqls);
072
073                logInfo(ourLog, "Going to create table {} using {} SQL statements", getTableName(), sqlStatements.size());
074                executeSqlListInTransaction(getTableName(), sqlStatements);
075        }
076
077        public void addSql(String theSql) {
078                Validate.notBlank("theSql must not be null", theSql);
079                myDriverNeutralSqls.add(theSql);
080        }
081
082        @Override
083        protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) {
084                super.generateEquals(theBuilder, theOtherObject);
085                AddTableRawSqlTask otherObject = (AddTableRawSqlTask) theOtherObject;
086                theBuilder.append(myDriverNeutralSqls, otherObject.myDriverNeutralSqls);
087        }
088
089        @Override
090        protected void generateHashCode(HashCodeBuilder theBuilder) {
091                super.generateHashCode(theBuilder);
092                theBuilder.append(myDriverNeutralSqls);
093        }
094}