Project

General

Profile

Download (3.92 KB) Statistics
| Branch: | Tag: | Revision:
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
	private static final Logger logger = Logger.getLogger(TableNameChanger.class);
27
	
28
	private String oldName;
29
	private String newName;
30
	private boolean includeAudTable;
31
	
32
	public static final TableNameChanger NewInstance(String stepName, String oldName, String newName, boolean includeAudTable){
33
		return new TableNameChanger(stepName, oldName, newName, includeAudTable);
34
	}
35
	
36
	protected TableNameChanger(String stepName, String oldName, String newName, boolean includeAudTable) {
37
		super(stepName);
38
		this.oldName = oldName;
39
		this.newName = newName;
40
		this.includeAudTable = includeAudTable;
41
	}
42

    
43
	@Override
44
	public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
45
		boolean result = true;
46
		result &= invokeOnTable(caseType.transformTo(oldName), caseType.transformTo(newName), datasource, monitor);
47
		updateHibernateSequence(datasource, monitor, newName, oldName); //no result&= as hibernateSequence problems may not lead to a complete fail
48
		if (includeAudTable){
49
			String aud = "_AUD";
50
			result &= invokeOnTable(caseType.transformTo(oldName + aud), caseType.transformTo(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
			String message = "Could not perform rename table operation";
77
			monitor.warning("Could not perform rename table operation", e);
78
			logger.warn(message+ ": "  + e.getMessage());
79
			return false;
80
		}
81
		return true;
82
	}
83

    
84
	/**
85
	 * 
86
	 * @param datasource
87
	 * @param monitor
88
	 * @param table
89
	 * @param oldVal
90
	 * @return
91
	 */
92
	private boolean updateHibernateSequence(ICdmDataSource datasource, IProgressMonitor monitor, String newName, String oldName){
93
		try{
94
			//TODO do we need to "case" this table name?
95
			String sql = " UPDATE hibernate_sequences SET sequence_name = '%s' WHERE sequence_name = '%s'";	
96
			datasource.executeUpdate(String.format(sql, newName ,oldName));
97
			return true;
98
		} catch (Exception e) {
99
			String message = "Exception occurred when trying to read or update hibernate_sequences table for value " + this.newName + ": " + e.getMessage();
100
			monitor.warning(message, e);
101
			logger.error(message);
102
			return false;
103
		}
104
		
105
	}
106

    
107
}
(27-27/34)