Project

General

Profile

Download (2.78 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
	public String transformTo(String camelCaseTableName){
32
		if (camelCaseTableName == null){
33
			return null;
34
		}else if (this == CamelCase){
35
			return camelCaseTableName;
36
		}else if (this == UpperCase){
37
			return camelCaseTableName.toUpperCase(Locale.ENGLISH);
38
		}else if (this == LowerCase){
39
			return camelCaseTableName.toLowerCase(Locale.ENGLISH);
40
		}else{
41
			throw new RuntimeException("Unhandled CaseType: " + this);
42
		}
43
	}
44
	
45

    
46
    /**
47
     * Defines the CaseType (camel, upper, lower) of a datasource depending on the case used for the CdmMetaData table.
48
     * @param datasource the datasource
49
     * @return the CaseType used
50
     */
51
    public static CaseType caseTypeOfDatasource(ICdmDataSource datasource) {
52
		String sql = "SELECT value FROM ";
53
    	
54
    	try {
55
			datasource.executeQuery(sql +  "CdmMetaData");
56
			return CaseType.CamelCase;
57
		} catch (SQLException e) {
58
			try {
59
				datasource.executeQuery(sql+  "CDMMETADATA");
60
				return CaseType.UpperCase;
61
			} catch (SQLException e1) {
62
				try {
63
					datasource.executeQuery(sql+ "cdmmetadata");
64
				} catch (SQLException e2) {
65
					throw new RuntimeException("Case type (camel, upper, lower) of the database could be defined. Maybe the CdmMetaData table is missing in the datasource", e2);
66
				}
67
				return CaseType.LowerCase;
68
			}
69
		}
70
	}
71

    
72

    
73
	/**
74
	 * Replaces all words marked with @@ at the beginning and end by the correctly cased name.
75
	 * E.g. it replaces <i>@@CdmMetaData@@</i> by <i>cdmmetadata</i> if {@link CaseType} is 
76
	 * {@link CaseType#LowerCase} 
77
	 * @param sql the original sql string with masked table names
78
	 * @return the corrected sql string
79
	 */
80
	public String replaceTableNames(String sql) {
81
		Pattern pattern = Pattern.compile("@@[a-zA-Z_]+@@");
82
		
83
		Matcher matcher = pattern.matcher(sql);
84
		while (matcher.find()){
85
			String newName = transformTo(matcher.group().replaceAll("@", ""));
86
			sql = sql.replace(matcher.group(), newName);
87
			matcher = pattern.matcher(sql);
88
		}
89
		
90
		return sql;
91
	}
92
	
93
}
(2-2/32)