Project

General

Profile

Download (2.81 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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.database.update;
10

    
11
import java.sql.SQLException;
12
import java.util.Locale;
13
import java.util.regex.Matcher;
14
import java.util.regex.Pattern;
15

    
16
import eu.etaxonomy.cdm.database.ICdmDataSource;
17

    
18
/**
19
 * Tables may be stored in different cases: CamelCase (preferred), upper case (capital), or lower case,
20
 * depending on the database preferences.
21
 * This enumeration is defining the case used.
22
 *
23
 * @author a.mueller
24
 *
25
 */
26
public enum CaseType {
27
	CamelCase,
28
	UpperCase,
29
	LowerCase;
30

    
31
	/**
32
	 * Transforms the camel case table name to the required case
33
	 * @param camelCaseTableName
34
	 * @return the transformed table name
35
	 */
36
	public String transformTo(String camelCaseTableName){
37
		if (camelCaseTableName == null){
38
			return null;
39
		}else if (this == CamelCase){
40
			return camelCaseTableName;
41
		}else if (this == UpperCase){
42
			return camelCaseTableName.toUpperCase(Locale.ENGLISH);
43
		}else if (this == LowerCase){
44
			return camelCaseTableName.toLowerCase(Locale.ENGLISH);
45
		}else{
46
			throw new RuntimeException("Unhandled CaseType: " + this);
47
		}
48
	}
49

    
50

    
51
    /**
52
     * Defines the CaseType (camel, upper, lower) of a datasource depending on the case used for the CdmMetaData table.
53
     * @param datasource the datasource
54
     * @return the CaseType used
55
     */
56
    public static CaseType caseTypeOfDatasource(ICdmDataSource datasource) {
57
		String sql = "SELECT value FROM ";
58

    
59
    	try {
60
			datasource.executeQuery(sql +  "CdmMetaData");
61
			return CaseType.CamelCase;
62
		} catch (SQLException e) {
63
			try {
64
				datasource.executeQuery(sql+  "CDMMETADATA");
65
				return CaseType.UpperCase;
66
			} catch (SQLException e1) {
67
				try {
68
					datasource.executeQuery(sql+ "cdmmetadata");
69
				} catch (SQLException e2) {
70
					throw new RuntimeException("Case type (camel, upper, lower) of the database could be defined. Maybe the CdmMetaData table is missing in the datasource", e2);
71
				}
72
				return CaseType.LowerCase;
73
			}
74
		}
75
	}
76

    
77

    
78
	/**
79
	 * Replaces all words marked with @@ at the beginning and end by the correctly cased name.
80
	 * E.g. it replaces <i>@@CdmMetaData@@</i> by <i>cdmmetadata</i> if {@link CaseType} is
81
	 * {@link CaseType#LowerCase}
82
	 * @param sql the original sql string with masked table names
83
	 * @return the corrected sql string
84
	 */
85
	public String replaceTableNames(String sql) {
86
		Pattern pattern = Pattern.compile("@@[a-zA-Z_]+@@");
87

    
88
		Matcher matcher = pattern.matcher(sql);
89
		while (matcher.find()){
90
			String newName = transformTo(matcher.group().replaceAll("@", ""));
91
			sql = sql.replace(matcher.group(), newName);
92
			matcher = pattern.matcher(sql);
93
		}
94

    
95
		return sql;
96
	}
97

    
98
}
(2-2/34)