ref #6719 fixing bug releated to sorting
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / util / SQLUtils.java
1 /**
2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.vaadin.util;
10
11 import java.sql.DatabaseMetaData;
12 import java.sql.SQLException;
13 import java.util.Locale;
14
15 import org.apache.log4j.Logger;
16
17 /**
18 *
19 * The methods correctCase and isEscaped have been inspired from the
20 * org.dbunit.util.SQLHelper class
21 *
22 * @author cmathew
23 * @date 7 Apr 2015
24 *
25 */
26 public class SQLUtils {
27
28 private static final Logger logger = Logger.getLogger(SQLUtils.class);
29
30 public static String correctCase(final String databaseIdentifier, DatabaseMetaData databaseMetaData) throws SQLException
31 {
32 String resultIdentifier = databaseIdentifier;
33 String dbIdentifierQuoteString = databaseMetaData.getIdentifierQuoteString();
34 if(!isEscaped(databaseIdentifier, dbIdentifierQuoteString)){
35 if(databaseMetaData.storesLowerCaseIdentifiers()) {
36 resultIdentifier = databaseIdentifier.toLowerCase(Locale.ENGLISH);
37 }
38 else if(databaseMetaData.storesUpperCaseIdentifiers()) {
39 resultIdentifier = databaseIdentifier.toUpperCase(Locale.ENGLISH);
40 }
41 }
42 return resultIdentifier;
43
44 }
45
46 private static final boolean isEscaped(String tableName, String dbIdentifierQuoteString) {
47 return tableName!=null && dbIdentifierQuoteString!= null && (tableName.startsWith(dbIdentifierQuoteString));
48 }
49
50 }