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
025/**
026 * Helper to extract database information about supported migrations.
027 */
028public class MetadataSource {
029
030        /**
031         * Does this database support index operations without write-locking the table?
032         */
033        public boolean isOnlineIndexSupported(DriverTypeEnum.ConnectionProperties theConnectionProperties) {
034
035                switch (theConnectionProperties.getDriverType()) {
036                        case POSTGRES_9_4:
037                        case COCKROACHDB_21_1:
038                                return true;
039                        case MSSQL_2012:
040                                String mssqlEdition = getEdition(theConnectionProperties);
041                                return mssqlEdition.startsWith("Enterprise");
042                        case ORACLE_12C:
043                                String oracleEdition = getEdition(theConnectionProperties);
044                                return oracleEdition.contains("Enterprise");
045                        default:
046                                return false;
047                }
048        }
049
050        /**
051         * Get the MS Sql Server or Oracle Server edition.  Other databases are not supported yet.
052         *
053         * @param theConnectionProperties the database to inspect
054         * @return the edition string (e.g. Standard, Enterprise, Developer, etc.)
055         */
056        private String getEdition(DriverTypeEnum.ConnectionProperties theConnectionProperties) {
057                final String result;
058                if (theConnectionProperties.getDriverType() == DriverTypeEnum.MSSQL_2012) {
059                        result = theConnectionProperties
060                                        .newJdbcTemplate()
061                                        .queryForObject("SELECT SERVERPROPERTY ('edition')", String.class);
062                } else if (theConnectionProperties.getDriverType() == DriverTypeEnum.ORACLE_12C) {
063                        result = theConnectionProperties
064                                        .newJdbcTemplate()
065                                        .queryForObject("SELECT BANNER FROM v$version WHERE banner LIKE 'Oracle%'", String.class);
066                } else {
067                        throw new UnsupportedOperationException(Msg.code(2084) + "We only know about MSSQL editions.");
068                }
069                return result;
070        }
071}