Project

General

Profile

Download (1.59 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2015 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.vaadin.util;
11

    
12
import java.sql.DatabaseMetaData;
13
import java.sql.SQLException;
14
import java.util.Locale;
15

    
16
import org.apache.log4j.Logger;
17

    
18
/**
19
 *
20
 * The methods correctCase and isEscaped have been inspired from the
21
 * org.dbunit.util.SQLHelper class
22
 *
23
 * @author cmathew
24
 * @date 7 Apr 2015
25
 *
26
 */
27
public class SQLUtils {
28

    
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
}
(8-8/8)