com.onewebsql.query
Class SelectQuery

java.lang.Object
  extended by com.onewebsql.query.WhereQuery
      extended by com.onewebsql.query.SelectQuery
All Implemented Interfaces:
Expression

public class SelectQuery
extends WhereQuery

Represents a SELECT query. The methods use Fluent API: they returned the modified object so that you can chain expression as follows:

        SelectQuery query = new SelectQuery(table);
        query.setColumns(columns).setOrderBy(orderBy).setGroupBy(groupBy) ....
 


Field Summary
static String AS
          Constant used to build a query.
static String DISTINCT
          Constant used to build a query.
static String FROM
          Constant used to build a query.
static String GROUP_BY
          Constant used to build a query.
static String HAVING
          Constant used to build a query.
static String ORDER_BY
          Constant used to build a query.
static String queryLexem
          Constant used to build a query.
 
Fields inherited from class com.onewebsql.query.WhereQuery
where, WHERE
 
Fields inherited from interface com.onewebsql.query.Expression
COMMA, DOT, LB, QUESTION_MARK, RB, SPACE
 
Constructor Summary
SelectQuery(List<? extends TableReference> tableReferences)
          Creates a SELECT query retrieving all columns from the specified table references.
SelectQuery(List<? extends TableReference> tableReferences, AExp exp)
          Creates a SELECT query retrieving the specified column (expression) from the specified table references.
SelectQuery(List<? extends TableReference> tableReferences, List<? extends AExp> columns)
          Creates a SELECT query retrieving the specified columns (expressions) from the specified table references.
SelectQuery(List<? extends TableReference> tableReferences, List<? extends AExp> columns, LExp where, List<String> aliases, List<String> columnAliases, List<? extends AExp> groupBy, List<OrderByItem> orderBy, LExp having, int limit, int offset, boolean distinct, String schemaName)
          Full constructor.
SelectQuery(TableReference tableReference)
          Creates a SELECT query retrieving rows from the specified table reference.
SelectQuery(TableReference tableReference, AExp aexp)
          Creates a SELECT query retrieving the specified column (expression) from the specified table reference.
SelectQuery(TableReference tableReference, List<? extends AExp> columns)
          Creates a SELECT query retrieving rows the specified columns (expressions) from the specified table reference.
 
Method Summary
 void addOrderBy(OrderByItem oi)
          Adds an element to the ORDER BY clause.
 void build(StringBuilder sb, List<Value> objs, DBAdapter adapter)
          Builds the query using the schema name from this object.
 void build(StringBuilder sb, String schemaName, List<Value> objs, DBAdapter adapter)
          Builds the query.
 void buildStandard(StringBuilder sb, String schemaName, List<Value> objs, DBAdapter adapter)
          Builds the query in a standard way.
protected  void buildStandardFrom(StringBuilder sb, String schemaName, List<Value> objs, DBAdapter adapter)
          Builds a FROM part of query in a standard way
protected  void buildStandardGroupBy(StringBuilder sb, String schemaName, List<Value> objs, DBAdapter adapter)
          Builds a GROUP BY part of query in a standard way.
protected  void buildStandardHaving(StringBuilder sb, String schemaName, List<Value> objs, DBAdapter adapter)
          Builds a HAVING part of query in a standard way.
protected  void buildStandardOrderBy(StringBuilder sb, String schemaName, List<Value> objs, DBAdapter adapter)
          Builds a ORDER BY part of query in a standard way.
protected  void buildStandardSelect(StringBuilder sb, String schemaName, List<Value> objs, DBAdapter adapter)
          Builds a SELECT part of query in a standard way
