include more auditing in schema update and refactor to better allow transaction suppo...
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / update / TableNameChanger.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.cdm.database.update;
11
12 import java.sql.SQLException;
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 * @date 16.09.2010
23 *
24 */
25 public class TableNameChanger extends SchemaUpdaterStepBase<TableNameChanger> implements ISchemaUpdaterStep {
26 @SuppressWarnings("unused")
27 private static final Logger logger = Logger.getLogger(TableNameChanger.class);
28
29 private String oldName;
30 private String newName;
31 private boolean includeAudTable;
32
33 public static final TableNameChanger NewInstance(String stepName, String oldName, String newName, boolean includeAudTable){
34 return new TableNameChanger(stepName, oldName, newName, includeAudTable);
35 }
36
37 protected TableNameChanger(String stepName, String oldName, String newName, boolean includeAudTable) {
38 super(stepName);
39 this.oldName = oldName;
40 this.newName = newName;
41 this.includeAudTable = includeAudTable;
42 }
43
44 @Override
45 public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {
46 boolean result = true;
47 result &= invokeOnTable(oldName, newName, datasource, monitor);
48 if (includeAudTable){
49 String aud = "_AUD";
50 result &= invokeOnTable(oldName + aud, newName + aud, datasource, monitor);
51 }
52 return (result == true )? 0 : null;
53 }
54
55 //does not support AuditedSchemaUpdaterStepBase signature
56 private boolean invokeOnTable(String oldName, String newName, ICdmDataSource datasource, IProgressMonitor monitor) {
57 DatabaseTypeEnum type = datasource.getDatabaseType();
58 String updateQuery;
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'";
66 }else{
67 updateQuery = null;
68 monitor.warning("Update step '" + this.getStepName() + "' is not supported by " + type.getName());
69 return false;
70 }
71 updateQuery = updateQuery.replace("@oldName", oldName);
72 updateQuery = updateQuery.replace("@newName", newName);
73 try {
74 datasource.executeUpdate(updateQuery);
75 } catch (SQLException e) {
76 monitor.warning("Could not perform rename table operation", e);
77 }
78 return true;
79 }
80
81 }