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.i18n.Msg;
023import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
024
025import javax.annotation.Nonnull;
026
027public class ForeignKeyContainer {
028
029        /**
030         * The parent table name
031         */
032        private String myParentTableName;
033
034        /**
035         * The name of the column in this table that holds the foreign key
036         */
037        private String myColumnName;
038
039        /**
040         * The column data type
041         */
042        private ColumnTypeEnum myColumnTypeEnum;
043
044        /**
045         * The name of the column in the parent table (that is the foreign key)
046         */
047        private String myParentTableColumnName;
048
049        public ForeignKeyContainer(
050                        String theColumnName,
051                        ColumnTypeEnum theColumnTypeEnum,
052                        String theParentTableName,
053                        String theParentTableColumnName) {
054                myColumnName = theColumnName;
055                myColumnTypeEnum = theColumnTypeEnum;
056                myParentTableName = theParentTableName;
057                myParentTableColumnName = theParentTableColumnName;
058        }
059
060        public String getParentTableName() {
061                return myParentTableName;
062        }
063
064        public void setParentTableName(String theParentTableName) {
065                myParentTableName = theParentTableName;
066        }
067
068        public String getColumnName() {
069                return myColumnName;
070        }
071
072        public void setColumnName(String theColumnName) {
073                myColumnName = theColumnName;
074        }
075
076        public String getParentTableColumnName() {
077                return myParentTableColumnName;
078        }
079
080        public void setParentTableColumnName(String theParentTableColumnName) {
081                myParentTableColumnName = theParentTableColumnName;
082        }
083
084        public ColumnTypeEnum getColumnTypeEnum() {
085                return myColumnTypeEnum;
086        }
087
088        public void setColumnTypeEnum(ColumnTypeEnum theColumnTypeEnum) {
089                myColumnTypeEnum = theColumnTypeEnum;
090        }
091
092        public String generateSQL(@Nonnull DriverTypeEnum theDriverTypeEnum, boolean thePrettyPrint) {
093                switch (theDriverTypeEnum) {
094                        case MYSQL_5_7:
095                                return String.format(
096                                                "FOREIGN KEY (%s) REFERENCES %s(%s)", myColumnName, myParentTableName, myParentTableColumnName);
097                        case MSSQL_2012:
098                        case ORACLE_12C:
099                                return String.format(
100                                                "%s %s FOREIGN KEY REFERENCES %s(%s)",
101                                                myColumnName, myColumnTypeEnum.name(), myParentTableName, myParentTableColumnName);
102                        case POSTGRES_9_4:
103                                return String.format(
104                                                "FOREIGN KEY(%s) REFERENCES %s(%s)", myColumnName, myParentTableName, myParentTableColumnName);
105                        default:
106                                throw new UnsupportedOperationException(
107                                                Msg.code(2232) + " SQL Engine " + theDriverTypeEnum.name() + " not supported for foreign key!");
108                }
109        }
110}