View Javadoc
1   /**
2    *    Copyright 2009-2019 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  
18  import java.sql.Connection;
19  import java.sql.DatabaseMetaData;
20  import java.sql.SQLException;
21  import java.util.Map;
22  import java.util.Properties;
23  
24  import javax.sql.DataSource;
25  
26  import org.apache.ibatis.logging.Log;
27  import org.apache.ibatis.logging.LogFactory;
28  
29  /**
30   * Vendor DatabaseId provider.
31   *
32   * It returns database product name as a databaseId.
33   * If the user provides a properties it uses it to translate database product name
34   * key="Microsoft SQL Server", value="ms" will return "ms".
35   * It can return null, if no database product name or
36   * a properties was specified and no translation was found.
37   *
38   * @author Eduardo Macarron
39   */
40  public class VendorDatabaseIdProvider implements DatabaseIdProvider {
41  
42    private Properties properties;
43  
44    @Override
45    public String getDatabaseId(DataSource dataSource) {
46      if (dataSource == null) {
47        throw new NullPointerException("dataSource cannot be null");
48      }
49      try {
50        return getDatabaseName(dataSource);
51      } catch (Exception e) {
52        LogHolder.log.error("Could not get a databaseId from dataSource", e);
53      }
54      return null;
55    }
56  
57    @Override
58    public void setProperties(Properties p) {
59      this.properties = p;
60    }
61  
62    private String getDatabaseName(DataSource dataSource) throws SQLException {
63      String productName = getDatabaseProductName(dataSource);
64      if (this.properties != null) {
65        for (Map.Entry<Object, Object> property : properties.entrySet()) {
66          if (productName.contains((String) property.getKey())) {
67            return (String) property.getValue();
68          }
69        }
70        // no match, return null
71        return null;
72      }
73      return productName;
74    }
75  
76    private String getDatabaseProductName(DataSource dataSource) throws SQLException {
77      try (Connection con = dataSource.getConnection()) {
78        DatabaseMetaData metaData = con.getMetaData();
79        return metaData.getDatabaseProductName();
80      }
81  
82    }
83  
84    private static class LogHolder {
85      private static final Log log = LogFactory.getLog(VendorDatabaseIdProvider.class);
86    }
87  
88  }