Project

General

Profile

Revision 49f50a2d

ID49f50a2de8927beeb126321a88f304f53d12da74
Parent c47d1771
Child 45090e06

Added by Andreas Müller about 5 years ago

Minor

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/view/AuditEvent.java
41 41
	private UUID uuid;
42 42

  
43 43
    @Id
44
//    @GeneratedValue(strategy=GenerationType.AUTO)  // (generator = "custom-enhanced-table")  //see also CdmBase.id
44 45
    @GeneratedValue(generator = "custom-enhanced-table")  //see also CdmBase.id
45 46
    @RevisionNumber
46 47
    private Integer revisionNumber;
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/CaseType.java
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
}
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
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)