| java.lang.Object org.josql.Query
All known Subclasses: org.josql.contrib.JoSQLJRDataSource, org.josql.contrib.JoSQLFreeChartPieDataset, org.josql.contrib.JoSQLSwingTableModel, org.josql.contrib.JoSQLFreeChartXYDataset, org.josql.contrib.JoSQLAntFileSelector, org.josql.contrib.JoSQLFreeChartCategoryDataset,
Query | public class Query (Code) | | This class provides the ability for a developer to apply an arbitrary SQL statement
(using suitable syntax) to a collection of Java objects.
Basic usage:
Query q = new Query ();
q.parse (myStatement);
List results = q.execute (myObjects);
An example statement would look like:
SELECT lastModified,
name
FROM java.io.File
WHERE name LIKE '%.html'
The JoSQL functionality is large and complex, whilst basic queries like the one above are
perfectly possible, very complex queries are also possible, for example:
SELECT name,
formatDate(lastModified),
formatNumber(length),
formatNumber(length - @avg_length),
formatTimeDuration(@max_last_modified - lastModified)
FROM java.io.File
WHERE lastModified > @avg_last_modified
AND length > @avg_length
AND lower(name) LIKE '%.html'
GROUP BY path
ORDER BY name, lastModified DESC
EXECUTE ON ALL avg (:_allobjs, length) avg_length,
avg (:_allobjs, lastModified) avg_last_modified,
max (:_allobjs, lastModified) max_last_modified
Note: the "EXECUTE ON ALL" syntax is an extension used by JoSQL because it has no notion
of "aggregate functions".
For full details of how a query works and what is possible, see the
JoSQL User Manual.
Please note that the package structure for JoSQL is deliberate, JoSQL is designed to be a
black box and lightweight thus only the Query object and associated exceptions
are exposed in the main package. Also, the class structure for JoSQL is not designed to
exactly represent the SQL statement passed to it, rather the classes are optimised for
ease of execution of the statement. If you wish to have a completely accurate Java object
view of ANY SQL statement then please see:
JSqlParser
which will provide what you need.
|
Constructor Summary | |
public | Query() Create a new blank Query object. |
Method Summary | |
public void | addBindVariableChangedListener(BindVariableChangedListener bvl) | public void | addFunctionHandler(Object o) | public void | addSaveValueChangedListener(SaveValueChangedListener svl) | protected void | addTiming(String id, double time) | public void | doExecuteOn(List l, String t) Execute all the expressions for the specified type, either:
Query.ALL or:
Query.RESULTS . | public QueryResults | execute(List objs) Execute this query on the specified objects.
Parameters: objs - The list of objects to execute the query on. | protected void | fireBindVariableChangedEvent(String name, Object from, Object to) | protected void | fireSaveValueChangedEvent(Object id, Object from, Object to) | public Map | getAliases() | public List | getAllObjects() Get the current list of objects in context (value of the :_allobjs special bind variable). | public String | getAnonymousBindVariableName() | public ClassLoader | getClassLoader() | public List | getColumns() | public Object | getCurrentObject() Get the current object (value of the :_currobj special bind variable). | public List | getDefaultFunctionHandlers() | public Map | getExecuteOnFunctions() | public Expression | getFrom() | public Class | getFromObjectClass() | public FunctionHandler | getFunctionHandler(String id) | public List | getFunctionHandlers() | public List | getGroupByColumns() | public Object | getGroupBySaveValue(Object id, List gbs) Get the save value for a particular key and group by list.
Parameters: id - The id of the save value. Parameters: gbs - The group by list key. | public Map | getGroupBySaveValues(List gbs) Get the save values for the specified group bys.
Parameters: gbs - The group bys. | public Object | getGroupByVariable(int ind) Get the value of a group by variable from the current group bys.
Parameters: ind - The variable index. | public Expression | getHavingClause() Return the HAVING clause expression. | public Limit | getLimit() Get the object that represents the limit clause. | public List | getOrderByColumns() Get the "order bys". | public Comparator | getOrderByComparator() Return the
Comparator we will use to do the ordering of the results, may be null. | public Query | getParent() Get the parent query. | public String | getQuery() Get the query string that this Query object represents. | public QueryResults | getQueryResults() Get the results of
Query.execute(java.util.List) executing this query. | public Object | getSaveValue(Object id) Get the save values for a particular key. | public Query | getTopLevelQuery() Get the top level query if "this" is a sub-query, the query chain is traversed until
the top level query is found, i.e. | public Object | getVariable(int index) Get the value of an indexed bind variable.
Parameters: index - The index. | public Object | getVariable(String name) Get the value of a named bind variable.
Parameters: name - The name of the bind variable. | public Class | getVariableClass(String name) Get the class that the named variable has.
Parameters: name - The name of the variable. | public Map | getVariables() Get all the bind variables as a Map. | public Expression | getWhereClause() Return the WHERE clause expression. | public char | getWildcardCharacter() Get the character that represents a wildcard in LIKE searches. | public void | init() | public void | initOrderByComparator() Will cause the order by comparator used to order the results
to be initialized. | public boolean | isWantObjects() Return whether the query should return objects. | public boolean | isWhereTrue(Object o) A helper method that will evaluate the WHERE clause for the object passed in.
Parameters: o - The object to evaluate the WHERE clause against. | public Class | loadClass(String name) | public void | parse(String q) Parse the JoSQL query. | public boolean | parsed() Return whether this Query object has had a statement applied to it
and has been parsed. | public void | removeBindVariableChangedListener(BindVariableChangedListener bvl) | public void | removeSaveValueChangedListener(SaveValueChangedListener svl) | public QueryResults | reorder(List objs, SortedMap dirs) Re-order the objects according to the columns supplied in the dirs Map.
The Map should be keyed on an Integer and map to a String value, the String value should
be either:
Query.ORDER_BY_ASC for the column to be in ascending order or:
Query.ORDER_BY_DESC for the column to be in descending order. | public QueryResults | reorder(List objs, String orderBys) Allows the re-ordering of the results via a textual representation of the order bys.
This is effectively like providing a new ORDER BY clause to the sql.
For example:
SELECT name,
directory,
file
length
FROM java.io.File
Can be (re)ordered via the following code:
Query q = new Query ();
q.parse (sql);
// Note: this call will cause the entire statement to be executed.
q.reorder (myFiles,
"name DESC, 3 ASC, length, 1 DESC");
Parameters: objs - The objects you wish to re-order. Parameters: orderBys - The order bys. | public void | setAllObjects(List objs) | public void | setClassLoader(ClassLoader cl) | public void | setClassName(String n) | public void | setColumns(List cols) | public void | setCurrentGroupByObjects(List objs) | public void | setCurrentObject(Object o) | public void | setExecuteOnFunctions(Map ex) | public void | setFrom(Expression exp) | public void | setFromObjectClass(Class c) Set the "FROM" object class. | public void | setGroupByColumns(List cols) | public void | setGroupByLimit(Limit g) | public void | setGroupByOrderColumns(List cols) | public void | setHaving(Expression be) Set the expression for the HAVING clause. | public void | setLimit(Limit l) Set the object that represents the limit clause. | public void | setOrderByColumns(List cols) | public void | setParent(Query q) Set the parent query. | public void | setSaveValue(Object id, Object value) | public void | setSaveValues(Map s) | public void | setVariable(String name, Object v) Set the value of a named bind variable. | public void | setVariable(int index, Object v) Set the value of an indexed bind variable. | public void | setVariables(Map bVars) Set the bind variables in one go. | public void | setWantDistinctResults(boolean v) Indicate whether "distinct" results are required. | public void | setWantObjects(boolean v) Set whether the query should return objects (use true ). | public void | setWantTimings(boolean v) | public void | setWhere(Expression be) Set the expression for the WHERE clause. | public void | setWildcardCharacter(char c) Set the character that represents a wildcard in LIKE searches. | public String | toString() Get a string version of this query suitable for debugging. |
ALL_OBJS_VAR_NAME | public static String ALL_OBJS_VAR_NAME(Code) | | |
CURR_OBJ_VAR_NAME | public static String CURR_OBJ_VAR_NAME(Code) | | |
GROUP_BY_RESULTS | final public static String GROUP_BY_RESULTS(Code) | | |
GRPBY_OBJ_VAR_NAME | public static String GRPBY_OBJ_VAR_NAME(Code) | | |
GRPBY_OBJ_VAR_NAME_SYNONYM | public static String GRPBY_OBJ_VAR_NAME_SYNONYM(Code) | | |
HAVING_RESULTS | final public static String HAVING_RESULTS(Code) | | |
INT_BIND_VAR_PREFIX | final public static String INT_BIND_VAR_PREFIX(Code) | | |
PARENT_BIND_VAR_NAME | public static String PARENT_BIND_VAR_NAME(Code) | | |
QUERY_BIND_VAR_NAME | public static String QUERY_BIND_VAR_NAME(Code) | | |
nullQueryList | final public static List nullQueryList(Code) | | |
Query | public Query()(Code) | | Create a new blank Query object.
|
addFunctionHandler | public void addFunctionHandler(Object o)(Code) | | |
addTiming | protected void addTiming(String id, double time)(Code) | | |
doExecuteOn | public void doExecuteOn(List l, String t) throws QueryExecutionException(Code) | | Execute all the expressions for the specified type, either:
Query.ALL or:
Query.RESULTS . If the expressions are aliased then the results will be
available in the save results upon completion.
Parameters: l - The List of objects to execute the functions on. Parameters: t - The type of expressions to execute. throws: QueryExecutionException - If there is an issue with executing one of theexpressions or if the Query hasn't been inited yet. |
getAllObjects | public List getAllObjects()(Code) | | Get the current list of objects in context (value of the :_allobjs special bind variable).
Note: the value of the :_allobjs bind variable will change depending upon where the query execution
is up to.
The list of objects in context. |
getAnonymousBindVariableName | public String getAnonymousBindVariableName()(Code) | | |
getCurrentObject | public Object getCurrentObject()(Code) | | Get the current object (value of the :_currobj special bind variable). Note: the value
of the :_currobj bind variable will change depending upon where the query execution is up to.
The current object in context. |
getDefaultFunctionHandlers | public List getDefaultFunctionHandlers()(Code) | | |
getExecuteOnFunctions | public Map getExecuteOnFunctions()(Code) | | |
getFromObjectClass | public Class getFromObjectClass()(Code) | | |
getFunctionHandlers | public List getFunctionHandlers()(Code) | | |
getGroupByColumns | public List getGroupByColumns()(Code) | | |
getGroupBySaveValue | public Object getGroupBySaveValue(Object id, List gbs)(Code) | | Get the save value for a particular key and group by list.
Parameters: id - The id of the save value. Parameters: gbs - The group by list key. The object the key maps to. |
getGroupBySaveValues | public Map getGroupBySaveValues(List gbs)(Code) | | Get the save values for the specified group bys.
Parameters: gbs - The group bys. The save values (name/value pairs). |
getGroupByVariable | public Object getGroupByVariable(int ind)(Code) | | Get the value of a group by variable from the current group bys.
Parameters: ind - The variable index. The value. |
getHavingClause | public Expression getHavingClause()(Code) | | Return the HAVING clause expression.
The HAVING clause as an expression. |
getOrderByColumns | public List getOrderByColumns()(Code) | | Get the "order bys". This will return a List of
OrderBy objects.
This is generally only useful when you want to
Query.reorder(List,String)
the search and wish to get access to the textual representation of the order bys.
It is therefore possible to modify the orderbys in place, perhaps by using a different
expression or changing the direction (since the objects are not cloned before being
returned). However do so at YOUR OWN RISK. If you do so, then ensure you
call:
Query.setOrderByColumns(List) , then:
Query.initOrderByComparator() before re-executing the statement, otherwise nothing will happen!
The order bys. |
getOrderByComparator | public Comparator getOrderByComparator()(Code) | | Return the
Comparator we will use to do the ordering of the results, may be null.
The Comparator. |
getParent | public Query getParent()(Code) | | Get the parent query.
The query, will be null if there is no parent. |
getQuery | public String getQuery()(Code) | | Get the query string that this Query object represents.
The query string. |
getSaveValue | public Object getSaveValue(Object id)(Code) | | Get the save values for a particular key.
The object the key maps to. |
getTopLevelQuery | public Query getTopLevelQuery()(Code) | | Get the top level query if "this" is a sub-query, the query chain is traversed until
the top level query is found, i.e. when
Query.getParent() returns null.
The top level query, will be null if there is no parent. |
getVariable | public Object getVariable(int index)(Code) | | Get the value of an indexed bind variable.
Parameters: index - The index. The value. |
getVariable | public Object getVariable(String name)(Code) | | Get the value of a named bind variable.
Parameters: name - The name of the bind variable. The value. |
getVariableClass | public Class getVariableClass(String name)(Code) | | Get the class that the named variable has.
Parameters: name - The name of the variable. The Class. |
getVariables | public Map getVariables()(Code) | | Get all the bind variables as a Map.
The name/value mappings of the bind variables. |
getWhereClause | public Expression getWhereClause()(Code) | | Return the WHERE clause expression.
The WHERE clause as an expression. |
getWildcardCharacter | public char getWildcardCharacter()(Code) | | Get the character that represents a wildcard in LIKE searches.
The char. |
initOrderByComparator | public void initOrderByComparator() throws QueryParseException(Code) | | Will cause the order by comparator used to order the results
to be initialized. This is generally only useful if you are specifying the
the order bys yourself via:
Query.setOrderByColumns(List) . Usage of
this method is NOT supported, so don't use unless you really know what
you are doing!
|
isWantObjects | public boolean isWantObjects()(Code) | | Return whether the query should return objects.
true if the query should return objects. |
isWhereTrue | public boolean isWhereTrue(Object o) throws QueryExecutionException(Code) | | A helper method that will evaluate the WHERE clause for the object passed in.
Parameters: o - The object to evaluate the WHERE clause against. The result of calling: Expression.isTrue(Object,Query) for the WHERE clause. |
parsed | public boolean parsed()(Code) | | Return whether this Query object has had a statement applied to it
and has been parsed.
Whether the query is associated with a statement. |
reorder | public QueryResults reorder(List objs, SortedMap dirs) throws QueryExecutionException, QueryParseException(Code) | | Re-order the objects according to the columns supplied in the dirs Map.
The Map should be keyed on an Integer and map to a String value, the String value should
be either:
Query.ORDER_BY_ASC for the column to be in ascending order or:
Query.ORDER_BY_DESC for the column to be in descending order. The Integer refers
to a column in the SELECT part of the statement.
For example:
SELECT name,
directory,
file
length
FROM java.io.File
Can be (re)ordered via the following code:
Query q = new Query ();
q.parse (sql);
Map reorderBys = new TreeMap ();
reorderBys.put (new Integer (2), Query.ORDER_BY_ASC);
reorderBys.put (new Integer (3), Query.ORDER_BY_DESC);
reorderBys.put (new Integer (1), Query.ORDER_BY_ASC);
reorderBys.put (new Integer (4), Query.ORDER_BY_DESC);
// Note: this call will cause the entire statement to be executed.
q.reorder (myFiles,
reorderBys);
Parameters: objs - The objects you wish to reorder. Parameters: dirs - The order bys. The QueryResults. throws: QueryParseException - If the statement can be parsed, i.e. if any of the order bycolumns is out of range. throws: QueryExecutionException - If the call to: Query.execute(List) fails. See Also: Query.reorder(List,String) |
reorder | public QueryResults reorder(List objs, String orderBys) throws QueryParseException, QueryExecutionException(Code) | | Allows the re-ordering of the results via a textual representation of the order bys.
This is effectively like providing a new ORDER BY clause to the sql.
For example:
SELECT name,
directory,
file
length
FROM java.io.File
Can be (re)ordered via the following code:
Query q = new Query ();
q.parse (sql);
// Note: this call will cause the entire statement to be executed.
q.reorder (myFiles,
"name DESC, 3 ASC, length, 1 DESC");
Parameters: objs - The objects you wish to re-order. Parameters: orderBys - The order bys. The execution results. throws: QueryParseException - If the statement can be parsed, i.e. if any of the order bycolumns is out of range or the order bys cannot be parsed. throws: QueryExecutionException - If the call to: Query.execute(List) fails. See Also: Query.reorder(List,SortedMap) |
setAllObjects | public void setAllObjects(List objs)(Code) | | |
setCurrentGroupByObjects | public void setCurrentGroupByObjects(List objs)(Code) | | |
setCurrentObject | public void setCurrentObject(Object o)(Code) | | |
setExecuteOnFunctions | public void setExecuteOnFunctions(Map ex)(Code) | | |
setFromObjectClass | public void setFromObjectClass(Class c)(Code) | | Set the "FROM" object class. It is advised that you NEVER call this method, do so
at your own risk, dragons will swoop from the sky and crisp your innards if you do so!!!
Seriously though ;), this method should ONLY be called by those who know what they
are doing, whatever you think you know about how this method operates is irrelevant
which is why the dangers of calling this method are not documented...
YOU HAVE BEEN WARNED!!! NO BUGS WILL BE ACCEPTED THAT ARISE FROM THE CALLING OF
THIS METHOD!!!
Parameters: c - The FROM class. |
setGroupByColumns | public void setGroupByColumns(List cols)(Code) | | |
setGroupByLimit | public void setGroupByLimit(Limit g)(Code) | | |
setGroupByOrderColumns | public void setGroupByOrderColumns(List cols)(Code) | | |
setHaving | public void setHaving(Expression be)(Code) | | Set the expression for the HAVING clause.
Caution: do NOT use this method unless you are sure about what you are doing!
Parameters: be - The expression. |
setLimit | public void setLimit(Limit l)(Code) | | Set the object that represents the limit clause.
Caution: Do NOT use unless you are sure about what you are doing!
Parameters: l - The object. |
setOrderByColumns | public void setOrderByColumns(List cols)(Code) | | |
setParent | public void setParent(Query q)(Code) | | Set the parent query.
Caution: Do NOT use unless you are sure about what you are doing!
Parameters: q - The parent query. |
setSaveValues | public void setSaveValues(Map s)(Code) | | |
setVariable | public void setVariable(String name, Object v)(Code) | | Set the value of a named bind variable.
Parameters: name - The name. Parameters: v - The value. |
setVariable | public void setVariable(int index, Object v)(Code) | | Set the value of an indexed bind variable.
Parameters: index - The index. Parameters: v - The value. |
setVariables | public void setVariables(Map bVars)(Code) | | Set the bind variables in one go.
Parameters: bVars - The bind variable name/value mappings. |
setWantDistinctResults | public void setWantDistinctResults(boolean v)(Code) | | Indicate whether "distinct" results are required.
Parameters: v - Set to true to make the results distinct. |
setWantObjects | public void setWantObjects(boolean v)(Code) | | Set whether the query should return objects (use true ).
Caution: Do NOT use unless you are sure about what you are doing!
Parameters: v - Set to true to indicate that the query should return objects. |
setWantTimings | public void setWantTimings(boolean v)(Code) | | |
setWhere | public void setWhere(Expression be)(Code) | | Set the expression for the WHERE clause.
Caution: do NOT use this method unless you are sure about what you are doing!
Parameters: be - The expression. |
setWildcardCharacter | public void setWildcardCharacter(char c)(Code) | | Set the character that represents a wildcard in LIKE searches.
Parameters: c - The char. |
toString | public String toString()(Code) | | Get a string version of this query suitable for debugging. This will reconstruct the query
based on the objects it holds that represent the various clauses.
The reconstructed query. |
|
|