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