/* * Copyright 2004-2011 H2 Group. * 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; import java.util.List; import com.iciql.NestedConditions.And; import com.iciql.NestedConditions.Or; /** * This class represents a query with a condition. * * @param * the return type */ public class QueryWhere { Query query; QueryWhere(Query query) { 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 and(boolean x) { query.getFrom().getAliasDefinition().checkMultipleBooleans(); 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 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 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 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 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 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 and(double x) { return addPrimitive(ConditionAndOr.AND, x); } private QueryCondition 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(query, x); } return new QueryCondition(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 QueryCondition and(A x) { query.getFrom().getAliasDefinition().checkMultipleEnums(x); query.addConditionToken(ConditionAndOr.AND); return new QueryCondition(query, x); } public QueryWhere and(And conditions) { andOpen(); query.addConditionToken(conditions.where.query); return close(); } public QueryWhere and(Or conditions) { andOpen(); query.addConditionToken(conditions.where.query); return close(); } public QueryWhere 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 */ public QueryCondition or(boolean x) { query.getFrom().getAliasDefinition().checkMultipleBooleans(); 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 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 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 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 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 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 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 QueryCondition or(A x) { query.getFrom().getAliasDefinition().checkMultipleEnums(x); query.addConditionToken(ConditionAndOr.OR); return new QueryCondition(query, x); } public QueryWhere or(And conditions) { orOpen(); query.addConditionToken(conditions.where.query); return close(); } public QueryWhere or(Or conditions) { orOpen(); query.addConditionToken(conditions.where.query); return close(); } public QueryWhere orOpen() { return open(ConditionAndOr.OR); } private QueryWhere open(ConditionAndOr andOr) { query.addConditionToken(andOr); query.addConditionToken(ConditionOpenClose.OPEN); return this; } public QueryWhere close() { query.addConditionToken(ConditionOpenClose.CLOSE); return this; } public QueryWhere limit(long limit) { query.limit(limit); return this; } public QueryWhere offset(long offset) { query.offset(offset); return this; } public String getSQL() { SQLStatement stat = new SQLStatement(query.getDb()); stat.appendSQL("SELECT *"); query.appendFromWhere(stat); return stat.getSQL().trim(); } /** * toSQL returns a static string version of the query with runtime variables * 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() { return query.toSQL(false); } /** * toSQL returns a static string version of the query with runtime variables * 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 */ public String toSQL(boolean distinct) { return query.toSQL(distinct); } /** * toSQL returns a static string version of the query with runtime variables * 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 * k is used to select only the columns of the specified alias * for an inner join statement. An example of a generated * statement is: SELECT DISTINCT t1.* FROM sometable AS t1 INNER * JOIN othertable AS t2 ON t1.id = t2.id WHERE t2.flag = true * without the alias parameter the statement would start with * SELECT DISTINCT * FROM... * @return the sql query as plain text */ public String toSQL(boolean distinct, K k) { return query.toSQL(distinct, k); } public SubQuery subQuery(Z x) { return new SubQuery(query, x); } public SubQuery subQuery(boolean x) { return subQuery(query.getPrimitiveAliasByValue(x)); } public SubQuery subQuery(byte x) { return subQuery(query.getPrimitiveAliasByValue(x)); } public SubQuery subQuery(short x) { return subQuery(query.getPrimitiveAliasByValue(x)); } public SubQuery subQuery(int x) { return subQuery(query.getPrimitiveAliasByValue(x)); } public SubQuery subQuery(long x) { return subQuery(query.getPrimitiveAliasByValue(x)); } public SubQuery subQuery(float x) { return subQuery(query.getPrimitiveAliasByValue(x)); } public SubQuery subQuery(double x) { return subQuery(query.getPrimitiveAliasByValue(x)); } public List select(Z x) { return query.select(x); } public List selectDistinct(Z x) { return query.selectDistinct(x); } public X selectFirst(Z x) { List list = query.select(x); return list.isEmpty() ? null : list.get(0); } public List select() { return query.select(); } public T selectFirst() { List list = select(); return list.isEmpty() ? null : list.get(0); } public List selectDistinct() { return query.selectDistinct(); } public void createView(Class viewClass) { query.createView(viewClass); } public void replaceView(Class viewClass) { query.replaceView(viewClass); } /** * Order by primitive boolean field * * @param field * a primitive boolean field * @return the query */ public QueryWhere orderBy(boolean field) { query.getFrom().getAliasDefinition().checkMultipleBooleans(); return orderByPrimitive(field); } /** * Order by primitive byte field * * @param field * a primitive byte field * @return the query */ public QueryWhere orderBy(byte field) { return orderByPrimitive(field); } /** * Order by primitive short field * * @param field * a primitive short field * @return the query */ public QueryWhere orderBy(short field) { return orderByPrimitive(field); } public QueryWhere orderBy(int field) { return orderByPrimitive(field); } /** * Order by primitive long field * * @param field * a primitive long field * @return the query */ public QueryWhere orderBy(long field) { return orderByPrimitive(field); } /** * Order by primitive float field * * @param field * a primitive float field * @return the query */ public QueryWhere orderBy(float field) { return orderByPrimitive(field); } /** * Order by primitive double field * * @param field * a primitive double field * @return the query */ public QueryWhere orderBy(double field) { return orderByPrimitive(field); } private QueryWhere orderByPrimitive(Object field) { query.orderByPrimitive(field); return this; } public QueryWhere orderBy(Object field) { query.getFrom().getAliasDefinition().checkMultipleEnums(field); query.orderBy(field); return this; } /** * Order by a number of Object columns. * * @param expressions * the order by expressions * @return the query */ public QueryWhere orderBy(Object... expressions) { query.orderBy(expressions); return this; } public QueryWhere orderByNullsFirst(Object expr) { query.getFrom().getAliasDefinition().checkMultipleEnums(expr); OrderExpression e = new OrderExpression(query, expr, false, true, false); query.addOrderBy(e); return this; } public QueryWhere orderByNullsLast(Object expr) { query.getFrom().getAliasDefinition().checkMultipleEnums(expr); OrderExpression e = new OrderExpression(query, expr, false, false, true); query.addOrderBy(e); return this; } public QueryWhere orderByDesc(Object expr) { query.getFrom().getAliasDefinition().checkMultipleEnums(expr); OrderExpression e = new OrderExpression(query, expr, true, false, false); query.addOrderBy(e); return this; } public QueryWhere orderByDescNullsFirst(Object expr) { query.getFrom().getAliasDefinition().checkMultipleEnums(expr); OrderExpression e = new OrderExpression(query, expr, true, true, false); query.addOrderBy(e); return this; } public QueryWhere orderByDescNullsLast(Object expr) { query.getFrom().getAliasDefinition().checkMultipleEnums(expr); OrderExpression e = new OrderExpression(query, expr, true, false, true); query.addOrderBy(e); return this; } public int delete() { return query.delete(); } public int update() { return query.update(); } public long selectCount() { return query.selectCount(); } }