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 MigrateColumnClobTypeToTextTypeTask extends BaseTableColumnTask {
027
028        private final String myFromColumName;
029        private final String myToColumName;
030
031        public MigrateColumnClobTypeToTextTypeTask(
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 CLob (oid) from colum  " + myFromColumName + " to " + myToColumName
047                                + ".TEXT for table " + getTableName() + " (only affects Postgresql)");
048        }
049
050        @Override
051        protected void doExecute() throws SQLException {
052                String sql = buildSqlStatement();
053                executeSql(getTableName(), sql);
054        }
055
056        String buildSqlStatement() {
057                String tableName = getTableName().toLowerCase();
058                String fromColumName = myFromColumName.toLowerCase();
059                String toColumName = myToColumName.toLowerCase();
060
061                String retVal;
062
063                switch (getDriverType()) {
064                        case MYSQL_5_7:
065                        case DERBY_EMBEDDED:
066                        case ORACLE_12C:
067                        case MARIADB_10_1:
068                        case COCKROACHDB_21_1:
069                        case H2_EMBEDDED:
070                        case MSSQL_2012:
071                                retVal = "update " + tableName + " set " + toColumName + " = " + fromColumName + " where "
072                                                + fromColumName + " is not null";
073                                break;
074                        case POSTGRES_9_4:
075                                retVal = "update " + tableName + " set " + toColumName + " = convert_from(lo_get(" + fromColumName
076                                                + "), 'UTF8') where " + fromColumName + " is not null";
077                                break;
078                        default:
079                                throw new IllegalStateException(Msg.code(2515));
080                }
081
082                return retVal;
083        }
084}