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 ) { 055 myColumnName = theColumnName; 056 myColumnTypeEnum = theColumnTypeEnum; 057 myParentTableName = theParentTableName; 058 myParentTableColumnName = theParentTableColumnName; 059 } 060 061 public String getParentTableName() { 062 return myParentTableName; 063 } 064 065 public void setParentTableName(String theParentTableName) { 066 myParentTableName = theParentTableName; 067 } 068 069 public String getColumnName() { 070 return myColumnName; 071 } 072 073 public void setColumnName(String theColumnName) { 074 myColumnName = theColumnName; 075 } 076 077 public String getParentTableColumnName() { 078 return myParentTableColumnName; 079 } 080 081 public void setParentTableColumnName(String theParentTableColumnName) { 082 myParentTableColumnName = theParentTableColumnName; 083 } 084 085 public ColumnTypeEnum getColumnTypeEnum() { 086 return myColumnTypeEnum; 087 } 088 089 public void setColumnTypeEnum(ColumnTypeEnum theColumnTypeEnum) { 090 myColumnTypeEnum = theColumnTypeEnum; 091 } 092 093 public String generateSQL( 094 @Nonnull DriverTypeEnum theDriverTypeEnum, 095 boolean thePrettyPrint 096 ) { 097 switch (theDriverTypeEnum) { 098 case MYSQL_5_7: 099 return String.format( 100 "FOREIGN KEY (%s) REFERENCES %s(%s)", 101 myColumnName, 102 myParentTableName, 103 myParentTableColumnName 104 ); 105 case MSSQL_2012: 106 case ORACLE_12C: 107 return String.format( 108 "%s %s FOREIGN KEY REFERENCES %s(%s)", 109 myColumnName, 110 myColumnTypeEnum.name(), 111 myParentTableName, 112 myParentTableColumnName 113 ); 114 case POSTGRES_9_4: 115 return String.format( 116 "FOREIGN KEY(%s) REFERENCES %s(%s)", 117 myColumnName, 118 myParentTableName, 119 myParentTableColumnName 120 ); 121 default: 122 throw new UnsupportedOperationException( 123 Msg.code(2232) + " SQL Engine " + theDriverTypeEnum.name() + " not supported for foreign key!"); 124 } 125 } 126}