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.Arrays; import java.util.Collection; import java.util.Collections; import org.apache.commons.lang.StringUtils; import com.google.common.base.Functions; import com.google.common.base.Preconditions; import com.google.common.collect.Collections2; import com.google.common.collect.Lists; /** * Builder for CQL3 DELETE statements. */ public class DeleteStatementBuilder implements Mutating { private Collection<String> selection; private Identifier table; private Collection<Option> options; private Collection<Relation> relations; private boolean usingTimestamp; /** * */ DeleteStatementBuilder() { this.selection = Lists.newArrayList(); this.table = null; this.options = Lists.newArrayList(); this.relations = Lists.newArrayList(); this.usingTimestamp = false; } /** * * * @param columns * @return this builder. */ public DeleteStatementBuilder columns(String... columns) { this.selection.addAll(Arrays.asList(columns)); return this; } /** * * * @param selection */ public DeleteStatementBuilder values(Collection<String> values) { this.selection.addAll(values); return this; } /** * * * @param identifier * @return this builder. */ public DeleteStatementBuilder from(String identifier) { this.table = new Identifier(identifier); return this; } /** * * * @param timestamp * @return this builder. */ public DeleteStatementBuilder usingTimestamp(long value) { this.options.add(Option.timestamp(value)); usingTimestamp = true; return this; } /** * * * @param identifier * @param numVariables * @return */ public DeleteStatementBuilder where(String identifier, int numVariables) { relations.add(new In(new Identifier(identifier), Collections.nCopies(numVariables, Term.VARIABLE))); return this; } /** * * * @param identifier * @param comparator * @return this builder. */ public DeleteStatementBuilder 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 relations * @param comparator * @return this builder. */ public DeleteStatementBuilder where(Collection<String> values, String comparator) { if (values != null) { for (String value : values) { relations.add(new Relation(new Identifier(value), Comparator.getEnum(comparator))); } } return this; } /** * Adds a relation for the given identifier, comparator, and literal value. * @param identifier the identifier. * @param comparator the comparator. * @param value the literal value. * @return this builder. */ public DeleteStatementBuilder where(String identifier, String comparator, String value) { relations.add(new Relation(new Identifier(identifier), Comparator.getEnum(comparator), new Term(value))); return this; } /** * Adds a relation for the given identifier and values for an IN clause. * @param identifier the identifier. * @param in the values used in the IN clause * @return this builder. */ public DeleteStatementBuilder 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; } /** * Adds a relation. * @param relation the relation. * @return this builder. */ public DeleteStatementBuilder where(Relation relation) { relations.add(relation); return this; } /** * {@inheritDoc} */ @Override public String build() { Preconditions.checkNotNull(table, "You must set the name of a table"); StringBuilder sb = new StringBuilder(); sb.append("DELETE "); sb.append(StringUtils.join(selection, ", ")); sb.append(" FROM "); sb.append(table); if (!options.isEmpty()) { sb.append(" USING "); sb.append(StringUtils.join(Collections2.transform(options, Functions.toStringFunction()), " AND ")); } if (!relations.isEmpty()) { sb.append(" WHERE "); sb.append(StringUtils.join(Collections2.transform(relations, Functions.toStringFunction()), " AND ")); } sb.append(";"); return sb.toString(); } /** * {@inheritDoc} */ @Override public boolean usingTimestamp() { return usingTimestamp; } }