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