1
|
/**
|
2
|
* Copyright (C) 2007 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
|
|
10
|
package eu.etaxonomy.cdm.database.types;
|
11
|
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
|
15
|
import org.hibernate.dialect.Dialect;
|
16
|
import org.hibernate.dialect.SQLServer2005Dialect;
|
17
|
|
18
|
import eu.etaxonomy.cdm.database.CdmDataSource;
|
19
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
20
|
|
21
|
|
22
|
/**
|
23
|
* @author a.mueller
|
24
|
*
|
25
|
*/
|
26
|
public class SqlServer2005DatabaseType extends DatabaseTypeBase {
|
27
|
|
28
|
//name
|
29
|
protected String typeName = "SQL Server";
|
30
|
|
31
|
//driver class
|
32
|
protected String classString = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
|
33
|
|
34
|
//url
|
35
|
protected String urlString = "jdbc:sqlserver://";
|
36
|
|
37
|
//[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
|
38
|
|
39
|
//default port
|
40
|
protected int defaultPort = 1433;
|
41
|
|
42
|
//hibernate dialect
|
43
|
protected Dialect hibernateDialect = new SQLServer2005Dialect();
|
44
|
|
45
|
@Override
|
46
|
public String getConnectionString(ICdmDataSource ds, int port){
|
47
|
return getConnectionString(ds, port, null);
|
48
|
}
|
49
|
|
50
|
public String getConnectionString(ICdmDataSource ds, int port, String instanceName){
|
51
|
String instance = "";
|
52
|
if (instanceName != null && ! instanceName.equals("")){
|
53
|
instance = "\\" + instanceName;
|
54
|
}
|
55
|
return urlString + ds.getServer() + instance + ":" + port + ";databaseName=" + ds.getDatabase() +";SelectMethod=cursor";
|
56
|
}
|
57
|
|
58
|
@Override
|
59
|
public String getServerNameByConnectionString(String connectionString) {
|
60
|
String dbSeparator = ";";
|
61
|
return super.getServerNameByConnectionString(connectionString, urlString, dbSeparator);
|
62
|
}
|
63
|
|
64
|
@Override
|
65
|
public int getPortByConnectionString(String connectionString) {
|
66
|
String dbSeparator = ";";
|
67
|
return getPortByConnectionString(connectionString, urlString, dbSeparator);
|
68
|
}
|
69
|
|
70
|
@Override
|
71
|
public String getDatabaseNameByConnectionString(String connectionString){
|
72
|
String result;
|
73
|
String dbStart = ";databaseName=";
|
74
|
int posDbStart = connectionString.indexOf(dbStart);
|
75
|
result = connectionString.substring(posDbStart + dbStart.length());
|
76
|
int posNextAttr = result.indexOf(";");
|
77
|
if (posNextAttr != 0){
|
78
|
result = result.substring(0, posNextAttr);
|
79
|
}
|
80
|
return result;
|
81
|
}
|
82
|
|
83
|
//Constructor
|
84
|
public SqlServer2005DatabaseType() {
|
85
|
init (typeName, classString, urlString, defaultPort, hibernateDialect );
|
86
|
}
|
87
|
|
88
|
|
89
|
/**
|
90
|
* Deletes all foreign keys between tables in a sql server database.
|
91
|
* This makes deleting tables easier.
|
92
|
* @param sqlServerDataSource
|
93
|
* @return
|
94
|
* @throws SQLException
|
95
|
*/
|
96
|
public boolean deleteForeignKeys(CdmDataSource sqlServerDataSource) throws SQLException{
|
97
|
String sql = "SELECT name, id FROM sys.sysobjects WHERE (xtype = 'U')"; //all tables
|
98
|
ResultSet rs = sqlServerDataSource.executeQuery(sql);
|
99
|
while (rs.next()){
|
100
|
String tableName = rs.getString("name");
|
101
|
long tableId = rs.getLong("id");
|
102
|
sql = "SELECT name FROM sys.sysobjects WHERE xtype='F' and parent_obj = " + tableId;//get foreignkeys
|
103
|
ResultSet rsFk = sqlServerDataSource.executeQuery(sql);
|
104
|
while (rsFk.next()){
|
105
|
String fk = rsFk.getString("name");
|
106
|
sql = " ALTER TABLE "+tableName+" DROP CONSTRAINT "+fk + "";
|
107
|
sqlServerDataSource.executeUpdate(sql);
|
108
|
}
|
109
|
|
110
|
}
|
111
|
return true;
|
112
|
}
|
113
|
|
114
|
}
|