Skip to main content
summaryrefslogtreecommitdiffstats
blob: ab7f5656e9b202bfacd8a991645c8b2e46b0086a (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
/*******************************************************************************
 * 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.text.Collator;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.jpt.utility.internal.iterators.TransformationIterator;

/**
 *  Database wrapper base class.
 */
public abstract class Database extends DTPWrapper implements Comparable<Database> {
	
	private boolean caseSensitive = false;  // TODO allow user to configure

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

	static Database createDatabase( ConnectionProfile profile, org.eclipse.datatools.modelbase.sql.schema.Database dtpDatabase) {
		return ( dtpDatabase == null) ? NullDatabase.instance() : new DTPDatabaseWrapper( profile, dtpDatabase);
	}

	Database() {
		super();
	}

	// ********** behavior **********

	abstract void catalogChanged( Catalog catalog, int eventType);

	abstract void schemaChanged( Schema schema, int eventType);

	abstract void tableChanged( Table table,  Schema schema, int eventType);
	
	abstract void refresh();
	
	protected Schema wrap( org.eclipse.datatools.modelbase.sql.schema.Schema schema) {
		return new Schema( this, schema);
	}
	
	protected Catalog wrap( org.eclipse.datatools.modelbase.sql.schema.Catalog catalog) {
		return new Catalog( this, catalog);
	}
	
	// ********** queries **********

	public abstract String getVendor();
	
	public abstract String getVersion();

	/**
	 * return the column for the specified dtp column
	 */
	Column column( org.eclipse.datatools.modelbase.sql.tables.Column dtpColumn) {
		return this.table( dtpColumn.getTable()).column( dtpColumn);
	}
	
	/**
	 * return the table for the specified dtp table
	 */
	Table table( org.eclipse.datatools.modelbase.sql.tables.Table dtpTable) {
		return this.schema( dtpTable.getSchema()).table( dtpTable);
	}

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

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

	// ***** caseSensitive

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

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

	// ***** catalogs

	abstract Set<Catalog> getCatalogs();

	/**
	 * Returns true if this database accepts catalogs.
	 */
	public abstract boolean supportsCatalogs();
	
	/**
	 * Returns the catalog to use by default.
	 */
	public abstract String getDefaultCatalogName();
	
	public Iterator<Catalog> catalogs() {
		return this.getCatalogs().iterator();
	}

	public int catalogSize() {
		return this.getCatalogs().size();
	}

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

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

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

	/**
	 * return the catalog for the specified dtp catalog
	 */
	Catalog catalog( org.eclipse.datatools.modelbase.sql.schema.Catalog dtpCatalog) {
		for ( Iterator<Catalog> stream = this.catalogs(); stream.hasNext(); ) {
			Catalog catalog = stream.next();
			if (catalog.wraps( dtpCatalog)) {
				return catalog;
			}
		}
		throw new IllegalArgumentException( "invalid dtp catalog: " + dtpCatalog);
	}


	// ***** schemata

	abstract Set<Schema> getSchemata();

	public Iterator<Schema> schemata() {
		return this.getSchemata().iterator();
	}

	public int schemataSize() {
		return this.getSchemata().size();
	}

	public boolean schemataContains( Column column) {
		return this.getSchemata().contains( column);
	}

	public Iterator<String> schemaNames() {
		return new TransformationIterator<Schema, String>( this.schemata()) {
			@Override
			protected String transform( Schema schema) {
				 return schema.getName();
			}
		};
	}

	public boolean containsSchemaNamed( String name) {
		return this.schemaNamed( name) != null;
	}

	public Schema schemaNamed( String name) {
		return this.isCaseSensitive() ? this.schemaNamedInternal( name) : this.schemaNamedIgnoreCase( name);
	}
	
	private Schema schemaNamedInternal( String name) {
		for ( Iterator<Schema> stream = this.schemata(); stream.hasNext(); ) {
			Schema schema = stream.next();
			if ( schema.getName().equals( name)) {
				return schema;
			}
		}
		return null;
	}
	
	private Schema schemaNamedIgnoreCase( String name) {
		for ( Iterator<Schema> stream = this.schemata(); stream.hasNext(); ) {
			Schema schema = stream.next();
			if ( StringTools.stringsAreEqualIgnoreCase( schema.getName(), name)) {
				return schema;
			}
		}
		return null;
	}

	/**
	 * return the schema for the specified dtp schema
	 */
	Schema schema( org.eclipse.datatools.modelbase.sql.schema.Schema dtpSchema) {
		for ( Iterator<Schema> stream = this.schemata(); stream.hasNext(); ) {
			Schema schema = stream.next();
			if ( schema.wraps( dtpSchema)) {
				return schema;
			}
		}
		throw new IllegalArgumentException( "invalid dtp schema: " + dtpSchema);
	}

}

Back to the top