Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *      Copyright (C) 2012 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 java.util.List;
 
A built SELECT statement.
 
 public class Select extends BuiltStatement {
 
     private static final List<ObjectCOUNT_ALL = Collections.<Object>singletonList(new Utils.FCall("count"new Utils.RawString("*")));
 
     private final String keyspace;
     private final String table;
     private final List<ObjectcolumnNames;
     private final Where where;
     private List<Orderingorderings;
     private int limit = -1;
     private boolean allowFiltering;
 
     Select(String keyspaceString tableList<ObjectcolumnNames) {
         super();
         this. = keyspace;
         this. = table;
         this. = columnNames;
         this. = new Where(this);
     }
 
     Select(TableMetadata tableList<ObjectcolumnNames) {
         super(table);
         this. = table.getKeyspace().getName();
         this. = table.getName();
         this. = columnNames;
         this. = new Where(this);
     }
 
     @Override
     protected StringBuilder buildQueryString() {
         StringBuilder builder = new StringBuilder();
 
         builder.append("SELECT ");
         if ( == null) {
             builder.append("*");
         } else {
             Utils.joinAndAppendNames(builder",");
         }
         builder.append(" FROM ");
         if ( != null)
             Utils.appendName(builder).append(".");
         Utils.appendName(builder);
 
         if (!..isEmpty()) {
             builder.append(" WHERE ");
             Utils.joinAndAppend(builder" AND ".);
         }
 
         if ( != null) {
             builder.append(" ORDER BY ");
             Utils.joinAndAppend(builder",");
         }
 
         if ( > 0) {
             builder.append(" LIMIT ").append();
         }
 
         if () {
             builder.append(" ALLOW FILTERING");
         }
 
         return builder;
     }

    
Adds a WHERE clause to this statement. This is a shorter/more readable version for where().and(clause).

Parameters:
clause the clause to add.
Returns:
the where clause of this query to which more clause can be added.
 
    public Where where(Clause clause) {
        return .and(clause);
    }

    
Returns a Where statement for this query without adding clause.

Returns:
the where clause of this query to which more clause can be added.
    public Where where() {
        return ;
    }

    
Adds an ORDER BY clause to this statement.

Parameters:
orderings the orderings to define for this query.
Returns:
this statement.
Throws:
java.lang.IllegalStateException if an ORDER BY clause has already been provided.
    public Select orderBy(Ordering... orderings) {
        if (this. != null)
            throw new IllegalStateException("An ORDER BY clause has already been provided");
        this. = Arrays.asList(orderings);
        setDirty();
        return this;
    }

    
Adds a LIMIT clause to this statement.

Parameters:
limit the limit to set.
Returns:
this statement.
Throws:
java.lang.IllegalArgumentException if limit &gte; 0.
java.lang.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. > 0)
            throw new IllegalStateException("A LIMIT value has already been provided");
        this. = limit;
        setDirty();
        return this;
    }

    
Adds an ALLOW FILTERING directive to this statement.

Returns:
this statement.
    public Select allowFiltering() {
         = true;
        return this;
    }

    
The WHERE clause of a SELECT statement.
    public static class Where extends BuiltStatement.ForwardingStatement<Select> {
        private final List<Clauseclauses = new ArrayList<Clause>();
        Where(Select statement) {
            super(statement);
        }

        
Adds the provided clause to this WHERE clause.

Parameters:
clause the clause to add.
Returns:
this WHERE clause.
        public Where and(Clause clause) {
            .add(clause);
            .maybeAddRoutingKey(clause.name(), clause.firstValue());
            setDirty();
            return this;
        }

        
Adds an ORDER BY clause to the SELECT statement this WHERE clause if part of.

Parameters:
orderings the orderings to add.
Returns:
the select statement this Where clause if part of.
Throws:
java.lang.IllegalStateException if an ORDER BY clause has already been provided.
        public Select orderBy(Ordering... orderings) {
            return .orderBy(orderings);
        }

        
Adds a LIMIT clause to the SELECT statement this Where clause if part of.

Parameters:
limit the limit to set.
Returns:
the select statement this Where clause if part of.
Throws:
java.lang.IllegalArgumentException if limit &gte; 0.
java.lang.IllegalStateException if a LIMIT clause has already been provided.
        public Select limit(int limit) {
            return .limit(limit);
        }
    }

    
An in-construction SELECT statement.
    public static class Builder {
        protected List<ObjectcolumnNames;
        protected Builder() {}
        Builder(List<ObjectcolumnNames) {
            this. = columnNames;
        }

        
Adds the table to select from.

Parameters:
table the name of the table to select from.
Returns:
a newly built SELECT statement that selects from table.
        public Select from(String table) {
            return from(nulltable);
        }

        
Adds the table to select from.

Parameters:
keyspace the name of the keyspace to select from.
table the name of the table to select from.
Returns:
a newly built SELECT statement that selects from keyspace.table.
        public Select from(String keyspaceString table) {
            return new Select(keyspacetable);
        }

        
Adds the table to select from.

Parameters:
table the table to select from.
Returns:
a newly built SELECT statement that selects from table.
        public Select from(TableMetadata table) {
            return new Select(table);
        }
    }

    
An Selection clause for an in-construction SELECT statement.
    public static class Selection extends Builder {

        
Selects all columns (i.e. "SELECT * ...")

Returns:
an in-build SELECT statement.
Throws:
java.lang.IllegalStateException if some columns had already been selected for this builder.
        public Builder all() {
            if ( != null)
                throw new IllegalStateException(String.format("Some columns (%s) have already been selected."));
            return (Builder)this;
        }

        
Selects the count of all returned rows (i.e. "SELECT count(*) ...").

Returns:
an in-build SELECT statement.
Throws:
java.lang.IllegalStateException if some columns had already been selected for this builder.
        public Builder countAll() {
            if ( != null)
                throw new IllegalStateException(String.format("Some columns (%s) have already been selected."));
             = ;
            return (Builder)this;
        }
        private Selection addName(Object name) {
            if ( == null)
                 = new ArrayList<Object>();
            .add(name);
            return this;
        }

        
Selects the provided column.

Parameters:
name the new column name to add.
Returns:
this in-build SELECT statement
        public Selection column(String name) {
            return addName(name);
        }

        
Selects the write time of provided column.

This is a shortcut for fcall("writetime", QueryBuilder.column(name)).

Parameters:
name the name of the column to select the write time of.
Returns:
this in-build SELECT statement
        public Selection writeTime(String name) {
            return addName(new Utils.FCall("writetime"new Utils.CName(name)));
        }

        
Selects the ttl of provided column.

This is a shortcut for fcall("ttl", QueryBuilder.column(name)).

Parameters:
name the name of the column to select the ttl of.
Returns:
this in-build SELECT statement
        public Selection ttl(String name) {
            return addName(new Utils.FCall("ttl"new Utils.CName(name)));
        }

        
Creates a function call.

Please note that the parameters are interpreted as values, and so fcall("textToBlob", "foo") will generate the string "textToBlob('foo')". If you want to generate "textToBlob(foo)", i.e. if the argument must be interpreted as a column name (in a select clause), you will need to use the QueryBuilder.column(java.lang.String) method, and so fcall("textToBlob", QueryBuilder.column(foo).

        public Selection fcall(String nameObject... parameters) {
            return addName(new Utils.FCall(nameparameters));
        }
    }
New to GrepCode? Check out our FAQ X