protected  void buildStandardWhere(StringBuilder sb, String schemaName, List<Value> objs, DBAdapter adapter)
          Builds a WHERE part of query in a standard way
 SelectQuery distinct(boolean distinct)
          The same as setDistinct(boolean) but it returns the modifies query.
 boolean equals(Object obj)
           
 List<String> getAliases()
          Returns the list of table reference aliases the query uses.
 List<String> getColumnAliases()
          Returns the list of column aliases used by the query.
 List<AExp> getColumns()
          Returns the list of columns (expressions) the query retrieves.
 List<AExp> getGroupBy()
          Returns the list of columns in the GROUP BY clause
 LExp getHaving()
          Returns the HAVING condition of the query
 int getLimit()
          Returns the number of rows to retrieve by the query.
 int getOffset()
          Returns the offset of rows retrieved by the query.
 List<OrderByItem> getOrderBy()
          Returns the list of columns (expressions) in the ORDER BY clause of the query.
 String getSchemaName()
          Returns the value of the field schemaName
 List<TableReference> getTableReferences()
          Returns the list of table references the query uses
 SelectQuery groupBy(AExp exp)
          Adds a column to the GROUP BY clause.
 int hashCode()
           
 boolean isDistinct()
          Returns information whether the query is DISTINCT.
 SelectQuery limit(int limit)
          Sets the number of rows to retrieve by the query.
 SelectQuery limit(int limit, int offset)
          Sets the number and offset of rows to retrieve by the query.
 SelectQuery orderBy(AExp column)
          Adds an element of the ORDER BY clause with the ascending order
 SelectQuery orderBy(AExp column, boolean direction)
          Adds an element of the ORDER BY clause with the specified sorting direction.
 SelectQuery removeOrderBy()
          Remove the ORDER BY clause from the query.
 SelectQuery setAliases(List<String> aliases)
          Sets the list of table reference aliases.
 SelectQuery setColumnAliases(List<String> columnAliases)
          Sets the list of column aliases used by the query.
 SelectQuery setColumns(List<? extends AExp> columns)
          Sets the list of columns (expressions) the query retrieves.
 void setDistinct(boolean distinct)
          Sets information whether the query is DISTINCT.
 SelectQuery setHaving(LExp exp)
          Sets the HAVING clause of the query.
 void setOrderBy(List<OrderByItem> orderBy)
          Sets the list of columns (expressions) in the ORDER BY clause of the query.
 void setSchemaName(String schemaName)
          Sets the value of the field schemaName.
 String toString()
           
 SelectQuery where(LExp where)
          The same as WhereQuery.setWhere(LExp) but it returns the modified query.
 
Methods inherited from class com.onewebsql.query.WhereQuery
getWhere, setWhere
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

queryLexem

public static final String queryLexem
Constant used to build a query.

See Also:
Constant Field Values

DISTINCT

public static final String DISTINCT
Constant used to build a query.

See Also:
Constant Field Values

FROM

public static final String FROM
Constant used to build a query.

See Also:
Constant Field Values

GROUP_BY

public static final String GROUP_BY
Constant used to build a query.

See Also:
Constant Field Values

HAVING

public static final String HAVING
Constant used to build a query.

See Also:
Constant Field Values

ORDER_BY

public static final String ORDER_BY
Constant used to build a query.

See Also:
Constant Field Values

AS

public static final String AS
Constant used to build a query.

See Also:
Constant Field Values
Constructor Detail

SelectQuery

public SelectQuery(TableReference tableReference)
Creates a SELECT query retrieving rows from the specified table reference. The resulting query selects all columns of the reference.

Parameters:
tableReference - table reference

SelectQuery

public SelectQuery(TableReference tableReference,
                   List<? extends AExp> columns)
Creates a SELECT query retrieving rows the specified columns (expressions) from the specified table reference.

Parameters:
tableReference - table reference
columns - list of columns (expressions) to select

SelectQuery

public SelectQuery(TableReference tableReference,
                   AExp aexp)
Creates a SELECT query retrieving the specified column (expression) from the specified table reference.

Parameters:
tableReference - table reference
aexp - expression

SelectQuery

public SelectQuery(List<? extends TableReference> tableReferences)
Creates a SELECT query retrieving all columns from the specified table references.

Parameters:
tableReferences - list of table references

SelectQuery

public SelectQuery(List<? extends TableReference> tableReferences,
                   List<? extends AExp> columns)
Creates a SELECT query retrieving the specified columns (expressions) from the specified table references.

Parameters:
tableReferences - list of table references
columns - list of columns (expressions)

SelectQuery

public SelectQuery(List<? extends TableReference> tableReferences,
                   AExp exp)
Creates a SELECT query retrieving the specified column (expression) from the specified table references.

Parameters:
tableReferences - list of table references
exp - expression

SelectQuery

public SelectQuery(List<? extends TableReference> tableReferences,
                   List<? extends AExp> columns,
                   LExp where,
                   List<String> aliases,
                   List<String> columnAliases,
                   List<? extends AExp> groupBy,
                   List<OrderByItem> orderBy,
                   LExp having,
                   int limit,
                   int offset,
                   boolean distinct,
                   String schemaName)
Full constructor.

Parameters:
tableReferences - list of table references (FROM clause)
columns - list of columns (SELECT clause)
where - WHERE condition
aliases - list of aliases (in the same order as tableReferences)
columnAliases - list of column aliasese (in the same order as columns)
groupBy - GROUP BY clause
orderBy - ORDER BY clause
having - HAVING clause
limit - the number of rows to retrieve
offset - offset of rows to retrieve
distinct - is the query DISTINCT
schemaName - name of the schema
Method Detail

