From 35973f16d6e408fff3e9eeeda4dac4ab3f7cb048 Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 8 Aug 2011 11:12:50 -0400 Subject: Documentation. EnumType default is NAME. --- src/com/iciql/Iciql.java | 53 ++++++++++++++++++++++++++++---------- src/com/iciql/ModelUtils.java | 13 +++++----- src/com/iciql/TableDefinition.java | 3 ++- src/com/iciql/util/Utils.java | 8 ++---- 4 files changed, 49 insertions(+), 28 deletions(-) (limited to 'src/com') diff --git a/src/com/iciql/Iciql.java b/src/com/iciql/Iciql.java index 6abe97d..64c2158 100644 --- a/src/com/iciql/Iciql.java +++ b/src/com/iciql/Iciql.java @@ -33,8 +33,9 @@ import java.lang.annotation.Target; * If a class is annotated with IQTable and at the same time implements Table, * the define() method is not called. *

- * Supported data types: + * Fully Supported Data Types: * + * * * * @@ -88,13 +89,9 @@ import java.lang.annotation.Target; * * * - * - * - * - * * * + * EnumType.NAME * * * @@ -107,10 +104,36 @@ import java.lang.annotation.Target; * * + * + * + * + * * *
All Databases
java.lang.StringVARCHAR (length > 0) or TEXT (length == 0)TIMESTAMP
byte []BLOB
java.lang.Enum.name()VARCHAR (length > 0) or TEXT (length == 0)
- * EnumType.STRING
java.lang.Enum.ordinal()INT
* EnumType.ENUMID
H2 Databases
java.util.UUIDUUID
*

- * Unsupported data types: primitives, Array Types, and custom types. + * Partially Supported Data Types: + *

+ * The following data types can be mapped to columns for all general statements + * BUT these field types may not be used to specify compile-time clauses or + * constraints. + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
byte []BLOB
booleanBIT
byteTINYINT
shortSMALLINT
intINT
longBIGINT
floatREAL
doubleDOUBLE
*

* Table and field mapping: by default, the mapped table name is the class name * and the public fields are reflectively mapped, by their name, to columns. As @@ -403,7 +426,7 @@ public interface Iciql { * Enumeration representing how to map a java.lang.Enum to a column. *

*

@@ -411,7 +434,9 @@ public interface Iciql { * @see com.iciql.Iciql.EnumId interface */ public enum EnumType { - STRING, ORDINAL, ENUMID; + NAME, ORDINAL, ENUMID; + + public static final EnumType DEFAULT_TYPE = NAME; } /** @@ -427,20 +452,20 @@ public interface Iciql { * be overridden for an individual field by specifying the IQEnum * annotation. *

- * The default mapping is by STRING. + * The default mapping is by NAME. * *

-	 * IQEnum(EnumType.STRING)
+	 * IQEnum(EnumType.NAME)
 	 * 
* - * A string mapping will generate either a VARCHAR, if IQColumn.length > - * 0 or a TEXT column if IQColumn.length == 0 + * A string mapping will generate either a VARCHAR, if IQColumn.length > 0 + * or a TEXT column if IQColumn.length == 0 * */ @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD, ElementType.TYPE }) public @interface IQEnum { - EnumType value() default EnumType.STRING; + EnumType value() default EnumType.NAME; } /** diff --git a/src/com/iciql/ModelUtils.java b/src/com/iciql/ModelUtils.java index 51c2d6c..4607e30 100644 --- a/src/com/iciql/ModelUtils.java +++ b/src/com/iciql/ModelUtils.java @@ -28,6 +28,7 @@ import java.util.Map; import java.util.UUID; import java.util.regex.Pattern; +import com.iciql.Iciql.EnumType; import com.iciql.TableDefinition.FieldDefinition; import com.iciql.util.StringUtils; @@ -150,18 +151,16 @@ class ModelUtils { static String getDataType(FieldDefinition fieldDef, boolean strictTypeMapping) { Class fieldClass = fieldDef.field.getType(); if (fieldClass.isEnum()) { - if (fieldDef.enumType == null) { - throw new IciqlException(fieldDef.field.getName() + " enum field does not specify @IQEnum!"); - } switch (fieldDef.enumType) { - case STRING: + case ORDINAL: + case ENUMID: + return "INT"; + case NAME: + default: if (fieldDef.maxLength <= 0) { return "TEXT"; } return "VARCHAR"; - case ORDINAL: - case ENUMID: - return "INT"; } } if (SUPPORTED_TYPES.containsKey(fieldClass)) { diff --git a/src/com/iciql/TableDefinition.java b/src/com/iciql/TableDefinition.java index f0ee4fa..f506484 100644 --- a/src/com/iciql/TableDefinition.java +++ b/src/com/iciql/TableDefinition.java @@ -293,6 +293,7 @@ class TableDefinition { // configure Java -> SQL enum mapping if (f.getType().isEnum()) { + enumType = EnumType.DEFAULT_TYPE; if (f.getType().isAnnotationPresent(IQEnum.class)) { // enum definition is annotated for all instances IQEnum iqenum = f.getType().getAnnotation(IQEnum.class); @@ -343,7 +344,7 @@ class TableDefinition { // convert enumeration to INT or STRING Enum iqenum = (Enum) value; switch (field.enumType) { - case STRING: + case NAME: if (field.trimString && field.maxLength > 0) { if (iqenum.name().length() > field.maxLength) { return iqenum.name().substring(0, field.maxLength); diff --git a/src/com/iciql/util/Utils.java b/src/com/iciql/util/Utils.java index 3846c32..3237742 100644 --- a/src/com/iciql/util/Utils.java +++ b/src/com/iciql/util/Utils.java @@ -33,7 +33,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.IdentityHashMap; -import java.util.List; import java.util.Map; import java.util.UUID; import java.util.concurrent.atomic.AtomicLong; @@ -162,8 +161,6 @@ public class Utils { return clazz.getEnumConstants()[0]; } else if (clazz == java.util.UUID.class) { return (T) UUID.randomUUID(); - } else if (clazz == List.class) { - return (T) new ArrayList(); } try { return clazz.newInstance(); @@ -282,7 +279,7 @@ public class Utils { } public static Object convertEnum(Enum o, EnumType type) { - if (o == null || type == null) { + if (o == null) { return null; } switch (type) { @@ -294,7 +291,7 @@ public class Utils { } EnumId enumid = (EnumId) o; return enumid.enumId(); - case STRING: + case NAME: default: return o.name(); } @@ -308,7 +305,6 @@ public class Utils { if (targetType.isAssignableFrom(currentType)) { return o; } - // convert from VARCHAR/TEXT/INT to Enum Enum[] values = (Enum[]) targetType.getEnumConstants(); if (Clob.class.isAssignableFrom(currentType)) { -- cgit v1.2.3