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.CdmDatabaseException;
|
17
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
18
|
|
19
|
/**
|
20
|
* Tables may be stored in different cases: CamelCase (preferred), upper case (capital), or lower case,
|
21
|
* depending on the database preferences.
|
22
|
* This enumeration is defining the case used.
|
23
|
*
|
24
|
* @author a.mueller
|
25
|
*
|
26
|
*/
|
27
|
public enum CaseType {
|
28
|
CamelCase,
|
29
|
UpperCase,
|
30
|
LowerCase;
|
31
|
|
32
|
/**
|
33
|
* Transforms the camel case table name to the required case
|
34
|
* @param camelCaseTableName
|
35
|
* @return the transformed table name
|
36
|
*/
|
37
|
public String transformTo(String camelCaseTableName){
|
38
|
if (camelCaseTableName == null){
|
39
|
return null;
|
40
|
}else if (this == CamelCase){
|
41
|
return camelCaseTableName;
|
42
|
}else if (this == UpperCase){
|
43
|
return camelCaseTableName.toUpperCase(Locale.ENGLISH);
|
44
|
}else if (this == LowerCase){
|
45
|
return camelCaseTableName.toLowerCase(Locale.ENGLISH);
|
46
|
}else{
|
47
|
throw new RuntimeException("Unhandled CaseType: " + this);
|
48
|
}
|
49
|
}
|
50
|
|
51
|
|
52
|
/**
|
53
|
* Defines the CaseType (camel, upper, lower) of a datasource depending on the case used for the CdmMetaData table.
|
54
|
* @param datasource the datasource
|
55
|
* @return the CaseType used
|
56
|
*/
|
57
|
public static CaseType caseTypeOfDatasource(ICdmDataSource datasource) {
|
58
|
String sql = "SELECT value FROM ";
|
59
|
|
60
|
try {
|
61
|
datasource.executeQuery(sql + "CdmMetaData");
|
62
|
return CaseType.CamelCase;
|
63
|
} catch (SQLException e) {
|
64
|
try {
|
65
|
datasource.executeQuery(sql+ "CDMMETADATA");
|
66
|
return CaseType.UpperCase;
|
67
|
} catch (SQLException e1) {
|
68
|
try {
|
69
|
datasource.executeQuery(sql+ "cdmmetadata");
|
70
|
} catch (SQLException e2) {
|
71
|
throw new CdmDatabaseException("Case type (camel, upper, lower) of the database could be defined. Maybe the CdmMetaData table is missing in the datasource", e2);
|
72
|
}
|
73
|
return CaseType.LowerCase;
|
74
|
}
|
75
|
}
|
76
|
}
|
77
|
|
78
|
|
79
|
/**
|
80
|
* Replaces all words marked with @@ at the beginning and end by the correctly cased name.
|
81
|
* E.g. it replaces <i>@@CdmMetaData@@</i> by <i>cdmmetadata</i> if {@link CaseType} is
|
82
|
* {@link CaseType#LowerCase}
|
83
|
* @param sql the original sql string with masked table names
|
84
|
* @return the corrected sql string
|
85
|
*/
|
86
|
public String replaceTableNames(String sql) {
|
87
|
Pattern pattern = Pattern.compile("@@[a-zA-Z_]+@@");
|
88
|
|
89
|
Matcher matcher = pattern.matcher(sql);
|
90
|
while (matcher.find()){
|
91
|
String newName = transformTo(matcher.group().replaceAll("@", ""));
|
92
|
sql = sql.replace(matcher.group(), newName);
|
93
|
matcher = pattern.matcher(sql);
|
94
|
}
|
95
|
|
96
|
return sql;
|
97
|
}
|
98
|
|
99
|
}
|