|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.josql.Query
public class Query
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.
Field Summary | |
---|---|
static String |
ALL
|
static String |
ALL_OBJS_VAR_NAME
|
static String |
CURR_OBJ_VAR_NAME
|
static String |
GROUP_BY_RESULTS
|
static String |
GRPBY_OBJ_VAR_NAME
|
static String |
GRPBY_OBJ_VAR_NAME_SYNONYM
|
static String |
HAVING_RESULTS
|
static String |
INT_BIND_VAR_PREFIX
|
static List |
nullQueryList
|
static String |
ORDER_BY_ASC
|
static String |
ORDER_BY_DESC
|
static String |
PARENT_BIND_VAR_NAME
|
static String |
QUERY_BIND_VAR_NAME
|
static String |
RESULTS
|
static String |
WHERE_RESULTS
|
Constructor Summary | |
---|---|
Query()
Create a new blank Query object. |
Method Summary | |
---|---|
void |
addBindVariableChangedListener(BindVariableChangedListener bvl)
|
void |
addFunctionHandler(Object o)
|
void |
addSaveValueChangedListener(SaveValueChangedListener svl)
|
protected void |
addTiming(String id,
double time)
|
void |
doExecuteOn(List l,
String t)
Execute all the expressions for the specified type, either: ALL or:
RESULTS . |
QueryResults |
execute(Collection objs)
Execute this query on the specified objects. |
QueryResults |
execute(Iterator iter)
Execute this query on the specified objects provided by the iterator. |
QueryResults |
execute(List objs)
Execute this query on the specified objects. |
protected void |
fireBindVariableChangedEvent(String name,
Object from,
Object to)
|
protected void |
fireSaveValueChangedEvent(Object id,
Object from,
Object to)
|
Map |
getAliases()
|
List |
getAllObjects()
Get the current list of objects in context (value of the :_allobjs special bind variable). |
String |
getAnonymousBindVariableName()
|
ClassLoader |
getClassLoader()
|
List |
getColumns()
|
Object |
getCurrentObject()
Get the current object (value of the :_currobj special bind variable). |
List |
getDefaultFunctionHandlers()
|
Map |
getExecuteOnFunctions()
|
Expression |
getFrom()
|
Class |
getFromObjectClass()
|
FunctionHandler |
getFunctionHandler(String id)
|
List |
getFunctionHandlers()
|
List |
getGroupByColumns()
|
Object |
getGroupBySaveValue(Object id,
List gbs)
Get the save value for a particular key and group by list. |
Map |
getGroupBySaveValues(List gbs)
Get the save values for the specified group bys. |
Object |
getGroupByVariable(int ind)
Get the value of a group by variable from the current group bys. |
Expression |
getHavingClause()
Return the HAVING clause expression. |
Limit |
getLimit()
Get the object that represents the limit clause. |
List |
getOrderByColumns()
Get the "order bys". |
Comparator |
getOrderByComparator()
Return the Comparator we will use to do the ordering of the results, may be null. |
Query |
getParent()
Get the parent query. |
String |
getQuery()
Get the query string that this Query object represents. |
QueryResults |
getQueryResults()
Get the results of executing this query. |
Object |
getSaveValue(Object id)
Get the save values for a particular key. |
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. |
Object |
getVariable(int index)
Get the value of an indexed bind variable. |
Object |
getVariable(String name)
Get the value of a named bind variable. |
Class |
getVariableClass(String name)
Get the class that the named variable has. |
Map |
getVariables()
Get all the bind variables as a Map. |
Expression |
getWhereClause()
Return the WHERE clause expression. |
char |
getWildcardCharacter()
Get the character that represents a wildcard in LIKE searches. |
void |
init()
|
void |
initOrderByComparator()
Will cause the order by comparator used to order the results to be initialized. |
boolean |
isWantObjects()
Return whether the query should return objects. |
boolean |
isWhereTrue(Object o)
A helper method that will evaluate the WHERE clause for the object passed in. |
Class |
loadClass(String name)
|
void |
parse(String q)
Parse the JoSQL query. |
static QueryResults |
parseAndExec(String query,
List objs)
|
boolean |
parsed()
Return whether this Query object has had a statement applied to it and has been parsed. |
void |
removeBindVariableChangedListener(BindVariableChangedListener bvl)
|
void |
removeSaveValueChangedListener(SaveValueChangedListener svl)
|
QueryResults |
reorder(List objs,
SortedMap dirs)
Re-order the objects according to the columns supplied in the dirs Map. |
QueryResults |
reorder(List objs,
String orderBys)
Allows the re-ordering of the results via a textual representation of the order bys. |
void |
setAllObjects(List objs)
|
void |
setClassLoader(ClassLoader cl)
|
void |
setClassName(String n)
|
void |
setColumns(List cols)
|
void |
setCurrentGroupByObjects(List objs)
|
void |
setCurrentObject(Object o)
|
void |
setExecuteOnFunctions(Map ex)
|
void |
setFrom(Expression exp)
|
void |
setFromObjectClass(Class c)
Set the "FROM" object class. |
void |
setGroupByColumns(List cols)
|
void |
setGroupByLimit(Limit g)
|
void |
setGroupByOrderColumns(List cols)
|
void |
setHaving(Expression be)
Set the expression for the HAVING clause. |
void |
setLimit(Limit l)
Set the object that represents the limit clause. |
void |
setOrderByColumns(List cols)
|
void |
setParent(Query q)
Set the parent query. |
void |
setSaveValue(Object id,
Object value)
|
void |
setSaveValues(Map s)
|
void |
setVariable(int index,
Object v)
Set the value of an indexed bind variable. |
void |
setVariable(String name,
Object v)
Set the value of a named bind variable. |
void |
setVariables(Map bVars)
Set the bind variables in one go. |
void |
setWantDistinctResults(boolean v)
Indicate whether "distinct" results are required. |
void |
setWantObjects(boolean v)
Set whether the query should return objects (use true ). |
void |
setWantTimings(boolean v)
|
void |
setWhere(Expression be)
Set the expression for the WHERE clause. |
void |
setWildcardCharacter(char c)
Set the character that represents a wildcard in LIKE searches. |
String |
toString()
Get a string version of this query suitable for debugging. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static String QUERY_BIND_VAR_NAME
public static String PARENT_BIND_VAR_NAME
public static String CURR_OBJ_VAR_NAME
public static String ALL_OBJS_VAR_NAME
public static String GRPBY_OBJ_VAR_NAME
public static String GRPBY_OBJ_VAR_NAME_SYNONYM
public static final String INT_BIND_VAR_PREFIX
public static final String ALL
public static final String RESULTS
public static final String GROUP_BY_RESULTS
public static final String WHERE_RESULTS
public static final String HAVING_RESULTS
public static final String ORDER_BY_ASC
public static final String ORDER_BY_DESC
public static final List nullQueryList
Constructor Detail |
---|
public Query()
Method Detail |
---|
public Expression getWhereClause()
public Expression getHavingClause()
public Comparator getOrderByComparator()
Comparator
we will use to do the ordering of the results, may be null.
public FunctionHandler getFunctionHandler(String id)
public Map getExecuteOnFunctions()
public void setExecuteOnFunctions(Map ex)
public String getAnonymousBindVariableName()
public List getDefaultFunctionHandlers()
public List getFunctionHandlers()
public void addFunctionHandler(Object o)
public void setFrom(Expression exp)
public Expression getFrom()
public void setClassName(String n)
public void setOrderByColumns(List cols)
public void setGroupByLimit(Limit g)
public void setGroupByOrderColumns(List cols)
public List getGroupByColumns()
public void setGroupByColumns(List cols)
public List getColumns()
public void setColumns(List cols)
public void setHaving(Expression be)
be
- The expression.public void setWhere(Expression be)
be
- The expression.public void setWantTimings(boolean v)
protected void addTiming(String id, double time)
public Object getVariable(int index)
index
- The index.
public Class getVariableClass(String name)
name
- The name of the variable.
public Object getGroupByVariable(int ind)
ind
- The variable index.
public Object getVariable(String name)
name
- The name of the bind variable.
public void setVariable(String name, Object v)
name
- The name.v
- The value.public void setVariable(int index, Object v)
index
- The index.v
- The value.public Map getVariables()
public boolean isWhereTrue(Object o) throws QueryExecutionException
o
- The object to evaluate the WHERE clause against.
QueryExecutionException
public void setVariables(Map bVars)
bVars
- The bind variable name/value mappings.public void doExecuteOn(List l, String t) throws QueryExecutionException
ALL
or:
RESULTS
. If the expressions are aliased then the results will be
available in the save results upon completion.
l
- The List of objects to execute the functions on.t
- The type of expressions to execute.
QueryExecutionException
- If there is an issue with executing one of the
expressions or if the Query hasn't been inited yet.public QueryResults execute(Iterator iter) throws QueryExecutionException
execute(List)
method for execution.
iter
- The iterator to use to get the objects.
QueryExecutionException
- If the query cannot be executed.public QueryResults execute(Collection objs) throws QueryExecutionException
execute(List)
method for execution.
objs
- The collection of objects to execute the query on.
QueryExecutionException
- If the query cannot be executed.public QueryResults execute(List objs) throws QueryExecutionException
objs
- The list of objects to execute the query on.
QueryExecutionException
- If the query cannot be executed.public void setCurrentGroupByObjects(List objs)
public List getAllObjects()
public void setAllObjects(List objs)
public void setCurrentObject(Object o)
public Object getCurrentObject()
public void setSaveValues(Map s)
public void setSaveValue(Object id, Object value)
protected void fireSaveValueChangedEvent(Object id, Object from, Object to)
protected void fireBindVariableChangedEvent(String name, Object from, Object to)
public Object getGroupBySaveValue(Object id, List gbs)
id
- The id of the save value.gbs
- The group by list key.
public Map getGroupBySaveValues(List gbs)
gbs
- The group bys.
public Object getSaveValue(Object id)
public String getQuery()
public void initOrderByComparator() throws QueryParseException
setOrderByColumns(List)
. Usage of
this method is NOT supported, so don't use unless you really know what
you are doing!
QueryParseException
public QueryResults reorder(List objs, SortedMap dirs) throws QueryExecutionException, QueryParseException
ORDER_BY_ASC
for the column to be in ascending order or:
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.FileCan 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);
objs
- The objects you wish to reorder.dirs
- The order bys.
QueryParseException
- If the statement can be parsed, i.e. if any of the order by
columns is out of range.
QueryExecutionException
- If the call to: execute(List)
fails.reorder(List,String)
public QueryResults reorder(List objs, String orderBys) throws QueryParseException, QueryExecutionException
For example:
SELECT name, directory, file length FROM java.io.FileCan 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");
objs
- The objects you wish to re-order.orderBys
- The order bys.
QueryParseException
- If the statement can be parsed, i.e. if any of the order by
columns is out of range or the order bys cannot be parsed.
QueryExecutionException
- If the call to: execute(List)
fails.reorder(List,SortedMap)
public void setClassLoader(ClassLoader cl)
public ClassLoader getClassLoader()
public Class loadClass(String name) throws Exception
Exception
public void parse(String q) throws QueryParseException
q
- The query string.
QueryParseException
- If the query cannot be parsed and/or inited
.public void init() throws QueryParseException
QueryParseException
public void setFromObjectClass(Class c)
YOU HAVE BEEN WARNED!!! NO BUGS WILL BE ACCEPTED THAT ARISE FROM THE CALLING OF THIS METHOD!!!
c
- The FROM class.public Class getFromObjectClass()
public void removeBindVariableChangedListener(BindVariableChangedListener bvl)
public void addBindVariableChangedListener(BindVariableChangedListener bvl)
public void removeSaveValueChangedListener(SaveValueChangedListener svl)
public void addSaveValueChangedListener(SaveValueChangedListener svl)
public Map getAliases()
public boolean isWantObjects()
true
if the query should return objects.public void setWantObjects(boolean v)
true
).
Caution: Do NOT use unless you are sure about what you are doing!
v
- Set to true
to indicate that the query should return objects.public char getWildcardCharacter()
public void setWildcardCharacter(char c)
c
- The char.public void setLimit(Limit l)
l
- The object.public Limit getLimit()
public boolean parsed()
public void setWantDistinctResults(boolean v)
v
- Set to true
to make the results distinct.public QueryResults getQueryResults()
executing
this query.
public List getOrderByColumns()
OrderBy
objects.
This is generally only useful when you want to 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: setOrderByColumns(List)
, then: initOrderByComparator()
before re-executing the statement, otherwise nothing will happen!
public void setParent(Query q)
q
- The parent query.public Query getParent()
null
if there is no parent.public Query getTopLevelQuery()
getParent()
returns null.
null
if there is no parent.public String toString()
toString
in class Object
public static QueryResults parseAndExec(String query, List objs) throws QueryParseException, QueryExecutionException
QueryParseException
QueryExecutionException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |