001/*
002 * Copyright 2015-2022 Transmogrify LLC, 2022-2025 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 javax.annotation.Nonnull;
020import javax.sql.DataSource;
021import java.sql.Connection;
022import java.sql.DatabaseMetaData;
023import java.sql.SQLException;
024import java.util.Locale;
025
026import static java.util.Objects.requireNonNull;
027
028/**
029 * Identifies different types of databases, which allows for special platform-specific handling.
030 *
031 * @author <a href="https://www.revetkn.com">Mark Allen</a>
032 * @since 1.0.0
033 */
034public enum DatabaseType {
035        /**
036         * A database which requires no special handling.
037         */
038        GENERIC,
039        /**
040         * A PostgreSQL database.
041         */
042        POSTGRESQL,
043        /**
044         * An Oracle database.
045         */
046        ORACLE;
047
048        /**
049         * Determines the type of database to which the given {@code dataSource} connects.
050         * <p>
051         * Note: this will establish a {@link Connection} to the database.
052         *
053         * @param dataSource the database connection factory
054         * @return the type of database
055         * @throws DatabaseException if an exception occurs while attempting to read database metadata
056         */
057        @Nonnull
058        public static DatabaseType fromDataSource(@Nonnull DataSource dataSource) {
059                requireNonNull(dataSource);
060                
061                try (Connection connection = dataSource.getConnection()) {
062                        DatabaseMetaData databaseMetaData = connection.getMetaData();
063                        String databaseProductName = databaseMetaData.getDatabaseProductName();
064                        String url = databaseMetaData.getURL();
065                        String driverName = databaseMetaData.getDriverName();
066
067                        // All of our checks are against databases with English names
068                        String databaseProductNameLowercase = databaseProductName == null ? "" : databaseProductName.toLowerCase(Locale.ENGLISH);
069                        String urlLowercase = url == null ? "" : url.toLowerCase(Locale.ENGLISH);
070                        String driverNameLowercase = driverName == null ? "" : driverName.toLowerCase(Locale.ENGLISH);
071
072                        // Prefer product name
073                        if (databaseProductNameLowercase.startsWith("oracle"))
074                                return DatabaseType.ORACLE;
075
076                        // Strict match for PostgreSQL
077                        if (databaseProductNameLowercase.contains("postgresql") || databaseProductNameLowercase.equals("postgres"))  // some proxies shorten it
078                                return DatabaseType.POSTGRESQL;
079
080                        // Fallbacks if product name is absent/weird but we're clearly using the PG driver/URL
081                        if (urlLowercase.startsWith("jdbc:postgresql:") || driverNameLowercase.contains("postgresql"))
082                                return DatabaseType.POSTGRESQL;
083
084                        return DatabaseType.GENERIC;
085                } catch (SQLException e) {
086                        throw new DatabaseException("Unable to connect to database to determine its type", e);
087                }
088        }
089}