2 * Copyright (C) 2017 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
.SQLException
;
12 import java
.util
.List
;
14 import org
.apache
.logging
.log4j
.LogManager
;
15 import org
.apache
.logging
.log4j
.Logger
;
17 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
18 import eu
.etaxonomy
.cdm
.database
.DatabaseTypeEnum
;
19 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
26 public class IndexRenamer
extends SchemaUpdaterStepBase
{
28 private static final Logger logger
= LogManager
.getLogger();
30 private String tableName
;
32 private String oldIndexName
;
34 private String newIndexName
;
36 private String columnName
;
38 private Integer length
;
40 // ********************** FACTORY ****************************************/
42 public static final IndexRenamer
NewStringInstance(List
<ISchemaUpdaterStep
> stepList
, String tableName
, String oldIndexName
,
43 String newIndexName
, String columnName
, Integer length
){
44 return new IndexRenamer(stepList
, tableName
, oldIndexName
,
45 newIndexName
, columnName
, length
== null ?
255 : length
);
48 public static final IndexRenamer
NewIntegerInstance(List
<ISchemaUpdaterStep
> stepList
, String tableName
, String oldIndexName
,
49 String newIndexName
, String columnName
){
50 return new IndexRenamer(stepList
, tableName
, oldIndexName
, newIndexName
, columnName
, null);
55 protected IndexRenamer(List
<ISchemaUpdaterStep
> stepList
, String tableName
, String oldIndexName
,
56 String newIndexName
, String columnName
, Integer length
) {
57 super(stepList
, "Rename index " + oldIndexName
+ " to " + newIndexName
);
58 this.tableName
= tableName
;
59 this.oldIndexName
= oldIndexName
;
60 this.newIndexName
= newIndexName
;
61 this.columnName
= columnName
;
69 public void invoke(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
,
70 SchemaUpdateResult result
) throws SQLException
{
72 String
[] updateQuery
= getCreateQuery(datasource
, caseType
, tableName
, oldIndexName
, newIndexName
, columnName
, length
);
74 datasource
.executeUpdate(updateQuery
[0]);
75 } catch (Exception e
) {
76 if (updateQuery
.length
> 1){
77 datasource
.executeUpdate(updateQuery
[1]);
83 } catch (Exception e
) {
84 String message
= "Index ("+tableName
+"."+oldIndexName
+") could not be renamed "
85 + "to ("+newIndexName
+") or drop/add was not possible.\n"
86 + "Please ask your admin to rename manually.";
88 result
.addWarning(message
, this, "invoke");
94 private String
[] getCreateQuery(ICdmDataSource datasource
, CaseType caseType
, String tableName
, String oldIndexName
, String newIndexName
, String columnName
, Integer length
) {
95 DatabaseTypeEnum type
= datasource
.getDatabaseType();
96 // String indexName = "_UniqueKey";
97 String
[] updateQueries
;
98 if (type
.equals(DatabaseTypeEnum
.MySQL
)){
99 //https://stackoverflow.com/questions/1463363/how-do-i-rename-an-index-in-mysql
100 //in future: https://dev.mysql.com/worklog/task/?id=6555
101 String format
= "ALTER TABLE @@%s@@ DROP INDEX %s, ADD INDEX %s (%s%s)";
102 updateQueries
= new String
[]{String
.format(format
, tableName
, oldIndexName
, newIndexName
, columnName
, length
!= null ?
"("+length
+")": "")};
103 }else if (type
.equals(DatabaseTypeEnum
.H2
) || type
.equals(DatabaseTypeEnum
.PostgreSQL
)){
104 //http://www.h2database.com/html/grammar.html#alter_index_rename
105 //https://www.postgresql.org/docs/9.4/static/sql-alterindex.html (maybe IF EXISTS does not work prior to 9.x)
106 String format
= "ALTER INDEX %s %s RENAME TO %s";
107 updateQueries
= new String
[]{String
.format(format
, " IF EXISTS ", oldIndexName
, newIndexName
),
108 String
.format(format
, "", oldIndexName
, newIndexName
)};
110 }else if (type
.equals(DatabaseTypeEnum
.SqlServer2005
)){
112 //https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-rename-transact-sql
113 //https://www.mssqltips.com/sqlservertip/2709/script-to-rename-constraints-and-indexes-to-conform-to-a-sql-server-naming-convention/
114 //https://stackoverflow.com/questions/40865886/rename-sql-server-index-in-ms-sql-server
115 String format
= "EXEC sp_rename N'%s.%s', N'%s', N'INDEX'";
116 updateQueries
= new String
[]{String
.format(format
, tableName
, oldIndexName
, newIndexName
)};
118 throw new IllegalArgumentException("Datasource type not supported yet: " + type
.getName());
120 // updateQuery = updateQuery.replace("@indexName", indexName);
121 for (int i
= 0; i
< updateQueries
.length
; i
++ ){
122 updateQueries
[i
] = caseType
.replaceTableNames(updateQueries
[i
]);
124 return updateQueries
;