From 35973f16d6e408fff3e9eeeda4dac4ab3f7cb048 Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 8 Aug 2011 11:12:50 -0400 Subject: [PATCH] Documentation. EnumType default is NAME. --- docs/01_model_classes.mkd | 92 +++++++++++++++---- docs/02_table_versioning.mkd | 2 +- docs/02_usage.mkd | 2 +- docs/05_releases.mkd | 17 +++- 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 +- tests/com/iciql/test/ModelsTest.java | 2 +- tests/com/iciql/test/PrimitivesTest.java | 29 +++--- tests/com/iciql/test/models/EnumModels.java | 2 +- .../com/iciql/test/models/SupportedTypes.java | 8 +- 12 files changed, 166 insertions(+), 65 deletions(-) diff --git a/docs/01_model_classes.mkd b/docs/01_model_classes.mkd index 6e58f0a..846ef6c 100644 --- a/docs/01_model_classes.mkd +++ b/docs/01_model_classes.mkd @@ -5,9 +5,16 @@ Models can be manually written using one of two approaches: *annotation configur Alternatively, model classes can be automatically generated by iciql using the model generation tool. Please see the [tools](tools.html) page for details. -### Supported Data Types - +### Configuration Requirements and Limitations + +1. Your model class **must** provide a public default constructor. +2. Only the specified types are supported. Other types such as arrays and custom types are not supported. +3. Triggers, views, and other advanced database features are not supported. +### Fully Supported Data Types +The following data types can be used for all iciql expressions. +
+ @@ -47,11 +54,8 @@ Alternatively, model classes can be automatically generated by iciql using the m - - - - - + + @@ -59,23 +63,79 @@ Alternatively, model classes can be automatically generated by iciql using the m + + + +
All Databases
java.lang.String VARCHAR *(length > 0)* or TEXT *(length == 0)*
java.util.Date TIMESTAMP
byte []BLOB
java.lang.Enum.name()VARCHAR *(length > 0)* or TEXT *(length == 0)*
*EnumType.STRING*
java.lang.Enum.name()
*default type*
VARCHAR *(length > 0)* or TEXT *(length == 0)*
*EnumType.NAME*
java.lang.Enum.ordinal() INT
*EnumType.ORDINAL*
java.lang.Enum implements
*com.iciql.Iciql.EnumId.enumId()*
INT
*EnumType.ENUMID*
H2 Databases
java.util.UUIDUUID
**NOTE:**
The reverse lookup used for model generation, SQL type -> Java type, contains more mappings.
Please consult the `com.iciql.ModelUtils` class for details. -### Unsupported Types -- Java primitives (use their object counterparts instead) -- array types -- 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
-### Configuration Rules -1. field mappings must be Objects not primitives -2. the model class must have a default public constructor +#### Partially Supported Data Types Example +%BEGINCODE% +class Primitives { + @IQColumn(primaryKey = true) + int id; + + @IQColumn + String name; + + public Primitives() { + } + + public Primitives(int id, String name) { + this.id = id; + this.name = name; + } +} + +Primitives p = new Primitives(); + +// the following expressions compile, but will throw iciql runtime exceptions +db.from(p).where(p.id).is(100).selectFirst(); +db.from(p).where(p.id).atLeast(10).select(); + +// the following expressions will work as expected +db.from(p).select(); +db.from(p).where("id = ?", 100).selectFirst(); +db.from(p).where("id >= ?", 10).select(); +db.insert(new Primitives(150, "test")); +db.update(new Primitives(150, "modified")); +db.delete(new Primitives(150, "test")); +%ENDCODE% -### Configuration Limitations -Triggers, views, and other advanced database features are unimplemented. ## Annotation Configuration The recommended approach to setup a model class is to annotate the class and field declarations. diff --git a/docs/02_table_versioning.mkd b/docs/02_table_versioning.mkd index 4650f74..12cdf6c 100644 --- a/docs/02_table_versioning.mkd +++ b/docs/02_table_versioning.mkd @@ -10,7 +10,7 @@ Your `com.iciql.DbUpgrader` implementation must specify the `IQVersion(version)` ### How does it work? If you choose to use versioning, iciql will maintain a table within your database named *_iq_versions* which is defined as: - CREATE TABLE _IQ_VERSIONS(SCHEMANAME TEXT NOT NULL, TABLENAME TEXT NOT NULL, VERSION INT NOT NULL) + CREATE TABLE _IQ_VERSIONS(SCHEMANAME VARCHAR(255) NOT NULL, TABLENAME VARCHAR(255) NOT NULL, VERSION INT NOT NULL) This database table is automatically created if and only if at least one of your model classes specifies a *version* > 0. diff --git a/docs/02_usage.mkd b/docs/02_usage.mkd index d872ee0..4cca129 100644 --- a/docs/02_usage.mkd +++ b/docs/02_usage.mkd @@ -48,7 +48,7 @@ List<Product> restock = db.from(p).where("unitsInStock=? and productName l #### Db.executeQuery Approaches There may be times when the hybrid approach is still too restrictive and you'd prefer to write straight SQL. You can do that too and use iciql to build objects from your ResultSet, but be careful: -1. Make sure to _select *_ in your query otherwise db.bindResultSet() will throw a RuntimeException +1. Make sure to _select *_ in your query otherwise db.buildObjects() will throw a RuntimeException 2. There is no model class type checking nor field type checking. %BEGINCODE% diff --git a/docs/05_releases.mkd b/docs/05_releases.mkd index eb2c93c..f2f14cc 100644 --- a/docs/05_releases.mkd +++ b/docs/05_releases.mkd @@ -1,8 +1,19 @@ ## Release History ### Current Release + **%VERSION%** ([zip](http://code.google.com/p/iciql/downloads/detail?name=%ZIP%)|[jar](http://code.google.com/p/iciql/downloads/detail?name=%JAR%))   *released %BUILDDATE%* +- api change release (API v3) +- finished enum support (issue 4) +- added UUID type support (H2 databases only) +- added partial primitives support *(primitives may not be used for compile-time condition clauses)* +- added *between(A y).and(A z)* condition syntax + +### Older Releases + +**0.6.2**   *released 2011-08-05* + - api change release (API v2) - fix to versioning to support H2 1.3.158+ - added BLOB support (issue 1) @@ -20,10 +31,8 @@ %ENDCODE% - @IQColumn(maxLength=20) -> @IQColumn(length=20) - @IQColumn(trimString=true) -> @IQColumn(trim=true) - -### Older Releases - -**0.5.0** ([zip](http://code.google.com/p/iciql/downloads/detail?name=iciql-0.5.0.zip)|[jar](http://code.google.com/p/iciql/downloads/detail?name=iciql-0.5.0.jar))   *released 2011-08-03* + +**0.5.0**   *released 2011-08-03* - initial release (API v1) 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)) { diff --git a/tests/com/iciql/test/ModelsTest.java b/tests/com/iciql/test/ModelsTest.java index 75c2073..8a933ae 100644 --- a/tests/com/iciql/test/ModelsTest.java +++ b/tests/com/iciql/test/ModelsTest.java @@ -163,7 +163,7 @@ public class ModelsTest { true); assertEquals(1, models.size()); // a poor test, but a start - assertEquals(1697, models.get(0).length()); + assertEquals(1780, models.get(0).length()); } @Test diff --git a/tests/com/iciql/test/PrimitivesTest.java b/tests/com/iciql/test/PrimitivesTest.java index aee2479..2d591c3 100644 --- a/tests/com/iciql/test/PrimitivesTest.java +++ b/tests/com/iciql/test/PrimitivesTest.java @@ -37,22 +37,27 @@ public class PrimitivesTest { db.insert(model); PrimitivesModel p = new PrimitivesModel(); - + // retrieve model and compare PrimitivesModel retrievedModel = db.from(p).selectFirst(); assertTrue(model.equivalentTo(retrievedModel)); - // retrieve with conditions and compare -// StatementLogger.activateConsoleLogger(); -// retrievedModel = db.from(p).where(p.myLong).is(model.myLong).and(p.myInteger).is(model.myInteger) -// .selectFirst(); -// assertTrue(model.equivalentTo(retrievedModel)); -// -// // update myInteger and compare -// db.from(p).set(p.myInteger).to(10).where(p.myLong).is(model.myLong).update(); -// retrievedModel = db.from(p).selectFirst(); - -// assertEquals(10, retrievedModel.myInteger); + retrievedModel = db.from(p).where("mylong = ? and myinteger = ?", model.myLong, model.myInteger) + .selectFirst(); + assertTrue(model.equivalentTo(retrievedModel)); + + // retrieve with conditions and compare + // StatementLogger.activateConsoleLogger(); + // retrievedModel = + // db.from(p).where(p.myLong).is(model.myLong).and(p.myInteger).is(model.myInteger) + // .selectFirst(); + // assertTrue(model.equivalentTo(retrievedModel)); + // + // // update myInteger and compare + // db.from(p).set(p.myInteger).to(10).where(p.myLong).is(model.myLong).update(); + // retrievedModel = db.from(p).selectFirst(); + + // assertEquals(10, retrievedModel.myInteger); db.close(); } diff --git a/tests/com/iciql/test/models/EnumModels.java b/tests/com/iciql/test/models/EnumModels.java index bd9d88a..e5f6f39 100644 --- a/tests/com/iciql/test/models/EnumModels.java +++ b/tests/com/iciql/test/models/EnumModels.java @@ -127,7 +127,7 @@ public abstract class EnumModels { // override the enumtype to string // ensure that we specify a length so that the column is VARCHAR - @IQEnum(EnumType.STRING) + @IQEnum(EnumType.NAME) @IQColumn(length = 25) private Tree tree; diff --git a/tests/com/iciql/test/models/SupportedTypes.java b/tests/com/iciql/test/models/SupportedTypes.java index b8abf2d..49dd951 100644 --- a/tests/com/iciql/test/models/SupportedTypes.java +++ b/tests/com/iciql/test/models/SupportedTypes.java @@ -96,7 +96,11 @@ public class SupportedTypes { @IQColumn private byte[] myBlob; - @IQEnum(EnumType.STRING) + // test default enum type NAME + @IQColumn(trim = true, length = 25) + private Flower myDefaultFlower; + + @IQEnum(EnumType.NAME) @IQColumn(trim = true, length = 25) private Flower myFavoriteFlower; @@ -139,6 +143,7 @@ public class SupportedTypes { s.mySqlTime = new java.sql.Time(rand.nextLong()); s.mySqlTimestamp = new java.sql.Timestamp(rand.nextLong()); s.myBlob = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + s.myDefaultFlower = Flower.DAFFODIL; s.myFavoriteFlower = Flower.MUM; s.myOtherFavoriteFlower = Flower.MARIGOLD; s.myFavoriteTree = Tree.BIRCH; @@ -162,6 +167,7 @@ public class SupportedTypes { same &= mySqlTime.toString().equals(s.mySqlTime.toString()); same &= myString.equals(s.myString); same &= compare(myBlob, s.myBlob); + same &= myDefaultFlower.equals(s.myDefaultFlower); same &= myFavoriteFlower.equals(s.myFavoriteFlower); same &= myOtherFavoriteFlower.equals(s.myOtherFavoriteFlower); same &= myFavoriteTree.equals(s.myFavoriteTree); -- 2.39.5