Skip to main content
summaryrefslogtreecommitdiffstats
blob: f7e08028d4e7de316ffaf0f65c70b17068dfc8a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*******************************************************************************
 * Copyright (c) 2006, 2007 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.db.internal;

import java.util.Properties;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.datatools.connectivity.IManagedConnection;
import org.eclipse.datatools.connectivity.db.generic.IDBDriverDefinitionConstants;
import org.eclipse.datatools.connectivity.drivers.DriverManager;
import org.eclipse.datatools.sqltools.core.DatabaseIdentifier;
import org.eclipse.datatools.sqltools.core.profile.ProfileUtil;

/**
 *  Wrap a DTP ConnectionProfile
 */
public final class DTPConnectionProfileWrapper extends ConnectionProfile {
	
	final private org.eclipse.datatools.connectivity.IConnectionProfile dtpConnectionProfile;
	
    public static final String CONNECTION_TYPE = "java.sql.Connection";  //$NON-NLS-1$
    public static final String CONNECTION_PROFILE_TYPE = "org.eclipse.datatools.connectivity.db.generic.connectionProfile";  //$NON-NLS-1$
    public static final String DATABASE_PRODUCT_PROPERTY = "org.eclipse.datatools.connectivity.server.version";  //$NON-NLS-1$
	/**
	 * This property is used in ConnectionProfile creation.
	 */
	public static final String DATABASE_SAVE_PWD_PROP_ID = IDBDriverDefinitionConstants.PROP_PREFIX + "savePWD"; //$NON-NLS-1$
	/**
	 * This property is used in ConnectionProfile creation.
	 */
	public static final String DRIVER_DEFINITION_PROP_ID = "org.eclipse.datatools.connectivity.driverDefinitionID"; //$NON-NLS-1$
	/**
	 * This property is used in DriverDefinition creation.
	 */
	public static final String DRIVER_DEFINITION_TYPE_PROP_ID = "org.eclipse.datatools.connectivity.drivers.defnType"; //$NON-NLS-1$
	/**
	 * This property is used in DriverDefinition creation.
	 */
	public static final String DRIVER_JAR_LIST_PROP_ID = "jarList"; //$NON-NLS-1$

	public static final String POSTGRESQL_VENDOR = "postgres"; //$NON-NLS-1$

	public static final String PUBLIC_SCHEMA = "public"; //$NON-NLS-1$

	// ********** constructors **********

	DTPConnectionProfileWrapper( ConnectionProfileRepository profileRepository, org.eclipse.datatools.connectivity.IConnectionProfile dtpConnectionProfile) {
		super( profileRepository);
		this.dtpConnectionProfile = dtpConnectionProfile;
	}
	
	// ********** listeners **********

	@Override
	public void addProfileListener( ProfileListener listener) {
		
		this.getProfileRepository().addProfileListener( listener);
	}

	@Override
	public void removeProfileListener( ProfileListener listener) {
		
		this.getProfileRepository().removeProfileListener( listener);
	}
	
	@Override
	public void addConnectionListener( ConnectionListener listener) {
		
		this.getConnection().addConnectionListener( listener);
	}

	@Override
	public void removeConnectionListener( ConnectionListener listener) {

		this.getConnection().removeConnectionListener( listener);
	}
	
	// ********** behavior **********
	
	private IManagedConnection buildDtpManagedConnection( org.eclipse.datatools.connectivity.IConnectionProfile dtpProfile) {
		return dtpProfile.getManagedConnection( CONNECTION_TYPE);
	}
	/**
	 * Connect using this profile.
	 */
	@Override
	public void connect() {
		if( !this.dtpConnectionProfile.isConnected()) {
			
			IStatus status = this.dtpConnectionProfile.connect();
			if( !status.isOK()) {
				if( status.isMultiStatus()) {
					IStatus[] statusChildren = status.getChildren();
					throw new RuntimeException( statusChildren[ 0].getMessage(), statusChildren[ 0].getException());
				}
				throw new RuntimeException( status.getMessage(), status.getException());
			}
		}
	}
	
	@Override
	public void disconnect() {
		
		IStatus status = this.dtpConnectionProfile.disconnect();
		if( !status.isOK()) {
			if( status.isMultiStatus()) {
				IStatus[] statusChildren = status.getChildren();
				throw new RuntimeException( statusChildren[ 0].getMessage(), statusChildren[ 0].getException());
			}
			throw new RuntimeException( status.getMessage(), status.getException());
		}
	}
	