getColumns

public List<AExp> getColumns()
Returns the list of columns (expressions) the query retrieves.

Returns:
list of columns (expressions) the query retrieves.

setColumns

public SelectQuery setColumns(List<? extends AExp> columns)
Sets the list of columns (expressions) the query retrieves.

Parameters:
columns - list of columns (expressions) the query retrieves.
Returns:
modified query

getTableReferences

public List<TableReference> getTableReferences()
Returns the list of table references the query uses

Returns:
list of table references the query uses

getAliases

public List<String> getAliases()
Returns the list of table reference aliases the query uses.

Returns:
list of table reference aliases the query uses

setAliases

public SelectQuery setAliases(List<String> aliases)
Sets the list of table reference aliases.

Parameters:
aliases - lista of table reference aliases.
Returns:
modified query

getColumnAliases

public List<String> getColumnAliases()
Returns the list of column aliases used by the query.

Returns:
list of column aliases used by the query

setColumnAliases

public SelectQuery setColumnAliases(List<String> columnAliases)
Sets the list of column aliases used by the query.

Parameters:
columnAliases - list of column aliases used by the query.
Returns:
modified query

getGroupBy

public List<AExp> getGroupBy()
Returns the list of columns in the GROUP BY clause

Returns:
list of columns in the GROUP BY clause

groupBy

public SelectQuery groupBy(AExp exp)
Adds a column to the GROUP BY clause.

Parameters:
exp - column to add
Returns:
modified query

getHaving

public LExp getHaving()
Returns the HAVING condition of the query

Returns:
HAVING condition of the query

setHaving

public SelectQuery setHaving(LExp exp)
Sets the HAVING clause of the query.

Parameters:
exp - HAVING condition
Returns:
modified query

getOrderBy

public List<OrderByItem> getOrderBy()
Returns the list of columns (expressions) in the ORDER BY clause of the query.

Returns:
list of columns (expressions) in the ORDER BY clause of the query

setOrderBy

public void setOrderBy(List<OrderByItem> orderBy)
Sets the list of columns (expressions) in the ORDER BY clause of the query.

Parameters:
orderBy - list of columns (expressions) in the ORDER BY clause of the query

addOrderBy

public void addOrderBy(OrderByItem oi)
Adds an element to the ORDER BY clause.

Parameters:
oi - element to add

orderBy

public SelectQuery orderBy(AExp column)
Adds an element of the ORDER BY clause with the ascending order

Parameters:
column - the column we order by
Returns:
modified query

orderBy

public SelectQuery orderBy(AExp column,
                           boolean direction)
Adds an element of the ORDER BY clause with the specified sorting direction.

Parameters:
column - the column we order by
direction - sorting direction (true - ascending, false - descending)
Returns:
modified query

removeOrderBy

public SelectQuery removeOrderBy()
Remove the ORDER BY clause from the query.

Returns:
modified query

isDistinct

public boolean isDistinct()
Returns information whether the query is DISTINCT.

Returns:
true if the query is DISTINCT, false otherwise

setDistinct

public void setDistinct(boolean distinct)
Sets information whether the query is DISTINCT.

Parameters:
distinct - information whether the query is DISTINCT.

distinct

public SelectQuery distinct(boolean distinct)
The same as setDistinct(boolean) but it returns the modifies query.

Parameters:
distinct - true if SELECT should have DISTINCT clause, false otherwise.
Returns:
modified query

limit

public SelectQuery limit(int limit)
Sets the number of rows to retrieve by the query.

Parameters:
limit - number of rows to retrieve by the query
Returns:
modified query

limit

public SelectQuery limit(int limit,
                         int offset)
Sets the number and offset of rows to retrieve by the query. Suppose the query returns the following list of rows:
ID...
1...
2...
3...
4...
5...
If you set limit to 2 and offset to 1 then the query returns 2 rows: one with ID equal 2 and one with ID equal 3.
The offset parametr can only be set if the query has ORDER BY clause.

Parameters:
limit - number of rows retrieved by the query, 0 means no limit
offset - offset of the records retrieved by the query; 0 means the query returns the first row returned by the query
Returns:
modified query
Throws:
IllegalArgumentException - if the limit or offset parameters are less that 0 or if limit is 0 but offset is greater than 0

getLimit

public int getLimit()
Returns the number of rows to retrieve by the query.

Returns:
number of rows to retrieve by the query.

