butler.sql.query
Class Query

java.lang.Object
  extended by butler.sql.query.Query
All Implemented Interfaces:
Joinable, java.lang.Cloneable
Direct Known Subclasses:
CachingQuery, DeleteQuery, ScrollQuery, SQLQuery

public class Query
extends java.lang.Object
implements java.lang.Cloneable, Joinable

Database query. A query is a representation for a select SQL statement.

Author:
Fredrik Bertilsson

Field Summary
protected  java.util.List _cols
           
protected  RecordComparator _comparator
           
protected  Filter _filter
           
protected  int _maxNoOfRec
           
protected  java.lang.String _name
           
protected  Table _table
           
protected  boolean _uniqueSort
           
 
Constructor Summary
Query(Query q)
           
Query(java.lang.String xml)
          Deprecated. Use Query.parse
Query(Table table)
           
 
Method Summary
 void addColumn(AggregateFunction func, Column col)
          Adds a column which should be calculated with an aggregate function (sum, count, etc).
 void addColumn(Column col)
          Adds a column to retrieve.
 void addFunctionColumn(java.lang.String name, ColumnFunction func)
          Adds a function column.
 void addSortCriteria(Column col, boolean ascending)
          Add sort-critera (order by) to query.
 void addSortCriteria(Index index, boolean ascending)
          Add sort-critera (order by) to query.
 void addSortCriteria(int pos, boolean ascending)
          Add sort-critera (order by) to query, with positional parameters.
 void addSortCriteria(java.lang.String alias, Column col, boolean ascending)
          Add sort-critera (order by) to query.
 void addSubQuery(Query q)
           
 void checkPermission()
           
 Query copy()
           
 QueryInstance createInstance()
          Creates a instance of this query.
 java.util.Map createTableDictonary()
           
static Column getColumn(org.dom4j.Element element, Query q)
           
 java.util.List getColumns()
          Returns a list of the columns to retrieve.
 RecordComparator getComparator()
           
 Filter getFilter()
          Returns the filter.
 Filter getFilter(java.lang.Object id)
           
 Filter getHaving()
           
 Join getJoinByAlias(java.lang.String alias)
           
 java.util.List getJoins()
          Returns the joins for this query.
 Table getMainTable()
          Returns the main (start) table for this query.
 int getMaxNoOfRecords()
           
 java.lang.String getName()
          Returns the query name.
protected static java.util.List getPhysicalColumns(java.util.List cols)
           
 java.util.List getSortCriterias()
           
 java.lang.String getSQL()
          Returns the resulting select SQL statement.
 java.util.List getSubQueries()
           
 Table getTable()
           
 java.util.List getUnions()
          Returns the unions for this query.
 boolean is2D()
           
 boolean isNested()
          Returns if this query is nested into an other query.
 boolean isUnion()
          Returns if this query is part of a union.
 boolean isUniqueSort()
          Returns if the sort criterias will give a unique sort.
 Join join(ForeignKey fk)
          Joins another table to this query by a given foreign key.
 Join join(Table table)
          Joins another table to this query by a given table.
 Join join(Table table, Filter filter)
          Joins another table using a filter (instead of a foreign key).
 Join join(Table table, java.lang.String alias)
           
 void modifyFilter(QueryParams params)
           
 java.util.Iterator newIterator(java.sql.ResultSet rs, java.lang.Object source)
          Creates a new Iterator for a ResultSet.
 Join outerJoin(ForeignKey fk)
          Joins another table to this query by a given foreign key, using left outer join.
 Join outerJoin(Table table)
          Joins another table to this query by a given table, using left outer join.
 Join outerJoin(Table table, Filter filter)
          Joins another table using a filter (instead of a foreign key).
static Query parse(java.io.InputStream inStream)
           
static Query parse(java.lang.String xml)
           
static Query parseQuery(org.dom4j.Element element)
           
 Join recursiveJoin(Table table)
          Joins another table to this query by a given table, using a recursive.
protected  void registerJoin(java.lang.String alias, Join join)
           
 RecordList run()
           
protected  void setAggregate(boolean flag)
           
 void setAlias(java.lang.String value)
           
 void setDistinct(boolean flag)
           
 void setFilter(Filter filter)
          Sets the filter (where-clause).
 void setHaving(Filter filter)
           
 void setMaxNoOfRecords(int value)
           
 void setName(java.lang.String name)
          Sets the query name
 void setNested(boolean flag)
           
 void setRecordComparator(RecordComparator comparator)
           
 void setSortCriterias(java.util.List sortCriterias)
          Replaces the current sort-criteria (order by) of the query with the given list.
