ei.ne.ke.cassandra.cql3.Cql3StatementGenerator.java Source code

Java tutorial

Introduction

Here is the source code for ei.ne.ke.cassandra.cql3.Cql3StatementGenerator.java

Source

/*
 * 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;

// CHECKSTYLE:OFF
import static ei.ne.ke.cassandra.cql3.template.Statement.batch;
import static ei.ne.ke.cassandra.cql3.template.Statement.delete;
import static ei.ne.ke.cassandra.cql3.template.Statement.insert;
import static ei.ne.ke.cassandra.cql3.template.Statement.select;
import static ei.ne.ke.cassandra.cql3.template.Statement.truncate;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

import org.springframework.data.domain.Sort;

import ei.ne.ke.cassandra.cql3.template.Mutating;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.mysema.query.types.Predicate;
// CHECKSTYLE:ON

/**
 * @param <T>
 * @param <ID>
 */
public class Cql3StatementGenerator<T, ID extends Serializable> {

    private final EntitySpecification<T, ID> spec;

    /**
     * Constructor.
     * @param spec
     */
    public Cql3StatementGenerator(EntitySpecification<T, ID> spec) {
        this.spec = Preconditions.checkNotNull(spec);
    }

    /**
     * Constructs a CQL3 COUNT(*) statement for a table.
     * @return a CQL3 statement
     */
    public String buildCountStatement() {
        return select().count().from(spec.getTable()).build();
    }

    /**
     * Constructs a CQL3 COUNT(*) statement for a table.
     * @return a CQL3 statement
     */
    public String buildCountStatement(List<String> keysSet) {
        return select().count().from(spec.getTable()).where(keysSet, "=").build();
    }

    /**
     * Constructs a CQL3 DELETE prepared statement for one entity.
     * @return a CQL3 statement
     */
    public String buildDeleteStatement() {
        return buildDeleteStatement(1);
    }

    /**
     * Constructs a CQL3 DELETE prepared statement for several entities.
     * @param numEntity
     * @return a CQL3 statement
     */
    public String buildDeleteStatement(int numEntity) {
        List<String> keyColumns = spec.getKeyColumns();
        if (keyColumns.size() == 1) {
            return delete().from(spec.getTable()).where(keyColumns.get(0), numEntity).build();
        } else {
            Mutating statement = delete().from(spec.getTable()).where(spec.getKeyColumns(), "=");
            return batch().statement(Collections.nCopies(numEntity, statement)).build();
        }
    }

    /**
     * Constructs a CQL3 TRUNCATE statement for a table.
     * @return a CQL3 statement
     */
    public String buildDeleteAllStatement() {
        return truncate().table(spec.getTable()).build();
    }

    /**
     * Constructs a CQL3 SELECT prepared statement to check for the existance of
     * an entity.
     * @return a CQL3 statement
     */
    public String buildExistsStatement() {
        List<String> keyColumns = spec.getKeyColumns();
        return select().columns(keyColumns).from(spec.getTable()).where(keyColumns, "=").build();
    }

    /**
     * Constructs a CQL3 SELECT statement to find all entities.
     * @return a CQL3 statement
     */
    public String buildFindAllStatement() {
        return select().all().from(spec.getTable()).build();
    }

    /**
     * Constructs a CQL3 SELECT prepared statement to find entities limited by
     * the given number and sorted by the given conditions.
     * @param keysSet
     * @param sort
     * @param limit
     * @return a CQL3 statement
     */
    public String buildLimitedFindAllKeysStatement(List<String> keysSet, Sort sort, int limit) {
        List<String> keyColumns = spec.getKeyColumns();
        return select().columns(keyColumns).from(spec.getTable()).where(keysSet, "=").orderBy(sort).limit(limit)
                .build();
    }

    /**
     * Constructs a CQL3 SELECT prepared statement for one entity with the given
     * number of keys set.
     * @param keyColumnsSet a list of key columns that are set
     * @return
     */
    public String buildFindAllStatement(List<String> keyColumnsSet) {
        return select().all().from(spec.getTable()).where(keyColumnsSet, "=").build();
    }

    /**
     * Constructs a CQL3 SELECT statement to find one entity.
     * @return a CQL3 statement
     */
    public String buildFindOneStatement() {
        return select().all().from(spec.getTable()).where(spec.getKeyColumns(), "=").build();
    }

    /**
     * Constructs a CQL3 INSERT prepared statement for one entity.
     * @return a CQL3 statement
     */
    public String buildSaveStatement() {
        return buildSaveStatement(1);
    }

    /**
     * Constructs a CQL3 INSERT prepared statement for one entity.
     * @param numEntity
     * @return a CQL3 statement
     */
    public String buildSaveStatement(int numEntity) {
        List<String> mappedColumns = spec.getMappedColumns();
        Mutating statement = insert().into(spec.getTable()).columns(mappedColumns);
        if (numEntity == 1) {
            return statement.build();
        } else {
            return batch().statement(Collections.nCopies(numEntity, statement)).build();
        }
    }

}