diff options
author | James Moger <james.moger@gmail.com> | 2011-08-08 12:03:10 -0400 |
---|---|---|
committer | James Moger <james.moger@gmail.com> | 2011-08-08 12:03:10 -0400 |
commit | d5a74dcac41eeb96a656b7033e43d51b92f75d18 (patch) | |
tree | 2fb7d174ddbf0c2fea64666917d992daae00398f | |
parent | 35973f16d6e408fff3e9eeeda4dac4ab3f7cb048 (diff) | |
download | iciql-d5a74dcac41eeb96a656b7033e43d51b92f75d18.tar.gz iciql-d5a74dcac41eeb96a656b7033e43d51b92f75d18.zip |
Fixed first enum constant special case. (issue 4)
-rw-r--r-- | src/com/iciql/Query.java | 9 | ||||
-rw-r--r-- | tests/com/iciql/test/EnumsTest.java | 111 | ||||
-rw-r--r-- | tests/com/iciql/test/IciqlSuite.java | 5 | ||||
-rw-r--r-- | tests/com/iciql/test/ModelsTest.java | 48 | ||||
-rw-r--r-- | tests/com/iciql/test/models/EnumModels.java | 6 |
5 files changed, 126 insertions, 53 deletions
diff --git a/src/com/iciql/Query.java b/src/com/iciql/Query.java index 2d31e8d..2e58fe5 100644 --- a/src/com/iciql/Query.java +++ b/src/com/iciql/Query.java @@ -359,6 +359,15 @@ public class Query<T> { token.appendSQL(stat, this);
return;
}
+ if (alias != null && value.getClass().isEnum()) {
+ // special case:
+ // value is first enum constant which is also the alias object.
+ // the first enum constant is used as the alias because we can not
+ // instantiate an enum reflectively.
+ stat.appendSQL("?");
+ addParameter(stat, alias, value);
+ return;
+ }
SelectColumn<T> col = aliasMap.get(value);
if (col != null) {
col.appendSQL(stat);
diff --git a/tests/com/iciql/test/EnumsTest.java b/tests/com/iciql/test/EnumsTest.java new file mode 100644 index 0000000..b66aa9e --- /dev/null +++ b/tests/com/iciql/test/EnumsTest.java @@ -0,0 +1,111 @@ +/*
+ * Copyright 2011 James Moger.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.iciql.test;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.iciql.Db;
+import com.iciql.test.models.EnumModels;
+import com.iciql.test.models.EnumModels.EnumIdModel;
+import com.iciql.test.models.EnumModels.EnumOrdinalModel;
+import com.iciql.test.models.EnumModels.EnumStringModel;
+import com.iciql.test.models.EnumModels.Tree;
+
+/**
+ * Tests enum support.
+ */
+public class EnumsTest {
+
+ private Db db;
+
+ @Before
+ public void setUp() {
+ db = Db.open("jdbc:h2:mem:", "sa", "sa");
+ db.insertAll(EnumIdModel.createList());
+ db.insertAll(EnumOrdinalModel.createList());
+ db.insertAll(EnumStringModel.createList());
+ }
+
+ @After
+ public void tearDown() {
+ db.close();
+ }
+
+ @Test
+ public void testEnumQueries() {
+ testIntEnums(new EnumIdModel());
+ testIntEnums(new EnumOrdinalModel());
+ testStringEnums(new EnumStringModel());
+ }
+
+ private void testIntEnums(EnumModels e) {
+ // ensure all records inserted
+ long count = db.from(e).selectCount();
+ assertEquals(5, count);
+
+ // special case:
+ // value is first enum constant which is also the alias object.
+ // the first enum constant is used as the alias because we can not
+ // instantiate an enum reflectively.
+ EnumModels firstEnumValue = db.from(e).where(e.tree()).is(Tree.PINE).selectFirst();
+ assertEquals(Tree.PINE, firstEnumValue.tree());
+
+ EnumModels model = db.from(e).where(e.tree()).is(Tree.WALNUT).selectFirst();
+
+ assertEquals(400, model.id.intValue());
+ assertEquals(Tree.WALNUT, model.tree());
+
+ List<EnumModels> list = db.from(e).where(e.tree()).atLeast(Tree.BIRCH).select();
+ assertEquals(3, list.size());
+
+ // between is an int compare
+ list = db.from(e).where(e.tree()).between(Tree.BIRCH).and(Tree.WALNUT).select();
+ assertEquals(2, list.size());
+
+ }
+
+ private void testStringEnums(EnumModels e) {
+ // ensure all records inserted
+ long count = db.from(e).selectCount();
+ assertEquals(5, count);
+
+ // special case:
+ // value is first enum constant which is also the alias object.
+ // the first enum constant is used as the alias because we can not
+ // instantiate an enum reflectively.
+ EnumModels firstEnumValue = db.from(e).where(e.tree()).is(Tree.PINE).selectFirst();
+ assertEquals(Tree.PINE, firstEnumValue.tree());
+
+ EnumModels model = db.from(e).where(e.tree()).is(Tree.WALNUT).selectFirst();
+
+ assertEquals(400, model.id.intValue());
+ assertEquals(Tree.WALNUT, model.tree());
+
+ List<EnumModels> list = db.from(e).where(e.tree()).isNot(Tree.BIRCH).select();
+ assertEquals(count - 1, list.size());
+
+ // between is a string compare
+ list = db.from(e).where(e.tree()).between(Tree.MAPLE).and(Tree.PINE).select();
+ assertEquals(3, list.size());
+ }
+}
diff --git a/tests/com/iciql/test/IciqlSuite.java b/tests/com/iciql/test/IciqlSuite.java index 056c996..c9dab77 100644 --- a/tests/com/iciql/test/IciqlSuite.java +++ b/tests/com/iciql/test/IciqlSuite.java @@ -26,8 +26,9 @@ import org.junit.runners.Suite.SuiteClasses; */
@RunWith(Suite.class)
@SuiteClasses({ AliasMapTest.class, AnnotationsTest.class, BooleanModelTest.class, ClobTest.class,
- ConcurrencyTest.class, ModelsTest.class, PrimitivesTest.class, RuntimeQueryTest.class,
- SamplesTest.class, StatementLoggerTest.class, UpdateTest.class, UUIDTest.class })
+ ConcurrencyTest.class, EnumsTest.class, ModelsTest.class, PrimitivesTest.class,
+ RuntimeQueryTest.class, SamplesTest.class, StatementLoggerTest.class, UpdateTest.class,
+ UUIDTest.class })
public class IciqlSuite {
public static void assertStartsWith(String a, String b) {
diff --git a/tests/com/iciql/test/ModelsTest.java b/tests/com/iciql/test/ModelsTest.java index 8a933ae..d5edcfd 100644 --- a/tests/com/iciql/test/ModelsTest.java +++ b/tests/com/iciql/test/ModelsTest.java @@ -36,17 +36,11 @@ import com.iciql.DbUpgrader; import com.iciql.DbVersion; import com.iciql.Iciql.IQVersion; import com.iciql.ValidationRemark; -import com.iciql.test.models.EnumModels; -import com.iciql.test.models.EnumModels.EnumIdModel; -import com.iciql.test.models.EnumModels.EnumOrdinalModel; -import com.iciql.test.models.EnumModels.EnumStringModel; -import com.iciql.test.models.EnumModels.Tree; import com.iciql.test.models.Product; import com.iciql.test.models.ProductAnnotationOnly; import com.iciql.test.models.ProductMixedAnnotation; import com.iciql.test.models.SupportedTypes; import com.iciql.test.models.SupportedTypes.SupportedTypes2; -import com.iciql.util.StatementLogger; /** * Test that the mapping between classes and tables is done correctly. @@ -113,48 +107,6 @@ public class ModelsTest { } @Test - public void testEnumQueries() { - StatementLogger.activateConsoleLogger(); - testIntEnums(new EnumIdModel(), EnumIdModel.createList()); - testIntEnums(new EnumOrdinalModel(), EnumOrdinalModel.createList()); - testStringEnums(new EnumStringModel(), EnumStringModel.createList()); - StatementLogger.deactivateConsoleLogger(); - } - - private void testIntEnums(EnumModels e, List<?> models) { - db.insertAll(models); - - EnumModels model = db.from(e).where(e.tree()).is(Tree.WALNUT).selectFirst(); - - assertEquals(400, model.id.intValue()); - assertEquals(Tree.WALNUT, model.tree()); - - List<EnumModels> list = db.from(e).where(e.tree()).atLeast(Tree.BIRCH).select(); - assertEquals(3, list.size()); - - // between is an int compare - list = db.from(e).where(e.tree()).between(Tree.BIRCH).and(Tree.WALNUT).select(); - assertEquals(2, list.size()); - - } - - private void testStringEnums(EnumModels e, List<?> models) { - db.insertAll(models); - - EnumModels model = db.from(e).where(e.tree()).is(Tree.WALNUT).selectFirst(); - - assertEquals(400, model.id.intValue()); - assertEquals(Tree.WALNUT, model.tree()); - - List<EnumModels> list = db.from(e).where(e.tree()).isNot(Tree.BIRCH).select(); - assertEquals(models.size() - 1, list.size()); - - // between is a string compare - list = db.from(e).where(e.tree()).between(Tree.MAPLE).and(Tree.PINE).select(); - assertEquals(3, list.size()); - } - - @Test public void testModelGeneration() { List<SupportedTypes> original = SupportedTypes.createList(); db.insertAll(original); diff --git a/tests/com/iciql/test/models/EnumModels.java b/tests/com/iciql/test/models/EnumModels.java index e5f6f39..40b7806 100644 --- a/tests/com/iciql/test/models/EnumModels.java +++ b/tests/com/iciql/test/models/EnumModels.java @@ -54,7 +54,7 @@ public abstract class EnumModels { @IQColumn(primaryKey = true)
public Integer id;
-
+
public abstract Tree tree();
/**
@@ -106,7 +106,7 @@ public abstract class EnumModels { this.id = id;
this.tree = tree;
}
-
+
@Override
public Tree tree() {
return tree;
@@ -143,7 +143,7 @@ public abstract class EnumModels { public Tree tree() {
return tree;
}
-
+
public static List<EnumStringModel> createList() {
return Arrays.asList(new EnumStringModel(400, Tree.WALNUT), new EnumStringModel(200, Tree.OAK),
new EnumStringModel(500, Tree.MAPLE), new EnumStringModel(300, Tree.BIRCH),
|