From: James Moger Date: Thu, 5 Jan 2012 03:12:56 +0000 (-0500) Subject: Disallow multiple instances of an enum within a model class X-Git-Tag: v0.7.7~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0932e559b420d46f8394a464c466804c1df0ede1;p=iciql.git Disallow multiple instances of an enum within a model class --- diff --git a/docs/01_model_classes.mkd b/docs/01_model_classes.mkd index f534660..3fb1d15 100644 --- a/docs/01_model_classes.mkd +++ b/docs/01_model_classes.mkd @@ -59,13 +59,13 @@ can be used for all iciql expressions TIMESTAMP java.lang.Enum.name()
*default type* -VARCHAR *(length > 0)* or CLOB *(length == 0)*
*EnumType.NAME* +VARCHAR *(length > 0)* or CLOB *(length == 0)*
*EnumType.NAME*
can only **declare and explicitly reference** one instance of each enum type per model
multiple instances of an enum type within a model is allowed if not using where/set/on/and/or/groupBy/orderBy(enum)
java.lang.Enum.ordinal() -INT
*EnumType.ORDINAL* +INT
*EnumType.ORDINAL*
can only **declare and explicitly reference** one instance of each enum type per model
multiple instances of an enum type within a model is allowed if not using where/set/on/and/or/groupBy/orderBy(enum)
java.lang.Enum implements
*com.iciql.Iciql.EnumId.enumId()* -INT
*EnumType.ENUMID* +INT
*EnumType.ENUMID*
can only **declare and explicitly reference** one instance of each enum type per model
multiple instances of an enum type within a model is allowed if not using where/set/on/and/or/groupBy/orderBy(enum)
Partially Supported Types
can not be directly referenced in an expression diff --git a/docs/05_releases.mkd b/docs/05_releases.mkd index 33a3e6a..685120b 100644 --- a/docs/05_releases.mkd +++ b/docs/05_releases.mkd @@ -6,6 +6,18 @@ **%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%* +- added *Query.toSQL()* and *QueryWhere.toSQL()* methods which, when combined with the following new methods, allows for generation of a parameterized, static sql string to be reused with a dynamic query or a PreparedStatement. + - QueryCondition.isParameter() + - QueryCondition.atLeastParameter() + - QueryCondition.atMostParameter() + - QueryCondition.exceedsParameter() + - QueryCondition.lessThanParameter() + - QueryCondition.likeParameter() + - QueryCondition.isNotParameter() +- Disallow **declaring and explicitly referencing** multiple instances of an enum type within a single model.
A runtime exception will be thrown if an attempt to use where/set/on/and/or/groupBy/orderBy(enum) and your model has multiple fields of a single enum type. + +**0.7.6**   *released 2011-12-21* + - Iciql now tries to instantiate a default value from an annotated default value IFF the field object is null, it is specified *nullable = false*, and a defaultValue exists. This only applies to *db.insert* or *db.update*. **0.7.5**   *released 2011-12-12* diff --git a/src/com/iciql/Query.java b/src/com/iciql/Query.java index a8aa8b2..e656c0d 100644 --- a/src/com/iciql/Query.java +++ b/src/com/iciql/Query.java @@ -176,6 +176,7 @@ public class Query { } public UpdateColumnSet set(A field) { + from.getAliasDefinition().checkMultipleEnums(field); return new UpdateColumnSet(this, field); } @@ -453,6 +454,7 @@ public class Query { * @return a query condition to continue building the condition */ public QueryCondition where(A x) { + from.getAliasDefinition().checkMultipleEnums(x); return new QueryCondition(this, x); } @@ -551,6 +553,7 @@ public class Query { } public Query orderBy(Object expr) { + from.getAliasDefinition().checkMultipleEnums(expr); OrderExpression e = new OrderExpression(this, expr, false, false, false); addOrderBy(e); return this; @@ -566,6 +569,7 @@ public class Query { public Query orderBy(Object... expressions) { for (Object expr : expressions) { + from.getAliasDefinition().checkMultipleEnums(expr); OrderExpression e = new OrderExpression(this, expr, false, false, false); addOrderBy(e); } @@ -616,6 +620,7 @@ public class Query { } public Query groupBy(Object expr) { + from.getAliasDefinition().checkMultipleEnums(expr); groupByExpressions.add(expr); return this; } diff --git a/src/com/iciql/QueryWhere.java b/src/com/iciql/QueryWhere.java index 0a07c40..7503ea1 100644 --- a/src/com/iciql/QueryWhere.java +++ b/src/com/iciql/QueryWhere.java @@ -130,6 +130,7 @@ public class QueryWhere { * @return a query condition to continue building the condition */ public QueryCondition and(A x) { + query.getFrom().getAliasDefinition().checkMultipleEnums(x); query.addConditionToken(ConditionAndOr.AND); return new QueryCondition(query, x); } @@ -220,6 +221,7 @@ public class QueryWhere { * @return a query condition to continue building the condition */ public QueryCondition or(A x) { + query.getFrom().getAliasDefinition().checkMultipleEnums(x); query.addConditionToken(ConditionAndOr.OR); return new QueryCondition(query, x); } @@ -377,6 +379,7 @@ public class QueryWhere { } public QueryWhere orderBy(Object field) { + query.getFrom().getAliasDefinition().checkMultipleEnums(field); query.orderBy(field); return this; } @@ -395,30 +398,35 @@ public class QueryWhere { } public QueryWhere orderByNullsFirst(Object expr) { + query.getFrom().getAliasDefinition().checkMultipleEnums(expr); OrderExpression e = new OrderExpression(query, expr, false, true, false); query.addOrderBy(e); return this; } public QueryWhere orderByNullsLast(Object expr) { + query.getFrom().getAliasDefinition().checkMultipleEnums(expr); OrderExpression e = new OrderExpression(query, expr, false, false, true); query.addOrderBy(e); return this; } public QueryWhere orderByDesc(Object expr) { + query.getFrom().getAliasDefinition().checkMultipleEnums(expr); OrderExpression e = new OrderExpression(query, expr, true, false, false); query.addOrderBy(e); return this; } public QueryWhere orderByDescNullsFirst(Object expr) { + query.getFrom().getAliasDefinition().checkMultipleEnums(expr); OrderExpression e = new OrderExpression(query, expr, true, true, false); query.addOrderBy(e); return this; } public QueryWhere orderByDescNullsLast(Object expr) { + query.getFrom().getAliasDefinition().checkMultipleEnums(expr); OrderExpression e = new OrderExpression(query, expr, true, false, true); query.addOrderBy(e); return this; diff --git a/src/com/iciql/TableDefinition.java b/src/com/iciql/TableDefinition.java index f7bbc13..b85b941 100644 --- a/src/com/iciql/TableDefinition.java +++ b/src/com/iciql/TableDefinition.java @@ -415,7 +415,30 @@ public class TableDefinition { void checkMultipleBooleans() { if (multiplePrimitiveBools) { throw new IciqlException( - "Can not explicitly reference multiple primitive booleans in a model class!"); + "Can not explicitly reference a primitive boolean if there are multiple boolean fields in your model class!"); + } + } + + void checkMultipleEnums(Object o) { + if (o == null) { + return; + } + Class clazz = o.getClass(); + if (!clazz.isEnum()) { + return; + } + + int fieldCount = 0; + for (FieldDefinition fieldDef : fields) { + Class targetType = fieldDef.field.getType(); + if (clazz.equals(targetType)) { + fieldCount++; + } + } + + if (fieldCount > 1) { + throw new IciqlException( + "Can not explicitly reference {0} because there are {1} {0} fields in your model class!", clazz.getSimpleName(), fieldCount); } } diff --git a/tests/com/iciql/test/EnumsTest.java b/tests/com/iciql/test/EnumsTest.java index d010683..d8a0589 100644 --- a/tests/com/iciql/test/EnumsTest.java +++ b/tests/com/iciql/test/EnumsTest.java @@ -17,6 +17,7 @@ package com.iciql.test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.util.List; @@ -25,6 +26,7 @@ import org.junit.Before; import org.junit.Test; import com.iciql.Db; +import com.iciql.IciqlException; import com.iciql.test.models.EnumModels; import com.iciql.test.models.EnumModels.EnumIdModel; import com.iciql.test.models.EnumModels.EnumOrdinalModel; @@ -108,4 +110,20 @@ public class EnumsTest { list = db.from(e).where(e.tree()).between(Tree.MAPLE).and(Tree.PINE).select(); assertEquals(3, list.size()); } + + @Test + public void testMultipleEnumInstances() { + BadEnums b = new BadEnums(); + try { + db.from(b).where(b.tree1).is(Tree.BIRCH).and (b.tree2).is(Tree.MAPLE).getSQL(); + assertTrue("Failed to detect multiple Tree fields?!", false); + } catch (IciqlException e) { + assertTrue(e.getMessage(), e.getMessage().startsWith("Can not explicitly reference Tree")); + } + } + + public static class BadEnums { + Tree tree1 = Tree.BIRCH; + Tree tree2 = Tree.MAPLE; + } }