Project

General

Profile

Download (5.45 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
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 16.09.2010
23
 *
24
 */
25
public class TableNameChanger
26
            extends SchemaUpdaterStepBase{
27

    
28
    private static final Logger logger = Logger.getLogger(TableNameChanger.class);
29

    
30
	private String oldName;
31
	private String newName;
32
	private boolean includeAudTable;
33
	private boolean includeDtype;
34

    
35
	public static final TableNameChanger NewInstance(List<ISchemaUpdaterStep> stepList, String stepName, String oldName, String newName, boolean includeAudTable){
36
		return new TableNameChanger(stepList, stepName, oldName, newName, includeAudTable, false);
37
	}
38

    
39
	public static final TableNameChanger NewInstance(List<ISchemaUpdaterStep> stepList, String stepName, String oldName, String newName, boolean includeAudTable, boolean includeDtype){
40
	    return new TableNameChanger(stepList, stepName, oldName, newName, includeAudTable, includeDtype);
41
	}
42

    
43
	protected TableNameChanger(List<ISchemaUpdaterStep> stepList, String stepName, String oldName, String newName, boolean includeAudTable, boolean includeDtype) {
44
		super(stepList, stepName);
45
		this.oldName = oldName;
46
		this.newName = newName;
47
		this.includeAudTable = includeAudTable;
48
		this.includeDtype = includeDtype;
49
	}
50

    
51
    @Override
52
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
53
            CaseType caseType, SchemaUpdateResult result) throws SQLException {
54
		invokeOnTable(oldName, newName,
55
		        datasource, monitor, result, caseType);
56
		updateHibernateSequence(datasource, monitor, newName, oldName); //no result&= as hibernateSequence problems may not lead to a complete fail
57
		if (includeAudTable){
58
			String aud = "_AUD";
59
			invokeOnTable(oldName + aud, newName + aud,
60
			        datasource, monitor, result, caseType);
61
		}
62
		return;
63
	}
64

    
65
	//does not support AuditedSchemaUpdaterStepBase signature
66
	private void invokeOnTable(String oldNameOrig, String newNameOrig, ICdmDataSource datasource,
67
	        IProgressMonitor monitor, SchemaUpdateResult result, CaseType caseType) {
68
		String oldName = caseType.transformTo(oldNameOrig);
69
		String newName = caseType.transformTo(newNameOrig);
70
        DatabaseTypeEnum type = datasource.getDatabaseType();
71
		String updateQuery;
72
		if (type.equals(DatabaseTypeEnum.MySQL)){
73
			//MySQL allows both syntaxes
74
			updateQuery = "RENAME TABLE @oldName TO @newName";
75
		}else if (type.equals(DatabaseTypeEnum.H2) || type.equals(DatabaseTypeEnum.PostgreSQL) || type.equals(DatabaseTypeEnum.MySQL)){
76
			updateQuery = "ALTER TABLE @oldName RENAME TO @newName";
77
		}else if (type.equals(DatabaseTypeEnum.SqlServer2005)){
78
			updateQuery = "EXEC sp_rename '@oldName', '@newName'";
79
		}else{
80
			updateQuery = null;
81
			String message ="Update step '" + this.getStepName() + "' is not supported by " + type.getName();
82
			monitor.warning(message);
83
			result.addError(message, getStepName() + ", TableNameChanger.invokeOnTable");
84
            return;
85
		}
86
		updateQuery = updateQuery.replace("@oldName", oldName);
87
		updateQuery = updateQuery.replace("@newName", newName);
88
		try {
89
			datasource.executeUpdate(updateQuery);
90
		} catch (SQLException e) {
91
			String message = "Could not perform rename table operation";
92
			monitor.warning("Could not perform rename table operation", e);
93
			logger.warn(message+ ": "  + e.getMessage());
94
			result.addException(e, message, getStepName() + ", TableNameChanger.invokeOnTable");
95
			return;
96
		}
97
		if(includeDtype){
98
		    updateDtype(datasource, monitor, caseType, newNameOrig, oldNameOrig);
99
		}
100
		return;
101
	}
102

    
103
	/**
104
	 *
105
	 * @param datasource
106
	 * @param monitor
107
	 * @param table
108
	 * @param oldVal
109
	 * @return
110
	 */
111
	private boolean updateHibernateSequence(ICdmDataSource datasource, IProgressMonitor monitor, String newName, String oldName){
112
		try{
113
			//TODO do we need to "case" this table name?
114
			String sql = " UPDATE hibernate_sequences SET sequence_name = '%s' WHERE sequence_name = '%s'";
115
			datasource.executeUpdate(String.format(sql, newName ,oldName));
116
			return true;
117
		} catch (Exception e) {
118
			String message = "Exception occurred when trying to read or update hibernate_sequences table for value " + this.newName + ": " + e.getMessage();
119
			monitor.warning(message, e);
120
			logger.error(message);
121
			return false;
122
		}
123

    
124
	}
125

    
126
    private boolean updateDtype(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType, String newNameOrig, String oldNameOrig){
127
        try{
128
            String sql = " UPDATE %s SET dtype = '%s' WHERE dtype = '%s'";
129
            sql =  String.format(sql, caseType.transformTo(newNameOrig), newNameOrig ,oldNameOrig);
130
            datasource.executeUpdate(sql);
131
            return true;
132
        } catch (Exception e) {
133
            String message = "Exception occurred when trying to update DTYPE for table " + this.newName + ": " + e.getMessage();
134
            monitor.warning(message, e);
135
            logger.error(message);
136
            return false;
137
        }
138
    }
139

    
140
}
(34-34/41)