Project

General

Profile

« Previous | Next » 

Revision 7d7355ba

Added by Andreas Müller over 8 years ago

integrate parameters into constructor for updaters

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/AuditedSchemaUpdaterStepBase.java
14 14
import eu.etaxonomy.cdm.database.ICdmDataSource;
15 15

  
16 16
/**
17
 * Base class for a {@link ISchemaUpdaterStep schema update step} which supports automated handling 
17
 * Base class for a {@link ISchemaUpdaterStep schema update step} which supports automated handling
18 18
 * of auditing tables.
19
 * 
19
 *
20 20
 * @see CdmUpdater
21 21
 * @see ISchemaUpdater
22
 * 
22
 *
23 23
 * @author a.mueller
24 24
 *
25 25
 * @param <T>
......
30 30
	protected boolean includeAudTable;
31 31
	protected boolean isAuditing;
32 32

  
33
	
34
	/**
35
	 * Constructor
36
	 * @param stepName
37
	 */
38
	protected AuditedSchemaUpdaterStepBase(String stepName) {
39
		super(stepName);
40
	}
33
    protected AuditedSchemaUpdaterStepBase(String stepName, boolean includedAudTable) {
34
        super(stepName);
35
        this.includeAudTable = includedAudTable;
36
    }
37

  
38
    protected AuditedSchemaUpdaterStepBase(String stepName, String tableName, boolean includedAudTable) {
39
        super(stepName);
40
        this.includeAudTable = includedAudTable;
41
        this.tableName = tableName;
42
    }
41 43

  
42 44
	@Override
43 45
	public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
......
51 53
		}
52 54
		return (result == true )? 0 : null;
53 55
	}
54
	
56

  
55 57
	/**
56 58
	 * Invoke the update on the given table of name tableName.
57 59
	 * @param tableName the tableName, already in the correct case
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/ClassBaseTypeUpdater.java
10 10
package eu.etaxonomy.cdm.database.update;
11 11

  
12 12
import java.util.ArrayList;
13
import java.util.Arrays;
14 13
import java.util.List;
15 14

  
16
import org.apache.commons.lang.StringUtils;
17 15
import org.apache.log4j.Logger;
18 16

  
19 17
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
......
29 27
public class ClassBaseTypeUpdater extends AuditedSchemaUpdaterStepBase<ClassBaseTypeUpdater> implements ISchemaUpdaterStep {
30 28
	@SuppressWarnings("unused")
31 29
	private static final Logger logger = Logger.getLogger(TableCreator.class);
32
	
30

  
33 31
	private static final boolean SORT_INDEX = true;
34 32

  
35
	private boolean includeIdentifiableEntity;
36
	private boolean includeAnnotatableEntity;
33
	private final boolean includeIdentifiableEntity;
34
	private final boolean includeAnnotatableEntity;
37 35
	protected List<ISchemaUpdaterStep> mnTablesStepList = new ArrayList<ISchemaUpdaterStep>();
38 36
	protected List<ISchemaUpdaterStep> columnAdderStepList = new ArrayList<ISchemaUpdaterStep>();
39 37

  
40
	
38

  
41 39
	public static final ClassBaseTypeUpdater NewVersionableToAnnotatableInstance(String stepName, String tableName, boolean includeAudTable){
42 40
		return new ClassBaseTypeUpdater(stepName, tableName, includeAudTable, true, false);
43 41
	}
......
47 45
	public static final ClassBaseTypeUpdater NewVersionableToIdentifiableInstance(String stepName, String tableName, boolean includeAudTable){
48 46
		return new ClassBaseTypeUpdater(stepName, tableName, includeAudTable, true, true);
49 47
	}
50
	
48

  
51 49
	protected ClassBaseTypeUpdater(String stepName, String tableName, boolean includeAudit, boolean includeAnnotatable, boolean includeIdentifiable) {
52
		super(stepName);
53
		this.tableName = tableName;
54
		this.includeAudTable = includeAudit;
50
		super(stepName, tableName, includeAudit);
55 51
		this.includeAnnotatableEntity = includeAnnotatable;
56 52
		this.includeIdentifiableEntity = includeIdentifiable;
57 53
		TableCreator.makeMnTables(mnTablesStepList, tableName, includeAnnotatable, includeIdentifiable);
58 54
		makeColumns();
59 55
	}
60 56

  
61
	
57

  
62 58
	private void makeColumns() {
63 59
		String innerStepName;
64 60
		String newColumnName;
65 61
		ColumnAdder adder;
66 62
		if (this.includeIdentifiableEntity){
67
			
63

  
68 64
			//lsid authority
69 65
			innerStepName = "-add lsid_authority";
70 66
			newColumnName = "lsid_authority";
71
			adder = ColumnAdder.NewStringInstance(stepName + innerStepName, tableName, 
67
			adder = ColumnAdder.NewStringInstance(stepName + innerStepName, tableName,
72 68
					newColumnName, SchemaUpdaterBase.INCLUDE_AUDIT);
73 69
			this.columnAdderStepList.add(adder);
74
			
70

  
75 71
			//lsid lsid
76 72
			innerStepName = "-add lsid_lsid";
77 73
			newColumnName = "lsid_lsid";
78
			adder = ColumnAdder.NewStringInstance(stepName + innerStepName, tableName, 
74
			adder = ColumnAdder.NewStringInstance(stepName + innerStepName, tableName,
79 75
					newColumnName, SchemaUpdaterBase.INCLUDE_AUDIT);
80 76
			this.columnAdderStepList.add(adder);
81
			
77

  
82 78
			//lsid namespace
83 79
			innerStepName = "-add lsid_namespace";
84 80
			newColumnName = "lsid_namespace";
85
			adder = ColumnAdder.NewStringInstance(stepName + innerStepName, tableName, 
81
			adder = ColumnAdder.NewStringInstance(stepName + innerStepName, tableName,
86 82
					newColumnName, SchemaUpdaterBase.INCLUDE_AUDIT);
87 83
			this.columnAdderStepList.add(adder);
88
			
84

  
89 85
			//lsid object
90 86
			innerStepName = "-add lsid_object";
91 87
			newColumnName = "lsid_object";
92
			adder = ColumnAdder.NewStringInstance(stepName + innerStepName, tableName, 
88
			adder = ColumnAdder.NewStringInstance(stepName + innerStepName, tableName,
93 89
					newColumnName, SchemaUpdaterBase.INCLUDE_AUDIT);
94 90
			this.columnAdderStepList.add(adder);
95
			
91

  
96 92
			//lsid revision
97 93
			innerStepName = "-add lsid_revision";
98 94
			newColumnName = "lsid_revision";
99
			adder = ColumnAdder.NewStringInstance(stepName + innerStepName, tableName, 
95
			adder = ColumnAdder.NewStringInstance(stepName + innerStepName, tableName,
100 96
					newColumnName, SchemaUpdaterBase.INCLUDE_AUDIT);
101 97
			this.columnAdderStepList.add(adder);
102
			
98

  
103 99
			//protected title cache
104 100
			innerStepName = "-add protected title cache";
105 101
			newColumnName = "protectedTitleCache";
106 102
			adder = ColumnAdder.NewBooleanInstance(innerStepName, tableName, newColumnName,
107 103
					SchemaUpdaterBase.INCLUDE_AUDIT, false);
108
			
109
			adder = ColumnAdder.NewStringInstance(stepName + innerStepName, tableName, 
104

  
105
			adder = ColumnAdder.NewStringInstance(stepName + innerStepName, tableName,
110 106
					newColumnName, SchemaUpdaterBase.INCLUDE_AUDIT);
111
			this.columnAdderStepList.add(adder);			
112
			
107
			this.columnAdderStepList.add(adder);
108

  
113 109
			//title cache
114 110
			innerStepName = "-add titleCache";
115 111
			newColumnName = "titleCache";
116
			adder = ColumnAdder.NewStringInstance(stepName + innerStepName, tableName, 
112
			adder = ColumnAdder.NewStringInstance(stepName + innerStepName, tableName,
117 113
					newColumnName, SchemaUpdaterBase.INCLUDE_AUDIT);
118 114
			this.columnAdderStepList.add(adder);
119 115
		}
120
		
116

  
121 117
	}
122 118
	@Override
123 119
	protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType)  {
124 120
		//we only do have inner steps here
125 121
		return true;
126 122
	}
127
	
123

  
128 124

  
129 125
	@Override
130 126
	public List<ISchemaUpdaterStep> getInnerSteps() {
......
133 129
		result.addAll(columnAdderStepList);
134 130
		return result;
135 131
	}
136
	
132

  
137 133
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/ClassChanger.java
1 1
// $Id$
2 2
/**
3 3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
4
* European Distributed Institute of Taxonomy
5 5
* http://www.e-taxonomy.eu
6
* 
6
*
7 7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
* See LICENSE.TXT at the top of this package for the full license terms.
9 9
*/
......
24 24
public class ClassChanger extends AuditedSchemaUpdaterStepBase<ClassChanger> implements ISchemaUpdaterStep {
25 25
	private static final Logger logger = Logger.getLogger(ClassChanger.class);
26 26

  
27
	private String newClassName;
28
	private String[] oldClassNames;
29
	private boolean isIdentifiable;
30
	private boolean isAnnotatable;
31
	private boolean isSourcable;
27
	private final String newClassName;
28
	private final String[] oldClassNames;
29
	private final boolean isIdentifiable;
30
	private final boolean isAnnotatable;
31
	private final boolean isSourcable;
32 32

  
33 33
	public static final ClassChanger NewIdentifiableInstance(String stepName, String tableName, String newClassNamePath, String[] oldClassNames, boolean includeAudTable){
34 34
		return new ClassChanger(stepName, tableName, newClassNamePath, oldClassNames, includeAudTable, true, true, true);
......
39 39
	public static final ClassChanger NewDescriptionElementInstance(String stepName, String tableName, String newClassNamePath, String[] oldClassNames, boolean includeAudTable){
40 40
		return new ClassChanger(stepName, tableName, newClassNamePath, oldClassNames, includeAudTable, true, true, false);
41 41
	}
42
	
43
	
42

  
43

  
44 44
	protected ClassChanger(String stepName, String tableName, String newClassName, String[] oldClassNames, boolean includeAudTable, boolean isAnnotatable, boolean isSourcable, boolean isIdentifiable) {
45
		super(stepName);
46
		this.tableName = tableName;
45
		super(stepName, tableName, includeAudTable);
47 46
		this.newClassName = newClassName;
48 47
		this.oldClassNames = oldClassNames;
49
		this.includeAudTable = includeAudTable;
50 48
		this.isIdentifiable = isIdentifiable;
51 49
		this.isAnnotatable = isAnnotatable;
52 50
		this.isSourcable = isSourcable;
......
60 58
				String updateQuery = getDtypeUpdateQueryString(tableName, datasource, monitor);
61 59
				datasource.executeUpdate(updateQuery);
62 60
			}
63
			
61

  
64 62
			if (isAnnotatable){
65 63
				updateAnnotatables(tableName, datasource, monitor, caseType);
66 64
			}
67 65
			if (isSourcable){
68 66
				updateSourcable(tableName, datasource, monitor, caseType);
69 67
			}
70
			
68

  
71 69
			if (isIdentifiable){
72 70
				updateIdentifiables(tableName, datasource, monitor, caseType);
73 71
			}
74
			
72

  
75 73
			return result;
76 74
		} catch ( Exception e) {
77 75
			monitor.warning(e.getMessage(), e);
......
94 92
		String sql = " UPDATE %s " +
95 93
				" SET %s = '%s' " +
96 94
				" WHERE %s = '%s'";
97
		
95

  
98 96
		for (String oldClassPath : oldClassNames){
99
			String query = String.format(sql, caseType.transformTo(extensionClass), 
100
					typeAttr, newClassName, 
97
			String query = String.format(sql, caseType.transformTo(extensionClass),
98
					typeAttr, newClassName,
101 99
					typeAttr, oldClassPath);
102 100
			datasource.executeUpdate(query);
103 101
		}
104 102
	}
105 103

  
106
	
104

  
107 105
	public String getDtypeUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
108 106
		String updateQuery;
109
		updateQuery = " UPDATE @tableName " + 
107
		updateQuery = " UPDATE @tableName " +
110 108
				" SET DTYPE = '@newTableName' " +
111 109
				" WHERE (1=0 @dtypes)";
112
		
110

  
113 111
		updateQuery = updateQuery.replace("@tableName", tableName);
114 112
		updateQuery = updateQuery.replace("@newTableName", getSimpleName(newClassName));
115 113
		String dtypes = "";
......
117 115
			dtypes += String.format(" OR DTYPE = '%s' ", getSimpleName(oldClassName)) ;
118 116
		}
119 117
		updateQuery = updateQuery.replace("@dtypes", dtypes);
120
		
118

  
121 119
		return updateQuery;
122 120
	}
