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.ResultSet;
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
|
17
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
18
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
19
|
import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
|
20
|
import eu.etaxonomy.cdm.model.metadata.CdmMetaDataPropertyName;
|
21
|
|
22
|
/**
|
23
|
* Base class for updating a schema.
|
24
|
*
|
25
|
* @author a.mueller
|
26
|
* @since 10.09.2010
|
27
|
*
|
28
|
*/
|
29
|
public abstract class SchemaUpdaterBase
|
30
|
extends UpdaterBase<ISchemaUpdaterStep, ISchemaUpdater>
|
31
|
implements ISchemaUpdater {
|
32
|
|
33
|
@SuppressWarnings("unused")
|
34
|
private static final Logger logger = Logger.getLogger(SchemaUpdaterBase.class);
|
35
|
|
36
|
public static final boolean INCLUDE_AUDIT = true;
|
37
|
protected static final boolean INCLUDE_CDM_BASE = true;
|
38
|
protected static final boolean NOT_NULL = true;
|
39
|
protected static final boolean IS_LIST = true;
|
40
|
protected static final boolean IS_1_TO_M = true;
|
41
|
protected static final boolean IS_M_TO_M = false;
|
42
|
|
43
|
|
44
|
protected abstract List<ISchemaUpdaterStep> getUpdaterList();
|
45
|
|
46
|
|
47
|
protected SchemaUpdaterBase(String startSchemaVersion, String endSchemaVersion){
|
48
|
this.startVersion = startSchemaVersion;
|
49
|
this.targetVersion = endSchemaVersion;
|
50
|
list = getUpdaterList();
|
51
|
}
|
52
|
|
53
|
@Override
|
54
|
protected void updateVersion(ICdmDataSource datasource, IProgressMonitor monitor,
|
55
|
CaseType caseType, SchemaUpdateResult result) throws SQLException {
|
56
|
int intSchemaVersion = 0;
|
57
|
String sqlUpdateSchemaVersionOld = "UPDATE %s SET value = '" + this.targetVersion + "' WHERE propertyname = " + intSchemaVersion;
|
58
|
sqlUpdateSchemaVersionOld = String.format(sqlUpdateSchemaVersionOld, caseType.transformTo("CdmMetaData"));
|
59
|
String sqlUpdateSchemaVersion = "UPDATE %s SET value = '" + this.targetVersion + "' WHERE propertyname = '%s'";
|
60
|
sqlUpdateSchemaVersion = String.format(sqlUpdateSchemaVersion, caseType.transformTo("CdmMetaData"), CdmMetaDataPropertyName.DB_SCHEMA_VERSION.getKey());
|
61
|
|
62
|
boolean isPriorTo4_7 = CdmMetaData.compareVersion("4.6.0.0", this.targetVersion, 2, monitor) > 0;
|
63
|
|
64
|
String sql = isPriorTo4_7 ? sqlUpdateSchemaVersionOld : sqlUpdateSchemaVersion;
|
65
|
try {
|
66
|
int n = datasource.executeUpdate(sql);
|
67
|
if (n == 0){
|
68
|
result.addError("Schema version was not updated", "SchemaUpdaterBase.updateVersion()");
|
69
|
}
|
70
|
return;
|
71
|
|
72
|
} catch (Exception e) {
|
73
|
monitor.warning("Error when trying to set new schemaversion: ", e);
|
74
|
throw new SQLException(e);
|
75
|
}
|
76
|
}
|
77
|
|
78
|
@Override
|
79
|
protected String getCurrentVersion(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
|
80
|
String sqlSchemaVersion = caseType.replaceTableNames( "SELECT value FROM @@CdmMetaData@@ WHERE propertyname = 'SCHEMA_VERSION'");
|
81
|
try {
|
82
|
String value = (String)datasource.getSingleValue(sqlSchemaVersion);
|
83
|
return value;
|
84
|
} catch (Exception e) {
|
85
|
//looks like propertyname is still integer;
|
86
|
//ATTENTION: the below SQL returns all records if run against CdmMetaData with propertyname being a string
|
87
|
sqlSchemaVersion = caseType.replaceTableNames( "SELECT value FROM @@CdmMetaData@@ WHERE propertyname = 0 ORDER BY propertyname");
|
88
|
try {
|
89
|
ResultSet rs = datasource.executeQuery(sqlSchemaVersion);
|
90
|
boolean hasMoreThanOneRecord = false;
|
91
|
String result = null;
|
92
|
while(rs.next()){
|
93
|
if (hasMoreThanOneRecord){
|
94
|
String message = "Reading schema version from database returns more than 1 record";
|
95
|
monitor.warning(message);
|
96
|
throw new RuntimeException(message);
|
97
|
}
|
98
|
result = rs.getString("value");
|
99
|
}
|
100
|
if (result == null){
|
101
|
String message = "Reading schema version from database returned no result";
|
102
|
monitor.warning(message);
|
103
|
throw new RuntimeException(message);
|
104
|
}
|
105
|
return result;
|
106
|
} catch (SQLException e1) {
|
107
|
monitor.warning("Error when trying to receive schemaversion: ", e1);
|
108
|
throw e;
|
109
|
}
|
110
|
}
|
111
|
|
112
|
}
|
113
|
}
|