/** * */ package com.primeton.dams.utils.template; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.InitializingBean; import javax.sql.DataSource; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.SQLException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * 数据库辨认,如将全称的“Microsoft SQL Server”辨认为简称“SQLServer” * @author user * @version 2.2 2010-10-21 */ @Slf4j public class DatabaseRecognizer implements InitializingBean { /** * 需要辨认的数据库名称,KEY为全称,VALUE为简称 */ private Map databaseNames = new HashMap(); /** * 当前数据库类型 */ String dbType; /** * 当前元数据系统所使用的数据库的产品名称,如“Microsoft SQL Server”、“DB2/NT”、“DB2/LINUX”、“DB2/SUN” */ String currentDatabaseProductName; /** * 当前元数据系统所使用的数据库的产品版本 */ String currentDatabaseProductVersion; @Override public void afterPropertiesSet() throws Exception { } /** * 获取数据库连接并取得数据库名称,然后选择合适的与数据库相关的Spring配置文件 * @param dataSource dataSource * @return Spring配置文件名 */ public String determineDatabase(DataSource dataSource) { String databaseName = null; try (Connection conn = dataSource.getConnection()){ DatabaseMetaData meta = conn.getMetaData(); databaseName = meta.getDatabaseProductName(); if(databaseName.equals("DM DBMS")){ databaseName = "DM"; } String productVersion = meta.getDatabaseProductVersion(); if (log.isInfoEnabled()) { log.info("Current Database is " + databaseName + ", Version is " + productVersion); } setCurrentDatabaseProductName(databaseName); setCurrentDatabaseProductVersion(productVersion); databaseName = StringUtils.lowerCase(getDatabaseName(databaseName)); this.dbType = databaseName; } catch (SQLException e) { String s = "Cannot determine the database brand name: " + databaseName; log.error(s, e); throw new IllegalArgumentException(s, e); } return databaseName; } /** * 返回数据库的简称 * @param dbFullName 数据库全称 * @return 简称,若没有找到则返回dbFullName */ public String getDatabaseName(String dbFullName) { if (databaseNames.containsKey(dbFullName)) { // 名称全匹配的情况 return databaseNames.get(dbFullName); } for (Iterator it = databaseNames.keySet().iterator(); it.hasNext();) { String key = it.next(); // 忽略大小写,匹配一次 if(StringUtils.equalsIgnoreCase(dbFullName, key)) { return databaseNames.get(key); } // 正则表达式匹配,如DB2的产品名称可能返回不一样,如:DB2/NT, DB2/LINUX, DB2/SUN if (dbFullName.matches(key)) { return databaseNames.get(key); } } return dbFullName; } /** * 返回当前元数据系统所使用的数据库的产品名称,如“Microsoft SQL Server”、“DB2/NT”、“DB2/LINUX” * @return */ public String getCurrentDatabaseProductName() { return this.currentDatabaseProductName; } /** * 设置当前元数据系统所使用的数据库的产品名称 * @param currentDatabaseProductName */ public void setCurrentDatabaseProductName(String currentDatabaseProductName) { this.currentDatabaseProductName = currentDatabaseProductName; } public String getCurrentDatabaseProductVersion() { return currentDatabaseProductVersion; } public void setCurrentDatabaseProductVersion( String currentDatabaseProductVersion) { this.currentDatabaseProductVersion = currentDatabaseProductVersion; } public Map getDatabaseNames() { return databaseNames; } public void setDatabaseNames(Map databaseNames) { this.databaseNames = databaseNames; } public String getDbType() { return dbType; } public void setDbType(String dbType) { this.dbType = dbType; } }