/* * 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; /** * This class represents a query with a condition. * * @param * the return type */ public class QueryWhere { Query query; QueryWhere(Query query) { this.query = query; } public QueryCondition and(A x) { query.addConditionToken(ConditionAndOr.AND); return new QueryCondition(query, x); } public QueryCondition or(A x) { query.addConditionToken(ConditionAndOr.OR); return new QueryCondition(query, x); } public QueryWhere limit(long limit) { query.limit(limit); return this; } public QueryWhere offset(long offset) { query.offset(offset); return this; } public List select(Z x) { return query.select(x); } public String getSQL() { SQLStatement stat = new SQLStatement(query.getDb()); stat.appendSQL("SELECT *"); query.appendFromWhere(stat); return stat.getSQL().trim(); } 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(); } /** * Order by a number of columns. * * @param expressions * the order by expressions * @return the query */ public QueryWhere orderBy(Object... expressions) { for (Object expr : expressions) { OrderExpression e = new OrderExpression(query, expr, false, false, false); query.addOrderBy(e); } return this; } public QueryWhere orderByNullsFirst(Object expr) { OrderExpression e = new OrderExpression(query, expr, false, true, false); query.addOrderBy(e); return this; } public QueryWhere orderByNullsLast(Object expr) { OrderExpression e = new OrderExpression(query, expr, false, false, true); query.addOrderBy(e); return this; } public QueryWhere orderByDesc(Object expr) { OrderExpression e = new OrderExpression(query, expr, true, false, false); query.addOrderBy(e); return this; } public QueryWhere orderByDescNullsFirst(Object expr) { OrderExpression e = new OrderExpression(query, expr, true, true, false); query.addOrderBy(e); return this; } public QueryWhere orderByDescNullsLast(Object 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(); } }