	@Override
	void databaseChanged( Database database, int eventType) {
		this.getConnection().databaseChanged( database, eventType);
		return;
	}
	
	@Override
	 void catalogChanged( Catalog catalog, Database database, int eventType) {
		 //TODO
//		this.getConnection().catalogChanged( catalog, eventType);
		 return;
	}
	
	@Override
	void schemaChanged( Schema schema, Database database, int eventType) {
		this.getConnection().schemaChanged( schema, database, eventType);
	}
		
	@Override
	void tableChanged( Table table, Schema schema, Database database, int eventType) {
		this.getConnection().tableChanged( table, schema, database, eventType);
	}
		
	// ********** queries **********

	@Override
	public boolean isConnected() {

		return this.getConnection().isConnected();
	}
	
	@Override
	public boolean isNull() {
		return false;
	}
	
	@Override
	public String getName() {

		return this.dtpConnectionProfile.getName();
	}
	
	@Override
	public String getDatabaseName() {
		return this.getProperties().getProperty( IDBDriverDefinitionConstants.DATABASE_NAME_PROP_ID);
	}
	
	@Override
	public String getDatabaseProduct() {
		return this.getProperties().getProperty( DATABASE_PRODUCT_PROPERTY);
	}
	
	@Override
	public String getDatabaseVendor() {
		return this.getProperties().getProperty( IDBDriverDefinitionConstants.DATABASE_VENDOR_PROP_ID);
	}
	
	@Override
	public String getDatabaseVersion() {
		return this.getProperties().getProperty( IDBDriverDefinitionConstants.DATABASE_VERSION_PROP_ID);
	}

	@Override
	public String getUserName() {
		return this.getProperties().getProperty( IDBDriverDefinitionConstants.USERNAME_PROP_ID);
	}

	@Override
	public String getUserPassword() {
		return this.getProperties().getProperty( IDBDriverDefinitionConstants.PASSWORD_PROP_ID);
	}

	@Override
	public String getDefaultSchema() {
		if( this.getDatabase().getVendor().equalsIgnoreCase( POSTGRESQL_VENDOR)) {
			return PUBLIC_SCHEMA;
		}
		return this.getUserName();
	}

	@Override
	public String getDriverClass() {
		return this.getProperties().getProperty( IDBDriverDefinitionConstants.DRIVER_CLASS_PROP_ID);
	}

	@Override
	public String getUrl() {
		return this.getProperties().getProperty( IDBDriverDefinitionConstants.URL_PROP_ID);
	}
	
	@Override
	public String getInstanceId() {
		return this.dtpConnectionProfile.getInstanceID();
	}

	@Override
	public String getProviderId() {
		return this.dtpConnectionProfile.getProviderId();
	}

	@Override
	public String getDriverDefinitionId() {
		return this.getProperties().getProperty( DTPConnectionProfileWrapper.DRIVER_DEFINITION_PROP_ID);
	}

	@Override
	public String getDriverJarList() {
		return DriverManager.getInstance().getDriverInstanceByID( this.getDriverDefinitionId()).getJarList();
	}
	
	private Properties getProperties() {
		return this.dtpConnectionProfile.getBaseProperties();
	}
	
	@Override
	protected Connection buildConnection() {

		Connection connection = Connection.createConnection( this.buildDtpManagedConnection( this.dtpConnectionProfile));  //$NON-NLS-1$
		return connection;
	}

	@Override
	protected Database buildDatabase() {
		
		org.eclipse.datatools.modelbase.sql.schema.Database dtpDatabase;
		if( this.isConnected()) {
			//TODO see DTP bug 202306
			//pass connect=true in to ProfileUtil.getDatabase()
			//there is a bug mentioned in a comment: 
            //     "during the profile connected event notification, 
            //     IManagedConnection is connected while IConnectionProfile is not"
			//so some hackery here for their hackery
			dtpDatabase = ProfileUtil.getDatabase( new DatabaseIdentifier( this.getName(), this.getDatabaseName()), true);
			return Database.createDatabase( this, dtpDatabase);
		}
		return NullDatabase.instance();
	}
	
	@Override
	boolean wraps( org.eclipse.datatools.connectivity.IConnectionProfile dtpProfile) {
		return this.dtpConnectionProfile == dtpProfile;
	}

}

Back to the top