summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJames Moger <james.moger@gmail.com>2011-08-07 15:53:18 -0400
committerJames Moger <james.moger@gmail.com>2011-08-07 15:53:18 -0400
commit684838def9e1646c266ffb46bbc65b5dfdc8f14d (patch)
treeab0625631b4c845ee4d9b2e48b12035df13a038b /src
parent0c5463e7372fe4c47341dcf0c48bdd94682d0c7c (diff)
downloadiciql-684838def9e1646c266ffb46bbc65b5dfdc8f14d.tar.gz
iciql-684838def9e1646c266ffb46bbc65b5dfdc8f14d.zip
Finished enum query support (issue 4)
Diffstat (limited to 'src')
-rw-r--r--src/com/iciql/Condition.java4
-rw-r--r--src/com/iciql/Function.java16
-rw-r--r--src/com/iciql/OrderExpression.java2
-rw-r--r--src/com/iciql/Query.java29
-rw-r--r--src/com/iciql/QueryCondition.java7
-rw-r--r--src/com/iciql/SQLStatement.java2
-rw-r--r--src/com/iciql/TableDefinition.java2
-rw-r--r--src/com/iciql/TestCondition.java32
-rw-r--r--src/com/iciql/UpdateColumnIncrement.java6
-rw-r--r--src/com/iciql/UpdateColumnSet.java7
-rw-r--r--src/com/iciql/bytecode/Variable.java2
-rw-r--r--src/com/iciql/util/Utils.java19
12 files changed, 82 insertions, 46 deletions
diff --git a/src/com/iciql/Condition.java b/src/com/iciql/Condition.java
index df6b033..df9762a 100644
--- a/src/com/iciql/Condition.java
+++ b/src/com/iciql/Condition.java
@@ -35,12 +35,12 @@ class Condition<A> implements Token {
}
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
- query.appendSQL(stat, x);
+ query.appendSQL(stat, null, x);
stat.appendSQL(" ");
stat.appendSQL(compareType.getString());
if (compareType.hasRightExpression()) {
stat.appendSQL(" ");
- query.appendSQL(stat, y);
+ query.appendSQL(stat, x, y);
}
}
}
diff --git a/src/com/iciql/Function.java b/src/com/iciql/Function.java
index f2c9e30..f07a5ea 100644
--- a/src/com/iciql/Function.java
+++ b/src/com/iciql/Function.java
@@ -42,7 +42,7 @@ public class Function implements Token {
if (i++ > 0) {
stat.appendSQL(",");
}
- query.appendSQL(stat, o);
+ query.appendSQL(stat, null, o);
}
stat.appendSQL(")");
}
@@ -67,7 +67,7 @@ public class Function implements Token {
public static Boolean isNull(Object x) {
return Db.registerToken(Utils.newObject(Boolean.class), new Function("", x) {
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
- query.appendSQL(stat, x[0]);
+ query.appendSQL(stat, null, x[0]);
stat.appendSQL(" IS NULL");
}
});
@@ -76,7 +76,7 @@ public class Function implements Token {
public static Boolean isNotNull(Object x) {
return Db.registerToken(Utils.newObject(Boolean.class), new Function("", x) {
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
- query.appendSQL(stat, x[0]);
+ query.appendSQL(stat, null, x[0]);
stat.appendSQL(" IS NOT NULL");
}
});
@@ -86,7 +86,7 @@ public class Function implements Token {
return Db.registerToken(Utils.newObject(Boolean.class), new Function("", x) {
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
stat.appendSQL("NOT ");
- query.appendSQL(stat, x[0]);
+ query.appendSQL(stat, null, x[0]);
}
});
}
@@ -99,7 +99,7 @@ public class Function implements Token {
if (i++ > 0) {
stat.appendSQL(" OR ");
}
- query.appendSQL(stat, o);
+ query.appendSQL(stat, null, o);
}
}
});
@@ -113,7 +113,7 @@ public class Function implements Token {
if (i++ > 0) {
stat.appendSQL(" AND ");
}
- query.appendSQL(stat, o);
+ query.appendSQL(stat, null, o);
}
}
});
@@ -138,9 +138,9 @@ public class Function implements Token {
return Db.registerToken(o, new Function("LIKE", x, pattern) {
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
stat.appendSQL("(");
- query.appendSQL(stat, x[0]);
+ query.appendSQL(stat, null, x[0]);
stat.appendSQL(" LIKE ");
- query.appendSQL(stat, x[1]);
+ query.appendSQL(stat, x[0], x[1]);
stat.appendSQL(")");
}
});
diff --git a/src/com/iciql/OrderExpression.java b/src/com/iciql/OrderExpression.java
index 36acf16..f450bfb 100644
--- a/src/com/iciql/OrderExpression.java
+++ b/src/com/iciql/OrderExpression.java
@@ -40,7 +40,7 @@ class OrderExpression<T> {
}
void appendSQL(SQLStatement stat) {
- query.appendSQL(stat, expression);
+ query.appendSQL(stat, null, expression);
if (desc) {
stat.appendSQL(" DESC");
}
diff --git a/src/com/iciql/Query.java b/src/com/iciql/Query.java
index b6018ea..c44cac7 100644
--- a/src/com/iciql/Query.java
+++ b/src/com/iciql/Query.java
@@ -27,6 +27,7 @@ import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
+import com.iciql.Iciql.EnumType;
import com.iciql.bytecode.ClassReader;
import com.iciql.util.JdbcUtils;
import com.iciql.util.StatementLogger;
@@ -213,7 +214,7 @@ public class Query<T> {
@SuppressWarnings("unchecked")
private <X> List<X> selectSimple(X x, boolean distinct) {
SQLStatement stat = getSelectStatement(distinct);
- appendSQL(stat, x);
+ appendSQL(stat, null, x);
appendFromWhere(stat);
ResultSet rs = stat.executeQuery();
List<X> result = Utils.newArrayList();
@@ -343,26 +344,36 @@ public class Query<T> {
*
* @param stat
* the statement
- * @param x
- * the alias object
+ * @param alias
+ * the alias object (can be null)
+ * @param value
+ * the value
*/
- public void appendSQL(SQLStatement stat, Object x) {
- if (x == Function.count()) {
+ public void appendSQL(SQLStatement stat, Object alias, Object value) {
+ if (value == Function.count()) {
stat.appendSQL("COUNT(*)");
return;
}
- Token token = Db.getToken(x);
+ Token token = Db.getToken(value);
if (token != null) {
token.appendSQL(stat, this);
return;
}
- SelectColumn<T> col = aliasMap.get(x);
+ SelectColumn<T> col = aliasMap.get(value);
if (col != null) {
col.appendSQL(stat);
return;
}
stat.appendSQL("?");
- stat.addParameter(x);
+ if (alias != null && value.getClass().isEnum()) {
+ col = aliasMap.get(alias);
+ EnumType type = col.getFieldDefinition().enumType;
+ Enum<?> anEnum = (Enum<?>) value;
+ Object y = Utils.convertEnum(anEnum, type);
+ stat.addParameter(y);
+ } else {
+ stat.addParameter(value);
+ }
}
void addConditionToken(Token condition) {
@@ -397,7 +408,7 @@ public class Query<T> {
if (i++ > 0) {
stat.appendSQL(", ");
}
- appendSQL(stat, obj);
+ appendSQL(stat, null, obj);
stat.appendSQL(" ");
}
}
diff --git a/src/com/iciql/QueryCondition.java b/src/com/iciql/QueryCondition.java
index 8e7cd42..9d13995 100644
--- a/src/com/iciql/QueryCondition.java
+++ b/src/com/iciql/QueryCondition.java
@@ -41,6 +41,11 @@ public class QueryCondition<T, A> {
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);
@@ -60,7 +65,7 @@ public class QueryCondition<T, A> {
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);
diff --git a/src/com/iciql/SQLStatement.java b/src/com/iciql/SQLStatement.java
index b6c5e18..3a7c901 100644
--- a/src/com/iciql/SQLStatement.java
+++ b/src/com/iciql/SQLStatement.java
@@ -63,7 +63,7 @@ public class SQLStatement {
}
return sql;
}
-
+
SQLStatement addParameter(Object o) {
params.add(o);
return this;
diff --git a/src/com/iciql/TableDefinition.java b/src/com/iciql/TableDefinition.java
index 5d9b9c4..f0ee4fa 100644
--- a/src/com/iciql/TableDefinition.java
+++ b/src/com/iciql/TableDefinition.java
@@ -705,7 +705,7 @@ class TableDefinition<T> {
}
FieldDefinition def = fields.get(i);
Object obj = def.getValue(x);
- query.appendSQL(stat, obj);
+ query.appendSQL(stat, x, obj);
}
}
diff --git a/src/com/iciql/TestCondition.java b/src/com/iciql/TestCondition.java
index b4c1c15..010f5a1 100644
--- a/src/com/iciql/TestCondition.java
+++ b/src/com/iciql/TestCondition.java
@@ -39,61 +39,61 @@ public class TestCondition<A> {
return Db.registerToken(o, new Function("=", x, y) {
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
stat.appendSQL("(");
- query.appendSQL(stat, x[0]);
+ query.appendSQL(stat, null, x[0]);
stat.appendSQL(" = ");
- query.appendSQL(stat, x[1]);
+ query.appendSQL(stat, x[0], x[1]);
stat.appendSQL(")");
}
});
}
- public Boolean bigger(A y) {
+ public Boolean exceeds(A y) {
Boolean o = Utils.newObject(Boolean.class);
return Db.registerToken(o, new Function(">", x, y) {
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
stat.appendSQL("(");
- query.appendSQL(stat, x[0]);
+ query.appendSQL(stat, null, x[0]);
stat.appendSQL(" > ");
- query.appendSQL(stat, x[1]);
+ query.appendSQL(stat, x[0], x[1]);
stat.appendSQL(")");
}
});
}
- public Boolean biggerEqual(A y) {
+ public Boolean atLeast(A y) {
Boolean o = Utils.newObject(Boolean.class);
return Db.registerToken(o, new Function(">=", x, y) {
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
stat.appendSQL("(");
- query.appendSQL(stat, x[0]);
+ query.appendSQL(stat, null, x[0]);
stat.appendSQL(" >= ");
- query.appendSQL(stat, x[1]);
+ query.appendSQL(stat, x[0], x[1]);
stat.appendSQL(")");
}
});
}
- public Boolean smaller(A y) {
+ public Boolean lessThan(A y) {
Boolean o = Utils.newObject(Boolean.class);
return Db.registerToken(o, new Function("<", x, y) {
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
stat.appendSQL("(");
- query.appendSQL(stat, x[0]);
+ query.appendSQL(stat, null, x[0]);
stat.appendSQL(" < ");
- query.appendSQL(stat, x[1]);
+ query.appendSQL(stat, x[0], x[1]);
stat.appendSQL(")");
}
});
}
- public Boolean smallerEqual(A y) {
+ public Boolean atMost(A y) {
Boolean o = Utils.newObject(Boolean.class);
return Db.registerToken(o, new Function("<=", x, y) {
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
stat.appendSQL("(");
- query.appendSQL(stat, x[0]);
+ query.appendSQL(stat, null, x[0]);
stat.appendSQL(" <= ");
- query.appendSQL(stat, x[1]);
+ query.appendSQL(stat, x[0], x[1]);
stat.appendSQL(")");
}
});
@@ -104,9 +104,9 @@ public class TestCondition<A> {
return Db.registerToken(o, new Function("LIKE", x, pattern) {
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
stat.appendSQL("(");
- query.appendSQL(stat, x[0]);
+ query.appendSQL(stat, null, x[0]);
stat.appendSQL(" LIKE ");
- query.appendSQL(stat, x[1]);
+ query.appendSQL(stat, x[0], x[1]);
stat.appendSQL(")");
}
});
diff --git a/src/com/iciql/UpdateColumnIncrement.java b/src/com/iciql/UpdateColumnIncrement.java
index 43ef6d6..143ce48 100644
--- a/src/com/iciql/UpdateColumnIncrement.java
+++ b/src/com/iciql/UpdateColumnIncrement.java
@@ -44,11 +44,11 @@ public class UpdateColumnIncrement<T, A> implements UpdateColumn {
}
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(")");
}
diff --git a/src/com/iciql/UpdateColumnSet.java b/src/com/iciql/UpdateColumnSet.java
index 55d3790..f2322e3 100644
--- a/src/com/iciql/UpdateColumnSet.java
+++ b/src/com/iciql/UpdateColumnSet.java
@@ -17,6 +17,7 @@
package com.iciql;
+
/**
* This class represents "SET column = value" in an UPDATE statement.
*
@@ -44,9 +45,9 @@ public class UpdateColumnSet<T, A> implements UpdateColumn {
}
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);
}
}
diff --git a/src/com/iciql/bytecode/Variable.java b/src/com/iciql/bytecode/Variable.java
index 3412349..f3dbc01 100644
--- a/src/com/iciql/bytecode/Variable.java
+++ b/src/com/iciql/bytecode/Variable.java
@@ -45,7 +45,7 @@ public class Variable implements Token {
}
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
- query.appendSQL(stat, obj);
+ query.appendSQL(stat, null, obj);
}
}
diff --git a/src/com/iciql/util/Utils.java b/src/com/iciql/util/Utils.java
index dac30fc..b8e2d60 100644
--- a/src/com/iciql/util/Utils.java
+++ b/src/com/iciql/util/Utils.java
@@ -275,6 +275,25 @@ public class Utils {
throw new IciqlException("Can not convert the value {0} from {1} to {2}", o, currentType, targetType);
}
+ public static Object convertEnum(Enum<?> o, EnumType type) {
+ if (o == null || type == null) {
+ return null;
+ }
+ switch (type) {
+ case ORDINAL:
+ return o.ordinal();
+ case ENUMID:
+ if (!EnumId.class.isAssignableFrom(o.getClass())) {
+ throw new IciqlException("Can not convert the enum {0} using ENUMID", o);
+ }
+ EnumId enumid = (EnumId) o;
+ return enumid.enumId();
+ case STRING:
+ default:
+ return o.name();
+ }
+ }
+
public static Object convertEnum(Object o, Class<?> targetType, EnumType type) {
if (o == null) {
return null;