protected  void setUnion(boolean flag)
          Sets if this query is a subquery in a union.
 void setUniqueSort(boolean flag)
           
 java.lang.String toXML()
           
 void toXML(org.xml.sax.ContentHandler handler)
           
 void union(Query unionQuery)
          Make a union of this query and the other query.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_name

protected java.lang.String _name

_table

protected Table _table

_filter

protected Filter _filter

_cols

protected java.util.List _cols

_comparator

protected RecordComparator _comparator

_uniqueSort

protected boolean _uniqueSort

_maxNoOfRec

protected int _maxNoOfRec
Constructor Detail

Query

public Query(Table table)
      throws java.lang.Exception
Throws:
java.lang.Exception

Query

public Query(java.lang.String xml)
      throws java.lang.Exception
Deprecated. Use Query.parse

Constructs a Query from the xml representation.

Throws:
java.lang.Exception

Query

public Query(Query q)
Method Detail

setName

public void setName(java.lang.String name)
Sets the query name


getName

public java.lang.String getName()
Returns the query name.


setNested

public void setNested(boolean flag)

addColumn

public void addColumn(Column col)
Adds a column to retrieve.


addFunctionColumn

public void addFunctionColumn(java.lang.String name,
                              ColumnFunction func)
                       throws java.lang.Exception
Adds a function column.

Throws:
java.lang.Exception

addColumn

public void addColumn(AggregateFunction func,
                      Column col)
               throws java.lang.Exception
Adds a column which should be calculated with an aggregate function (sum, count, etc).

Throws:
java.lang.Exception

getColumns

public java.util.List getColumns()
Returns a list of the columns to retrieve.


setFilter

public void setFilter(Filter filter)
               throws java.lang.Exception
Sets the filter (where-clause).

Throws:
java.lang.Exception

getFilter

public Filter getFilter()
                 throws java.lang.Exception
Returns the filter.

Throws:
java.lang.Exception

setHaving

public void setHaving(Filter filter)

getHaving

public Filter getHaving()

join

public Join join(ForeignKey fk)
          throws java.lang.Exception
Joins another table to this query by a given foreign key. Returns the resulting join.

Specified by:
join in interface Joinable
Throws:
java.lang.Exception

outerJoin

public Join outerJoin(ForeignKey fk)
               throws java.lang.Exception
Joins another table to this query by a given foreign key, using left outer join. Returns the resulting join.

Specified by:
outerJoin in interface Joinable
Throws:
java.lang.Exception

join

public Join join(Table table)
          throws java.lang.Exception
Joins another table to this query by a given table. If there are more than one foreign keys between the main table and this, an exception will occur. Returns the resulting join.

Throws:
java.lang.Exception

join

public Join join(Table table,
                 java.lang.String alias)
          throws java.lang.Exception
Throws:
java.lang.Exception

join

public Join join(Table table,
                 Filter filter)
          throws java.lang.Exception
Joins another table using a filter (instead of a foreign key).

Throws:
java.lang.Exception

outerJoin

public Join outerJoin(Table table,
                      Filter filter)
               throws java.lang.Exception
Joins another table using a filter (instead of a foreign key).

Throws:
java.lang.Exception

outerJoin

public Join outerJoin(Table table)
               throws java.lang.Exception
Joins another table to this query by a given table, using left outer join. If there are more than one foreign keys between the main table and this, an exception will occur. Returns the resulting join.

Throws:
java.lang.Exception

recursiveJoin

public Join recursiveJoin(Table table)
                   throws java.lang.Exception
Joins another table to this query by a given table, using a recursive. This means that recursive sub-queries will be made. If there are more than one foreign keys between the main table and this, an exception will occur. Returns the resulting join.

Throws:
java.lang.Exception

getJoins

public java.util.List getJoins()
Returns the joins for this query.


union

public void union(Query unionQuery)
Make a union of this query and the other query.


getUnions

public java.util.List getUnions()
Returns the unions for this query.


isUnion

public boolean isUnion()
Returns if this query is part of a union.


isNested

public boolean isNested()
Returns if this query is nested into an other query.


addSubQuery

public void addSubQuery(Query q)
                 throws java.lang.Exception
