diff options
Diffstat (limited to 'jpa/plugins/org.eclipse.jpt.db/src/org/eclipse/jpt/db/internal/Column.java')
-rw-r--r-- | jpa/plugins/org.eclipse.jpt.db/src/org/eclipse/jpt/db/internal/Column.java | 76 |
1 files changed, 74 insertions, 2 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.db/src/org/eclipse/jpt/db/internal/Column.java b/jpa/plugins/org.eclipse.jpt.db/src/org/eclipse/jpt/db/internal/Column.java index 547ba01aad..2f8bbd222c 100644 --- a/jpa/plugins/org.eclipse.jpt.db/src/org/eclipse/jpt/db/internal/Column.java +++ b/jpa/plugins/org.eclipse.jpt.db/src/org/eclipse/jpt/db/internal/Column.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2007 Oracle. All rights reserved. + * 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. @@ -13,8 +13,10 @@ 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; @@ -35,6 +37,14 @@ public final class Column extends DTPWrapper implements Comparable<Column> { 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 ********** @@ -109,6 +119,54 @@ public final class Column extends DTPWrapper implements Comparable<Column> { /** * 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() { @@ -122,11 +180,21 @@ public final class Column extends DTPWrapper implements Comparable<Column> { public JavaType javaType() { DataType dataType = this.dtpColumn.getDataType(); return (dataType instanceof PredefinedDataType) ? - this.jpaSpecCompliantJavaType(DTPTools.javaTypeFor(((PredefinedDataType) dataType).getPrimitiveType())) + 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. @@ -153,6 +221,10 @@ public final class Column extends DTPWrapper implements Comparable<Column> { return this.dtpColumn == column; } + public Database database() { + return this.table.database(); + } + // ********** Comparable implementation ********** public int compareTo( Column column) { |