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