Project

General

Profile

Download (1.62 KB) Statistics
| Branch: | Tag: | Revision:
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
 * @since 7 Apr 2015
24
 *
25
 */
26
public class SQLUtils {
27

    
28
    @SuppressWarnings("unused")
29
    private static final Logger logger = Logger.getLogger(SQLUtils.class);
30

    
31
    public static String correctCase(final String databaseIdentifier, DatabaseMetaData databaseMetaData) throws SQLException
32
    {
33
        String resultIdentifier = databaseIdentifier;
34
        String dbIdentifierQuoteString = databaseMetaData.getIdentifierQuoteString();
35
        if(!isEscaped(databaseIdentifier, dbIdentifierQuoteString)){
36
            if(databaseMetaData.storesLowerCaseIdentifiers()) {
37
                resultIdentifier = databaseIdentifier.toLowerCase(Locale.ENGLISH);
38
            }
39
            else if(databaseMetaData.storesUpperCaseIdentifiers()) {
40
                resultIdentifier = databaseIdentifier.toUpperCase(Locale.ENGLISH);
41
            }
42
        }
43
        return resultIdentifier;
44

    
45
    }
46

    
47
    private static final boolean isEscaped(String tableName, String dbIdentifierQuoteString) {
48
        return tableName!=null && dbIdentifierQuoteString!= null && (tableName.startsWith(dbIdentifierQuoteString));
49
    }
50

    
51
}
(15-15/16)