aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/com/iciql/NestedConditions.java (renamed from src/main/java/com/iciql/Conditions.java)20
-rw-r--r--src/main/java/com/iciql/Query.java76
-rw-r--r--src/main/java/com/iciql/QueryWhere.java93
3 files changed, 97 insertions, 92 deletions
diff --git a/src/main/java/com/iciql/Conditions.java b/src/main/java/com/iciql/NestedConditions.java
index 1699641..5c92a86 100644
--- a/src/main/java/com/iciql/Conditions.java
+++ b/src/main/java/com/iciql/NestedConditions.java
@@ -1,11 +1,11 @@
/*
* Copyright (c) 2009-2014, Architector Inc., Japan
* All rights reserved.
- *
+ *
* 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
@@ -17,9 +17,9 @@
package com.iciql;
-public abstract class Conditions<T> {
+public abstract class NestedConditions<T> {
- public static class And<T> extends Conditions<T> {
+ public static class And<T> extends NestedConditions<T> {
public And(Db db, T alias) {
super(db, alias);
@@ -58,20 +58,20 @@ public abstract class Conditions<T> {
}
protected QueryWhere<T> and(And<T> conditions) {
- where.andOpenTrue();
+ where.andOpen();
where.query.addConditionToken(conditions.where.query);
return where.close();
}
protected QueryWhere<T> and(Or<T> conditions) {
- where.andOpenFalse();
+ where.andOpen();
where.query.addConditionToken(conditions.where.query);
return where.close();
}
}
- public static class Or<T> extends Conditions<T> {
+ public static class Or<T> extends NestedConditions<T> {
public Or(Db db, T alias) {
super(db, alias);
@@ -110,13 +110,13 @@ public abstract class Conditions<T> {
}
protected QueryWhere<T> or(And<T> conditions) {
- where.orOpenTrue();
+ where.orOpen();
where.query.addConditionToken(conditions.where.query);
return where.close();
}
protected QueryWhere<T> or(Or<T> conditions) {
- where.orOpenFalse();
+ where.orOpen();
where.query.addConditionToken(conditions.where.query);
return where.close();
}
@@ -125,7 +125,7 @@ public abstract class Conditions<T> {
QueryWhere<T> where;
- private Conditions(Db db, T alias) {
+ private NestedConditions(Db db, T alias) {
where = new QueryWhere<T>(Query.rebuild(db, alias));
}
diff --git a/src/main/java/com/iciql/Query.java b/src/main/java/com/iciql/Query.java
index 7edd8fc..f22215c 100644
--- a/src/main/java/com/iciql/Query.java
+++ b/src/main/java/com/iciql/Query.java
@@ -27,17 +27,18 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
-import com.iciql.Conditions.And;
-import com.iciql.Conditions.Or;
+
+import com.iciql.NestedConditions.And;
+import com.iciql.NestedConditions.Or;
import com.iciql.Iciql.EnumType;
import com.iciql.bytecode.ClassReader;
-import com.iciql.util.JdbcUtils;
import com.iciql.util.IciqlLogger;
+import com.iciql.util.JdbcUtils;
import com.iciql.util.Utils;
/**
* This class represents a query.
- *
+ *
* @param <T>
* the return type
*/
@@ -62,7 +63,7 @@ public class Query<T> {
/**
* from() is a static factory method to build a Query object.
- *
+ *
* @param db
* @param alias
* @return a query object
@@ -119,13 +120,13 @@ public class Query<T> {
List<X> list = limit(1).select(x);
return list.isEmpty() ? null : list.get(0);
}
-
+
public <X> void createView(Class<X> viewClass) {
TableDefinition<X> viewDef = db.define(viewClass);
-
+
SQLStatement fromWhere = new SQLStatement(db);
appendFromWhere(fromWhere, false);
-
+
SQLStatement stat = new SQLStatement(db);
db.getDialect().prepareCreateView(stat, viewDef, fromWhere.toSQL());
IciqlLogger.create(stat.toSQL());
@@ -149,7 +150,7 @@ public class Query<T> {
* properly encoded. This method is also useful when combined with the where
* clause methods like isParameter() or atLeastParameter() which allows
* iciql to generate re-usable parameterized string statements.
- *
+ *
* @return the sql query as plain text
*/
public String toSQL() {
@@ -161,7 +162,7 @@ public class Query<T> {
* properly encoded. This method is also useful when combined with the where
* clause methods like isParameter() or atLeastParameter() which allows
* iciql to generate re-usable parameterized string statements.
- *
+ *
* @param distinct
* if true SELECT DISTINCT is used for the query
* @return the sql query as plain text
@@ -175,7 +176,7 @@ public class Query<T> {
* properly encoded. This method is also useful when combined with the where
* clause methods like isParameter() or atLeastParameter() which allows
* iciql to generate re-usable parameterized string statements.
- *
+ *
* @param distinct
* if true SELECT DISTINCT is used for the query
* @param k
@@ -228,7 +229,7 @@ public class Query<T> {
stat.appendSQL("*");
}
appendFromWhere(stat);
- }
+ }
return stat.toSQL().trim();
}
@@ -239,7 +240,7 @@ public class Query<T> {
stat.appendColumn(columnName);
appendFromWhere(stat);
return stat.toSQL();
- }
+ }
private List<T> select(boolean distinct) {
List<T> result = Utils.newArrayList();
@@ -456,7 +457,7 @@ public class Query<T> {
/**
* Begin a primitive boolean field condition clause.
- *
+ *
* @param x
* the primitive boolean field to query
* @return a query condition to continue building the condition
@@ -468,7 +469,7 @@ public class Query<T> {
/**
* Begin a primitive short field condition clause.
- *
+ *
* @param x
* the primitive short field to query
* @return a query condition to continue building the condition
@@ -479,7 +480,7 @@ public class Query<T> {
/**
* Begin a primitive short field condition clause.
- *
+ *
* @param x
* the primitive short field to query
* @return a query condition to continue building the condition
@@ -490,7 +491,7 @@ public class Query<T> {
/**
* Begin a primitive int field condition clause.
- *
+ *
* @param x
* the primitive int field to query
* @return a query condition to continue building the condition
@@ -501,7 +502,7 @@ public class Query<T> {
/**
* Begin a primitive long field condition clause.
- *
+ *
* @param x
* the primitive long field to query
* @return a query condition to continue building the condition
@@ -512,7 +513,7 @@ public class Query<T> {
/**
* Begin a primitive float field condition clause.
- *
+ *
* @param x
* the primitive float field to query
* @return a query condition to continue building the condition
@@ -523,7 +524,7 @@ public class Query<T> {
/**
* Begin a primitive double field condition clause.
- *
+ *
* @param x
* the primitive double field to query
* @return a query condition to continue building the condition
@@ -534,7 +535,7 @@ public class Query<T> {
/**
* Begins a primitive field condition clause.
- *
+ *
* @param value
* @return a query condition to continue building the condition
*/
@@ -549,7 +550,7 @@ public class Query<T> {
/**
* Begin an Object field condition clause.
- *
+ *
* @param x
* the mapped object to query
* @return a query condition to continue building the condition
@@ -622,7 +623,7 @@ public class Query<T> {
/**
* Sets the Limit and Offset of a query.
- *
+ *
* @return the query
*/
@@ -682,7 +683,7 @@ public class Query<T> {
/**
* Order by a number of columns.
- *
+ *
* @param expressions
* the columns
* @return the query
@@ -753,7 +754,7 @@ public class Query<T> {
/**
* INTERNAL
- *
+ *
* @param stat
* the statement
* @param alias
@@ -796,7 +797,7 @@ public class Query<T> {
/**
* INTERNAL
- *
+ *
* @param stat
* the statement
* @param alias
@@ -879,17 +880,30 @@ public class Query<T> {
}
if (!conditions.isEmpty()) {
stat.appendSQL(" WHERE ");
+
+ boolean skipNextConjunction = false;
+
for (Token token : conditions) {
+
+ if (skipNextConjunction && token instanceof ConditionAndOr) {
+ skipNextConjunction = false;
+ continue;
+ }
+
token.appendSQL(stat, this);
stat.appendSQL(" ");
+
+ if (ConditionOpenClose.OPEN == token) {
+ skipNextConjunction = true;
+ }
}
}
}
-
+
void appendFromWhere(SQLStatement stat) {
appendFromWhere(stat, true);
}
-
+
void appendFromWhere(SQLStatement stat, boolean log) {
stat.appendSQL(" FROM ");
from.appendSQL(stat);
@@ -927,7 +941,7 @@ public class Query<T> {
/**
* Join another table.
- *
+ *
* @param alias
* an alias for the table to join
* @return the joined query
@@ -977,7 +991,7 @@ public class Query<T> {
/**
* This method returns a mapped Object field by its reference.
- *
+ *
* @param obj
* @return
*/
@@ -988,7 +1002,7 @@ public class Query<T> {
/**
* This method returns the alias of a mapped primitive field by its value.
- *
+ *
* @param obj
* @return
*/
diff --git a/src/main/java/com/iciql/QueryWhere.java b/src/main/java/com/iciql/QueryWhere.java
index 5ea8bdb..731fc0b 100644
--- a/src/main/java/com/iciql/QueryWhere.java
+++ b/src/main/java/com/iciql/QueryWhere.java
@@ -19,12 +19,12 @@ package com.iciql;
import java.util.List;
-import com.iciql.Conditions.And;
-import com.iciql.Conditions.Or;
+import com.iciql.NestedConditions.And;
+import com.iciql.NestedConditions.Or;
/**
* This class represents a query with a condition.
- *
+ *
* @param <T>
* the return type
*/
@@ -39,7 +39,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -51,7 +51,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -62,7 +62,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -73,7 +73,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -84,7 +84,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -95,7 +95,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -106,7 +106,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -127,7 +127,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -139,28 +139,24 @@ public class QueryWhere<T> {
}
public QueryWhere<T> and(And<T> conditions) {
- andOpenTrue();
+ andOpen();
query.addConditionToken(conditions.where.query);
return close();
}
public QueryWhere<T> and(Or<T> conditions) {
- andOpenFalse();
+ andOpen();
query.addConditionToken(conditions.where.query);
return close();
}
- public QueryWhere<T> andOpenTrue() {
- return open(ConditionAndOr.AND, true);
- }
-
- public QueryWhere<T> andOpenFalse() {
- return open(ConditionAndOr.AND, false);
+ public QueryWhere<T> andOpen() {
+ return open(ConditionAndOr.AND);
}
/**
* 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
@@ -172,7 +168,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -183,7 +179,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -194,7 +190,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -205,7 +201,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -216,7 +212,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -227,7 +223,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -238,7 +234,7 @@ public class QueryWhere<T> {
/**
* 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
@@ -250,29 +246,24 @@ public class QueryWhere<T> {
}
public QueryWhere<T> or(And<T> conditions) {
- orOpenTrue();
+ orOpen();
query.addConditionToken(conditions.where.query);
return close();
}
public QueryWhere<T> or(Or<T> conditions) {
- orOpenFalse();
+ orOpen();
query.addConditionToken(conditions.where.query);
return close();
}
- public QueryWhere<T> orOpenTrue() {
- return open(ConditionAndOr.OR, true);
- }
-
- public QueryWhere<T> orOpenFalse() {
- return open(ConditionAndOr.OR, false);
+ public QueryWhere<T> orOpen() {
+ return open(ConditionAndOr.OR);
}
- private QueryWhere<T> open(ConditionAndOr andOr, Boolean condition) {
+ private QueryWhere<T> open(ConditionAndOr andOr) {
query.addConditionToken(andOr);
query.addConditionToken(ConditionOpenClose.OPEN);
- query.addConditionToken(new Function("", condition));
return this;
}
@@ -303,7 +294,7 @@ public class QueryWhere<T> {
* properly encoded. This method is also useful when combined with the where
* clause methods like isParameter() or atLeastParameter() which allows
* iciql to generate re-usable parameterized string statements.
- *
+ *
* @return the sql query as plain text
*/
public String toSQL() {
@@ -315,7 +306,7 @@ public class QueryWhere<T> {
* properly encoded. This method is also useful when combined with the where
* clause methods like isParameter() or atLeastParameter() which allows
* iciql to generate re-usable parameterized string statements.
- *
+ *
* @param distinct
* if true SELECT DISTINCT is used for the query
* @return the sql query as plain text
@@ -329,7 +320,7 @@ public class QueryWhere<T> {
* properly encoded. This method is also useful when combined with the where
* clause methods like isParameter() or atLeastParameter() which allows
* iciql to generate re-usable parameterized string statements.
- *
+ *
* @param distinct
* if true SELECT DISTINCT is used for the query
* @param k
@@ -344,12 +335,12 @@ public class QueryWhere<T> {
public <K> String toSQL(boolean distinct, K k) {
return query.toSQL(distinct, k);
}
-
+
public <Z> SubQuery<T, Z> subQuery(Z x) {
return new SubQuery<T, Z>(query, x);
}
- public SubQuery<T, Boolean> subQuery(boolean x) {
+ public SubQuery<T, Boolean> subQuery(boolean x) {
return subQuery(query.getPrimitiveAliasByValue(x));
}
@@ -376,7 +367,7 @@ public class QueryWhere<T> {
public SubQuery<T, Double> subQuery(double x) {
return subQuery(query.getPrimitiveAliasByValue(x));
}
-
+
public <X, Z> List<X> select(Z x) {
return query.select(x);
}
@@ -402,7 +393,7 @@ public class QueryWhere<T> {
public List<T> selectDistinct() {
return query.selectDistinct();
}
-
+
public void createView(Class<?> viewClass) {
query.createView(viewClass);
}
@@ -413,7 +404,7 @@ public class QueryWhere<T> {
/**
* Order by primitive boolean field
- *
+ *
* @param field
* a primitive boolean field
* @return the query
@@ -425,7 +416,7 @@ public class QueryWhere<T> {
/**
* Order by primitive byte field
- *
+ *
* @param field
* a primitive byte field
* @return the query
@@ -436,7 +427,7 @@ public class QueryWhere<T> {
/**
* Order by primitive short field
- *
+ *
* @param field
* a primitive short field
* @return the query
@@ -451,7 +442,7 @@ public class QueryWhere<T> {
/**
* Order by primitive long field
- *
+ *
* @param field
* a primitive long field
* @return the query
@@ -462,7 +453,7 @@ public class QueryWhere<T> {
/**
* Order by primitive float field
- *
+ *
* @param field
* a primitive float field
* @return the query
@@ -473,7 +464,7 @@ public class QueryWhere<T> {
/**
* Order by primitive double field
- *
+ *
* @param field
* a primitive double field
* @return the query
@@ -495,7 +486,7 @@ public class QueryWhere<T> {
/**
* Order by a number of Object columns.
- *
+ *
* @param expressions
* the order by expressions
* @return the query