Project

General

Profile

Download (5.36 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.List;
13

    
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

    
20
/**
21
 * @author a.mueller
22
 * @since 15.06.2017
23
 *
24
 */
25
public class IndexRenamer extends SchemaUpdaterStepBase {
26
    private static final Logger logger = Logger.getLogger(IndexRenamer.class);
27

    
28
    private String tableName;
29

    
30
    private String oldIndexName;
31

    
32
    private String newIndexName;
33

    
34
    private String columnName;
35

    
36
    private Integer length;
37

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

    
40
    public static final IndexRenamer NewStringInstance(List<ISchemaUpdaterStep> stepList, String tableName, String oldIndexName,
41
            String newIndexName, String columnName, Integer length){
42
        return new IndexRenamer(stepList, tableName, oldIndexName,
43
                newIndexName, columnName, length == null ? 255 : length);
44
    }
45

    
46
    public static final IndexRenamer NewIntegerInstance(List<ISchemaUpdaterStep> stepList, String tableName, String oldIndexName,
47
            String newIndexName, String columnName){
48
        return new IndexRenamer(stepList, tableName, oldIndexName, newIndexName, columnName, null);
49
    }
50
    /**
51
     * @param stepName
52
     */
53
    protected IndexRenamer(List<ISchemaUpdaterStep> stepList, String tableName, String oldIndexName,
54
            String newIndexName, String columnName, Integer length) {
55
        super(stepList, "Rename index " + oldIndexName + " to " + newIndexName);
56
        this.tableName = tableName;
57
        this.oldIndexName = oldIndexName;
58
        this.newIndexName = newIndexName;
59
        this.columnName = columnName;
60
        this.length = length;
61
    }
62

    
63
    /**
64
     * {@inheritDoc}
65
     */
66
    @Override
67
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType,
68
            SchemaUpdateResult result) throws SQLException {
69
        try {
70
            String[] updateQuery = getCreateQuery(datasource, caseType, tableName, oldIndexName, newIndexName, columnName, length);
71
            try {
72
                datasource.executeUpdate(updateQuery[0]);
73
            } catch (Exception e) {
74
                if (updateQuery.length > 1){
75
                    datasource.executeUpdate(updateQuery[1]);
76
                }else{
77
                    throw e;
78
                }
79
            }
80
            return;
81
        } catch (Exception e) {
82
            String message = "Index ("+tableName +"."+oldIndexName+") could not be renamed "
83
                    + "to ("+newIndexName+") or drop/add was not possible.\n"
84
                    + "Please ask your admin to rename manually.";
85
            logger.warn(message);
86
            result.addWarning(message, this, "invoke");
87
            return;
88
        }
89

    
90
    }
91

    
92
    private String[] getCreateQuery(ICdmDataSource datasource, CaseType caseType, String tableName, String oldIndexName, String newIndexName, String columnName, Integer length) {
93
        DatabaseTypeEnum type = datasource.getDatabaseType();
94
//      String indexName = "_UniqueKey";
95
        String[] updateQueries;
96
        if (type.equals(DatabaseTypeEnum.MySQL)){
97
            //https://stackoverflow.com/questions/1463363/how-do-i-rename-an-index-in-mysql
98
            //in future: https://dev.mysql.com/worklog/task/?id=6555
99
            String format = "ALTER TABLE @@%s@@ DROP INDEX %s, ADD INDEX %s (%s%s)";
100
            updateQueries = new String[]{String.format(format, tableName, oldIndexName, newIndexName, columnName, length!= null ? "("+length+")": "")};
101
        }else if (type.equals(DatabaseTypeEnum.H2) || type.equals(DatabaseTypeEnum.PostgreSQL)){
102
            //http://www.h2database.com/html/grammar.html#alter_index_rename
103
            //https://www.postgresql.org/docs/9.4/static/sql-alterindex.html (maybe IF EXISTS does not work prior to 9.x)
104
            String format = "ALTER INDEX %s %s RENAME TO %s";
105
            updateQueries = new String[]{String.format(format, " IF EXISTS ", oldIndexName, newIndexName),
106
                    String.format(format, "", oldIndexName, newIndexName)};
107

    
108
        }else if (type.equals(DatabaseTypeEnum.SqlServer2005)){
109
            //TODO Untested !!!!
110
            //https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-rename-transact-sql
111
            //https://www.mssqltips.com/sqlservertip/2709/script-to-rename-constraints-and-indexes-to-conform-to-a-sql-server-naming-convention/
112
            //https://stackoverflow.com/questions/40865886/rename-sql-server-index-in-ms-sql-server
113
            String format = "EXEC sp_rename N'%s.%s', N'%s', N'INDEX'";
114
            updateQueries = new String[]{String.format(format, tableName, oldIndexName, newIndexName)};
115
        }else{
116
            throw new IllegalArgumentException("Datasource type not supported yet: " + type.getName());
117
        }
118
//      updateQuery = updateQuery.replace("@indexName", indexName);
119
        for (int i = 0; i < updateQueries.length ; i++ ){
120
            updateQueries[i] = caseType.replaceTableNames(updateQueries[i]);
121
        }
122
        return updateQueries;
123
}
124

    
125
}
(19-19/41)