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