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; 023 024import java.sql.SQLException; 025 026public class MigrateColumBlobTypeToBinaryTypeTask extends BaseTableColumnTask { 027 028 private final String myFromColumName; 029 private final String myToColumName; 030 031 public MigrateColumBlobTypeToBinaryTypeTask( 032 String theProductVersion, 033 String theSchemaVersion, 034 String theTableName, 035 String theFromColumName, 036 String theToColumName) { 037 super(theProductVersion, theSchemaVersion); 038 myFromColumName = theFromColumName; 039 myToColumName = theToColumName; 040 041 setTableName(theTableName); 042 } 043 044 @Override 045 public void validate() { 046 setDescription("Migrating BLob (oid) from colum " + myFromColumName + " to BINARY on colum " + myToColumName 047 + " for table " + getTableName() + " (only affects Postgresql)"); 048 } 049 050 @Override 051 protected void doExecute() throws SQLException { 052 String sql = buildSqlStatement(); 053 054 executeSql(getTableName(), sql); 055 } 056 057 String buildSqlStatement() { 058 String tableName = getTableName().toLowerCase(); 059 String fromColumName = myFromColumName.toLowerCase(); 060 String toColumName = myToColumName.toLowerCase(); 061 062 String retVal; 063 064 switch (getDriverType()) { 065 case MYSQL_5_7: 066 case DERBY_EMBEDDED: 067 case ORACLE_12C: 068 case MARIADB_10_1: 069 case COCKROACHDB_21_1: 070 case H2_EMBEDDED: 071 case MSSQL_2012: 072 retVal = "update " + tableName + " set " + toColumName + " = " + fromColumName + " where " 073 + fromColumName + " is not null"; 074 break; 075 case POSTGRES_9_4: 076 retVal = "update " + tableName + " set " + toColumName + " = lo_get(" + fromColumName + ") where " 077 + fromColumName + " is not null"; 078 break; 079 default: 080 throw new IllegalStateException(Msg.code(2514) + "Driver is not supported or null."); 081 } 082 083 return retVal; 084 } 085}