All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datastax.driver.core.querybuilder.Select Maven / Gradle / Ivy

Go to download

A driver for Apache Cassandra 1.2+ that works exclusively with the Cassandra Query Language version 3 (CQL3) and Cassandra's binary protocol.

There is a newer version: 4.0.0
Show newest version
/*
 *      Copyright (C) 2012-2015 DataStax 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 com.datastax.driver.core.querybuilder;

import com.datastax.driver.core.CodecRegistry;
import com.datastax.driver.core.ColumnMetadata;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.TableMetadata;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * A built SELECT statement.
 */
public class Select extends BuiltStatement {

    private static final List COUNT_ALL = Collections.singletonList(new Utils.FCall("count", new Utils.RawString("*")));

    private final String table;
    private final boolean isDistinct;
    private final List columnNames;
    private final Where where;
    private List orderings;
    private Object limit;
    private boolean allowFiltering;

    Select(String keyspace, String table, List columnNames, boolean isDistinct) {
        this(keyspace, table, null, null, columnNames, isDistinct);
    }

    Select(TableMetadata table, List columnNames, boolean isDistinct) {
        this(escapeId(table.getKeyspace().getName()),
                escapeId(table.getName()),
                Arrays.asList(new Object[table.getPartitionKey().size()]),
                table.getPartitionKey(),
                columnNames,
                isDistinct);
    }

    Select(String keyspace,
           String table,
           List routingKeyValues,
           List partitionKey,
           List columnNames,
           boolean isDistinct) {
        super(keyspace, partitionKey, routingKeyValues);
        this.table = table;
        this.isDistinct = isDistinct;
        this.columnNames = columnNames;
        this.where = new Where(this);
    }

    @Override
    StringBuilder buildQueryString(List variables, CodecRegistry codecRegistry) {
        StringBuilder builder = new StringBuilder();

        builder.append("SELECT ");
        if (isDistinct)
            builder.append("DISTINCT ");
        if (columnNames == null) {
            builder.append('*');
        } else {
            Utils.joinAndAppendNames(builder, codecRegistry, ",", columnNames);
        }
        builder.append(" FROM ");
        if (keyspace != null)
            Utils.appendName(keyspace, builder).append('.');
        Utils.appendName(table, builder);

        if (!where.clauses.isEmpty()) {
            builder.append(" WHERE ");
            Utils.joinAndAppend(builder, codecRegistry, " AND ", where.clauses, variables);
        }

        if (orderings != null) {
            builder.append(" ORDER BY ");
            Utils.joinAndAppend(builder, codecRegistry, ",", orderings, variables);
        }

        if (limit != null) {
            builder.append(" LIMIT ").append(limit);
        }

        if (allowFiltering) {
            builder.append(" ALLOW FILTERING");
        }

        return builder;
    }

    /**
     * Adds a WHERE clause to this statement.
     * 

* This is a shorter/more readable version for {@code where().and(clause)}. * * @param clause the clause to add. * @return the where clause of this query to which more clause can be added. */ public Where where(Clause clause) { return where.and(clause); } /** * Returns a Where statement for this query without adding clause. * * @return the where clause of this query to which more clause can be added. */ public Where where() { return where; } /** * Adds an ORDER BY clause to this statement. * * @param orderings the orderings to define for this query. * @return this statement. * @throws IllegalStateException if an ORDER BY clause has already been * provided. */ public Select orderBy(Ordering... orderings) { if (this.orderings != null) throw new IllegalStateException("An ORDER BY clause has already been provided"); this.orderings = Arrays.asList(orderings); for (int i = 0; i < orderings.length; i++) checkForBindMarkers(orderings[i]); return this; } /** * Adds a LIMIT clause to this statement. * * @param limit the limit to set. * @return this statement. * @throws IllegalArgumentException if {@code limit >e; 0}. * @throws IllegalStateException if a LIMIT clause has already been * provided. */ public Select limit(int limit) { if (limit <= 0) throw new IllegalArgumentException("Invalid LIMIT value, must be strictly positive"); if (this.limit != null) throw new IllegalStateException("A LIMIT value has already been provided"); this.limit = limit; checkForBindMarkers(null); return this; } /** * Adds a prepared LIMIT clause to this statement. * * @param marker the marker to use for the limit. * @return this statement. * @throws IllegalStateException if a LIMIT clause has already been * provided. */ public Select limit(BindMarker marker) { if (this.limit != null) throw new IllegalStateException("A LIMIT value has already been provided"); this.limit = marker; checkForBindMarkers(marker); return this; } /** * Adds an ALLOW FILTERING directive to this statement. * * @return this statement. */ public Select allowFiltering() { allowFiltering = true; return this; } /** * The WHERE clause of a SELECT statement. */ public static class Where extends BuiltStatement.ForwardingStatement