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 org.apache.commons.lang3.Validate;
023import org.apache.commons.lang3.builder.EqualsBuilder;
024import org.apache.commons.lang3.builder.HashCodeBuilder;
025
026import java.util.Objects;
027
028public abstract class BaseTableTask extends BaseTask {
029        private String myTableName;
030
031
032        public BaseTableTask(String theProductVersion, String theSchemaVersion) {
033                super(theProductVersion, theSchemaVersion);
034        }
035
036        public String getTableName() {
037                return myTableName;
038        }
039
040        public BaseTableTask setTableName(String theTableName) {
041                Validate.notBlank(theTableName);
042                myTableName = theTableName;
043                return this;
044        }
045
046        @Override
047        public void validate() {
048                Validate.notBlank(myTableName);
049        }
050
051        @Override
052        protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) {
053                BaseTableTask otherObject = (BaseTableTask) theOtherObject;
054                theBuilder.append(myTableName, otherObject.myTableName);
055        }
056
057        protected String getSqlType(ColumnTypeEnum theColumnType, Long theColumnLength) {
058                String retVal = ColumnTypeToDriverTypeToSqlType.getColumnTypeToDriverTypeToSqlType().get(theColumnType).get(getDriverType());
059                Objects.requireNonNull(retVal);
060
061                if (theColumnType == ColumnTypeEnum.STRING) {
062                        retVal = retVal.replace("?", Long.toString(theColumnLength));
063                }
064
065                return retVal;
066        }
067
068        @Override
069        protected void generateHashCode(HashCodeBuilder theBuilder) {
070                theBuilder.append(myTableName);
071        }
072}