Project

General

Profile

Download (5.41 KB) Statistics
| Branch: | Tag: | Revision:
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

    
13
import org.apache.commons.lang.StringUtils;
14
import org.apache.log4j.Logger;
15

    
16
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
17
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
18
import eu.etaxonomy.cdm.database.ICdmDataSource;
19
import eu.etaxonomy.cdm.database.update.v33_34.UsernameConstraintUpdater;
20

    
21
/**
22
 * Adds an Index on a string column.
23
 *
24
 * @see {@link UsernameConstraintUpdater}
25
 * @author a.mueller
26
 * @since 2016-10-18
27
 *
28
 */
29
public class IndexAdder extends SchemaUpdaterStepBase {
30
	private static final Logger logger = Logger.getLogger(IndexAdder.class);
31

    
32
	private String tableName;
33

    
34
	private String columnName;
35

    
36
	private Integer length;
37

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

    
40
	public static final IndexAdder NewStringInstance(String stepName, String tableName, String columnName, Integer length){
41
		return new IndexAdder(stepName, tableName, columnName, length == null ? 255 : length);
42
	}
43

    
44
    public static final IndexAdder NewIntegerInstance(String stepName, String tableName, String columnName){
45
        return new IndexAdder(stepName, tableName, columnName, null);
46
    }
47

    
48
// **************************** CONSTRUCTOR *********************************/
49

    
50
	protected IndexAdder(String stepName, String tableName, String columnName, Integer length) {
51
		super(stepName);
52
		this.tableName = tableName;
53
		this.columnName = columnName;
54
		this.length = length;
55
	}
56

    
57
    @Override
58
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
59
            CaseType caseType, SchemaUpdateResult result) throws SQLException {
60
        //remove 2-fold constraint
61
//		result &= removeExistingConstraint(datasource, caseType);
62
		createColumnConstraint(datasource, caseType, result);
63
		return;
64
	}
65

    
66
	private void createColumnConstraint(ICdmDataSource datasource,
67
	        CaseType caseType, SchemaUpdateResult result) {
68
		try {
69
		    String constraintName = StringUtils.uncapitalize(tableName) + columnName + "Index";
70
			String updateQuery = getCreateQuery(datasource, caseType, tableName, constraintName, columnName);
71
			datasource.executeUpdate(updateQuery);
72
			return;
73
		} catch (Exception e) {
74
		    String message = "Unique index for " + columnName + " could not be created";
75
			logger.warn(message);
76
			result.addException(e, message, "IndexAdder.createColumnConstraint");
77
			return;
78
		}
79
	}
80

    
81
	private String getCreateQuery(ICdmDataSource datasource, CaseType caseType, String tableName, String constraintName, String columnName) {
82
			DatabaseTypeEnum type = datasource.getDatabaseType();
83
//			String indexName = "_UniqueKey";
84
			String updateQuery;
85
			if (type.equals(DatabaseTypeEnum.MySQL)){
86
				//Maybe MySQL also works with the below syntax. Did not check yet.
87
				updateQuery = "ALTER TABLE @@"+ tableName + "@@ ADD INDEX " + constraintName + " ("+columnName+ makeLength()+");";
88
			}else if (type.equals(DatabaseTypeEnum.H2) || type.equals(DatabaseTypeEnum.PostgreSQL) || type.equals(DatabaseTypeEnum.SqlServer2005)){
89
				updateQuery = "CREATE INDEX " + constraintName + " ON "+tableName+"(" + columnName + ")";
90
			}else{
91
				throw new IllegalArgumentException("Datasource type not supported yet: " + type.getName());
92
			}
93
//			updateQuery = updateQuery.replace("@indexName", indexName);
94
			updateQuery = caseType.replaceTableNames(updateQuery);
95
			return updateQuery;
96
	}
97

    
98
	/**
99
     * @param length2
100
     * @return
101
     */
102
    private String makeLength() {
103
        if (length != null){
104
            return "(" + length + ")";
105
        }else{
106
            return "";
107
        }
108
    }
109

    
110
    private boolean removeExistingConstraint(ICdmDataSource datasource, CaseType caseType) {
111
		try {
112
			DatabaseTypeEnum type = datasource.getDatabaseType();
113
			String indexName = "_UniqueKey";
114
			String updateQuery = makeRemoveConstraintUpdateQuery(caseType, type, indexName);
115
			try {
116
				datasource.executeUpdate(updateQuery);
117
			} catch (Exception e) {
118
				indexName = "uuid";
119
				updateQuery = makeRemoveConstraintUpdateQuery(caseType, type, indexName);
120
				datasource.executeUpdate(updateQuery);
121
			}
122
			return true;
123
		} catch (Exception e) {
124
			logger.warn("Old index could not be removed");
125
			return false;
126
		}
127
	}
128

    
129

    
130
	/**
131
	 * @param caseType
132
	 * @param type
133
	 * @param indexName
134
	 * @param updateQuery
135
	 * @return
136
	 */
137
	private String makeRemoveConstraintUpdateQuery(CaseType caseType,
138
			DatabaseTypeEnum type, String indexName) {
139
		String updateQuery;
140
		if (type.equals(DatabaseTypeEnum.MySQL)){
141
			updateQuery = "ALTER TABLE @@" + tableName + "@@ DROP INDEX @indexName";
142
		}else if (type.equals(DatabaseTypeEnum.H2)){
143
			updateQuery = "ALTER TABLE @@" + tableName + "@@ DROP CONSTRAINT IF EXISTS @indexName";
144
		}else if (type.equals(DatabaseTypeEnum.PostgreSQL)){
145
			updateQuery = "ALTER TABLE @@" + tableName + "@@ DROP CONSTRAINT @indexName";
146
		}else if (type.equals(DatabaseTypeEnum.SqlServer2005)){
147
			//TODO
148
			throw new RuntimeException("Remove index not yet supported for SQLServer");
149
		}else{
150
			throw new IllegalArgumentException("Datasource type not supported: " + type.getName());
151
		}
152
		updateQuery = updateQuery.replace("@indexName", indexName);
153
		updateQuery = caseType.replaceTableNames(updateQuery);
154
		return updateQuery;
155
	}
156
}
(14-14/35)