jimm.datavision.source
Class Query

java.lang.Object
  extended by jimm.datavision.source.Query
All Implemented Interfaces:
Writeable
Direct Known Subclasses:
CharSepQuery, ObjectQuery, SQLQuery

public class Query
extends java.lang.Object
implements Writeable

A data source query.

Author:
Jim Menard, jimm@io.com

Field Summary
protected  java.util.ArrayList joins
           
protected  Report report
           
protected  java.util.ArrayList selectables
           
static int SORT_ASCENDING
           
static int SORT_DESCENDING
           
static int SORT_UNDEFINED
           
protected  java.util.ArrayList sortOrders
           
protected  java.util.ArrayList sortSelectables
           
protected  java.lang.String whereClause
           
 
Constructor Summary
Query(Report r)
          Constructor.
 
Method Summary
 void addAllJoins(java.util.Collection coll)
          Adds all joins in the collection to our list.
 void addJoin(Join join)
          Adds a join to the list of joins used by this query.
 void addSort(Selectable sel, int order)
          Adds a sort order for the specified selectable.
 void clearJoins()
          Removes all joins in the collection from our list.
 void clearSorts()
          Removes all sorts from our list.
 boolean containsReferenceTo(Parameter p)
          Returns true if the specified parameter exists within this query's where clause.
 void findSelectablesUsed()
          Builds collections of the selectables actually used in the report.
 java.lang.String getEditableWhereClause()
          Returns the where clause fit for human consumption.
 int getNumSelectables()
          Returns the number of selectables in the query.
 java.lang.String getWhereClause()
          Returns the raw where clause string; may be null.
 int indexOfSelectable(Selectable selectable)
          Returns the index of the specified selectable.
 java.util.Iterator joins()
          Returns an iterator over all the joins used by this query.
 void reloadColumns(DataSource dataSource)
          Called from DataSource.reloadColumns, this method gives the query source a chance to tell its ancillary objects (such as joins and the sort) to reload selectable objects.
 void removeJoin(Join join)
          Removes a join from the list of joins used by this query.
 void removeSort(Selectable sel)
          Removes a sorting from the list.
 java.util.Iterator selectables()
          Returns an iterator over all selectables.
 void setEditableWhereClause(java.lang.String newWhereClause)
          Sets the where clause (may be null).
 void setWhereClause(java.lang.String newWhereClause)
          Sets the where clause (may be null).
 java.util.Iterator sortedSelectables()
          Returns an iterator over all the sorted selectables used by this query.
 int sortOrderOf(Selectable sel)
          Returns the sort order (SORT_DESCENDING, SORT_ASCENDING, or SORT_UNDEFINED) of the specified selectable.
protected  void writeExtras(XMLWriter out)
          This method exists so subclasses can write out extra information.
 void writeXML(XMLWriter out)
          Writes this query as an XML tag.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SORT_UNDEFINED

public static final int SORT_UNDEFINED
See Also:
Constant Field Values

SORT_DESCENDING

public static final int SORT_DESCENDING
See Also:
Constant Field Values

SORT_ASCENDING

public static final int SORT_ASCENDING
See Also:
Constant Field Values

report

protected Report report

joins

protected java.util.ArrayList joins

whereClause

protected java.lang.String whereClause

sortSelectables

protected java.util.ArrayList sortSelectables

sortOrders

protected java.util.ArrayList sortOrders

selectables

protected java.util.ArrayList selectables
Constructor Detail

Query

public Query(Report r)
Constructor.

Parameters:
r - the report for which this query will retrieve data
Method Detail

containsReferenceTo

public boolean containsReferenceTo(Parameter p)
Returns true if the specified parameter exists within this query's where clause.

Parameters:
p - a parameter
Returns:
true if the specified parameter exists within the where clause

addJoin

public void addJoin(Join join)
Adds a join to the list of joins used by this query.

Parameters:
join - a join

addAllJoins

public void addAllJoins(java.util.Collection coll)
Adds all joins in the collection to our list.

Parameters:
coll - a collection of joins

removeJoin

public void removeJoin(Join join)
Removes a join from the list of joins used by this query.

Parameters:
join - a join

clearJoins

public void clearJoins()
Removes all joins in the collection from our list.


joins

public java.util.Iterator joins()
Returns an iterator over all the joins used by this query.

Returns:
an iterator over all the joins

getEditableWhereClause

public java.lang.String getEditableWhereClause()
Returns the where clause fit for human consumption. This mainly means that we substitute formula, parameter, and user column id numbers with names. Called from a where clause editor. This code assumes that curly braces are never nested.

Returns:
the eval string with formula, parameter, and user column id numbers replaced with names
See Also:
WhereClauseWin

getWhereClause

public java.lang.String getWhereClause()
Returns the raw where clause string; may be null.

Returns:
the where clause string; may be null

setEditableWhereClause

public void setEditableWhereClause(java.lang.String newWhereClause)
Sets the where clause (may be null). The string passed to us contains parameter and formula display strings, not their "real" formulaString representations. Translate the latter into the former before saving this string.

Parameters:
newWhereClause - a where clause string; may be null

setWhereClause

public void setWhereClause(java.lang.String newWhereClause)
Sets the where clause (may be null).

Parameters:
newWhereClause - a where clause string; may be null

addSort

public void addSort(Selectable sel,
                    int order)
Adds a sort order for the specified selectable. The first character of the order string is inspected. If it is a 'd' or 'D', then it's taken to mean "descending". Anything else will result in an "ascending" sort order.

Parameters:
sel - a selectable
order - either SORT_DESCENDING or SORT_ASCENDING

removeSort

public void removeSort(Selectable sel)
Removes a sorting from the list.

Parameters:
sel - a selectable

clearSorts

public void clearSorts()
Removes all sorts from our list.


selectables

public java.util.Iterator selectables()
Returns an iterator over all selectables.

Returns:
an iterator over all selectables

sortedSelectables

public java.util.Iterator sortedSelectables()
Returns an iterator over all the sorted selectables used by this query.

Returns:
an iterator

sortOrderOf

public int sortOrderOf(Selectable sel)
Returns the sort order (SORT_DESCENDING, SORT_ASCENDING, or SORT_UNDEFINED) of the specified selectable.

Parameters:
sel - a database selectable
Returns:
the sort order (SORT_DESCENDING, SORT_ASCENDING, or SORT_UNDEFINED) of the specified selectable.

indexOfSelectable

public int indexOfSelectable(Selectable selectable)
Returns the index of the specified selectable.

Parameters:
selectable - a database selectable

findSelectablesUsed

public void findSelectablesUsed()
Builds collections of the selectables actually used in the report.


getNumSelectables

public int getNumSelectables()
Returns the number of selectables in the query. Does not recalculate the selectables used; we assume this is being called after the qurey has been run or findSelectablesUsed() has been called.

Returns:
the number of selectables (database and user columns) in the query

reloadColumns

public void reloadColumns(DataSource dataSource)
Called from DataSource.reloadColumns, this method gives the query source a chance to tell its ancillary objects (such as joins and the sort) to reload selectable objects.

This is necessary, for example, after a SQL database data source has reloaded all of its table and column information. The old column objects no longer exist. New ones (with the same ids, we assume) have taken their place.

Parameters:
dataSource - the data source

writeXML

public void writeXML(XMLWriter out)
Writes this query as an XML tag. Writes all joins, where clauses, and sorts as well.

Specified by:
writeXML in interface Writeable
Parameters:
out - a writer that knows how to write XML

writeExtras

protected void writeExtras(XMLWriter out)
This method exists so subclasses can write out extra information.