/* * 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 com.iciql.util.Utils; /** * This class represents a query with an incomplete condition. * * @param * the return type of the query * @param * the incomplete condition data type */ public class QueryCondition { private Query query; private A x; QueryCondition(Query query, A x) { this.query = query; this.x = x; } public QueryWhere in(SubQuery q) { query.addConditionToken(new SubQueryCondition(x, q)); return new QueryWhere(query); } public QueryWhere oneOf(A... a) { return oneOf(Utils.newArrayIterable(a)); } public QueryWhere oneOf(Iterable i) { query.addConditionToken(new Condition(x, i, CompareType.IN)); return new QueryWhere(query); } public QueryWhere noneOf(A... a) { return noneOf(Utils.newArrayIterable(a)); } public QueryWhere noneOf(Iterable i) { query.addConditionToken(new Condition(x, i, CompareType.NOT_IN)); return new QueryWhere(query); } public QueryWhere is(A y) { query.addConditionToken(new Condition(x, y, CompareType.EQUAL)); return new QueryWhere(query); } public QueryWhere isNot(A y) { query.addConditionToken(new Condition(x, y, CompareType.NOT_EQUAL)); return new QueryWhere(query); } public QueryWhere isNull() { query.addConditionToken(new Condition(x, CompareType.IS_NULL)); return new QueryWhere(query); } public QueryWhere isNotNull() { query.addConditionToken(new Condition(x, CompareType.IS_NOT_NULL)); return new QueryWhere(query); } public QueryWhere exceeds(A y) { query.addConditionToken(new Condition(x, y, CompareType.EXCEEDS)); return new QueryWhere(query); } public QueryWhere atLeast(A y) { query.addConditionToken(new Condition(x, y, CompareType.AT_LEAST)); return new QueryWhere(query); } public QueryWhere lessThan(A y) { query.addConditionToken(new Condition(x, y, CompareType.LESS_THAN)); return new QueryWhere(query); } public QueryWhere atMost(A y) { query.addConditionToken(new Condition(x, y, CompareType.AT_MOST)); return new QueryWhere(query); } public QueryBetween between(A y) { return new QueryBetween(query, x, y); } public QueryWhere like(A pattern) { query.addConditionToken(new Condition(x, pattern, CompareType.LIKE)); return new QueryWhere(query); } /* * These method allows you to generate "x=?", "x!=?", etc where conditions. * Parameter substitution must be done manually later with db.executeQuery. * This allows for building re-usable SQL string statements from your model * classes. */ public QueryWhere isParameter() { query.addConditionToken(new RuntimeParameter(x, CompareType.EQUAL)); return new QueryWhere(query); } public QueryWhere isNotParameter() { query.addConditionToken(new RuntimeParameter(x, CompareType.NOT_EQUAL)); return new QueryWhere(query); } public QueryWhere exceedsParameter() { query.addConditionToken(new RuntimeParameter(x, CompareType.EXCEEDS)); return new QueryWhere(query); } public QueryWhere lessThanParameter() { query.addConditionToken(new RuntimeParameter(x, CompareType.LESS_THAN)); return new QueryWhere(query); } public QueryWhere atMostParameter() { query.addConditionToken(new RuntimeParameter(x, CompareType.AT_MOST)); return new QueryWhere(query); } public QueryWhere likeParameter() { query.addConditionToken(new RuntimeParameter(x, CompareType.LIKE)); return new QueryWhere(query); } }