com.google.api.ads.dfp.lib.utils
Class QueryBuilder<V>

java.lang.Object
  extended by com.google.api.ads.dfp.lib.utils.QueryBuilder<V>
Type Parameters:
V - the type of the API Value
All Implemented Interfaces:
QueryBuilderInterface<V>

public class QueryBuilder<V>
extends Object
implements QueryBuilderInterface<V>

QueryBuilder allows for queries to be constructed in parts.

Author:
Adam Rogal

Field Summary
protected  String from
           
protected static String FROM
           
protected  Integer limit
           
protected static String LIMIT
           
protected  Integer offset
           
protected static String OFFSET
           
protected static String ORDER_BY
           
protected  String orderBy
           
protected  String select
           
protected static String SELECT
           
protected  Map<String,V> valueMap
           
protected  String where
           
protected static String WHERE
           
 
Constructor Summary
QueryBuilder()
          Constructs a query builder.
 
Method Summary
 String buildQuery()
          Builds the query from the clauses.
 QueryBuilder<V> from(String table)
          Sets the statement FROM clause in the form of "table".
 Map<String,V> getBindVariableMap()
          Returns an unmodifiable form of the key to value map.
 Integer getOffset()
          Gets the current offset.
 QueryBuilder<V> increaseOffsetBy(Integer amount)
          Increases the offset by the amount.
 QueryBuilder<V> limit(Integer count)
          Sets the statement LIMIT clause in the form of

"LIMIT <count>"

e.g.
 QueryBuilder<V> offset(Integer count)
          Sets the statement OFFSET clause in the form of

"OFFSET <count>"

e.g.
 QueryBuilder<V> orderBy(String orderBy)
          Sets the statement ORDER BY clause in the form of

"ORDER BY <property> [ASC | DESC]"

e.g.
 QueryBuilder<V> removeLimitAndOffset()
          Removes the limit and offset from the query.
 QueryBuilder<V> select(String columns)
          Sets the statement SELECT clause in the form of "a,b" or "*".
protected  void validateQuery()
          Checks that the query is valid.
 QueryBuilder<V> where(String conditions)
          Sets the statement WHERE clause in the form of

"WHERE <condition> {[AND | OR] <condition> ...}"

e.g.
 QueryBuilder<V> withBindVariableValue(String key, V value)
          Adds a value to the statement in the form of a Value.
 QueryBuilder<V> withBindVariableValues(Map<String,V> values)
          Adds all key value mappings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SELECT

protected static final String SELECT
See Also:
Constant Field Values

FROM

protected static final String FROM
See Also:
Constant Field Values

WHERE

protected static final String WHERE
See Also:
Constant Field Values

LIMIT

protected static final String LIMIT
See Also:
Constant Field Values

OFFSET

protected static final String OFFSET
See Also:
Constant Field Values

ORDER_BY

protected static final String ORDER_BY
See Also:
Constant Field Values

select

protected String select

from

protected String from

where

protected String where

limit

protected Integer limit

offset

protected Integer offset

orderBy

protected String orderBy

valueMap

protected Map<String,V> valueMap
Constructor Detail

QueryBuilder

public QueryBuilder()
Constructs a query builder.

Method Detail

select

public QueryBuilder<V> select(String columns)
Sets the statement SELECT clause in the form of "a,b" or "*". Only necessary for statements being sent to the PublisherQueryLanguageService. The "SELECT " keyword will be ignored.

Specified by:
select in interface QueryBuilderInterface<V>
Parameters:
columns - the statement select clause without "SELECT"
Returns:
a reference to this object

from

public QueryBuilder<V> from(String table)
Sets the statement FROM clause in the form of "table". Only necessary for statements being sent to the PublisherQueryLanguageService. The "FROM " keyword will be ignored.

Specified by:
from in interface QueryBuilderInterface<V>
Parameters:
table - the statement from clause without "FROM"
Returns:
a reference to this object

where

public QueryBuilder<V> where(String conditions)
Sets the statement WHERE clause in the form of

"WHERE <condition> {[AND | OR] <condition> ...}"

e.g. "a = b OR b = c". The "WHERE " keyword will be ignored.

Specified by:
where in interface QueryBuilderInterface<V>
Parameters:
conditions - the statement query without "WHERE"
Returns:
a reference to this object

limit

public QueryBuilder<V> limit(Integer count)
Sets the statement LIMIT clause in the form of

"LIMIT <count>"

e.g. 1000.

Specified by:
limit in interface QueryBuilderInterface<V>
Parameters:
count - the statement limit
Returns:
a reference to this object

offset

public QueryBuilder<V> offset(Integer count)
Sets the statement OFFSET clause in the form of

"OFFSET <count>"

e.g. 200.

Specified by:
offset in interface QueryBuilderInterface<V>
Parameters:
count - the statement offset
Returns:
a reference to this object

increaseOffsetBy

public QueryBuilder<V> increaseOffsetBy(Integer amount)
Increases the offset by the amount.

Specified by:
increaseOffsetBy in interface QueryBuilderInterface<V>
Parameters:
amount - the amount to increase the offset
Returns:
a reference to this object

getOffset

public Integer getOffset()
Gets the current offset.

Specified by:
getOffset in interface QueryBuilderInterface<V>
Returns:
the current offset

removeLimitAndOffset

public QueryBuilder<V> removeLimitAndOffset()
Removes the limit and offset from the query.

Specified by:
removeLimitAndOffset in interface QueryBuilderInterface<V>
Returns:
a reference to this object

orderBy

public QueryBuilder<V> orderBy(String orderBy)
Sets the statement ORDER BY clause in the form of

"ORDER BY <property> [ASC | DESC]"

e.g. "type ASC, lastModifiedDateTime DESC". The "ORDER BY " keyword will be ignored.

Specified by:
orderBy in interface QueryBuilderInterface<V>
Parameters:
orderBy - the statement order by without "ORDER BY"
Returns:
a reference to this object

withBindVariableValue

public QueryBuilder<V> withBindVariableValue(String key,
                                             V value)
Adds a value to the statement in the form of a Value.

Specified by:
withBindVariableValue in interface QueryBuilderInterface<V>
Parameters:
key - the value key
value - the value
Returns:
a reference to this object

withBindVariableValues

public QueryBuilder<V> withBindVariableValues(Map<String,V> values)
Adds all key value mappings.

Parameters:
values - the mappings of key to value of type
Returns:
a reference to this object

getBindVariableMap

public Map<String,V> getBindVariableMap()
Returns an unmodifiable form of the key to value map.

Specified by:
getBindVariableMap in interface QueryBuilderInterface<V>

validateQuery

protected void validateQuery()
Checks that the query is valid.


buildQuery

public String buildQuery()
Builds the query from the clauses.

Specified by:
buildQuery in interface QueryBuilderInterface<V>
Returns:
the c query


Copyright © 2012. All Rights Reserved.