001/*
002 * Copyright 2015-2022 Transmogrify LLC, 2022-2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.pyranid;
018
019import org.jspecify.annotations.NonNull;
020
021import javax.sql.DataSource;
022import java.sql.Connection;
023import java.sql.DatabaseMetaData;
024import java.sql.SQLException;
025import java.util.Locale;
026
027import static java.util.Objects.requireNonNull;
028
029/**
030 * Identifies different types of databases, which allows for special platform-specific handling.
031 *
032 * @author <a href="https://www.revetkn.com">Mark Allen</a>
033 * @since 1.0.0
034 */
035public enum DatabaseType {
036        /**
037         * A database which requires no special handling.
038         */
039        GENERIC,
040        /**
041         * A PostgreSQL database.
042         */
043        POSTGRESQL,
044        /**
045         * An Oracle database.
046         */
047        ORACLE;
048
049        /**
050         * Determines the type of database to which the given {@code dataSource} connects.
051         * <p>
052         * Note: this will establish a {@link Connection} to the database.
053         *
054         * @param dataSource the database connection factory
055         * @return the type of database
056         * @throws DatabaseException if an exception occurs while attempting to read database metadata
057         */
058        @NonNull
059        public static DatabaseType fromDataSource(@NonNull DataSource dataSource) {
060                requireNonNull(dataSource);
061                
062                try (Connection connection = dataSource.getConnection()) {
063                        DatabaseMetaData databaseMetaData = connection.getMetaData();
064                        String databaseProductName = databaseMetaData.getDatabaseProductName();
065                        String url = databaseMetaData.getURL();
066                        String driverName = databaseMetaData.getDriverName();
067
068                        // All of our checks are against databases with English names
069                        String databaseProductNameLowercase = databaseProductName == null ? "" : databaseProductName.toLowerCase(Locale.ENGLISH);
070                        String urlLowercase = url == null ? "" : url.toLowerCase(Locale.ENGLISH);
071                        String driverNameLowercase = driverName == null ? "" : driverName.toLowerCase(Locale.ENGLISH);
072
073                        // Prefer product name
074                        if (databaseProductNameLowercase.startsWith("oracle"))
075                                return DatabaseType.ORACLE;
076
077                        // Strict match for PostgreSQL
078                        if (databaseProductNameLowercase.contains("postgresql") || databaseProductNameLowercase.equals("postgres"))  // some proxies shorten it
079                                return DatabaseType.POSTGRESQL;
080
081                        // Fallbacks if product name is absent/weird but we're clearly using the PG driver/URL
082                        if (urlLowercase.startsWith("jdbc:postgresql:") || driverNameLowercase.contains("postgresql"))
083                                return DatabaseType.POSTGRESQL;
084
085                        return DatabaseType.GENERIC;
086                } catch (SQLException e) {
087                        throw new DatabaseException("Unable to connect to database to determine its type", e);
088                }
089        }
090}