Java tutorial
/* * Copyright 2013 EK3 Technologies, Inc. * * 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 ei.ne.ke.cassandra.cql3.template; import java.util.Collection; import org.apache.commons.lang.StringUtils; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Order; import com.google.common.base.Functions; import com.google.common.base.Preconditions; import com.google.common.collect.Collections2; import com.google.common.collect.Lists; /** * */ public class SelectStatementBuilder implements Preserving { private static final long VALUE_NOT_SET = -1; private Collection<Identifier> selection; private Identifier table; private Collection<Relation> relations; private Collection<Ordering> orderings; private long limit; private boolean allowFiltering; /** * * * @param selection */ SelectStatementBuilder() { this.selection = Lists.newArrayList(); this.table = null; this.relations = Lists.newArrayList(); this.orderings = Lists.newArrayList(); this.limit = VALUE_NOT_SET; this.allowFiltering = false; } /** * A short-hand for getting the count. * @return this builder. */ public SelectStatementBuilder count() { selection.add(Identifier.COUNT); return this; } /** * A short-hand for selecting all columns. * @return this builder. */ public SelectStatementBuilder all() { selection.add(Identifier.ALL); return this; } /** * * * @param value */ public SelectStatementBuilder columns(String... columns) { if (columns != null) { for (String column : columns) { selection.add(new Identifier(column)); } } return this; } /** * * * @param value * @return this builder. */ public SelectStatementBuilder columns(Collection<String> columns) { if (columns != null) { for (String column : columns) { selection.add(new Identifier(column)); } } return this; } /** * * * @param identifier * @return this builder. */ public SelectStatementBuilder from(String identifier) { this.table = new Identifier(identifier); return this; } /** * * * @param identifier * @param comparator * @return this builder. */ public SelectStatementBuilder where(String identifier, String comparator) { relations.add(new Relation(new Identifier(identifier), Comparator.getEnum(comparator))); return this; } /** * Adds a relation for each of the given column name with the given * comparator and a variable value. * * @param identifiers * @param comparator * @return this builder. */ public SelectStatementBuilder where(Collection<String> identifiers, String comparator) { if (identifiers != null) { for (String identifier : identifiers) { relations.add(new Relation(new Identifier(identifier), Comparator.getEnum(comparator))); } } return this; } /** * * * @param identifier * @param comparator * @param value * @return this builder. */ public SelectStatementBuilder where(String identifier, String comparator, String value) { relations.add(new Relation(new Identifier(identifier), Comparator.getEnum(comparator), new Term(value))); return this; } /** * * * @param identifier * @param strTerms * @return this builder. */ public SelectStatementBuilder where(String identifier, Collection<String> ins) { Collection<Term> terms = Lists.newArrayListWithExpectedSize(ins.size()); for (String in : ins) { terms.add(new Term(in)); } relations.add(new In(new Identifier(identifier), terms)); return this; } /** * * * @param ordering * @return this builder. */ public SelectStatementBuilder orderBy(String ordering) { orderings.add(new Ordering(new Identifier(ordering))); return this; } /** * * * @param ordering * @param direction * @return this builder. */ public SelectStatementBuilder orderBy(String ordering, String direction) { orderings.add(new Ordering(new Identifier(ordering), new Identifier(direction))); return this; } /** * * * @param sort * @return this builder. */ public SelectStatementBuilder orderBy(Sort sort) { if (sort != null) { for (Order order : sort) { orderings.add(new Ordering(new Identifier(order.getProperty()), new Identifier(order.getDirection().toString()))); } } return this; } /** * * * @param value * @return this builder. */ public SelectStatementBuilder limit(long value) { limit = value; return this; } /** * * * @return this builder. */ public SelectStatementBuilder allowFiltering(boolean value) { allowFiltering = value; return this; } /** * * * @return this builder. */ @Override public String build() { Preconditions.checkNotNull(selection); Preconditions.checkArgument(selection.size() > 0, "You must select at least one column"); Preconditions.checkNotNull(table, "You must set the name of a table"); StringBuilder sb = new StringBuilder(); sb.append("SELECT "); sb.append(StringUtils.join(selection, ", ")); sb.append(" FROM "); sb.append(table); if (relations.size() > 0) { sb.append(" WHERE "); sb.append(StringUtils.join(Collections2.transform(relations, Functions.toStringFunction()), " AND ")); } if (orderings.size() > 0) { sb.append(" ORDER BY "); sb.append(StringUtils.join(Collections2.transform(orderings, Functions.toStringFunction()), ", ")); } if (limit > VALUE_NOT_SET) { sb.append(" LIMIT "); sb.append(limit); } if (allowFiltering) { sb.append(" ALLOW FILTERING"); } sb.append(";"); return sb.toString(); } }