123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- /**
- *
- */
- 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<String,String> databaseNames = new HashMap<String, String>();
- /**
- * 当前数据库类型
- */
- 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<String> 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<String, String> getDatabaseNames() {
- return databaseNames;
- }
- public void setDatabaseNames(Map<String, String> databaseNames) {
- this.databaseNames = databaseNames;
- }
- public String getDbType() {
- return dbType;
- }
- public void setDbType(String dbType) {
- this.dbType = dbType;
- }
- }
|