Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2f8bbd222cece40e79bf34f7d234de71e1a766cb (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
/*******************************************************************************
 * 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 org.eclipse.datatools.connectivity.sqm.core.rte.ICatalogObject;
import org.eclipse.datatools.connectivity.sqm.core.rte.ICatalogObjectListener;
import org.eclipse.datatools.modelbase.dbdefinition.PredefinedDataTypeDefinition;
import org.eclipse.datatools.modelbase.sql.datatypes.DataType;
import org.eclipse.datatools.modelbase.sql.datatypes.PredefinedDataType;
import org.eclipse.jpt.utility.internal.ClassTools;
import org.eclipse.jpt.utility.internal.JavaType;
import org.eclipse.jpt.utility.internal.NameTools;

/**
 *  Wrap a DTP Column
 */
public final class Column extends DTPWrapper implements Comparable<Column> {
	private final Table table;
	private final org.eclipse.datatools.modelbase.sql.tables.Column dtpColumn;
	private ICatalogObjectListener columnListener;

	// TODO Object is the default?
	private static final JavaType DEFAULT_JAVA_TYPE = new JavaType(java.lang.Object.class);

	private static final JavaType BLOB_JAVA_TYPE = new JavaType(java.sql.Blob.class);
	private static final JavaType BYTE_ARRAY_JAVA_TYPE = new JavaType(byte[].class);

	private static final JavaType CLOB_JAVA_TYPE = new JavaType(java.sql.Clob.class);
	private static final JavaType STRING_JAVA_TYPE = new JavaType(java.lang.String.class);

	private static final JavaType UTIL_DATE_JAVA_TYPE = new JavaType(java.util.Date.class);
	private static final JavaType SQL_DATE_JAVA_TYPE = new JavaType(java.sql.Date.class);
	private static final JavaType SQL_TIME_JAVA_TYPE = new JavaType(java.sql.Time.class);
	private static final JavaType SQL_TIMESTAMP_JAVA_TYPE = new JavaType(java.sql.Timestamp.class);

	private static final JavaType BIG_DECIMAL_JAVA_TYPE = new JavaType(java.math.BigDecimal.class);
	private static final JavaType LONG_JAVA_TYPE = new JavaType(long.class);


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

	Column( Table table, org.eclipse.datatools.modelbase.sql.tables.Column dtpColumn) {
		super();
		this.table = table;
		this.dtpColumn = dtpColumn;
		this.initialize();
	}

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

	private void initialize() {
		if( this.connectionIsOnline()) {
			this.columnListener = this.buildColumnListener();
			this.addCatalogObjectListener(( ICatalogObject) this.dtpColumn, this.columnListener);
		}
	}
	
	@Override
	protected boolean connectionIsOnline() {
		return this.table.connectionIsOnline();
	}
	
	private ICatalogObjectListener buildColumnListener() {
       return new ICatalogObjectListener() {
    	    public void notifyChanged( final ICatalogObject column, final int eventType) { 
//				TODO
//    			if( column == Column.this.dtpColumn) {	    	    	
//    				Column.this.table.columnChanged( Column.this, eventType);
//    			}
    	    }
        };
    }
	
	@Override
	protected void dispose() {
		
		this.removeCatalogObjectListener(( ICatalogObject) this.dtpColumn, this.columnListener);
	}
	
	// ********** queries **********

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

	boolean isCaseSensitive() {
		return this.table.isCaseSensitive();
	}

	public String dataTypeName() {
		DataType dataType = this.dtpColumn.getDataType();
		return (dataType == null) ? null : dataType.getName();
	}

	public String javaFieldName() {
		String jName = this.getName();
		if ( ! this.isCaseSensitive()) {
			jName = jName.toLowerCase();
		}
		return NameTools.convertToJavaIdentifier(jName);
	}

	public boolean matchesJavaFieldName(String javaFieldName) {
		return this.isCaseSensitive() ?
			this.getName().equals(javaFieldName)
		:
			this.getName().equalsIgnoreCase(javaFieldName);
	}

	/**
	 * Return a Java type declaration that is reasonably
	 * similar to the column's data type and suitable for use as a
	 * primary key field.
	 */
	public String primaryKeyJavaTypeDeclaration() {
		return this.primaryKeyJavaType().declaration();
	}

	/**
	 * Return a Java type that is reasonably
	 * similar to the column's data type and suitable for use as a
	 * primary key field.
	 */
	public JavaType primaryKeyJavaType() {
		return this.jpaSpecCompliantPrimaryKeyJavaType(this.javaType());
	}

	/**
	 * The JPA spec [2.1.4] says only the following types are allowed in
	 * primary key fields:
	 *     [variable] primitives
	 *     [variable] primitive wrappers
	 *     java.lang.String
	 *     java.util.Date
	 *     java.sql.Date
	 */
	private JavaType jpaSpecCompliantPrimaryKeyJavaType(JavaType javaType) {
		if (javaType.isVariablePrimitive()
				|| javaType.isVariablePrimitiveWrapper()
				|| javaType.equals(STRING_JAVA_TYPE)
				|| javaType.equals(UTIL_DATE_JAVA_TYPE)
				|| javaType.equals(SQL_DATE_JAVA_TYPE)) {
			return javaType;
		}
		if (javaType.equals(BIG_DECIMAL_JAVA_TYPE)) {
			return LONG_JAVA_TYPE;  // ??
		}
		if (javaType.equals(SQL_TIME_JAVA_TYPE)) {
			return UTIL_DATE_JAVA_TYPE;  // ???
		}
		if (javaType.equals(SQL_TIMESTAMP_JAVA_TYPE)) {
			return UTIL_DATE_JAVA_TYPE;  // ???
		}
		// all the other typical types are pretty much un-mappable - return String(?)
		return STRING_JAVA_TYPE;
	}

	/**
	 * Return a Java type declaration that is reasonably
	 * similar to the column's data type.
	 */
	public String javaTypeDeclaration() {
		return this.javaType().declaration();
	}

	/**
	 * Return a Java type that is reasonably
	 * similar to the column's data type.
	 */
	public JavaType javaType() {
		DataType dataType = this.dtpColumn.getDataType();
		return (dataType instanceof PredefinedDataType) ?
			this.jpaSpecCompliantJavaType(this.javaType((PredefinedDataType) dataType))
		:
			DEFAULT_JAVA_TYPE;
	}

	private JavaType javaType(PredefinedDataType dataType) {
		// this is just a bit hacky: moving from a type declaration to a class name to a type declaration...
		String dtpJavaClassName = this.predefinedDataTypeDefinition(dataType).getJavaClassName();
		return new JavaType(ClassTools.classNameForTypeDeclaration(dtpJavaClassName));
	}

	private PredefinedDataTypeDefinition predefinedDataTypeDefinition(PredefinedDataType dataType) {
		return this.database().dtpDefinition().getPredefinedDataTypeDefinition(dataType.getName());
	}

	/**
	 * The JDBC spec says JDBC drivers should be able to map BLOBs and CLOBs
	 * directly, but the JPA spec does not allow them.
	 */
	private JavaType jpaSpecCompliantJavaType(JavaType javaType) {
		if (javaType.equals(BLOB_JAVA_TYPE)) {
			return BYTE_ARRAY_JAVA_TYPE;
		}
		if (javaType.equals(CLOB_JAVA_TYPE)) {
			return STRING_JAVA_TYPE;
		}
		return javaType;
	}

	public boolean isLob() {
		DataType dataType = this.dtpColumn.getDataType();
		return (dataType instanceof PredefinedDataType) ?
			DTPTools.dataTypeIsLob(((PredefinedDataType) dataType).getPrimitiveType())
		:
			false;
	}

	boolean wraps( org.eclipse.datatools.modelbase.sql.tables.Column column) {
		return this.dtpColumn == column;
	}

	public Database database() {
		return this.table.database();
	}

	// ********** Comparable implementation **********
	
	public int compareTo( Column column) {
		return Collator.getInstance().compare( this.getName(), column.getName());
	}
}

Back to the top