2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.database
.update
;
11 import java
.sql
.ResultSet
;
12 import java
.sql
.SQLException
;
13 import java
.util
.List
;
15 import org
.apache
.logging
.log4j
.LogManager
;import org
.apache
.logging
.log4j
.Logger
;
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
;
23 * Base class for updating a schema.
29 public abstract class SchemaUpdaterBase
30 extends UpdaterBase
<ISchemaUpdaterStep
, ISchemaUpdater
>
31 implements ISchemaUpdater
{
33 @SuppressWarnings("unused")
34 private static final Logger logger
= LogManager
.getLogger(SchemaUpdaterBase
.class);
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;
44 protected abstract List
<ISchemaUpdaterStep
> getUpdaterList();
47 protected SchemaUpdaterBase(String startSchemaVersion
, String endSchemaVersion
){
48 this.startVersion
= startSchemaVersion
;
49 this.targetVersion
= endSchemaVersion
;
50 list
= getUpdaterList();
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());
62 boolean isPriorTo4_7
= CdmMetaData
.compareVersion("4.6.0.0", this.targetVersion
, 2, monitor
) > 0;
64 String sql
= isPriorTo4_7 ? sqlUpdateSchemaVersionOld
: sqlUpdateSchemaVersion
;
66 int n
= datasource
.executeUpdate(sql
);
68 result
.addError("Schema version was not updated", "SchemaUpdaterBase.updateVersion()");
72 } catch (Exception e
) {
73 monitor
.warning("Error when trying to set new schemaversion: ", e
);
74 throw new SQLException(e
);
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'");
82 String value
= (String
)datasource
.getSingleValue(sqlSchemaVersion
);
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");
89 ResultSet rs
= datasource
.executeQuery(sqlSchemaVersion
);
90 boolean hasMoreThanOneRecord
= false;
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
);
98 result
= rs
.getString("value");
101 String message
= "Reading schema version from database returned no result";
102 monitor
.warning(message
);
103 throw new RuntimeException(message
);
106 } catch (SQLException e1
) {
107 monitor
.warning("Error when trying to receive schemaversion: ", e1
);
114 protected String
escape(String sql
){
115 return sql
== null ?
null : sql
.replace("'", "''");