getOffset

public int getOffset()
Returns the offset of rows retrieved by the query.

Returns:
offset of rows retrieved by the query

where

public SelectQuery where(LExp where)
The same as WhereQuery.setWhere(LExp) but it returns the modified query.

Parameters:
where - WHERE clause
Returns:
modified query

build

public void build(StringBuilder sb,
                  String schemaName,
                  List<Value> objs,
                  DBAdapter adapter)
Builds the query. The queries may be build in a way specific for the database by DBAdapter.buildQuery(SelectQuery, String, StringBuilder, List).

Parameters:
sb - StringBuilder to build the query text
schemaName - name of the database schema used to build the query
objs - an accumulator list to which query parameters are appended
adapter - database adapter which builds builds query in a way specific to the database engine
Throws:
IllegalArgumentException - if offset is greater that 0 but the query has no ORDER BY clause.

buildStandard

public void buildStandard(StringBuilder sb,
                          String schemaName,
                          List<Value> objs,
                          DBAdapter adapter)
Builds the query in a standard way.

Parameters:
sb - StringBuilder to build the query
schemaName - schema name
objs - objs an accumulator list to which query parameters are appended
adapter - database adapter which builds builds query in a way specific to the database engine

buildStandardSelect

protected void buildStandardSelect(StringBuilder sb,
                                   String schemaName,
                                   List<Value> objs,
                                   DBAdapter adapter)
Builds a SELECT part of query in a standard way

Parameters:
sb - StringBuilder to build the query
schemaName - schema name
objs - objs an accumulator list to which query parameters are appended
adapter - database adapter which builds builds query in a way specific to the database engine

buildStandardFrom

protected void buildStandardFrom(StringBuilder sb,
                                 String schemaName,
                                 List<Value> objs,
                                 DBAdapter adapter)
Builds a FROM part of query in a standard way

Parameters:
sb - StringBuilder to build the query
schemaName - schema name
objs - objs an accumulator list to which query parameters are appended
adapter - database adapter which builds builds query in a way specific to the database engine

buildStandardWhere

protected void buildStandardWhere(StringBuilder sb,
                                  String schemaName,
                                  List<Value> objs,
                                  DBAdapter adapter)
Builds a WHERE part of query in a standard way

Parameters:
sb - StringBuilder to build the query
schemaName - schema name
objs - objs an accumulator list to which query parameters are appended
adapter - database adapter which builds builds query in a way specific to the database engine

buildStandardGroupBy

protected void buildStandardGroupBy(StringBuilder sb,
                                    String schemaName,
                                    List<Value> objs,
                                    DBAdapter adapter)
Builds a GROUP BY part of query in a standard way.

Parameters:
sb - StringBuilder to build the query
schemaName - schema name
objs - objs an accumulator list to which query parameters are appended
adapter - database adapter which builds builds query in a way specific to the database engine

buildStandardOrderBy

protected void buildStandardOrderBy(StringBuilder sb,
                                    String schemaName,
                                    List<Value> objs,
                                    DBAdapter adapter)
Builds a ORDER BY part of query in a standard way.

Parameters:
sb - StringBuilder to build the query
schemaName - schema name
objs - objs an accumulator list to which query parameters are appended
adapter - database adapter which builds builds query in a way specific to the database engine

buildStandardHaving

protected void buildStandardHaving(StringBuilder sb,
                                   String schemaName,
                                   List<Value> objs,
                                   DBAdapter adapter)
Builds a HAVING part of query in a standard way.

Parameters:
sb - StringBuilder to build the query
schemaName - schema name
objs - objs an accumulator list to which query parameters are appended
adapter - database adapter which builds builds query in a way specific to the database engine

build

public void build(StringBuilder sb,
                  List<Value> objs,
                  DBAdapter adapter)
Builds the query using the schema name from this object.

Parameters:
sb - StringBuilder to build the query
objs - objs an accumulator list to which query parameters are appended
adapter - database adapter which builds builds query in a way specific to the database engine

getSchemaName

public String getSchemaName()
Returns the value of the field schemaName

Returns:
value of the field schemaName

setSchemaName

public void setSchemaName(String schemaName)
Sets the value of the field schemaName.

Parameters:
schemaName - value of the field schemaName

toString

public String toString()
Overrides:
toString in class WhereQuery

hashCode

public int hashCode()
Overrides:
hashCode in class WhereQuery

equals

public boolean equals(Object obj)
Overrides:
equals in class WhereQuery


Copyright © 2011-2013 e-point SA. All Rights Reserved.