}\r
\r
public <T> void appendSQL(SQLStatement stat, Query<T> query) {\r
- query.appendSQL(stat, x);\r
+ query.appendSQL(stat, null, x);\r
stat.appendSQL(" ");\r
stat.appendSQL(compareType.getString());\r
if (compareType.hasRightExpression()) {\r
stat.appendSQL(" ");\r
- query.appendSQL(stat, y);\r
+ query.appendSQL(stat, x, y);\r
}\r
}\r
}\r
if (i++ > 0) {\r
stat.appendSQL(",");\r
}\r
- query.appendSQL(stat, o);\r
+ query.appendSQL(stat, null, o);\r
}\r
stat.appendSQL(")");\r
}\r
public static Boolean isNull(Object x) {\r
return Db.registerToken(Utils.newObject(Boolean.class), new Function("", x) {\r
public <T> void appendSQL(SQLStatement stat, Query<T> query) {\r
- query.appendSQL(stat, x[0]);\r
+ query.appendSQL(stat, null, x[0]);\r
stat.appendSQL(" IS NULL");\r
}\r
});\r
public static Boolean isNotNull(Object x) {\r
return Db.registerToken(Utils.newObject(Boolean.class), new Function("", x) {\r
public <T> void appendSQL(SQLStatement stat, Query<T> query) {\r
- query.appendSQL(stat, x[0]);\r
+ query.appendSQL(stat, null, x[0]);\r
stat.appendSQL(" IS NOT NULL");\r
}\r
});\r
return Db.registerToken(Utils.newObject(Boolean.class), new Function("", x) {\r
public <T> void appendSQL(SQLStatement stat, Query<T> query) {\r
stat.appendSQL("NOT ");\r
- query.appendSQL(stat, x[0]);\r
+ query.appendSQL(stat, null, x[0]);\r
}\r
});\r
}\r
if (i++ > 0) {\r
stat.appendSQL(" OR ");\r
}\r
- query.appendSQL(stat, o);\r
+ query.appendSQL(stat, null, o);\r
}\r
}\r
});\r
if (i++ > 0) {\r
stat.appendSQL(" AND ");\r
}\r
- query.appendSQL(stat, o);\r
+ query.appendSQL(stat, null, o);\r
}\r
}\r
});\r
return Db.registerToken(o, new Function("LIKE", x, pattern) {\r
public <T> void appendSQL(SQLStatement stat, Query<T> query) {\r
stat.appendSQL("(");\r
- query.appendSQL(stat, x[0]);\r
+ query.appendSQL(stat, null, x[0]);\r
stat.appendSQL(" LIKE ");\r
- query.appendSQL(stat, x[1]);\r
+ query.appendSQL(stat, x[0], x[1]);\r
stat.appendSQL(")");\r
}\r
});\r
}\r
\r
void appendSQL(SQLStatement stat) {\r
- query.appendSQL(stat, expression);\r
+ query.appendSQL(stat, null, expression);\r
if (desc) {\r
stat.appendSQL(" DESC");\r
}\r
import java.util.IdentityHashMap;\r
import java.util.List;\r
\r
+import com.iciql.Iciql.EnumType;\r
import com.iciql.bytecode.ClassReader;\r
import com.iciql.util.JdbcUtils;\r
import com.iciql.util.StatementLogger;\r
@SuppressWarnings("unchecked")\r
private <X> List<X> selectSimple(X x, boolean distinct) {\r
SQLStatement stat = getSelectStatement(distinct);\r
- appendSQL(stat, x);\r
+ appendSQL(stat, null, x);\r
appendFromWhere(stat);\r
ResultSet rs = stat.executeQuery();\r
List<X> result = Utils.newArrayList();\r
* \r
* @param stat\r
* the statement\r
- * @param x\r
- * the alias object\r
+ * @param alias\r
+ * the alias object (can be null)\r
+ * @param value\r
+ * the value\r
*/\r
- public void appendSQL(SQLStatement stat, Object x) {\r
- if (x == Function.count()) {\r
+ public void appendSQL(SQLStatement stat, Object alias, Object value) {\r
+ if (value == Function.count()) {\r
stat.appendSQL("COUNT(*)");\r
return;\r
}\r
- Token token = Db.getToken(x);\r
+ Token token = Db.getToken(value);\r
if (token != null) {\r
token.appendSQL(stat, this);\r
return;\r
}\r
- SelectColumn<T> col = aliasMap.get(x);\r
+ SelectColumn<T> col = aliasMap.get(value);\r
if (col != null) {\r
col.appendSQL(stat);\r
return;\r
}\r
stat.appendSQL("?");\r
- stat.addParameter(x);\r
+ if (alias != null && value.getClass().isEnum()) {\r
+ col = aliasMap.get(alias);\r
+ EnumType type = col.getFieldDefinition().enumType;\r
+ Enum<?> anEnum = (Enum<?>) value;\r
+ Object y = Utils.convertEnum(anEnum, type);\r
+ stat.addParameter(y);\r
+ } else {\r
+ stat.addParameter(value);\r
+ }\r
}\r
\r
void addConditionToken(Token condition) {\r
if (i++ > 0) {\r
stat.appendSQL(", ");\r
}\r
- appendSQL(stat, obj);\r
+ appendSQL(stat, null, obj);\r
stat.appendSQL(" ");\r
}\r
}\r
return new QueryWhere<T>(query);
}
+ public QueryWhere<T> isNot(A y) {
+ query.addConditionToken(new Condition<A>(x, y, CompareType.NOT_EQUAL));
+ return new QueryWhere<T>(query);
+ }
+
public QueryWhere<T> exceeds(A y) {
query.addConditionToken(new Condition<A>(x, y, CompareType.EXCEEDS));
return new QueryWhere<T>(query);
query.addConditionToken(new Condition<A>(x, y, CompareType.AT_MOST));
return new QueryWhere<T>(query);
}
-
+
public QueryWhere<T> like(A pattern) {
query.addConditionToken(new Condition<A>(x, pattern, CompareType.LIKE));
return new QueryWhere<T>(query);
}\r
return sql;\r
}\r
-\r
+ \r
SQLStatement addParameter(Object o) {\r
params.add(o);\r
return this;\r
}\r
FieldDefinition def = fields.get(i);\r
Object obj = def.getValue(x);\r
- query.appendSQL(stat, obj);\r
+ query.appendSQL(stat, x, obj);\r
}\r
}\r
\r
return Db.registerToken(o, new Function("=", x, y) {\r
public <T> void appendSQL(SQLStatement stat, Query<T> query) {\r
stat.appendSQL("(");\r
- query.appendSQL(stat, x[0]);\r
+ query.appendSQL(stat, null, x[0]);\r
stat.appendSQL(" = ");\r
- query.appendSQL(stat, x[1]);\r
+ query.appendSQL(stat, x[0], x[1]);\r
stat.appendSQL(")");\r
}\r
});\r
}\r
\r
- public Boolean bigger(A y) {\r
+ public Boolean exceeds(A y) {\r
Boolean o = Utils.newObject(Boolean.class);\r
return Db.registerToken(o, new Function(">", x, y) {\r
public <T> void appendSQL(SQLStatement stat, Query<T> query) {\r
stat.appendSQL("(");\r
- query.appendSQL(stat, x[0]);\r
+ query.appendSQL(stat, null, x[0]);\r
stat.appendSQL(" > ");\r
- query.appendSQL(stat, x[1]);\r
+ query.appendSQL(stat, x[0], x[1]);\r
stat.appendSQL(")");\r
}\r
});\r
}\r
\r
- public Boolean biggerEqual(A y) {\r
+ public Boolean atLeast(A y) {\r
Boolean o = Utils.newObject(Boolean.class);\r
return Db.registerToken(o, new Function(">=", x, y) {\r
public <T> void appendSQL(SQLStatement stat, Query<T> query) {\r
stat.appendSQL("(");\r
- query.appendSQL(stat, x[0]);\r
+ query.appendSQL(stat, null, x[0]);\r
stat.appendSQL(" >= ");\r
- query.appendSQL(stat, x[1]);\r
+ query.appendSQL(stat, x[0], x[1]);\r
stat.appendSQL(")");\r
}\r
});\r
}\r
\r
- public Boolean smaller(A y) {\r
+ public Boolean lessThan(A y) {\r
Boolean o = Utils.newObject(Boolean.class);\r
return Db.registerToken(o, new Function("<", x, y) {\r
public <T> void appendSQL(SQLStatement stat, Query<T> query) {\r
stat.appendSQL("(");\r
- query.appendSQL(stat, x[0]);\r
+ query.appendSQL(stat, null, x[0]);\r
stat.appendSQL(" < ");\r
- query.appendSQL(stat, x[1]);\r
+ query.appendSQL(stat, x[0], x[1]);\r
stat.appendSQL(")");\r
}\r
});\r
}\r
\r
- public Boolean smallerEqual(A y) {\r
+ public Boolean atMost(A y) {\r
Boolean o = Utils.newObject(Boolean.class);\r
return Db.registerToken(o, new Function("<=", x, y) {\r
public <T> void appendSQL(SQLStatement stat, Query<T> query) {\r
stat.appendSQL("(");\r
- query.appendSQL(stat, x[0]);\r
+ query.appendSQL(stat, null, x[0]);\r
stat.appendSQL(" <= ");\r
- query.appendSQL(stat, x[1]);\r
+ query.appendSQL(stat, x[0], x[1]);\r
stat.appendSQL(")");\r
}\r
});\r
return Db.registerToken(o, new Function("LIKE", x, pattern) {\r
public <T> void appendSQL(SQLStatement stat, Query<T> query) {\r
stat.appendSQL("(");\r
- query.appendSQL(stat, x[0]);\r
+ query.appendSQL(stat, null, x[0]);\r
stat.appendSQL(" LIKE ");\r
- query.appendSQL(stat, x[1]);\r
+ query.appendSQL(stat, x[0], x[1]);\r
stat.appendSQL(")");\r
}\r
});\r
}
public void appendSQL(SQLStatement stat) {
- query.appendSQL(stat, x);
+ query.appendSQL(stat, null, x);
stat.appendSQL("=(");
- query.appendSQL(stat, x);
+ query.appendSQL(stat, null, x);
stat.appendSQL("+");
- query.appendSQL(stat, y);
+ query.appendSQL(stat, x, y);
stat.appendSQL(")");
}
package com.iciql;
+
/**
* This class represents "SET column = value" in an UPDATE statement.
*
}
public void appendSQL(SQLStatement stat) {
- query.appendSQL(stat, x);
- stat.appendSQL("=?");
- stat.addParameter(y);
+ query.appendSQL(stat, null, x);
+ stat.appendSQL("=");
+ query.appendSQL(stat, x, y);
}
}
}
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
- query.appendSQL(stat, obj);
+ query.appendSQL(stat, null, obj);
}
}
throw new IciqlException("Can not convert the value {0} from {1} to {2}", o, currentType, targetType);\r
}\r
\r
+ public static Object convertEnum(Enum<?> o, EnumType type) {\r
+ if (o == null || type == null) {\r
+ return null;\r
+ }\r
+ switch (type) {\r
+ case ORDINAL:\r
+ return o.ordinal();\r
+ case ENUMID:\r
+ if (!EnumId.class.isAssignableFrom(o.getClass())) {\r
+ throw new IciqlException("Can not convert the enum {0} using ENUMID", o);\r
+ }\r
+ EnumId enumid = (EnumId) o;\r
+ return enumid.enumId();\r
+ case STRING:\r
+ default:\r
+ return o.name();\r
+ }\r
+ }\r
+\r
public static Object convertEnum(Object o, Class<?> targetType, EnumType type) {\r
if (o == null) {\r
return null;\r
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.
private void log(String text) {
System.out.println(text);
}
-
+
@Before
public void setUp() {
db = Db.open("jdbc:h2:mem:", "sa", "sa");
}
}
+ @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());
+ }
+
+ 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());
+ }
+
@Test
public void testModelGeneration() {
List<SupportedTypes> original = SupportedTypes.createList();
--- /dev/null
+/*\r
+ * Copyright 2011 James Moger.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package com.iciql.test.models;\r
+\r
+import java.util.Arrays;\r
+import java.util.List;\r
+\r
+import com.iciql.Iciql.EnumId;\r
+import com.iciql.Iciql.EnumType;\r
+import com.iciql.Iciql.IQColumn;\r
+import com.iciql.Iciql.IQEnum;\r
+import com.iciql.Iciql.IQTable;\r
+\r
+public abstract class EnumModels {\r
+\r
+ /**\r
+ * Test of @IQEnum annotated enumeration. This strategy is the default\r
+ * strategy for all fields of the Tree enum.\r
+ * \r
+ * Individual Tree field declarations can override this strategy by\r
+ * specifying a different @IQEnum annotation.\r
+ * \r
+ * Here ORDINAL specifies that this enum will be mapped to an INT column.\r
+ */\r
+ @IQEnum(EnumType.ENUMID)\r
+ public enum Tree implements EnumId {\r
+ PINE(10), OAK(20), BIRCH(30), WALNUT(40), MAPLE(50);\r
+\r
+ private int enumid;\r
+\r
+ Tree(int id) {\r
+ this.enumid = id;\r
+ }\r
+\r
+ @Override\r
+ public int enumId() {\r
+ return enumid;\r
+ }\r
+ }\r
+\r
+ @IQColumn(primaryKey = true)\r
+ public Integer id;\r
+ \r
+ public abstract Tree tree();\r
+\r
+ /**\r
+ * Test model for enum-as-enumid.\r
+ */\r
+ @IQTable(inheritColumns = true)\r
+ public static class EnumIdModel extends EnumModels {\r
+\r
+ // no need to specify ENUMID type as the enumeration definition\r
+ // specifies it.\r
+ @IQColumn\r
+ private Tree tree;\r
+\r
+ public EnumIdModel() {\r
+ }\r
+\r
+ public EnumIdModel(int id, Tree tree) {\r
+ this.id = id;\r
+ this.tree = tree;\r
+ }\r
+\r
+ @Override\r
+ public Tree tree() {\r
+ return tree;\r
+ }\r
+\r
+ public static List<EnumIdModel> createList() {\r
+ return Arrays.asList(new EnumIdModel(400, Tree.WALNUT), new EnumIdModel(200, Tree.OAK),\r
+ new EnumIdModel(500, Tree.MAPLE), new EnumIdModel(300, Tree.BIRCH), new EnumIdModel(100,\r
+ Tree.PINE));\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Test model for enum-as-ordinal.\r
+ */\r
+ @IQTable(inheritColumns = true)\r
+ public static class EnumOrdinalModel extends EnumModels {\r
+\r
+ // override the enumtype to ordinal\r
+ @IQEnum(EnumType.ORDINAL)\r
+ @IQColumn\r
+ private Tree tree;\r
+\r
+ public EnumOrdinalModel() {\r
+ }\r
+\r
+ public EnumOrdinalModel(int id, Tree tree) {\r
+ this.id = id;\r
+ this.tree = tree;\r
+ }\r
+ \r
+ @Override\r
+ public Tree tree() {\r
+ return tree;\r
+ }\r
+\r
+ public static List<EnumOrdinalModel> createList() {\r
+ return Arrays.asList(new EnumOrdinalModel(400, Tree.WALNUT), new EnumOrdinalModel(200, Tree.OAK),\r
+ new EnumOrdinalModel(500, Tree.MAPLE), new EnumOrdinalModel(300, Tree.BIRCH),\r
+ new EnumOrdinalModel(100, Tree.PINE));\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Test model for enum-as-string.\r
+ */\r
+ @IQTable(inheritColumns = true)\r
+ public static class EnumStringModel extends EnumModels {\r
+\r
+ // override the enumtype to string\r
+ // ensure that we specify a length so that the column is VARCHAR\r
+ @IQEnum(EnumType.STRING)\r
+ @IQColumn(length = 25)\r
+ private Tree tree;\r
+\r
+ public EnumStringModel() {\r
+ }\r
+\r
+ public EnumStringModel(int id, Tree tree) {\r
+ this.id = id;\r
+ this.tree = tree;\r
+ }\r
+\r
+ @Override\r
+ public Tree tree() {\r
+ return tree;\r
+ }\r
+ \r
+ public static List<EnumStringModel> createList() {\r
+ return Arrays.asList(new EnumStringModel(400, Tree.WALNUT), new EnumStringModel(200, Tree.OAK),\r
+ new EnumStringModel(500, Tree.MAPLE), new EnumStringModel(300, Tree.BIRCH),\r
+ new EnumStringModel(100, Tree.PINE));\r
+ }\r
+ }\r
+}\r
import java.util.List;
import java.util.Random;
-import com.iciql.Iciql.EnumId;
import com.iciql.Iciql.EnumType;
import com.iciql.Iciql.IQColumn;
import com.iciql.Iciql.IQEnum;
import com.iciql.Iciql.IQTable;
import com.iciql.Iciql.IQVersion;
import com.iciql.Iciql.IndexType;
+import com.iciql.test.models.EnumModels.Tree;
import com.iciql.util.Utils;
/**
ROSE, TULIP, MUM, PETUNIA, MARIGOLD, DAFFODIL;
}
- /**
- * Test of @IQEnum annotated enumeration. This strategy is the default
- * strategy for all fields of the Tree enum.
- *
- * Individual Tree field declarations can override this strategy by
- * specifying a different @IQEnum annotation.
- *
- * Here ORDINAL specifies that this enum will be mapped to an INT column.
- */
- @IQEnum(EnumType.ORDINAL)
- public enum Tree implements EnumId {
- PINE(10), OAK(20), BIRCH(30), WALNUT(40), MAPLE(50);
-
- private int enumid;
-
- Tree(int id) {
- this.enumid = id;
- }
-
- @Override
- public int enumId() {
- return enumid;
- }
- }
-
@IQColumn(primaryKey = true, autoIncrement = true)
public Integer id;
@IQColumn
private Flower myOtherFavoriteFlower;
- @IQColumn(length = 25)
+ @IQEnum(EnumType.ORDINAL)
+ @IQColumn
+ // override the default enum strategy and use the ordinal value
+ private Tree myFavoriteTree;
+
// @IQEnum is set on the enumeration definition and is shared
// by all uses of Tree as an @IQColumn
- private Tree myFavoriteTree;
-
- @IQEnum(EnumType.ENUMID)
@IQColumn
- // override the default enum strategy and use the custom enumid
private Tree myOtherFavoriteTree;
public static List<SupportedTypes> createList() {