Throws:
java.lang.Exception

getSubQueries

public java.util.List getSubQueries()

isUniqueSort

public boolean isUniqueSort()
                     throws java.lang.Exception
Returns if the sort criterias will give a unique sort.

Throws:
java.lang.Exception

setUnion

protected void setUnion(boolean flag)
Sets if this query is a subquery in a union.


addSortCriteria

public void addSortCriteria(Column col,
                            boolean ascending)
                     throws java.lang.Exception
Add sort-critera (order by) to query.

Throws:
java.lang.Exception

addSortCriteria

public void addSortCriteria(java.lang.String alias,
                            Column col,
                            boolean ascending)
                     throws java.lang.Exception
Add sort-critera (order by) to query.

Throws:
java.lang.Exception

addSortCriteria

public void addSortCriteria(Index index,
                            boolean ascending)
                     throws java.lang.Exception
Add sort-critera (order by) to query.

Throws:
java.lang.Exception

addSortCriteria

public void addSortCriteria(int pos,
                            boolean ascending)
Add sort-critera (order by) to query, with positional parameters. Used in unions.


setSortCriterias

public void setSortCriterias(java.util.List sortCriterias)
                      throws java.lang.Exception
Replaces the current sort-criteria (order by) of the query with the given list.

Parameters:
sortCriterias - list of new sort-criterias
Throws:
java.lang.Exception

getSortCriterias

public java.util.List getSortCriterias()
                                throws java.lang.Exception
Throws:
java.lang.Exception

setRecordComparator

public void setRecordComparator(RecordComparator comparator)

setMaxNoOfRecords

public void setMaxNoOfRecords(int value)

getMaxNoOfRecords

public int getMaxNoOfRecords()

getMainTable

public Table getMainTable()
Returns the main (start) table for this query.


createInstance

public QueryInstance createInstance()
                             throws java.lang.Exception
Creates a instance of this query.

Throws:
java.lang.Exception

run

public RecordList run()
               throws java.lang.Exception
Throws:
java.lang.Exception

getTable

public Table getTable()

newIterator

public java.util.Iterator newIterator(java.sql.ResultSet rs,
                                      java.lang.Object source)
                               throws java.lang.Exception
Creates a new Iterator for a ResultSet. This may be overridden by subclasses.

Throws:
java.lang.Exception

is2D

public boolean is2D()

getComparator

public RecordComparator getComparator()
                               throws java.lang.Exception
Throws:
java.lang.Exception

toXML

public java.lang.String toXML()
                       throws java.lang.Exception
Throws:
java.lang.Exception

toXML

public void toXML(org.xml.sax.ContentHandler handler)
           throws java.lang.Exception
Throws:
java.lang.Exception

getJoinByAlias

public Join getJoinByAlias(java.lang.String alias)

registerJoin

protected void registerJoin(java.lang.String alias,
                            Join join)

parse

public static Query parse(java.lang.String xml)
                   throws java.lang.Exception
Throws:
java.lang.Exception

parse

public static Query parse(java.io.InputStream inStream)
                   throws java.lang.Exception
Throws:
java.lang.Exception

parseQuery

public static Query parseQuery(org.dom4j.Element element)
                        throws java.lang.Exception
Throws:
java.lang.Exception

getColumn

public static Column getColumn(org.dom4j.Element element,
                               Query q)
                        throws java.lang.Exception
Throws:
java.lang.Exception

checkPermission

public void checkPermission()
                     throws java.lang.Exception
Throws:
java.lang.Exception

createTableDictonary

public java.util.Map createTableDictonary()
                                   throws java.lang.Exception
Throws:
java.lang.Exception

getSQL

public java.lang.String getSQL()
                        throws java.lang.Exception
Returns the resulting select SQL statement.

Throws:
java.lang.Exception

getPhysicalColumns

protected static java.util.List getPhysicalColumns(java.util.List cols)

setAggregate

protected void setAggregate(boolean flag)

setUniqueSort

public void setUniqueSort(boolean flag)

setDistinct

public void setDistinct(boolean flag)

setAlias

public void setAlias(java.lang.String value)

modifyFilter

public void modifyFilter(QueryParams params)
                  throws java.lang.Exception
Throws:
java.lang.Exception

copy

public Query copy()
           throws java.lang.Exception
Throws:
java.lang.Exception

getFilter

public Filter getFilter(java.lang.Object id)