001/*-
002 * #%L
003 * HAPI FHIR Server - SQL Migration
004 * %%
005 * Copyright (C) 2014 - 2024 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        public BaseTableTask(String theProductVersion, String theSchemaVersion) {
032                super(theProductVersion, theSchemaVersion);
033        }
034
035        public String getTableName() {
036                return myTableName;
037        }
038
039        public BaseTableTask setTableName(String theTableName) {
040                Validate.notBlank(theTableName);
041                myTableName = theTableName;
042                return this;
043        }
044
045        @Override
046        public void validate() {
047                Validate.notBlank(myTableName);
048        }
049
050        @Override
051        protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) {
052                BaseTableTask otherObject = (BaseTableTask) theOtherObject;
053                theBuilder.append(myTableName, otherObject.myTableName);
054        }
055
056        protected String getSqlType(ColumnTypeEnum theColumnType, Long theColumnLength) {
057                String retVal = ColumnTypeToDriverTypeToSqlType.getColumnTypeToDriverTypeToSqlType()
058                                .get(theColumnType)
059                                .get(getDriverType());
060                Objects.requireNonNull(retVal);
061
062                if (theColumnType == ColumnTypeEnum.STRING) {
063                        retVal = retVal.replace("?", Long.toString(theColumnLength));
064                }
065
066                return retVal;
067        }
068
069        @Override
070        protected void generateHashCode(HashCodeBuilder theBuilder) {
071                theBuilder.append(myTableName);
072        }
073}