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 return fromConnection(connection); 064 } catch (SQLException e) { 065 throw new DatabaseException("Unable to connect to database to determine its type", e); 066 } 067 } 068 069 /** 070 * Determines the type of database represented by the given {@code connection}. 071 * 072 * @param connection an active database connection 073 * @return the type of database 074 * @throws DatabaseException if an exception occurs while attempting to read database metadata 075 */ 076 @NonNull 077 public static DatabaseType fromConnection(@NonNull Connection connection) { 078 requireNonNull(connection); 079 080 try { 081 DatabaseMetaData databaseMetaData = connection.getMetaData(); 082 String databaseProductName = databaseMetaData.getDatabaseProductName(); 083 String url = databaseMetaData.getURL(); 084 String driverName = databaseMetaData.getDriverName(); 085 086 // All of our checks are against databases with English names 087 String databaseProductNameLowercase = databaseProductName == null ? "" : databaseProductName.toLowerCase(Locale.ENGLISH); 088 String urlLowercase = url == null ? "" : url.toLowerCase(Locale.ENGLISH); 089 String driverNameLowercase = driverName == null ? "" : driverName.toLowerCase(Locale.ENGLISH); 090 091 // Prefer product name 092 if (databaseProductNameLowercase.startsWith("oracle")) 093 return DatabaseType.ORACLE; 094 095 // Strict match for PostgreSQL 096 if (databaseProductNameLowercase.contains("postgresql") || databaseProductNameLowercase.equals("postgres")) // some proxies shorten it 097 return DatabaseType.POSTGRESQL; 098 099 // Fallbacks if product name is absent/weird but we're clearly using the PG driver/URL 100 if (urlLowercase.startsWith("jdbc:postgresql:") || driverNameLowercase.contains("postgresql")) 101 return DatabaseType.POSTGRESQL; 102 103 return DatabaseType.GENERIC; 104 } catch (SQLException e) { 105 throw new DatabaseException("Unable to inspect database metadata to determine its type", e); 106 } 107 } 108}