123 121
	private String getSimpleName(String className) {
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/ColumnAdder.java
1 1
// $Id$
2 2
/**
3 3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
4
* European Distributed Institute of Taxonomy
5 5
* http://www.e-taxonomy.eu
6
* 
6
*
7 7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
* See LICENSE.TXT at the top of this package for the full license terms.
9 9
*/
......
24 24
 */
25 25
public class ColumnAdder extends AuditedSchemaUpdaterStepBase<ColumnAdder> implements ISchemaUpdaterStep {
26 26
	private static final Logger logger = Logger.getLogger(ColumnAdder.class);
27
	
28
	private String newColumnName;
29
	private String columnType;
30
	private Object defaultValue;
27

  
28
	private final String newColumnName;
29
	private final String columnType;
30
	private final Object defaultValue;
31 31
	private boolean isNotNull;
32 32

  
33
	private String referencedTable;
33
	private final String referencedTable;
34 34

  
35 35
	public static final ColumnAdder NewIntegerInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable, boolean notNull, String referencedTable){
36 36
		return new ColumnAdder(stepName, tableName, newColumnName, "int", includeAudTable, null, notNull, referencedTable);
37 37
	}
38
	
38

  
39 39
	public static final ColumnAdder NewIntegerInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable, Integer defaultValue, boolean notNull){
40 40
		return new ColumnAdder(stepName, tableName, newColumnName, "int", includeAudTable, defaultValue, notNull, null);
41 41
	}
42
	
42

  
43 43
	public static final ColumnAdder NewTinyIntegerInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable, boolean notNull){
44 44
		return new ColumnAdder(stepName, tableName, newColumnName, "tinyint", includeAudTable, null, notNull, null);
45 45
	}
46
	
46

  
47 47
	public static final ColumnAdder NewDoubleInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable, boolean notNull){
48 48
		return new ColumnAdder(stepName, tableName, newColumnName, "double", includeAudTable, null, notNull, null);
49 49
	}
......
51 51
	public static final ColumnAdder NewBooleanInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable, Boolean defaultValue){
52 52
		return new ColumnAdder(stepName, tableName, newColumnName, "bit", includeAudTable, defaultValue, false, null);
53 53
	}
54
	
54

  
55 55
	public static final ColumnAdder NewStringInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable){
56 56
		return new ColumnAdder(stepName, tableName, newColumnName, "nvarchar(255)", includeAudTable, null, false, null);
57 57
	}
......
63 63
	public static final ColumnAdder NewClobInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable){
64 64
		return new ColumnAdder(stepName, tableName, newColumnName, "clob", includeAudTable, null, false, null);
65 65
	}
66
	
