1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.store

File DatabaseProduct.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

20
29
6
1
170
68
17
0.59
4.83
6
2.83

Classes

Class Line # Actions
DatabaseProduct 32 29 0% 17 12
0.781818278.2%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.store;
21   
22    /**
23    * Represent a Database Product name as returned by {@link java.sql.DatabaseMetaData#getDatabaseProductName()}.
24    * <p>
25    * Ideally we shouldn't have to take care of Database specificities since we're using Hibernate to abstract ourselves
26    * from Databases. However it happens that Hibernate doesn't support setting Catalogs on some databases and instead we
27    * need to use our own tricks to do that and these tricks depend on the database. Hence the need to differentiate them
28    * and hence the need for this class.
29    *
30    * @version $Id: b19b542b86095451407007180c7c4f57ecac8e39 $
31    */
 
32    public final class DatabaseProduct
33    {
34    /**
35    * The product name for Oracle databases.
36    */
37    public static final DatabaseProduct ORACLE = new DatabaseProduct("Oracle");
38   
39    /**
40    * The product name for Derby databases.
41    */
42    public static final DatabaseProduct DERBY = new DatabaseProduct("Apache Derby");
43   
44    /**
45    * The product name for HSQLDB databases.
46    */
47    public static final DatabaseProduct HSQLDB = new DatabaseProduct("HSQL Database Engine");
48   
49    /**
50    * The product name for DB2 databases.
51    * <p>
52    * Per DB2 documentation at
53    * http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.apdv.java.doc/doc/c0053013.html, the
54    * database product name returned by the {@link java.sql.DatabaseMetaData#getDatabaseProductName()} method of DB2
55    * JDBC drivers varies by the OS and environment the product is running on. Hence the DB string here uses only the
56    * first 3 unique characters of the database product name. The {@link #toProduct(String)} method also hence checks
57    * for {@link java.lang.String#startsWith(String)} rather than an exact match.
58    * </p>
59    */
60    public static final DatabaseProduct DB2 = new DatabaseProduct("DB2/");
61   
62    /**
63    * The product name for MySQL databases.
64    */
65    public static final DatabaseProduct MYSQL = new DatabaseProduct("MySQL");
66   
67    /**
68    * The product name for PostgreSQL databases.
69    */
70    public static final DatabaseProduct POSTGRESQL = new DatabaseProduct("PostgreSQL");
71   
72    /**
73    * The product name for Microsoft SQL Server databases.
74    */
75    public static final DatabaseProduct MSSQL = new DatabaseProduct("Microsoft SQL Server");
76   
77    /**
78    * Represents an unknown database for which we were not able to find the product name.
79    */
80    public static final DatabaseProduct UNKNOWN = new DatabaseProduct("Unknown");
81   
82    /**
83    * The product name for H2 databases.
84    */
85    public static final DatabaseProduct H2 = new DatabaseProduct("H2");
86   
87    /**
88    * @see #getProductName()
89    */
90    private String productName;
91   
92    /**
93    * Private constructor to prevent instantiations.
94    *
95    * @param productName the database product name as returned by
96    * {@link java.sql.DatabaseMetaData#getDatabaseProductName()}.
97    */
 
98  369 toggle private DatabaseProduct(String productName)
99    {
100  369 this.productName = productName;
101    }
102   
103    /**
104    * @return the database product name. Example: "Oracle". The returned value should correspond to the value returned
105    * by {@link java.sql.DatabaseMetaData#getDatabaseProductName()}.
106    */
 
107  378291 toggle public String getProductName()
108    {
109  378288 return this.productName;
110    }
111   
 
112  6 toggle @Override
113    public boolean equals(Object object)
114    {
115  6 boolean result = false;
116  6 if ((object != null) && (object instanceof DatabaseProduct)) {
117  6 DatabaseProduct product = (DatabaseProduct) object;
118  6 if (product.getProductName().equals(getProductName())) {
119  6 result = true;
120    }
121    }
122   
123  6 return result;
124    }
125   
126    /**
127    * Transform a product name represented as a string into a {@link DatabaseProduct} object.
128    *
129    * @param productNameAsString the string to transform
130    * @return the {@link DatabaseProduct} object
131    */
 
132  126095 toggle public static DatabaseProduct toProduct(String productNameAsString)
133    {
134  126094 DatabaseProduct product;
135  126092 if (productNameAsString.equalsIgnoreCase(ORACLE.getProductName())) {
136  2 product = ORACLE;
137  126090 } else if (productNameAsString.equalsIgnoreCase(DERBY.getProductName())) {
138  1 product = DERBY;
139  126083 } else if (productNameAsString.equalsIgnoreCase(HSQLDB.getProductName())) {
140  126083 product = HSQLDB;
141  4 } else if (productNameAsString.equalsIgnoreCase(H2.getProductName())) {
142  1 product = H2;
143  3 } else if (productNameAsString.startsWith(DB2.getProductName())) {
144    // See documentation above on why we check starts with for DB2
145  1 product = DB2;
146  2 } else if (productNameAsString.equalsIgnoreCase(MYSQL.getProductName())) {
147  0 product = MYSQL;
148  2 } else if (productNameAsString.equalsIgnoreCase(POSTGRESQL.getProductName())) {
149  0 product = POSTGRESQL;
150  2 } else if (productNameAsString.equalsIgnoreCase(MSSQL.getProductName())) {
151  0 product = MSSQL;
152    } else {
153  2 product = UNKNOWN;
154    }
155   
156  126098 return product;
157    }
158   
 
159  0 toggle @Override
160    public int hashCode()
161    {
162  0 return getProductName().hashCode();
163    }
164   
 
165  0 toggle @Override
166    public String toString()
167    {
168  0 return getProductName();
169    }
170    }