3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.database
.update
;
12 import java
.sql
.SQLException
;
14 import org
.apache
.log4j
.Logger
;
16 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
17 import eu
.etaxonomy
.cdm
.database
.DatabaseTypeEnum
;
18 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
25 public class TableNameChanger
extends SchemaUpdaterStepBase
<TableNameChanger
> implements ISchemaUpdaterStep
{
26 @SuppressWarnings("unused")
27 private static final Logger logger
= Logger
.getLogger(TableNameChanger
.class);
29 private String oldName
;
30 private String newName
;
31 private boolean includeAudTable
;
33 public static final TableNameChanger
NewInstance(String stepName
, String oldName
, String newName
, boolean includeAudTable
){
34 return new TableNameChanger(stepName
, oldName
, newName
, includeAudTable
);
37 protected TableNameChanger(String stepName
, String oldName
, String newName
, boolean includeAudTable
) {
39 this.oldName
= oldName
;
40 this.newName
= newName
;
41 this.includeAudTable
= includeAudTable
;
45 public Integer
invoke(ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
46 boolean result
= true;
47 result
&= invokeOnTable(oldName
, newName
, datasource
, monitor
);
50 result
&= invokeOnTable(oldName
+ aud
, newName
+ aud
, datasource
, monitor
);
52 return (result
== true )?
0 : null;
55 //does not support AuditedSchemaUpdaterStepBase signature
56 private boolean invokeOnTable(String oldName
, String newName
, ICdmDataSource datasource
, IProgressMonitor monitor
) {
57 DatabaseTypeEnum type
= datasource
.getDatabaseType();
59 if (type
.equals(DatabaseTypeEnum
.MySQL
)){
60 //MySQL allows both syntaxes
61 updateQuery
= "RENAME TABLE @oldName TO @newName";
62 }else if (type
.equals(DatabaseTypeEnum
.H2
) || type
.equals(DatabaseTypeEnum
.PostgreSQL
) || type
.equals(DatabaseTypeEnum
.MySQL
)){
63 updateQuery
= "ALTER TABLE @oldName RENAME TO @newName";
64 }else if (type
.equals(DatabaseTypeEnum
.SqlServer2005
)){
65 updateQuery
= "EXEC sp_rename '@oldName', '@newName'";
68 monitor
.warning("Update step '" + this.getStepName() + "' is not supported by " + type
.getName());
71 updateQuery
= updateQuery
.replace("@oldName", oldName
);
72 updateQuery
= updateQuery
.replace("@newName", newName
);
74 datasource
.executeUpdate(updateQuery
);
75 } catch (SQLException e
) {
76 monitor
.warning("Could not perform rename table operation", e
);