66

  
67 67
	public static final ColumnAdder NewDateTimeInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable, boolean notNull){
68 68
		return new ColumnAdder(stepName, tableName, newColumnName, "datetime", includeAudTable, null, notNull, null);
69 69
	}
70
	
70

  
71 71
	protected ColumnAdder(String stepName, String tableName, String newColumnName, String columnType, boolean includeAudTable, Object defaultValue, boolean notNull, String referencedTable) {
72
		super(stepName);
73
		this.tableName = tableName;
72
		super(stepName, tableName, includeAudTable);
74 73
		this.newColumnName = newColumnName;
75 74
		this.columnType = columnType;
76
		this.includeAudTable = includeAudTable;
77 75
		this.defaultValue = defaultValue;
78 76
		this.isNotNull = notNull;
79 77
		this.referencedTable = referencedTable;
......
90 88
		try {
91 89
			String updateQuery = getUpdateQueryString(tableName, datasource, monitor);
92 90
			datasource.executeUpdate(updateQuery);
93
			
91

  
94 92
			if (defaultValue instanceof Boolean){
95 93
				String defaultValueQuery = "UPDATE @tableName SET @columnName = " + (defaultValue == null ? "NULL" : getBoolean((Boolean) defaultValue, datasource));
96 94
				defaultValueQuery = defaultValueQuery.replace("@tableName", tableName);
......
107 105
			if (referencedTable != null){
108 106
				result &= TableCreator.makeForeignKey(tableName, datasource, monitor, newColumnName, referencedTable, caseType);
109 107
			}
110
			
108

  
111 109
			return result;
112 110
		} catch ( Exception e) {
113 111
			monitor.warning(e.getMessage(), e);
......
117 115
	}
118 116

  
119 117
	/**
120
	 * Returns the update query string. tableName must already be cased correctly. See {@link CaseType}. 
118
	 * Returns the update query string. tableName must already be cased correctly. See {@link CaseType}.
121 119
	 * @param tableName correctly cased table name
122 120
	 * @param datasource data source
123 121
	 * @param monitor monitor
124
	 * @return the query string 
122
	 * @return the query string
125 123
	 * @throws DatabaseTypeNotSupportedException
126 124
	 */
127 125
	public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
......
147 145
		updateQuery = updateQuery.replace("@columnName", newColumnName);
148 146
		updateQuery = updateQuery.replace("@columnType", databaseColumnType);
149 147
		updateQuery = updateQuery.replace("@addSeparator", getAddColumnSeperator(datasource));
150
		
148

  
151 149
		return updateQuery;
152 150
	}
153 151

  
......
177 175
		}
178 176
		return result;
179 177
	}
180
	
178

  
181 179

  
182 180
	/**
183 181
	 * Returns the sql keywords for adding a column. This is usually 'ADD' or 'ADD COLUMN'
......
199 197
	public String getReferencedTable() {
200 198
		return referencedTable;
201 199
	}
202
	
200

  
203 201

  
204 202
	public String getNewColumnName() {
205 203
		return newColumnName;
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/ColumnNameChanger.java
1 1
// $Id$
2 2
/**
3 3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
4
* European Distributed Institute of Taxonomy
5 5
* http://www.e-taxonomy.eu
6
* 
6
*
7 7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
* See LICENSE.TXT at the top of this package for the full license terms.
9 9
*/
......
22 22
 */
