Skip to main content
summaryrefslogtreecommitdiffstats
blob: 542b22578cd573ecfb0c4b0e268f75b576ec93bf (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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
 *                                                                            
 * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
 *                                                                            
 * 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:   
 * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation 
 */
package org.eclipse.osbp.xtext.reportdsl.common;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;

import org.eclipse.birt.core.data.DataType;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.osbp.dsl.semantic.common.types.LDataType;
import org.eclipse.osbp.dsl.semantic.common.types.LScalarType;
import org.eclipse.osbp.dsl.semantic.entity.LEntityAttribute;

import org.eclipse.osbp.ui.api.datamart.IDataMart.EType;
import org.eclipse.osbp.xtext.datamartdsl.DatamartAttribute;
import org.eclipse.osbp.xtext.datamartdsl.jvmmodel.DatamartDSLJvmModelInferrer;



public enum DataTypes {
	
	STRING(DataType.STRING_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_STRING, "String", Types.CHAR, String.class),
	BIGDECIMAL(DataType.DECIMAL_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_DECIMAL, "BigDecimal", Types.DECIMAL, BigDecimal.class, Double.class, Float.class),
	TIMESTAMP(DataType.DATE_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_DATETIME, "Timestamp", Types.TIMESTAMP, Timestamp.class),
	BLOB(DataType.BLOB_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_BLOB, "Blob", Types.BLOB /*, .class*/),
	BLOBMAPPING(DataType.BLOB_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_BLOB, "blobMapping", Types.BLOB /*, .class*/),
// ???	CLOB(DesignChoiceConstants.COLUMN_DATA_TYPE_CLOB, "Clob", Types.CLOB /*, .class*/),
	INTEGER(DataType.INTEGER_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_INTEGER, "Integer", Types.INTEGER, Integer.class),
	INT(DataType.INTEGER_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_INTEGER, "Int", Types.INTEGER, Integer.class),
// ??? why not this ???	LONG(DataType.DECIMAL_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_DECIMAL, "Long", Types.DECIMAL, Long.class),
	LONG(DataType.INTEGER_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_INTEGER, "Long", Types.INTEGER, Long.class, Integer.class, Short.class),
	SHORT(DataType.INTEGER_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_INTEGER, "Short", Types.INTEGER, Short.class),
	DATE(DataType.SQL_DATE_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_DATE, "Date", Types.DATE, Date.class),
	TIME(DataType.SQL_TIME_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_TIME, "Time", Types.TIME, Time.class),
	BOOLEAN(DataType.BOOLEAN_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_BOOLEAN, "Boolean", Types.BOOLEAN, Boolean.class),
	DOUBLE(DataType.DOUBLE_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_FLOAT, "Double", Types.DOUBLE, Double.class, Float.class),
	FLOAT(DataType.DOUBLE_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_FLOAT, "Float", Types.DOUBLE, Float.class),
	OBJECT(DataType.JAVA_OBJECT_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_JAVA_OBJECT, "Object", Types.JAVA_OBJECT /*, .class*/),
	BINARY(DataType.BINARY_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_BLOB, "Binary", Types.BINARY /*, .class*/),
	@SuppressWarnings("deprecation")
	ANY(DataType.ANY_TYPE, DesignChoiceConstants.COLUMN_DATA_TYPE_ANY, "any", Types.OTHER);
	
	private DataTypes(int birtDataTypeId, String nativeDataTypeName, String odaScalaDataTypeName, int sqlDataTypeId, Class<?>...supportedClasses) {
		fBirtDataTypeId = birtDataTypeId;
		fNativeOdaDataTypeName = nativeDataTypeName;
		fOdaScalaDataTypeName = odaScalaDataTypeName;
		fNativeOdaDataTypeId = sqlDataTypeId;
		fSupportedClasses = supportedClasses;
	}
	
	public String getNativeOdaDataTypeName() {
		return fNativeOdaDataTypeName;
	}
	public String getOdaScalaDataTypeName() {
		return fOdaScalaDataTypeName;
	}
	public int getNativeOdaDataTypeId() {
		return fNativeOdaDataTypeId;
	}
	public int getBirtDataTypeId() {
		return fBirtDataTypeId;
	}
	public String getBirtDataTypeName() {
		return DataType.getName(fBirtDataTypeId);
	}

	/**
	 * @see http://docs.oracle.com/cd/E13190_01/liquiddata/docs81/xquery/datatypes.html
	 * @see http://docs.oracle.com/cd/B19306_01/java.102/b14188/datamap.htm#CHDBJAGH
	 * @param sqlPlSqlDataType
	 * @return
	 */
	public static DataTypes typeofSqlPlSQLDataType(String sqlPlSqlDataType) {
		switch (sqlPlSqlDataType.toUpperCase()) {
			case "CHAR":
			case "CHARACTER":
			case "STRING":
			case "VARCHAR":
			case "VARCHAR2":
				return STRING; // oracle.sql.CHAR / java.lang.String
			case "NCHAR":
			case "NVARCHAR2":
				return STRING; // oracle.sql.NCHAR / oracle.sql.NString
			case "NCLOB":
				return STRING; // oracle.sql.NCLOB / oracle.sql.NCLOB
			case "RAW":
			case "LONG RAW":
				return BLOB; // oracle.sql.RAW / byte[]
			case "LONG":
				return LONG;
			case "BINARY_INTEGER":
			case "NATURAL":
			case "NATURALN":
			case "PLS_INTEGER":
			case "POSITIVE":
			case "POSITIVEN":
			case "SIGNTYPE":
			case "INT":
			case "INTEGER":
				return INTEGER; // oracle.sql.NUMBER / int
			case "SHORT":
				return SHORT; // oracle.sql.NUMBER / int
			case "DEC":
			case "DECIMAL":
			case "NUMBER":
			case "NUMERIC":
				return BIGDECIMAL; // oracle.sql.NUMBER / java.math.BigDecimal
			case "DOUBLE PRECISION":
			case "FLOAT":
				return BIGDECIMAL; // oracle.sql.NUMBER / double
			case "SMALLINT":
				return INTEGER; // oracle.sql.NUMBER / int
			case "REAL":
				return BIGDECIMAL; // oracle.sql.NUMBER / float
			case "DATE":
				return DATE; // oracle.sql.DATE / java.sql.Timestamp
			case "TIMESTAMP":
			case "TIMESTAMP WITH TZ":
			case "TIMESTAMP WITH LOCAL TZ":
				return TIMESTAMP; // oracle.sql.TIMESTAMP / oracle.sql.TIMESTAMPTZ / oracle.sql.TIMESTAMPLTZ / java.sql.Timestamp
			case "INTERVAL YEAR TO MONTH":
			case "INTERVAL DAY TO SECOND":
				return STRING; // String
//			case "ROWID":
//			case "UROWID":
//				// oracle.sql.ROWID / oracle.sql.ROWID
			case "BOOLEAN":
				return BOOLEAN; // boolean / boolean
			case "CLOB":
				return STRING; // oracle.sql.CLOB / java.sql.Clob
			case "BLOB":
				return BLOB; // oracle.sql.BLOB / java.sql.Blob
			case "BFILE":
				return BLOB; // oracle.sql.BFILE / oracle.sql.BFILE
//Object types / Generated class / Generated class
//SQLJ object types / Java class defined at type creation / Java class defined at type creation
//OPAQUE types / Generated or predefined class / Generated or predefined class
//RECORD types / Through mapping to SQL object type / Through mapping to SQL object type
//Nested table, VARRAY / Generated class implemented using oracle.sql.ARRAY / java.sql.Array
//Reference to object type / Generated class implemented using oracle.sql.REF / java.sql.Ref
//REF CURSOR / java.sql.ResultSet / java.sql.ResultSet
//Index-by tables / Through mapping to SQL collection / Through mapping to SQL collection
//Scalar (numeric or character) / Index-by tables / Through mapping to Java array / Through mapping to Java array
//User-defined subtypes / Same as for base type / Same as for base type
		}
		return ANY;
	}
	
	public static DataTypes typeOf(String dataType) {
		if	(dataType != null) {
		for	(DataTypes step : values()) {
			if	(dataType.equalsIgnoreCase(step.fNativeOdaDataTypeName)) {
				return step;
			}
			if	(dataType.equalsIgnoreCase(step.fOdaScalaDataTypeName)) {
				return step;
			}
			if	(dataType.equalsIgnoreCase(step.getBirtDataTypeName())) {
				return step;
				}
			}
		}
		return ANY;
	}
	
	public static DataTypes typeOfNativeDataType(String nativeDataType) {
		for	(DataTypes step : values()) {
			if	(nativeDataType.equalsIgnoreCase(step.fNativeOdaDataTypeName)) {
				return step;
			}
		}
		return ANY;
	}

	public static DataTypes typeOfOdaScalaDataType(String odaScalaDataType) {
		for	(DataTypes step : values()) {
			if	(odaScalaDataType.equalsIgnoreCase(step.fOdaScalaDataTypeName)) {
				return step;
			}
		}
		return ANY;
	}

	public static DataTypes typeOfBirtDataType(String birtDataType) {
		for	(DataTypes step : values()) {
			if	(birtDataType.equalsIgnoreCase(step.getBirtDataTypeName())) {
				return step;
			}
		}
		return ANY;
	}

	public static DataTypes typeOfNativeOdaDataType(int nativeOdaDataType) {
		for	(DataTypes step : values()) {
			if	(nativeOdaDataType == step.fNativeOdaDataTypeId) {
				return step;
			}
		}
		return ANY;
	}

	public static DataTypes typeOfBirtDataType(int birtDataType) {
		for	(DataTypes step : values()) {
			if	(birtDataType == step.fBirtDataTypeId) {
				return step;
			}
		}
		return ANY;
	}

	/**
	 * @see {@link DatamartDSLJvmModelInferrer#evaluateExpression(org.eclipse.osbp.xtext.datamartdsl.Expression, org.eclipse.osbp.xtext.datamartdsl.DatamartEntity)}
	 * DatamartEntity.properties == DatamartAttribute
	 * <pre>
	if (element instanceof DatamartAttribute) {
		evaluation = '''«(element as DatamartAttribute).getAttributeName(false, entity)»'''
		var type = (element as DatamartAttribute).propertyRef.type
		var typeName = ""
		if (type != null && (type instanceof LDataType) && (type as LDataType).jvmTypeReference!=null){
			typeName = (type as LDataType).jvmTypeReference.simpleName
		} else {
			typeName = type.name
		}
		if (!typeName.equals("String")) {
			evaluation = '''to_char(«evaluation»)'''
		}   
	}
		</pre>
	 * 
	 */
	public static DataTypes typeOfOdaScalaDataType(DatamartAttribute datamartAttribute) {
		LEntityAttribute entityAttribute = datamartAttribute.getAttributeRef();
		LScalarType scalarType = null;
		Class<?> findClass = null;
		String typeName = null;
		if	(entityAttribute != null) {
			scalarType = entityAttribute.getType();
		}
		if	(scalarType instanceof LDataType) {
			findClass = scalarType.getClass();
			if	(findClass != null) {
				typeName = findClass.getSimpleName();
			}
		}
		else if	(scalarType != null) {
			typeName = scalarType.getName();
		}
		return typeOfNativeDataType(typeName);
	}

	public static DataTypes typeFor(LDataType dataType) {
		if	(dataType == null) {
			return DataTypes.ANY;
		}
		if	(dataType.isDate()) {
			return DataTypes.DATE;
		}
		return typeOf(dataType.getName());
	}

	public static DataTypes typeFor(EType eType) {
		switch (eType) {
			case BOOLEAN:
				return DataTypes.BOOLEAN;
			case DATE:
				return DataTypes.DATE;
			case FLOAT:
				return DataTypes.FLOAT;
			case DOUBLE:
				return DataTypes.DOUBLE;
			case INTEGER:
				return DataTypes.INT;
			case SHORT:
				return DataTypes.SHORT;
			case LONG:
				return DataTypes.LONG;
			case STRING:
				return DataTypes.STRING;
			case TIME:
				return DataTypes.TIME;
			case BLOPMAPPING:
				return DataTypes.BLOBMAPPING;
			case LENUM:
				return DataTypes.INT;
		}
		return DataTypes.ANY;
	}

	public static DataTypes typeFor(Object object) {
		if	(object instanceof EType) {
			return typeFor((EType)object);
		}
		else if	(object instanceof LDataType) {
			return typeFor((LDataType)object);
		}
		else if	(object == null) {
			return ANY;
		}
		else {
			DataTypes retcode = typeFor(object.getClass());
			if	(retcode == null) {
				return ANY;
			}
			else {
				return retcode;
			}
		}
	}

	public static DataTypes typeFor(Class<?> findClass) {
		for	(DataTypes step : values()) {
			if	(findClass.equals(DataType.getClass(step.fBirtDataTypeId))) {
				return step;
			}
			else if	(step.fSupportedClasses != null) {
				for	(Class<?> supportedClass : step.fSupportedClasses) {
					if	(findClass.equals(supportedClass)) {
						return step;
					}
				}
			}
		}
		return ANY;
	}
	
	/** see {@link org.eclipse.birt.core.data.DataType} */
	private final int fBirtDataTypeId;
	/** see <code>org.eclipse.osbp.xtext.reportdsl.oda.datamart/plugin.xml:dataTypeMapping</code> */
	private final int fNativeOdaDataTypeId;
	/** see <code>org.eclipse.osbp.xtext.reportdsl.oda.datamart/plugin.xml:dataTypeMapping</code>
	 *  see for example {@link org.eclipse.birt.report.model.api.elements.DesignChoiceConstants.COLUMN_DATA_TYPE_STRING}
	 */
	private final String fNativeOdaDataTypeName;
	/** see {@link java.sql.Types} has to be defined in org.eclipse.osbp.xtext.reportdsl.ord.datamart / pugin.xml */
	private final String fOdaScalaDataTypeName;
	private final Class<?>[] fSupportedClasses;
}

Back to the top