Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 911d334423bed52369a401f953f04ac4d1308a32 (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*******************************************************************************
 * Copyright (c) 2006, 2008 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.text.Collator;
import java.util.Iterator;
import java.util.List;

import org.eclipse.datatools.connectivity.sqm.core.definition.DatabaseDefinition;
import org.eclipse.datatools.connectivity.sqm.internal.core.RDBCorePlugin;
import org.eclipse.jpt.db.Catalog;
import org.eclipse.jpt.db.Database;
import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.jpt.utility.internal.iterators.ArrayIterator;
import org.eclipse.jpt.utility.internal.iterators.TransformationIterator;

/**
 * Wrap a DTP Database.
 * Sometimes the database will directly hold schemata; but if the database
 * supports catalogs, it will not hold the schemata directly, but will delegate
 * to the "default" catalog.
 */
final class DTPDatabaseWrapper
	extends DTPSchemaContainerWrapper
	implements InternalDatabase
{
	// backpointer to parent
	private final DTPConnectionProfileWrapper connectionProfile;

	// the wrapped DTP database
	private final org.eclipse.datatools.modelbase.sql.schema.Database dtpDatabase;

	// lazy-initialized
	private DTPCatalogWrapper[] catalogs;

	// lazy-initialized
	private DTPCatalogWrapper defaultCatalog;
	private boolean defaultCatalogCalculated = false;

	// TODO allow user to configure?
	private boolean caseSensitive = false;


	private static final DTPCatalogWrapper[] EMPTY_CATALOGS = new DTPCatalogWrapper[0];


	// ********** constructor **********

	DTPDatabaseWrapper(DTPConnectionProfileWrapper connectionProfile, org.eclipse.datatools.modelbase.sql.schema.Database dtpDatabase) {
		super(connectionProfile, dtpDatabase);
		this.connectionProfile = connectionProfile;
		this.dtpDatabase = dtpDatabase;
	}


	// ********** DTPWrapper implementation **********

	@Override
	synchronized void catalogObjectChanged(int eventType) {
		super.catalogObjectChanged(eventType);
		this.getConnectionProfile().databaseChanged(this, eventType);
	}


	// ********** Database implementation **********

	@Override
	public String getName() {
		return this.dtpDatabase.getName();
	}

	public String getVendor() {
		return this.dtpDatabase.getVendor();
	}

	public String getVersion() {
		return this.dtpDatabase.getVersion();
	}

	public DatabaseDefinition getDtpDefinition() {
		return RDBCorePlugin.getDefault().getDatabaseDefinitionRegistry().getDefinition(this.dtpDatabase);
	}

	// ***** caseSensitive

	@Override
	public boolean isCaseSensitive() {
		return this.caseSensitive;
	}

	// TODO
	public void setCaseSensitive(boolean caseSensitive) {
		this.caseSensitive = caseSensitive;
	}

	// ***** catalogs

	public boolean supportsCatalogs() {
		// if the DTP database does not have any schemata, it must have catalogs...
		List<org.eclipse.datatools.modelbase.sql.schema.Schema> dtpSchemata = this.dtpSchemata();
		return (dtpSchemata == null) || dtpSchemata.isEmpty();
	}

	public Iterator<Catalog> catalogs() {
		return new ArrayIterator<Catalog>(this.catalogs_());
	}

	private Iterator<DTPCatalogWrapper> catalogWrappers() {
		return new ArrayIterator<DTPCatalogWrapper>(this.catalogs_());
	}

	private synchronized DTPCatalogWrapper[] catalogs_() {
		if (this.catalogs == null) {
			this.catalogs = this.buildCatalogs();
		}
		return this.catalogs;
	}

	private DTPCatalogWrapper[] buildCatalogs() {
		List<org.eclipse.datatools.modelbase.sql.schema.Catalog> dtpCatalogs = this.dtpCatalogs();
		if ((dtpCatalogs == null) || dtpCatalogs.isEmpty()) {
			return EMPTY_CATALOGS;
		}
		DTPCatalogWrapper[] result = new DTPCatalogWrapper[dtpCatalogs.size()];
		for (int i = result.length; i-- > 0;) {
			result[i] = new DTPCatalogWrapper(this, dtpCatalogs.get(i));
		}
		return result;
	}

	// minimize scope of suppressed warnings
	@SuppressWarnings("unchecked")
	private List<org.eclipse.datatools.modelbase.sql.schema.Catalog> dtpCatalogs() {
		return this.dtpDatabase.getCatalogs();
	}

	public int catalogsSize() {
		return this.catalogs_().length;
	}

	public Iterator<String> catalogNames() {
		return new TransformationIterator<DTPCatalogWrapper, String>(this.catalogWrappers()) {
			@Override
			protected String transform(DTPCatalogWrapper catalog) {
				 return catalog.getName();
			}
		};
	}

	public boolean containsCatalogNamed(String name) {
		return this.catalogNamed(name) != null;
	}

	public DTPCatalogWrapper catalogNamed(String name) {
		return this.isCaseSensitive() ? this.catalogNamedCaseSensitive(name) : this.catalogNamedIgnoreCase(name);
	}
	
	private DTPCatalogWrapper catalogNamedCaseSensitive(String name) {
		for (Iterator<DTPCatalogWrapper> stream = this.catalogWrappers(); stream.hasNext(); ) {
			DTPCatalogWrapper catalog = stream.next();
			if (catalog.getName().equals(name)) {
				return catalog;
			}
		}
		return null;
	}
	
	private DTPCatalogWrapper catalogNamedIgnoreCase(String name) {
		for (Iterator<DTPCatalogWrapper> stream = this.catalogWrappers(); stream.hasNext(); ) {
			DTPCatalogWrapper catalog = stream.next();
			if (StringTools.stringsAreEqualIgnoreCase(catalog.getName(), name)) {
				return catalog;
			}
		}
		return null;
	}

	public synchronized DTPCatalogWrapper getDefaultCatalog() {
		if ( ! this.defaultCatalogCalculated) {
			this.defaultCatalogCalculated = true;
			this.defaultCatalog = this.buildDefaultCatalog();
		}
		return this.defaultCatalog;
	}

	private DTPCatalogWrapper buildDefaultCatalog() {
		if ( ! this.supportsCatalogs()) {
			return null;
		}
		String userName = this.connectionProfile.getUserName();
		for (Iterator<DTPCatalogWrapper> stream = this.catalogWrappers(); stream.hasNext(); ) {
			DTPCatalogWrapper catalog = stream.next();
			if (catalog.getName().length() == 0) {
				return catalog;  // special catalog that contains all schemata
			}
			if (catalog.getName().equals(userName)) {
				return catalog;  // user name is default catalog
			}
			if (catalog.getName().equals(this.getName())) {
				return catalog;  // special catalog with same name as DB (PostgreSQL)
			}
		}
		throw new IllegalStateException("unknown default catalog");  //$NON-NLS-1$
	}

	/**
	 * return the catalog for the specified DTP catalog
	 */
	DTPCatalogWrapper catalog(org.eclipse.datatools.modelbase.sql.schema.Catalog dtpCatalog) {
		for (Iterator<DTPCatalogWrapper> stream = this.catalogWrappers(); stream.hasNext(); ) {
			DTPCatalogWrapper catalog = stream.next();
			if (catalog.wraps(dtpCatalog)) {
				return catalog;
			}
		}
		throw new IllegalArgumentException("invalid DTP catalog: " + dtpCatalog);  //$NON-NLS-1$
	}

	// ***** schemata

	@Override
	@SuppressWarnings("unchecked")
	List<org.eclipse.datatools.modelbase.sql.schema.Schema> dtpSchemata() {
		return this.dtpDatabase.getSchemas();
	}

	@Override
	synchronized DTPSchemaWrapper[] schemata_() {
		return (this.supportsCatalogs()) ?
			this.getDefaultCatalog().schemata_()
		:
			super.schemata_();
	}


	// ********** Comparable implementation **********

	public int compareTo(Database database) {
		return Collator.getInstance().compare(this.getName(), database.getName());
	}


	// ********** internal methods **********

	boolean wraps(org.eclipse.datatools.modelbase.sql.schema.Database database) {
		return this.dtpDatabase == database;
	}

	/**
	 * return the table for the specified DTP table
	 */
	DTPTableWrapper table(org.eclipse.datatools.modelbase.sql.tables.Table dtpTable) {
		return this.schema(dtpTable.getSchema()).table(dtpTable);
	}

	/**
	 * return the column for the specified DTP column
	 */
	DTPColumnWrapper column(org.eclipse.datatools.modelbase.sql.tables.Column dtpColumn) {
		return this.table(dtpColumn.getTable()).column(dtpColumn);
	}

	@Override
	DTPDatabaseWrapper database() {
		return this;
	}
	

	// ********** disposal **********

	// must be public because it is defined in InternalDatabase interface
	@Override
	public synchronized void dispose() {
		super.dispose();
	}

	@Override
	void dispose_() {
		this.defaultCatalogCalculated = false;
		this.defaultCatalog = null;
		this.disposeCatalogs();
		super.dispose_();
	}

	private void disposeCatalogs() {
		if (this.catalogs != null) {
			for (DTPCatalogWrapper catalog : this.catalogs) {
				catalog.dispose();
			}
			this.catalogs = null;
		}
	}

}

Back to the top