23 23
public class ColumnNameChanger extends AuditedSchemaUpdaterStepBase<ColumnNameChanger> implements ISchemaUpdaterStep {
24 24
	private static final Logger logger = Logger.getLogger(ColumnNameChanger.class);
25
	
26
	private String newColumnName;
27
	private String oldColumnName;
28
	private Datatype datatype; //TODO make enum
29
	
25

  
26
	private final String newColumnName;
27
	private final String oldColumnName;
28
	private final Datatype datatype; //TODO make enum
29

  
30 30
	private enum Datatype{
31 31
		integer,
32 32
		clob
33 33
	}
34
	
34

  
35 35
	public static ColumnNameChanger NewIntegerInstance(String stepName, String tableName, String oldColumnName, String newColumnName, boolean includeAudTable){
36 36
		return new ColumnNameChanger(stepName, tableName, oldColumnName, newColumnName, includeAudTable, null, Datatype.integer);
37 37
	}
38
	
38

  
39 39
	public static ColumnNameChanger NewClobInstance(String stepName, String tableName, String oldColumnName, String newColumnName, boolean includeAudTable){
40 40
		return new ColumnNameChanger(stepName, tableName, oldColumnName, newColumnName, includeAudTable, null, Datatype.clob);
41 41
	}
42 42

  
43 43
	protected ColumnNameChanger(String stepName, String tableName, String oldColumnName, String newColumnName, boolean includeAudTable, Object defaultValue, Datatype datatype) {
44
		super(stepName);
45
		this.tableName = tableName;
44
		super(stepName, tableName, includeAudTable);
46 45
		this.newColumnName = newColumnName;
47 46
		this.oldColumnName = oldColumnName;
48
		this.includeAudTable = includeAudTable;
49 47
		this.datatype = datatype;
50 48
	}
51 49

  
......
55 53
			boolean result = true;
56 54
			DatabaseTypeEnum type = datasource.getDatabaseType();
57 55
			String updateQuery;
58
			
56

  
59 57
			if (type.equals(DatabaseTypeEnum.SqlServer2005)){
60 58
				logger.warn("SQLServer column name changer syntax not yet tested");
61 59
				updateQuery = "EXEC sp_rename '@oldName', '@newName'";
......
77 75
			updateQuery = updateQuery.replace("@newColumnName", newColumnName);
78 76
			updateQuery = updateQuery.replace("@definition", getDefinition());
79 77
			datasource.executeUpdate(updateQuery);
80
			
78

  
81 79
			return result;
82 80
		} catch (Exception e) {
83 81
			monitor.warning(e.getMessage(), e);
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/ColumnRemover.java
1 1
// $Id$
2 2
/**
3 3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
4
* European Distributed Institute of Taxonomy
5 5
* http://www.e-taxonomy.eu
6
* 
6
*
7 7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
* See LICENSE.TXT at the top of this package for the full license terms.
9 9
*/
......
22 22
 */
23 23
public class ColumnRemover extends AuditedSchemaUpdaterStepBase<ColumnRemover> implements ISchemaUpdaterStep {
24 24
	private static final Logger logger = Logger.getLogger(ColumnRemover.class);
25
	
26
	private String oldColumnName;
27
	
25

  
26
	private final String oldColumnName;
27

  
28 28
	public static final ColumnRemover NewInstance(String stepName, String tableName, String oldColumnName, boolean includeAudTable){
29 29
		return new ColumnRemover(stepName, tableName, oldColumnName, includeAudTable);
30 30
	}
31 31

  
32
	
32

  
33 33
	protected ColumnRemover(String stepName, String tableName, String oldColumnName, boolean includeAudTable) {
34
		super(stepName);
35
		this.tableName = tableName;
34
		super(stepName, tableName, includeAudTable);
36 35
		this.oldColumnName = oldColumnName;
37
		this.includeAudTable = includeAudTable;
38 36
	}
39 37

  
40 38
	@Override
......
54 52
	public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
55 53
		String updateQuery;
56 54
		DatabaseTypeEnum type = datasource.getDatabaseType();
57
		
55

  
58 56
		updateQuery = "ALTER TABLE @tableName DROP COLUMN @columnName";
59 57
		if (type.equals(DatabaseTypeEnum.SqlServer2005)){
60 58
			//MySQL allows both syntaxes
......
69 67
		}
70 68
		updateQuery = updateQuery.replace("@tableName", tableName);
71 69
		updateQuery = updateQuery.replace("@columnName", oldColumnName);
72
		
70

  
73 71
		return updateQuery;
74 72
	}
75 73

  
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/ColumnTypeChanger.java
1 1
// $Id$
2 2
/**
3 3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
4
* European Distributed Institute of Taxonomy
5 5
* http://www.e-taxonomy.eu
6
* 
6
*
7 7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
* See LICENSE.TXT at the top of this package for the full license terms.
9 9
*/
......
22 22
 */
23 23
public class ColumnTypeChanger extends AuditedSchemaUpdaterStepBase<ColumnTypeChanger> implements ISchemaUpdaterStep {
24 24
	private static final Logger logger = Logger.getLogger(ColumnTypeChanger.class);
25
	
26
	private String columnName;
27
	private String newColumnType;
28
	private Object defaultValue;
29
	private boolean isNotNull;
30
	private String referencedTable;
25

  
26
	private final String columnName;
27
	private final String newColumnType;
28
	private final Object defaultValue;
29
	private final boolean isNotNull;
30
	private final String referencedTable;
31 31

  
32 32

  
33 33
	public static final ColumnTypeChanger NewStringSizeInstance(String stepName, String tableName, String columnName, int newSize, boolean includeAudTable){
34 34
		return new ColumnTypeChanger(stepName, tableName, columnName, "nvarchar("+newSize+")", includeAudTable, null, false, null);
35 35
	}
36
	
36

  
37 37
	public static final ColumnTypeChanger NewClobInstance(String stepName, String tableName, String columnName, boolean includeAudTable){
38 38
		return new ColumnTypeChanger(stepName, tableName, columnName, "clob", includeAudTable, null, false, null);
39 39
	}
40
	
40

  
41 41
	public static final ColumnTypeChanger NewInt2DoubleInstance(String stepName, String tableName, String columnName, boolean includeAudTable){
42 42
		return new ColumnTypeChanger(stepName, tableName, columnName, "double", includeAudTable, null, false, null);
43 43
	}
......
45 45
	public static final ColumnTypeChanger NewInt2StringInstance(String stepName, String tableName, String columnName, int size, boolean includeAudTable, Integer defaultValue, boolean notNull){
46 46
		return new ColumnTypeChanger(stepName, tableName, columnName, "nvarchar("+size+")", includeAudTable, defaultValue, notNull, null);
47 47
	}
48
	
48

  
49 49
//	public static final ColumnTypeChanger NewChangeAllowNullOnStringChanger(){
50
//		
50
//
51 51
//	}
52
	
53
	
52

  
53

  
54 54
	protected ColumnTypeChanger(String stepName, String tableName, String columnName, String newColumnType, boolean includeAudTable, Object defaultValue, boolean notNull, String referencedTable) {
55
		super(stepName);
56
		this.tableName = tableName;
55
		super(stepName, tableName, includeAudTable);
57 56
		this.columnName = columnName;
58 57
		this.newColumnType = newColumnType;
59
		this.includeAudTable = includeAudTable;
60 58
		this.defaultValue = defaultValue;
61 59
		this.isNotNull = notNull;
62 60
		this.referencedTable = referencedTable;
......
66 64
	protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) {
67 65
		boolean result = true;
68 66
		try {
69
			
67

  
70 68
			String updateQuery;
71 69
			if (this.isNotNull){
72 70
				updateQuery = getNotNullUpdateQuery(tableName, datasource, monitor);
73 71
				datasource.executeUpdate(updateQuery);
74 72
			}
75
			
73

  
76 74
			updateQuery = getUpdateQueryString(tableName, datasource, monitor);
77 75
			datasource.executeUpdate(updateQuery);
78
			
76

  
79 77
			if (defaultValue instanceof Boolean){
80 78
				updateQuery = "UPDATE @tableName SET @columnName = " + (defaultValue == null ? "null" : getBoolean((Boolean) defaultValue, datasource));
81 79
				updateQuery = updateQuery.replace("@tableName", tableName);
......
85 83
			if (referencedTable != null){
86 84
				result &= TableCreator.makeForeignKey(tableName, datasource, monitor, columnName, referencedTable, caseType);
87 85
			}
88
				
86

  
89 87
			return result;
90 88
		} catch ( Exception e) {
91 89
			monitor.warning(e.getMessage(), e);
......
141 139
		updateQuery = updateQuery.replace("@columnName", columnName);
142 140
		updateQuery = updateQuery.replace("@columnType", databaseColumnType);
143 141
//		updateQuery = updateQuery.replace("@addSeparator", getAddColumnSeperator(datasource));
144
		
142

  
145 143
		return updateQuery;
146 144
	}
147 145

  
148 146
	private String getDatabaseColumnType(ICdmDataSource datasource, String columnType) {
149 147
		return ColumnAdder.getDatabaseColumnType(datasource, columnType);
150 148
	}
151
	
149

  
152 150
	public String getReferencedTable() {
153 151
		return referencedTable;
154 152
	}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/SimpleSchemaUpdaterStep.java
1 1
// $Id$
2 2
/**
3 3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
4
* European Distributed Institute of Taxonomy
5 5
* http://www.e-taxonomy.eu
6
* 
6
*
7 7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
* See LICENSE.TXT at the top of this package for the full license terms.
9 9
*/
......
21 21
import eu.etaxonomy.cdm.database.ICdmDataSource;
22 22

  
23 23
/**
24
 * This class represents one step in a schema update. 
24
 * This class represents one step in a schema update.
25 25
 * @author a.mueller
26 26
 * @date 13.09.2010
27 27
 *
28 28
 */
29 29
public class SimpleSchemaUpdaterStep extends SchemaUpdaterStepBase<SimpleSchemaUpdaterStep> implements ISchemaUpdaterStep, ITermUpdaterStep{
30 30
	private static final Logger logger = Logger.getLogger(SimpleSchemaUpdaterStep.class);
31
	
32
	private Map<DatabaseTypeEnum, String> queryMap = new HashMap<DatabaseTypeEnum, String>();
33
	private Map<DatabaseTypeEnum, String> auditQueryMap = new HashMap<DatabaseTypeEnum, String>();
34
	
31

  
32
	private final Map<DatabaseTypeEnum, String> queryMap = new HashMap<DatabaseTypeEnum, String>();
33
	private final Map<DatabaseTypeEnum, String> auditQueryMap = new HashMap<DatabaseTypeEnum, String>();
34

  
35 35
	private boolean includeAudit = false;
36 36
//	private String tableName;
37
	
37

  
38 38
// *************************** FACTORY ********************************/
39
	
39

  
40 40
	/**
41
	 * @deprecated use  {@link #NewNonAuditedInstance(String, String)}, 
42
	 * {@link #NewAuditedInstance(String, String, boolean, String)}, 
41
	 * @deprecated use  {@link #NewNonAuditedInstance(String, String)},
42
	 * {@link #NewAuditedInstance(String, String, boolean, String)},
43 43
	 * or {@link #NewExplicitAuditedInstance(String, String, String)} instead
44 44
	 */
45 45
	@Deprecated
46 46
	public static SimpleSchemaUpdaterStep NewInstance(String stepName, String defaultQuery, int adapt){
47 47
		return new SimpleSchemaUpdaterStep(stepName, defaultQuery, false, null, null);
48 48
	}
49
	
49

  
50 50
	public static SimpleSchemaUpdaterStep NewNonAuditedInstance(String stepName, String defaultQuery, int adapt){
51 51
		return new SimpleSchemaUpdaterStep(stepName, defaultQuery, false, null, null);
52 52
	}
......
54 54
	/**
55 55
	 * @param stepName Step name
56 56
	 * @param defaultQuery query
57
	 * @param nonAuditedTableName the name of the non audited table. E.g. TaxonNameBase (whild TaxonNameBase_AUD is the audited table
57
	 * @param nonAuditedTableName the name of the non audited table. E.g. TaxonNameBase
58
	 *     (while TaxonNameBase_AUD is the audited table
58 59
	 * @param adapt preliminary
59 60
	 * @return
60 61
	 */
......
68 69
		return new SimpleSchemaUpdaterStep(stepName, defaultQuery, audit, null, defaultQueryForAuditedTables);
69 70
	}
70 71

  
71
	
72

  
72 73
//************************ CONSTRUCTOR ***********************************/
73 74
	private SimpleSchemaUpdaterStep(String stepName, String defaultQuery, boolean includeAudit, String tableName, String defaultQueryForAuditedTables){
74 75
		super(stepName);
75 76
		this.includeAudit = includeAudit;
76 77
		queryMap.put(null, defaultQuery);
77
		
78

  
78 79
		if (includeAudit){
79 80
			if (StringUtils.isNotBlank(defaultQueryForAuditedTables)){
80 81
				auditQueryMap.put(null, defaultQueryForAuditedTables);
......
83 84
			}
84 85
		}
85 86
	}
86
	
87
// *************************** INVOKE *****************************	
87

  
88
// *************************** INVOKE *****************************
89

  
88 90

  
89 91

  
90
	
91 92
	@Override
92 93
	public Integer invoke (ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType){
93 94
		boolean result = true;
94
		
95

  
95 96
		//non audit
96 97
		result &= invokeQueryMap(datasource, queryMap, caseType); ;
97 98
		//audit
......
100 101
		}else{
101 102
			logger.info("SimpleSchemaUpdaterStep non Audited");
102 103
		}
103
		
104

  
104 105
		return (result == true )? 0 : null;
105 106
	}
106 107

  
......
136 137
		}
137 138
		return true;
138 139
	}
139
	
140

  
140 141
	private void makeAuditedQuery(DatabaseTypeEnum dbType, String tableName){
141 142
		String nonAuditQuery = this.queryMap.get(dbType);
142 143
		if (StringUtils.isBlank(nonAuditQuery)){
......
148 149
	}
149 150

  
150 151
//********************************* DELEGATES *********************************/
151
	
152

  
152 153
	/**
153 154
	 * For certain database types one may define special queries.<BR>
154 155
	 * Don't forget to put case-mask (@@) for table names
155 156
	 * @param dbType database type
156 157
	 * @param query query to use for the given database type.
157
	 * @return this schema updater step 
158
	 * @return this schema updater step
158 159
	 */
159 160
	public SimpleSchemaUpdaterStep put(DatabaseTypeEnum dbType, String query) {
160 161
		queryMap.put(dbType, query);
......
174 175
		makeAuditedQuery(null, nonAuditedTableName);
175 176
		return this;
176 177
	}
177
	
178

  
178 179
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/TableCreator.java
1 1
// $Id$
2 2
/**
3 3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
4
* European Distributed Institute of Taxonomy
5 5
* http://www.e-taxonomy.eu
6
* 
6
*
7 7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
* See LICENSE.TXT at the top of this package for the full license terms.
9 9
*/
......
29 29
 */
30 30
public class TableCreator extends AuditedSchemaUpdaterStepBase<TableCreator> implements ISchemaUpdaterStep {
31 31
	private static final Logger logger = Logger.getLogger(TableCreator.class);
32
	
32

  
33 33
	private static final boolean SORT_INDEX = true;
34
	
35
	private List<String> columnNames;
36
	private List<String> columnTypes;
37
	private List<Object> defaultValues;
38
	private List<Boolean> isNotNull;
39
	private List<String> referencedTables;
40
	private boolean includeCdmBaseAttributes;
41
	private boolean includeIdentifiableEntity;
42
	private boolean includeAnnotatableEntity;
34

  
35
	private final List<String> columnNames;
36
	private final List<String> columnTypes;
37
	private final List<Object> defaultValues;
38
	private final List<Boolean> isNotNull;
39
	private final List<String> referencedTables;
40
	private final boolean includeCdmBaseAttributes;
41
	private final boolean includeIdentifiableEntity;
42
	private final boolean includeAnnotatableEntity;
43 43
	private boolean includeEventBase;
44
	private boolean excludeVersionableAttributes;
44
	private final boolean excludeVersionableAttributes;
45 45
	protected List<ColumnAdder> columnAdders = new ArrayList<ColumnAdder>();
46 46
	protected List<ISchemaUpdaterStep> mnTablesStepList = new ArrayList<ISchemaUpdaterStep>();
47 47
	private String primaryKeyParams;
......
49 49
	private String uniqueParams;
50 50
	private String uniqueParams_AUD;
51 51

  
52
	
52

  
53 53
//	public static final TableCreator NewInstance(String stepName, String tableName, List<String> columnNames, List<String> columnTypes, List<Object> defaultValues, List<Boolean> isNull, boolean includeAudTable){
54 54
	public static final TableCreator NewInstance(String stepName, String tableName, List<String> columnNames, List<String> columnTypes, boolean includeAudTable, boolean includeCdmBaseAttributes){
55 55
		return new TableCreator(stepName, tableName, columnNames, columnTypes, null, null, null, includeAudTable, includeCdmBaseAttributes, false, false, false);
56 56
	}
57
	
57

  
58 58
	public static final TableCreator NewInstance(String stepName, String tableName, String[] columnNames, String[] columnTypes, String[] referencedTables, boolean includeAudTable, boolean includeCdmBaseAttributes){
59 59
		return new TableCreator(stepName, tableName, Arrays.asList(columnNames), Arrays.asList(columnTypes), null, null, Arrays.asList(referencedTables), includeAudTable, includeCdmBaseAttributes, false, false, false);
60 60
	}
61
	
61

  
62 62
	public static final TableCreator NewNonVersionableInstance(String stepName, String tableName, String[] columnNames, String[] columnTypes, String[] referencedTables){
63 63
		return new TableCreator(stepName, tableName, Arrays.asList(columnNames), Arrays.asList(columnTypes), null, null, Arrays.asList(referencedTables), false, true, false, false, true);
64 64
	}
65
	
65

  
66 66
	public static final TableCreator NewVersionableInstance(String stepName, String tableName, String[] columnNames, String[] columnTypes, String[] referencedTables, boolean includeAudTable){
67 67
		return new TableCreator(stepName, tableName, Arrays.asList(columnNames), Arrays.asList(columnTypes), null, null, Arrays.asList(referencedTables), includeAudTable, true, false, false, false);
68 68
	}
69
	
69

  
70 70
	public static final TableCreator NewAnnotatableInstance(String stepName, String tableName, String[] columnNames, String[] columnTypes, String[] referencedTables, boolean includeAudTable){
71 71
		return new TableCreator(stepName, tableName, Arrays.asList(columnNames), Arrays.asList(columnTypes), null, null, Arrays.asList(referencedTables), includeAudTable, true, true, false, false);
72 72
	}
73
	
73

  
74 74
	public static final TableCreator NewEventInstance(String stepName, String tableName, String[] columnNames, String[] columnTypes, String[] referencedTables, boolean includeAudTable){
75 75
		TableCreator result = new TableCreator(stepName, tableName, Arrays.asList(columnNames), Arrays.asList(columnTypes), null, null, Arrays.asList(referencedTables), includeAudTable, true, true, false, false);
76 76
		result.includeEventBase = true;
77 77
		return result;
78 78
	}
79
	
79

  
80 80
	public static final TableCreator NewIdentifiableInstance(String stepName, String tableName, String[] columnNames, String[] columnTypes, String[] referencedTables, boolean includeAudTable){
81 81
		return new TableCreator(stepName, tableName, Arrays.asList(columnNames), Arrays.asList(columnTypes), null, null, Arrays.asList(referencedTables), includeAudTable, true, true, true, false);
82 82
	}
83
	
84
	protected TableCreator(String stepName, String tableName, List<String> columnNames, List<String> columnTypes, List<Object> defaultValues, List<Boolean> isNotNull, List<String> referencedTables, 
83

  
84
	protected TableCreator(String stepName, String tableName, List<String> columnNames, List<String> columnTypes, List<Object> defaultValues, List<Boolean> isNotNull, List<String> referencedTables,
85 85
			boolean includeAudTable, boolean includeCdmBaseAttributes, boolean includeAnnotatableEntity, boolean includeIdentifiableEntity, boolean excludeVersionableAttributes) {
86
		super(stepName);
87
		this.tableName = tableName;
86
		super(stepName, tableName, includeAudTable);
88 87
		this.columnNames = columnNames;
89 88
		this.columnTypes = columnTypes;
90 89
		this.defaultValues = defaultValues;
91 90
		this.isNotNull = isNotNull;
92 91
		this.referencedTables = referencedTables;
93
		this.includeAudTable = includeAudTable;
94 92
		this.includeCdmBaseAttributes = includeCdmBaseAttributes;
95 93
		this.includeAnnotatableEntity = includeAnnotatableEntity;
96 94
		this.includeIdentifiableEntity = includeIdentifiableEntity;
......
112 110
		if (columnNames.size() != columnTypes.size()){
113 111
			throw new RuntimeException ("ColumnNames and columnTypes must be of same size. Step: " + getStepName());
114 112
		}
115
			
113

  
116 114
		try {
117 115
			for (int i = 0; i < columnNames.size(); i++){
118 116
				boolean isNotNull = this.isNotNull == null ? false : this.isNotNull.get(i);
......
148 146
			throw new RuntimeException(e);
149 147
		}
150 148
	}
151
	
149

  
152 150
	/**
153 151
	 * fills the mnTablesStepList
154
	 * @param mnTablesStepList, String tableName 
152
	 * @param mnTablesStepList, String tableName
155 153
	 */
156 154
	public static void makeMnTables(List<ISchemaUpdaterStep> mnTablesStepList, String tableName, boolean includeAnnotatable, boolean includeIdentifiable) {
157 155
		TableCreator tableCreator;
158 156
		String stepName;
159
		
157

  
160 158
		if (includeAnnotatable){
161 159
			//annotations
162 160
			stepName= "Add @tableName annotations";
......
169 167
			stepName = stepName.replace("@tableName", tableName);
170 168
			tableCreator = MnTableCreator.NewMnInstance(stepName, tableName, "Marker", SchemaUpdaterBase.INCLUDE_AUDIT);
171 169
			mnTablesStepList.add(tableCreator);
172
			
170

  
173 171
		}
174
		
172

  
175 173
		if (includeIdentifiable){
176 174

  
177 175
			//credits
......
179 177
			stepName = stepName.replace("@tableName", tableName);
180 178
			tableCreator = MnTableCreator.NewMnInstance(stepName, tableName, null, "Credit", null, SchemaUpdaterBase.INCLUDE_AUDIT, SORT_INDEX, false);
181 179
			mnTablesStepList.add(tableCreator);
182
			
183
			
180

  
181

  
184 182
			//identifier
185 183
			stepName= "Add @tableName identifiers";
186 184
			stepName = stepName.replace("@tableName", tableName);
187 185
			tableCreator = MnTableCreator.NewMnInstance(stepName, tableName, null, "Identifier", null, SchemaUpdaterBase.INCLUDE_AUDIT, SORT_INDEX, false);
188 186
			mnTablesStepList.add(tableCreator);
189 187

  
190
			
188

  
191 189
			//extensions
192 190
			stepName= "Add @tableName extensions";
193 191
			stepName = stepName.replace("@tableName", tableName);
194 192
			tableCreator = MnTableCreator.NewMnInstance(stepName, tableName, "Extension", SchemaUpdaterBase.INCLUDE_AUDIT);
195 193
			mnTablesStepList.add(tableCreator);
196
			
194

  
197 195
			//OriginalSourceBase
198 196
			stepName= "Add @tableName sources";
199 197
			stepName = stepName.replace("@tableName", tableName);
......
238 236
			}
239 237
			//specific columns
240 238
			updateQuery += 	getColumnsSql(tableName, datasource, monitor);
241
			
239

  
242 240
			//primary and unique keys
243 241
			String primaryKeySql = primaryKey(isAuditing)==null ? "" : "primary key (" + primaryKey(isAuditing) + "),";
244 242
			String uniqueSql = unique(isAuditing)== null ? "" : "unique(" + unique(isAuditing) + "),";
245 243
			updateQuery += primaryKeySql + uniqueSql;
246
			
244

  
247 245
			//finalize
248 246
			updateQuery = StringUtils.chomp(updateQuery.trim(), ",") + ")";
249
			
247

  
250 248
			//replace
251 249
			updateQuery = updateQuery.replace("@tableName", tableName);
252
			
250

  
253 251
			//append datasource specific string
254 252
			updateQuery += datasource.getDatabaseType().getHibernateDialect().getTableTypeString();
255 253
			logger.debug("UPDATE Query: " + updateQuery);
256
			
254

  
257 255
			//execute
258 256
			datasource.executeUpdate(updateQuery);
259
			
257

  
260 258
			//Foreign Keys
261 259
			result &= createForeignKeys(tableName, isAuditing, datasource, monitor, caseType);
262
			
260

  
263 261
			return result;
264 262
		} catch (Exception e) {
265 263
			monitor.warning(e.getMessage(), e);
......
277 275
		String result = "";
278 276
		for (ColumnAdder adder : this.columnAdders){
279 277
			String singleAdderSQL = adder.getUpdateQueryString(tableName, datasource, monitor) + ", ";
280
			
278

  
281 279
			String[] split = singleAdderSQL.split(ColumnAdder.getAddColumnSeperator(datasource));
282 280
			result += split[1];
283 281
		}
284 282
		return result;
285 283
	}
286 284

  
287
	
285

  
288 286
	private boolean createForeignKeys(String tableName, boolean isAudit, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
289 287
		boolean result = true;
290 288
		if (includeCdmBaseAttributes){
......
293 291
				String referencedTable = "UserAccount";
294 292
				result &= makeForeignKey(tableName, datasource, monitor, attribute, referencedTable, caseType);
295 293
			}
296
			
294

  
297 295
			String attribute = "createdby";
298 296
			String referencedTable = "UserAccount";
299
			result &= makeForeignKey(tableName, datasource, monitor, attribute, referencedTable, caseType);			
300
		
297
			result &= makeForeignKey(tableName, datasource, monitor, attribute, referencedTable, caseType);
298

  
301 299
		}
302 300
		if (isAudit){
303 301
			String attribute = "REV";
......
311 309
		}
312 310
		for (ColumnAdder adder : this.columnAdders){
313 311
			if (adder.getReferencedTable() != null){
314
				result &= makeForeignKey(tableName, datasource, monitor, adder.getNewColumnName(), adder.getReferencedTable(), caseType); 
312
				result &= makeForeignKey(tableName, datasource, monitor, adder.getNewColumnName(), adder.getReferencedTable(), caseType);
315 313
			}
316 314
		}
317 315
		return result;
......
319 317

  
320 318
	public static boolean makeForeignKey(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, String attribute, String referencedTable, CaseType caseType) throws SQLException {
321 319
		boolean result = true;
322
		
320

  
323 321
		referencedTable = caseType.transformTo(referencedTable);
324
		
322

  
325 323
		if (supportsForeignKeys(datasource, monitor, tableName, referencedTable)){
326 324
			String index = "FK@tableName_@attribute";
327 325
			index = index.replace("@tableName", tableName);
328 326
			index = index.replace("@attribute", attribute);
329
			
327

  
330 328
			String idSuffix = "_id";
331 329
			if (isRevAttribute(attribute) || attribute.endsWith(idSuffix)){
332 330
				idSuffix = "";
......
343 341
			}else{
344 342
				updateQuery = updateQuery.replace("@constraintName", "");  //H2 does not support "CONSTRAINT", didn't check for others
345 343
			}
346
			
344

  
347 345
			if (isRevAttribute(attribute)){
348 346
				updateQuery = updateQuery.replace("@id", "revisionnumber");
349 347
			}else{
......
358 356
				logger.warn(message, e);
359 357
				return true;   //we do not interrupt update if only foreign key generation did not work
360 358
			}
361
			return result;			
359
			return result;
362 360
		}else{
363 361
			return true;
364 362
		}
......
402 400
			} catch (Exception e) {
403 401
				String message = "Problems to determine table engine for MySQL.";
404 402
				monitor.warning(message);
405
				return true;  //default 
403
				return true;  //default
406 404
			}
407
			
405

  
408 406
		}
409
		
410
				
407

  
408

  
411 409
	}
412 410

  
413 411
	private static boolean isRevAttribute(String attribute) {
......
422 420
	 */
423 421
	protected String primaryKey(boolean isAudit){
424 422
		String result = null;
425
		if (! isAudit && this.primaryKeyParams != null){ 
423
		if (! isAudit && this.primaryKeyParams != null){
426 424
			return this.primaryKeyParams;
427
		}else if (isAudit && this.primaryKeyParams_AUD != null){ 
425
		}else if (isAudit && this.primaryKeyParams_AUD != null){
428 426
			return this.primaryKeyParams_AUD;
429
		} 
427
		}
430 428

  
431 429
		if (includeCdmBaseAttributes || ! includeCdmBaseAttributes){ //TODO how to handle not CDMBase includes
432 430
			if (! isAudit){
......
437 435
		}
438 436
		return result;
439 437
	}
440
	
438

  
441 439
	/**
442 440
	 * Constructs the unique key creation string
443 441
	 * @param isAudit
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/TableDroper.java
1 1
// $Id$
2 2
/**
3 3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
4
* European Distributed Institute of Taxonomy
5 5
* http://www.e-taxonomy.eu
6
* 
6
*
7 7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
* See LICENSE.TXT at the top of this package for the full license terms.
9 9
*/
......
22 22
 */
23 23
public class TableDroper extends AuditedSchemaUpdaterStepBase<TableDroper> implements ISchemaUpdaterStep {
24 24
	private static final Logger logger = Logger.getLogger(TableDroper.class);
25
	
25

  
26 26
	private boolean ifExists = true;
27
	
27

  
28 28
	public static final TableDroper NewInstance(String stepName, String tableName, boolean includeAudTable){
29 29
		return new TableDroper(stepName, tableName, includeAudTable, true);
30 30
	}
31
	
31

  
32 32
	/**
33 33
	 * @param stepName
34 34
	 * @param tableName
......
41 41
		return new TableDroper(stepName, tableName, includeAudTable, ifExists);
42 42
	}
43 43

  
44
	
44

  
45 45
	protected TableDroper(String stepName, String tableName, boolean includeAudTable, boolean ifExists) {
46
		super(stepName);
47
		this.tableName = tableName;
48
		this.includeAudTable = includeAudTable;
46
		super(stepName, tableName, includeAudTable);
49 47
		this.ifExists = ifExists;
50 48
	}
51 49

  
......
79 77
			logger.error(message);
80 78
			return false;
81 79
		}
82
		
80

  
83 81
	}
84 82

  
85 83
	/**
......
93 91
	public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
94 92
		String updateQuery;
95 93
		DatabaseTypeEnum type = datasource.getDatabaseType();
96
		
94

  
97 95
		updateQuery = "DROP TABLE @ifExists @tableName ";
98 96
		if (type.equals(DatabaseTypeEnum.SqlServer2005)){
99 97
			//MySQL allows both syntaxes
......
112 110
		}else{
113 111
			updateQuery = updateQuery.replace("@ifExists", "");
114 112
		}
115
		
113

  
116 114
		return updateQuery;
117 115
	}
118 116

  
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/TreeIndexUpdater.java
1 1
// $Id$
2 2
/**
3 3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
4
* European Distributed Institute of Taxonomy
5 5
* http://www.e-taxonomy.eu
6
* 
6
*
7 7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
* See LICENSE.TXT at the top of this package for the full license terms.
9 9
*/
......
23 23
 */
24 24
public class TreeIndexUpdater extends AuditedSchemaUpdaterStepBase<TreeIndexUpdater> implements ISchemaUpdaterStep {
25 25
	private static final Logger logger = Logger.getLogger(TreeIndexUpdater.class);
26
	
26

  
27 27
	private String indexColumnName = "treeIndex";
28
	private String treeIdColumnName;
29
	private String parentIdColumnName = "parent_id";
30
	
28
	private final String treeIdColumnName;
29
	private final String parentIdColumnName = "parent_id";
30

  
31 31
	public static final TreeIndexUpdater NewInstance(String stepName, String tableName, String treeIdColumnName, boolean includeAudTable){
32 32
		return new TreeIndexUpdater(stepName, tableName, treeIdColumnName, null, includeAudTable);
33 33
	}
......
35 35
	public static final TreeIndexUpdater NewInstance(String stepName, String tableName, String treeIdColumnName, String indexColumnName, boolean includeAudTable){
36 36
		return new TreeIndexUpdater(stepName, tableName, treeIdColumnName, indexColumnName, includeAudTable);
37 37
	}
38
	
39
	
38

  
39

  
40 40
	protected TreeIndexUpdater(String stepName, String tableName, String treeIdColumnName, String indexColumnName, boolean includeAudTable) {
41
		super(stepName);
42
		this.tableName = tableName;
41
		super(stepName, tableName, includeAudTable);
43 42
		this.treeIdColumnName = treeIdColumnName;
44 43
		this.indexColumnName = indexColumnName == null ? this.indexColumnName : indexColumnName;
45
		this.includeAudTable = includeAudTable;
46 44
	}
47 45

  
48 46
	@Override
49 47
	protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) {
50 48
		try{
51
			boolean result = true;			
52
			
49
			boolean result = true;
50

  
53 51
	//		String charType = "CHAR";  //TODO may depend on database type
54
			
52

  
55 53
			//clean up nodes without classification  //this should not happen with correct "delete" implementation
56 54
			String sql = String.format(" DELETE FROM %s WHERE %s IS NULL ", tableName, treeIdColumnName);
57 55
			datasource.executeUpdate(sql);
58
			
56

  
59 57
			//... set all index entries to NULL
60 58
			sql = String.format(" UPDATE %s SET %s = NULL", tableName, indexColumnName);
61 59
			datasource.executeUpdate(sql);
62
			
60

  
63 61
			//start
64 62
			String separator = ITreeNode.separator;
65 63
			String treePrefix = ITreeNode.treePrefix;
66 64
			sql = String.format(" UPDATE %s " +
67 65
					" SET %s = CONCAT('%s%s', %s, '%s', id, '%s') " +
68
					" WHERE %s IS NULL AND %s IS NOT NULL ", 
69
						tableName, 
70
						indexColumnName, separator, treePrefix, treeIdColumnName, separator, separator, 
66
					" WHERE %s IS NULL AND %s IS NOT NULL ",
67
						tableName,
68
						indexColumnName, separator, treePrefix, treeIdColumnName, separator, separator,
71 69
						parentIdColumnName, treeIdColumnName);
72 70
			datasource.executeUpdate(sql);
73
			
71

  
74 72
			//width search index creation
75 73
			String sqlCount = String.format(" SELECT count(*) as n " +
76 74
					" FROM %s child INNER JOIN %s parent ON child.%s = parent.id " +
77 75
					" WHERE parent.%s IS NOT NULL AND child.%s IS NULL ",
78 76
					tableName, tableName, parentIdColumnName, indexColumnName, indexColumnName);
79
			
77

  
80 78
			Long n;
81 79
			do {
82
			
80

  
83 81
				//MySQL
84 82
				if (datasource.getDatabaseType().equals(DatabaseTypeEnum.MySQL)){
85 83
					sql = String.format(" UPDATE %s child " +
86 84
							" INNER JOIN %s parent ON child.%s = parent.id " +
87 85
							" SET child.%s = CONCAT( parent.%s, child.id, '%s') " +
88
							" WHERE parent.%s IS NOT NULL AND child.%s IS NULL ", 
89
								tableName, 
90
								tableName, parentIdColumnName, 
86
							" WHERE parent.%s IS NOT NULL AND child.%s IS NULL ",
87
								tableName,
88
								tableName, parentIdColumnName,
91 89
								indexColumnName, indexColumnName, separator,
92 90
								indexColumnName, indexColumnName);
93 91
				}else{
94 92
					//ANSI
95 93
					//http://stackoverflow.com/questions/1293330/how-can-i-do-an-update-statement-with-join-in-sql
96
					//does not work with MySQL as MySQL does not allow to use the same table in Subselect and Update (error 1093: http://dev.mysql.com/doc/refman/5.1/de/subquery-errors.html) 
94
					//does not work with MySQL as MySQL does not allow to use the same table in Subselect and Update (error 1093: http://dev.mysql.com/doc/refman/5.1/de/subquery-errors.html)
97 95
					sql = String.format(" UPDATE %s " +
98 96
							" SET %s = ( " +
99
								" ( SELECT CONCAT ( parent.%s, %s.id, '%s') " + 
97
								" ( SELECT CONCAT ( parent.%s, %s.id, '%s') " +
100 98
									" FROM %s parent " +
101 99
									" WHERE parent.id = %s.%s ) " +
102 100
								" ) " +
......
111 109
								indexColumnName, tableName, separator,
112 110
								tableName,
113 111
								tableName, parentIdColumnName,
114
								
112

  
115 113
								tableName,
116 114
								tableName, parentIdColumnName, indexColumnName, tableName, indexColumnName
117 115
							);
118 116
				}
119
				
117

  
120 118
				datasource.executeUpdate(sql);
121
				
119

  
122 120
				n = (Long)datasource.getSingleValue(sqlCount);
123 121
			}	while (n > 0) ;
124
			
122

  
125 123
			sqlCount = String.format(" SELECT count(*) as n " +
126 124
					" FROM %s " +
127 125
					" WHERE %s IS NULL ", tableName, indexColumnName);
......
130 128
				String message = "There are tree nodes with no tree index in %s. This indicates that there is a problem in the tree structure of 1 or more classifications.";
131 129
				logger.error(String.format(message, tableName));
132 130
			}
133
			
131

  
134 132
			return result;
135 133
		}catch(Exception e){
136 134
			monitor.warning(e.getMessage(), e);
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/UniqueIndexDropper.java
1 1
// $Id$
2 2
/**
3 3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
4
* European Distributed Institute of Taxonomy
5 5
* http://www.e-taxonomy.eu
6
* 
6
*
7 7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
* See LICENSE.TXT at the top of this package for the full license terms.
9 9
*/
......
24 24
 */
25 25
public class UniqueIndexDropper extends AuditedSchemaUpdaterStepBase<UniqueIndexDropper> implements ISchemaUpdaterStep {
26 26
	private static final Logger logger = Logger.getLogger(UniqueIndexDropper.class);
27
	
28
	private String indexColumn;
29
	
27

  
28
	private final String indexColumn;
29

  
30 30
	public static final UniqueIndexDropper NewInstance(String tableName, String indexColumn, boolean includeAudTable){
31 31
		String stepName = "Drop index " + tableName + "-" + indexColumn;
32 32
		return new UniqueIndexDropper(stepName, tableName, indexColumn, includeAudTable);
33 33
	}
34 34

  
35
	
35

  
36 36
	protected UniqueIndexDropper(String stepName, String tableName, String indexColumn, boolean includeAudTable) {
37
		super(stepName);
38
		this.tableName = tableName;
37
		super(stepName, tableName, includeAudTable);
39 38
		this.indexColumn = indexColumn;
40
		this.includeAudTable = includeAudTable;
41 39
	}
42 40

  
43 41
	@Override
......
57 55
	private boolean checkExists(ICdmDataSource datasource) throws SQLException, DatabaseTypeNotSupportedException {
58 56
		DatabaseTypeEnum type = datasource.getDatabaseType();
59 57
		if (type.equals(DatabaseTypeEnum.MySQL)){
60
			String sql = "SELECT count(*)	FROM information_schema.TABLE_CONSTRAINTS " + 
58
			String sql = "SELECT count(*)	FROM information_schema.TABLE_CONSTRAINTS " +
61 59
					" WHERE table_name ='@tableName' AND CONSTRAINT_SCHEMA = '@dbName' AND CONSTRAINT_TYPE = 'UNIQUE' ";
62 60
			sql = sql.replace("@tableName", tableName);
63 61
			sql = sql.replace("@columnName", indexColumn);
......
82 80
		String updateQuery;
83 81
		DatabaseTypeEnum type = datasource.getDatabaseType();
84 82
		String indexName = getIndexName(datasource);
85
		
83

  
86 84
//		if (type.equals(DatabaseTypeEnum.SqlServer2005)){
87 85
			//MySQL allows both syntaxes
88 86
//			updateQuery = "ALTER TABLE @tableName ADD @columnName @columnType";
......
91 89
			updateQuery = "ALTER TABLE @tableName DROP CONSTRAINT IF EXISTS @indexName";
92 90
		}else if (type.equals(DatabaseTypeEnum.PostgreSQL)){
93 91
//			updateQuery = "DROP INDEX IF EXISTS @indexName";  // does not work because index is used in the constraint
94
//			updateQuery = "ALTER TABLE @tableName DROP CONSTRAINT IF EXISTS @indexName"; //"if exists" does not work (version 8.4) 
92
//			updateQuery = "ALTER TABLE @tableName DROP CONSTRAINT IF EXISTS @indexName"; //"if exists" does not work (version 8.4)
95 93
			updateQuery = "ALTER TABLE @tableName DROP CONSTRAINT @indexName";
96 94
		}else if (type.equals(DatabaseTypeEnum.MySQL)){
97 95
			updateQuery = "ALTER TABLE @tableName DROP INDEX @indexName";
......
103 101
		}
104 102
		updateQuery = updateQuery.replace("@tableName", tableName);
105 103
		updateQuery = updateQuery.replace("@indexName", indexName);
106
		
104

  
107 105
		return updateQuery;
108 106
	}
109 107

  
......
116 114
		}else if (type.equals(DatabaseTypeEnum.MySQL)){
117 115
			result = this.indexColumn;
118 116
		}else if (type.equals(DatabaseTypeEnum.H2) ){
119
//			String sql = "SELECT INDEX_NAME FROM INFORMATION_SCHEMA.INDEXES WHERE TABLE_NAME = @tableName AND INDEX_TYPE_NAME = 'UNIQUE INDEX'"; 
120
			String sql = "SELECT CONSTRAINT_NAME " + 
117
//			String sql = "SELECT INDEX_NAME FROM INFORMATION_SCHEMA.INDEXES WHERE TABLE_NAME = @tableName AND INDEX_TYPE_NAME = 'UNIQUE INDEX'";
118
			String sql = "SELECT CONSTRAINT_NAME " +
121 119
					" FROM INFORMATION_SCHEMA.CONSTRAINTS "+
122 120
					" WHERE CONSTRAINT_CATALOG = '@dbName' AND "+
123
					" TABLE_NAME = '@tableName' AND CONSTRAINT_TYPE = 'UNIQUE' AND "+ 
124
					" COLUMN_LIST = '@columnName'"; 
121
					" TABLE_NAME = '@tableName' AND CONSTRAINT_TYPE = 'UNIQUE' AND "+
122
					" COLUMN_LIST = '@columnName'";
125 123
			sql = sql.replace("@tableName", tableName.toUpperCase());
126 124
			sql = sql.replace("@columnName", indexColumn.toUpperCase());
127 125
			sql = sql.replace("@dbName", datasource.getDatabase().toUpperCase());
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff