diff options
author | James Moger <james.moger@gmail.com> | 2012-01-04 22:12:56 -0500 |
---|---|---|
committer | James Moger <james.moger@gmail.com> | 2012-01-04 22:12:56 -0500 |
commit | 0932e559b420d46f8394a464c466804c1df0ede1 (patch) | |
tree | 822a49e35ce8ff3d7dd37cff84cf9e19bce0e6ae | |
parent | cc0c4f0e0b632ebfb1635e5ed2fe9a2119240157 (diff) | |
download | iciql-0932e559b420d46f8394a464c466804c1df0ede1.tar.gz iciql-0932e559b420d46f8394a464c466804c1df0ede1.zip |
Disallow multiple instances of an enum within a model class
-rw-r--r-- | docs/01_model_classes.mkd | 6 | ||||
-rw-r--r-- | docs/05_releases.mkd | 12 | ||||
-rw-r--r-- | src/com/iciql/Query.java | 5 | ||||
-rw-r--r-- | src/com/iciql/QueryWhere.java | 8 | ||||
-rw-r--r-- | src/com/iciql/TableDefinition.java | 25 | ||||
-rw-r--r-- | tests/com/iciql/test/EnumsTest.java | 18 |
6 files changed, 70 insertions, 4 deletions
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 <td>TIMESTAMP</td></tr>
<tr><td>java.lang.Enum.name()<br/>*default type*</td><td></td>
-<td>VARCHAR *(length > 0)* or CLOB *(length == 0)*<br/>*EnumType.NAME*</td></tr>
+<td>VARCHAR *(length > 0)* or CLOB *(length == 0)*<br/>*EnumType.NAME*<br/><i>can only **declare and explicitly reference** one instance of <u>each enum type</u> per model<br/>multiple instances of an enum type within a model is allowed if not using where/set/on/and/or/groupBy/orderBy(enum)</i></td></tr>
<tr><td>java.lang.Enum.ordinal()</td><td></td>
-<td>INT<br/>*EnumType.ORDINAL*</td></tr>
+<td>INT<br/>*EnumType.ORDINAL*<br/><i>can only **declare and explicitly reference** one instance of <u>each enum type</u> per model<br/>multiple instances of an enum type within a model is allowed if not using where/set/on/and/or/groupBy/orderBy(enum)</i></td></tr>
<tr><td>java.lang.Enum implements<br/>*com.iciql.Iciql.EnumId.enumId()*</td><td></td>
-<td>INT<br/>*EnumType.ENUMID*</td></tr>
+<td>INT<br/>*EnumType.ENUMID*<br/><i>can only **declare and explicitly reference** one instance of <u>each enum type</u> per model<br/>multiple instances of an enum type within a model is allowed if not using where/set/on/and/or/groupBy/orderBy(enum)</i></td></tr>
<tr><td colspan="3"><b>Partially Supported Types</b><br/>
can not be directly referenced in an expression</td></tr>
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.<br/>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<T> { }
public <A> UpdateColumnSet<T, A> set(A field) {
+ from.getAliasDefinition().checkMultipleEnums(field);
return new UpdateColumnSet<T, A>(this, field);
}
@@ -453,6 +454,7 @@ public class Query<T> { * @return a query condition to continue building the condition
*/
public <A> QueryCondition<T, A> where(A x) {
+ from.getAliasDefinition().checkMultipleEnums(x);
return new QueryCondition<T, A>(this, x);
}
@@ -551,6 +553,7 @@ public class Query<T> { }
public Query<T> orderBy(Object expr) {
+ from.getAliasDefinition().checkMultipleEnums(expr);
OrderExpression<T> e = new OrderExpression<T>(this, expr, false, false, false);
addOrderBy(e);
return this;
@@ -566,6 +569,7 @@ public class Query<T> { public Query<T> orderBy(Object... expressions) {
for (Object expr : expressions) {
+ from.getAliasDefinition().checkMultipleEnums(expr);
OrderExpression<T> e = new OrderExpression<T>(this, expr, false, false, false);
addOrderBy(e);
}
@@ -616,6 +620,7 @@ public class Query<T> { }
public Query<T> 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<T> { * @return a query condition to continue building the condition
*/
public <A> QueryCondition<T, A> and(A x) {
+ query.getFrom().getAliasDefinition().checkMultipleEnums(x);
query.addConditionToken(ConditionAndOr.AND);
return new QueryCondition<T, A>(query, x);
}
@@ -220,6 +221,7 @@ public class QueryWhere<T> { * @return a query condition to continue building the condition
*/
public <A> QueryCondition<T, A> or(A x) {
+ query.getFrom().getAliasDefinition().checkMultipleEnums(x);
query.addConditionToken(ConditionAndOr.OR);
return new QueryCondition<T, A>(query, x);
}
@@ -377,6 +379,7 @@ public class QueryWhere<T> { }
public QueryWhere<T> orderBy(Object field) {
+ query.getFrom().getAliasDefinition().checkMultipleEnums(field);
query.orderBy(field);
return this;
}
@@ -395,30 +398,35 @@ public class QueryWhere<T> { }
public QueryWhere<T> orderByNullsFirst(Object expr) {
+ query.getFrom().getAliasDefinition().checkMultipleEnums(expr);
OrderExpression<T> e = new OrderExpression<T>(query, expr, false, true, false);
query.addOrderBy(e);
return this;
}
public QueryWhere<T> orderByNullsLast(Object expr) {
+ query.getFrom().getAliasDefinition().checkMultipleEnums(expr);
OrderExpression<T> e = new OrderExpression<T>(query, expr, false, false, true);
query.addOrderBy(e);
return this;
}
public QueryWhere<T> orderByDesc(Object expr) {
+ query.getFrom().getAliasDefinition().checkMultipleEnums(expr);
OrderExpression<T> e = new OrderExpression<T>(query, expr, true, false, false);
query.addOrderBy(e);
return this;
}
public QueryWhere<T> orderByDescNullsFirst(Object expr) {
+ query.getFrom().getAliasDefinition().checkMultipleEnums(expr);
OrderExpression<T> e = new OrderExpression<T>(query, expr, true, true, false);
query.addOrderBy(e);
return this;
}
public QueryWhere<T> orderByDescNullsLast(Object expr) {
+ query.getFrom().getAliasDefinition().checkMultipleEnums(expr);
OrderExpression<T> e = new OrderExpression<T>(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<T> { 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;
+ }
}
|