aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJames Moger <james.moger@gmail.com>2011-08-12 13:04:29 -0400
committerJames Moger <james.moger@gmail.com>2011-08-12 13:04:29 -0400
commit78d9f3ecbd9203a5ad63138037960e265509efba (patch)
treea62c1d2a61e44048ceb594ea9ee0935e95aa6cba /src
parent5c70dc9c2562f40211d51b35854c50a626c9c163 (diff)
downloadiciql-78d9f3ecbd9203a5ad63138037960e265509efba.tar.gz
iciql-78d9f3ecbd9203a5ad63138037960e265509efba.zip
Full primitives support.
Diffstat (limited to 'src')
-rw-r--r--src/com/iciql/Query.java293
-rw-r--r--src/com/iciql/QueryWhere.java264
-rw-r--r--src/com/iciql/TableDefinition.java12
-rw-r--r--src/com/iciql/util/Utils.java4
4 files changed, 551 insertions, 22 deletions
diff --git a/src/com/iciql/Query.java b/src/com/iciql/Query.java
index 5c2d225..699574d 100644
--- a/src/com/iciql/Query.java
+++ b/src/com/iciql/Query.java
@@ -23,6 +23,7 @@ import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
@@ -49,7 +50,7 @@ public class Query<T> {
private ArrayList<SelectTable<T>> joins = Utils.newArrayList();
private final IdentityHashMap<Object, SelectColumn<T>> aliasMap = Utils.newIdentityHashMap();
private ArrayList<OrderExpression<T>> orderByList = Utils.newArrayList();
- private Object[] groupByExpressions;
+ private ArrayList<Object> groupByExpressions = Utils.newArrayList();
private long limit;
private long offset;
@@ -148,10 +149,80 @@ public class Query<T> {
return new UpdateColumnSet<T, A>(this, field);
}
+ public UpdateColumnSet<T, Boolean> set(boolean field) {
+ return setPrimitive(field);
+ }
+
+ public UpdateColumnSet<T, Byte> set(byte field) {
+ return setPrimitive(field);
+ }
+
+ public UpdateColumnSet<T, Short> set(short field) {
+ return setPrimitive(field);
+ }
+
+ public UpdateColumnSet<T, Integer> set(int field) {
+ return setPrimitive(field);
+ }
+
+ public UpdateColumnSet<T, Long> set(long field) {
+ return setPrimitive(field);
+ }
+
+ public UpdateColumnSet<T, Float> set(float field) {
+ return setPrimitive(field);
+ }
+
+ public UpdateColumnSet<T, Double> set(double field) {
+ return setPrimitive(field);
+ }
+
+ private <A> UpdateColumnSet<T, A> setPrimitive(A field) {
+ A alias = getPrimitiveAliasByValue(field);
+ if (alias == null) {
+ // this will result in an unmapped field exception
+ return set(field);
+ }
+ return set(alias);
+ }
+
public <A> UpdateColumnIncrement<T, A> increment(A field) {
return new UpdateColumnIncrement<T, A>(this, field);
}
+ public UpdateColumnIncrement<T, Byte> increment(byte field) {
+ return incrementPrimitive(field);
+ }
+
+ public UpdateColumnIncrement<T, Short> increment(short field) {
+ return incrementPrimitive(field);
+ }
+
+ public UpdateColumnIncrement<T, Integer> increment(int field) {
+ return incrementPrimitive(field);
+ }
+
+ public UpdateColumnIncrement<T, Long> increment(long field) {
+ return incrementPrimitive(field);
+ }
+
+ public UpdateColumnIncrement<T, Float> increment(float field) {
+ return incrementPrimitive(field);
+ }
+
+ public UpdateColumnIncrement<T, Double> increment(double field) {
+ return incrementPrimitive(field);
+ }
+
+ private <A> UpdateColumnIncrement<T, A> incrementPrimitive(A field) {
+ A alias = getPrimitiveAliasByValue(field);
+ if (alias == null) {
+ // this will result in an unmapped field exception
+ return increment(field);
+ }
+ return increment(alias);
+ }
+
public int update() {
if (updateColumnDeclarations.size() == 0) {
throw new IciqlException("Missing set or increment call.");
@@ -249,6 +320,105 @@ public class Query<T> {
return stat;
}
+ /**
+ * Begin a primitive boolean field condition clause.
+ *
+ * @param x
+ * the primitive boolean field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Boolean> where(boolean x) {
+ return wherePrimitive(x);
+ }
+
+ /**
+ * Begin a primitive short field condition clause.
+ *
+ * @param x
+ * the primitive short field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Byte> where(byte x) {
+ return wherePrimitive(x);
+ }
+
+ /**
+ * Begin a primitive short field condition clause.
+ *
+ * @param x
+ * the primitive short field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Short> where(short x) {
+ return wherePrimitive(x);
+ }
+
+ /**
+ * Begin a primitive int field condition clause.
+ *
+ * @param x
+ * the primitive int field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Integer> where(int x) {
+ return wherePrimitive(x);
+ }
+
+ /**
+ * Begin a primitive long field condition clause.
+ *
+ * @param x
+ * the primitive long field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Long> where(long x) {
+ return wherePrimitive(x);
+ }
+
+ /**
+ * Begin a primitive float field condition clause.
+ *
+ * @param x
+ * the primitive float field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Float> where(float x) {
+ return wherePrimitive(x);
+ }
+
+ /**
+ * Begin a primitive double field condition clause.
+ *
+ * @param x
+ * the primitive double field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Double> where(double x) {
+ return wherePrimitive(x);
+ }
+
+ /**
+ * Begins a primitive field condition clause.
+ *
+ * @param value
+ * @return a query condition to continue building the condition
+ */
+ private <A> QueryCondition<T, A> wherePrimitive(A value) {
+ A alias = getPrimitiveAliasByValue(value);
+ if (alias == null) {
+ // this will result in an unmapped field exception
+ return where(value);
+ }
+ return where(alias);
+ }
+
+ /**
+ * Begin an Object field condition clause.
+ *
+ * @param x
+ * the mapped object to query
+ * @return a query condition to continue building the condition
+ */
public <A> QueryCondition<T, A> where(A x) {
return new QueryCondition<T, A>(this, x);
}
@@ -306,6 +476,48 @@ public class Query<T> {
return this;
}
+ public Query<T> orderBy(boolean field) {
+ return orderByPrimitive(field);
+ }
+
+ public Query<T> orderBy(byte field) {
+ return orderByPrimitive(field);
+ }
+
+ public Query<T> orderBy(short field) {
+ return orderByPrimitive(field);
+ }
+
+ public Query<T> orderBy(int field) {
+ return orderByPrimitive(field);
+ }
+
+ public Query<T> orderBy(long field) {
+ return orderByPrimitive(field);
+ }
+
+ public Query<T> orderBy(float field) {
+ return orderByPrimitive(field);
+ }
+
+ public Query<T> orderBy(double field) {
+ return orderByPrimitive(field);
+ }
+
+ Query<T> orderByPrimitive(Object field) {
+ Object alias = getPrimitiveAliasByValue(field);
+ if (alias == null) {
+ return orderBy(field);
+ }
+ return orderBy(alias);
+ }
+
+ public Query<T> orderBy(Object expr) {
+ OrderExpression<T> e = new OrderExpression<T>(this, expr, false, false, false);
+ addOrderBy(e);
+ return this;
+ }
+
/**
* Order by a number of columns.
*
@@ -328,8 +540,49 @@ public class Query<T> {
return this;
}
+ public Query<T> groupBy(boolean field) {
+ return orderByPrimitive(field);
+ }
+
+ public Query<T> groupBy(byte field) {
+ return orderByPrimitive(field);
+ }
+
+ public Query<T> groupBy(short field) {
+ return orderByPrimitive(field);
+ }
+
+ public Query<T> groupBy(int field) {
+ return orderByPrimitive(field);
+ }
+
+ public Query<T> groupBy(long field) {
+ return orderByPrimitive(field);
+ }
+
+ public Query<T> groupBy(float field) {
+ return orderByPrimitive(field);
+ }
+
+ public Query<T> groupBy(double field) {
+ return orderByPrimitive(field);
+ }
+
+ Query<T> groupByPrimitive(Object field) {
+ Object alias = getPrimitiveAliasByValue(field);
+ if (alias == null) {
+ return groupBy(field);
+ }
+ return groupBy(alias);
+ }
+
+ public Query<T> groupBy(Object expr) {
+ groupByExpressions.add(expr);
+ return this;
+ }
+
public Query<T> groupBy(Object... groupBy) {
- this.groupByExpressions = groupBy;
+ this.groupByExpressions.addAll(Arrays.asList(groupBy));
return this;
}
@@ -362,7 +615,7 @@ public class Query<T> {
addParameter(stat, alias, value);
return;
}
- SelectColumn<T> col = aliasMap.get(value);
+ SelectColumn<T> col = getColumnByReference(value);
if (col != null) {
col.appendSQL(stat);
return;
@@ -402,7 +655,7 @@ public class Query<T> {
private void addParameter(SQLStatement stat, Object alias, Object value) {
if (alias != null && value.getClass().isEnum()) {
- SelectColumn<T> col = aliasMap.get(alias);
+ SelectColumn<T> col = getColumnByReference(alias);
EnumType type = col.getFieldDefinition().enumType;
Enum<?> anEnum = (Enum<?>) value;
Object y = Utils.convertEnum(anEnum, type);
@@ -437,7 +690,7 @@ public class Query<T> {
join.appendSQLAsJoin(stat, this);
}
appendWhere(stat);
- if (groupByExpressions != null) {
+ if (!groupByExpressions.isEmpty()) {
stat.appendSQL(" GROUP BY ");
int i = 0;
for (Object obj : groupByExpressions) {
@@ -493,8 +746,34 @@ public class Query<T> {
return !joins.isEmpty();
}
- SelectColumn<T> getSelectColumn(Object obj) {
- return aliasMap.get(obj);
+ /**
+ * This method returns a mapped Object field by its reference.
+ *
+ * @param obj
+ * @return
+ */
+ private SelectColumn<T> getColumnByReference(Object obj) {
+ SelectColumn<T> col = aliasMap.get(obj);
+ return col;
+ }
+
+ /**
+ * This method returns the alias of a mapped primitive field by its value.
+ *
+ * @param obj
+ * @return
+ */
+ @SuppressWarnings("unchecked")
+ <A> A getPrimitiveAliasByValue(A obj) {
+ for (Object alias : aliasMap.keySet()) {
+ if (alias.equals(obj)) {
+ SelectColumn<T> match = aliasMap.get(alias);
+ if (match.getFieldDefinition().isPrimitive) {
+ return (A) alias;
+ }
+ }
+ }
+ return null;
}
void addOrderBy(OrderExpression<T> expr) {
diff --git a/src/com/iciql/QueryWhere.java b/src/com/iciql/QueryWhere.java
index 9071b52..727285d 100644
--- a/src/com/iciql/QueryWhere.java
+++ b/src/com/iciql/QueryWhere.java
@@ -34,11 +34,189 @@ public class QueryWhere<T> {
this.query = query;
}
+ /**
+ * Specify an AND condition with a mapped primitive boolean.
+ *
+ * @param x
+ * the primitive boolean field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Boolean> and(boolean x) {
+ return addPrimitive(ConditionAndOr.AND, x);
+ }
+
+ /**
+ * Specify an AND condition with a mapped primitive byte.
+ *
+ * @param x
+ * the primitive byte field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Byte> and(byte x) {
+ return addPrimitive(ConditionAndOr.AND, x);
+ }
+
+ /**
+ * Specify an AND condition with a mapped primitive short.
+ *
+ * @param x
+ * the primitive short field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Short> and(short x) {
+ return addPrimitive(ConditionAndOr.AND, x);
+ }
+
+ /**
+ * Specify an AND condition with a mapped primitive int.
+ *
+ * @param x
+ * the primitive int field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Integer> and(int x) {
+ return addPrimitive(ConditionAndOr.AND, x);
+ }
+
+ /**
+ * Specify an AND condition with a mapped primitive long.
+ *
+ * @param x
+ * the primitive long field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Long> and(long x) {
+ return addPrimitive(ConditionAndOr.AND, x);
+ }
+
+ /**
+ * Specify an AND condition with a mapped primitive float.
+ *
+ * @param x
+ * the primitive float field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Float> and(float x) {
+ return addPrimitive(ConditionAndOr.AND, x);
+ }
+
+ /**
+ * Specify an AND condition with a mapped primitive double.
+ *
+ * @param x
+ * the primitive double field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Double> and(double x) {
+ return addPrimitive(ConditionAndOr.AND, x);
+ }
+
+ private <A> QueryCondition<T, A> addPrimitive(ConditionAndOr condition, A x) {
+ query.addConditionToken(condition);
+ A alias = query.getPrimitiveAliasByValue(x);
+ if (alias == null) {
+ // this will result in an unmapped field exception
+ return new QueryCondition<T, A>(query, x);
+ }
+ return new QueryCondition<T, A>(query, alias);
+ }
+
+ /**
+ * Specify an AND condition with a mapped Object field.
+ *
+ * @param x
+ * the Object field to query
+ * @return a query condition to continue building the condition
+ */
public <A> QueryCondition<T, A> and(A x) {
query.addConditionToken(ConditionAndOr.AND);
return new QueryCondition<T, A>(query, x);
}
+ /**
+ * Specify an OR condition with a mapped primitive boolean.
+ *
+ * @param x
+ * the primitive boolean field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Boolean> or(boolean x) {
+ return addPrimitive(ConditionAndOr.OR, x);
+ }
+
+ /**
+ * Specify an OR condition with a mapped primitive byte.
+ *
+ * @param x
+ * the primitive byte field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Byte> or(byte x) {
+ return addPrimitive(ConditionAndOr.OR, x);
+ }
+
+ /**
+ * Specify an OR condition with a mapped primitive short.
+ *
+ * @param x
+ * the primitive short field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Short> or(short x) {
+ return addPrimitive(ConditionAndOr.OR, x);
+ }
+
+ /**
+ * Specify an OR condition with a mapped primitive int.
+ *
+ * @param x
+ * the primitive int field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Integer> or(int x) {
+ return addPrimitive(ConditionAndOr.OR, x);
+ }
+
+ /**
+ * Specify an OR condition with a mapped primitive long.
+ *
+ * @param x
+ * the primitive long field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Long> or(long x) {
+ return addPrimitive(ConditionAndOr.OR, x);
+ }
+
+ /**
+ * Specify an OR condition with a mapped primitive float.
+ *
+ * @param x
+ * the primitive float field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Float> or(float x) {
+ return addPrimitive(ConditionAndOr.OR, x);
+ }
+
+ /**
+ * Specify an OR condition with a mapped primitive double.
+ *
+ * @param x
+ * the primitive double field to query
+ * @return a query condition to continue building the condition
+ */
+ public QueryCondition<T, Double> or(double x) {
+ return addPrimitive(ConditionAndOr.OR, x);
+ }
+
+ /**
+ * Specify an OR condition with a mapped Object field.
+ *
+ * @param x
+ * the Object field to query
+ * @return a query condition to continue building the condition
+ */
public <A> QueryCondition<T, A> or(A x) {
query.addConditionToken(ConditionAndOr.OR);
return new QueryCondition<T, A>(query, x);
@@ -88,7 +266,86 @@ public class QueryWhere<T> {
}
/**
- * Order by a number of columns.
+ * Order by primitive boolean field
+ *
+ * @param field
+ * a primitive boolean field
+ * @return the query
+ */
+ public QueryWhere<T> orderBy(boolean field) {
+ return orderByPrimitive(field);
+ }
+
+ /**
+ * Order by primitive byte field
+ *
+ * @param field
+ * a primitive byte field
+ * @return the query
+ */
+ public QueryWhere<T> orderBy(byte field) {
+ return orderByPrimitive(field);
+ }
+
+ /**
+ * Order by primitive short field
+ *
+ * @param field
+ * a primitive short field
+ * @return the query
+ */
+ public QueryWhere<T> orderBy(short field) {
+ return orderByPrimitive(field);
+ }
+
+ public QueryWhere<T> orderBy(int field) {
+ return orderByPrimitive(field);
+ }
+
+ /**
+ * Order by primitive long field
+ *
+ * @param field
+ * a primitive long field
+ * @return the query
+ */
+ public QueryWhere<T> orderBy(long field) {
+ return orderByPrimitive(field);
+ }
+
+ /**
+ * Order by primitive float field
+ *
+ * @param field
+ * a primitive float field
+ * @return the query
+ */
+ public QueryWhere<T> orderBy(float field) {
+ return orderByPrimitive(field);
+ }
+
+ /**
+ * Order by primitive double field
+ *
+ * @param field
+ * a primitive double field
+ * @return the query
+ */
+ public QueryWhere<T> orderBy(double field) {
+ return orderByPrimitive(field);
+ }
+
+ private QueryWhere<T> orderByPrimitive(Object field) {
+ query.orderByPrimitive(field);
+ return this;
+ }
+
+ public QueryWhere<T> orderBy(Object field) {
+ query.orderBy(field);
+ return this;
+ }
+ /**
+ * Order by a number of Object columns.
*
* @param expressions
* the order by expressions
@@ -96,10 +353,7 @@ public class QueryWhere<T> {
*/
public QueryWhere<T> orderBy(Object... expressions) {
- for (Object expr : expressions) {
- OrderExpression<T> e = new OrderExpression<T>(query, expr, false, false, false);
- query.addOrderBy(e);
- }
+ query.orderBy(expressions);
return this;
}
diff --git a/src/com/iciql/TableDefinition.java b/src/com/iciql/TableDefinition.java
index 571ab1f..687ba53 100644
--- a/src/com/iciql/TableDefinition.java
+++ b/src/com/iciql/TableDefinition.java
@@ -79,6 +79,7 @@ public class TableDefinition<T> {
boolean nullable;
String defaultValue;
EnumType enumType;
+ boolean isPrimitive;
Object getValue(Object obj) {
try {
@@ -345,6 +346,7 @@ public class TableDefinition<T> {
boolean reflectiveMatch = isPublic && !byAnnotationsOnly;
if (reflectiveMatch || hasAnnotation) {
FieldDefinition fieldDef = new FieldDefinition();
+ fieldDef.isPrimitive = f.getType().isPrimitive();
fieldDef.field = f;
fieldDef.columnName = columnName;
fieldDef.isAutoIncrement = isAutoIncrement;
@@ -658,14 +660,4 @@ public class TableDefinition<T> {
query.appendSQL(stat, x, obj);
}
}
-
- <Y, X> void copyAttributeValues(Query<Y> query, X to, X map) {
- for (FieldDefinition def : fields) {
- Object obj = def.getValue(map);
- SelectColumn<Y> col = query.getSelectColumn(obj);
- Object value = col.getCurrentValue();
- def.setValue(to, value);
- }
- }
-
}
diff --git a/src/com/iciql/util/Utils.java b/src/com/iciql/util/Utils.java
index 6f9746d..d9e065c 100644
--- a/src/com/iciql/util/Utils.java
+++ b/src/com/iciql/util/Utils.java
@@ -140,6 +140,7 @@ public class Utils {
} else if (clazz == double.class || clazz == Double.class) {
return (T) new Double(COUNTER.getAndIncrement());
} else if (clazz == boolean.class || clazz == Boolean.class) {
+ COUNTER.getAndIncrement();
return (T) new Boolean(false);
} else if (clazz == BigDecimal.class) {
return (T) new BigDecimal(COUNTER.getAndIncrement());
@@ -154,12 +155,15 @@ public class Utils {
} else if (clazz == java.util.Date.class) {
return (T) new java.util.Date(COUNTER.getAndIncrement());
} else if (clazz == byte[].class) {
+ COUNTER.getAndIncrement();
return (T) new byte[0];
} else if (clazz.isEnum()) {
+ COUNTER.getAndIncrement();
// enums can not be instantiated reflectively
// return first constant as reference
return clazz.getEnumConstants()[0];
} else if (clazz == java.util.UUID.class) {
+ COUNTER.getAndIncrement();
return (T) UUID.randomUUID();
}
try {