VendorDatabaseIdProvider.java

  1. /*
  2.  *    Copyright 2009-2021 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.mapping;

  17. import java.sql.Connection;
  18. import java.sql.DatabaseMetaData;
  19. import java.sql.SQLException;
  20. import java.util.Map;
  21. import java.util.Properties;

  22. import javax.sql.DataSource;

  23. import org.apache.ibatis.logging.Log;
  24. import org.apache.ibatis.logging.LogFactory;

  25. /**
  26.  * Vendor DatabaseId provider.
  27.  *
  28.  * It returns database product name as a databaseId.
  29.  * If the user provides a properties it uses it to translate database product name
  30.  * key="Microsoft SQL Server", value="ms" will return "ms".
  31.  * It can return null, if no database product name or
  32.  * a properties was specified and no translation was found.
  33.  *
  34.  * @author Eduardo Macarron
  35.  */
  36. public class VendorDatabaseIdProvider implements DatabaseIdProvider {

  37.   private Properties properties;

  38.   @Override
  39.   public String getDatabaseId(DataSource dataSource) {
  40.     if (dataSource == null) {
  41.       throw new NullPointerException("dataSource cannot be null");
  42.     }
  43.     try {
  44.       return getDatabaseName(dataSource);
  45.     } catch (Exception e) {
  46.       LogHolder.log.error("Could not get a databaseId from dataSource", e);
  47.     }
  48.     return null;
  49.   }

  50.   @Override
  51.   public void setProperties(Properties p) {
  52.     this.properties = p;
  53.   }

  54.   private String getDatabaseName(DataSource dataSource) throws SQLException {
  55.     String productName = getDatabaseProductName(dataSource);
  56.     if (this.properties != null) {
  57.       for (Map.Entry<Object, Object> property : properties.entrySet()) {
  58.         if (productName.contains((String) property.getKey())) {
  59.           return (String) property.getValue();
  60.         }
  61.       }
  62.       // no match, return null
  63.       return null;
  64.     }
  65.     return productName;
  66.   }

  67.   private String getDatabaseProductName(DataSource dataSource) throws SQLException {
  68.     try (Connection con = dataSource.getConnection()) {
  69.       DatabaseMetaData metaData = con.getMetaData();
  70.       return metaData.getDatabaseProductName();
  71.     }

  72.   }

  73.   private static class LogHolder {
  74.     private static final Log log = LogFactory.getLog(VendorDatabaseIdProvider.class);
  75.   }

  76. }