Coverage Report - org.apache.ibatis.mapping.VendorDatabaseIdProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
VendorDatabaseIdProvider
69%
18/26
50%
5/10
4
 
 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  2
 public class VendorDatabaseIdProvider implements DatabaseIdProvider {
 42  
   
 43  1
   private static final Log log = LogFactory.getLog(BaseExecutor.class);
 44  
 
 45  
   private Properties properties;
 46  
 
 47  
   @Override
 48  
   public String getDatabaseId(DataSource dataSource) {
 49  2
     if (dataSource == null) {
 50  0
       throw new NullPointerException("dataSource cannot be null");
 51  
     }
 52  
     try {
 53  2
       return getDatabaseName(dataSource);
 54  0
     } catch (Exception e) {
 55  0
       log.error("Could not get a databaseId from dataSource", e);
 56  
     }
 57  0
     return null;
 58  
   }
 59  
 
 60  
   @Override
 61  
   public void setProperties(Properties p) {
 62  2
     this.properties = p;
 63  2
   }
 64  
 
 65  
   private String getDatabaseName(DataSource dataSource) throws SQLException {
 66  2
     String productName = getDatabaseProductName(dataSource);
 67  2
     if (this.properties != null) {
 68  2
       for (Map.Entry<Object, Object> property : properties.entrySet()) {
 69  2
         if (productName.contains((String) property.getKey())) {
 70  2
           return (String) property.getValue();
 71  
         }
 72  0
       }
 73  
       // no match, return null
 74  0
       return null;
 75  
     }
 76  0
     return productName;
 77  
   }
 78  
 
 79  
   private String getDatabaseProductName(DataSource dataSource) throws SQLException {
 80  2
     Connection con = null;
 81  
     try {
 82  2
       con = dataSource.getConnection();
 83  2
       DatabaseMetaData metaData = con.getMetaData();
 84  2
       return metaData.getDatabaseProductName();
 85  
     } finally {
 86  2
       if (con != null) {
 87  
         try {
 88  2
           con.close();
 89  0
         } catch (SQLException e) {
 90  
           // ignored
 91  4
         }
 92  
       }
 93  
     }
 94  
   }
 95  
   
 96  
 }