|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.mmbase.bridge.util.Queries
public abstract class Queries
This class contains various utility methods for manipulating and creating query objects. Most essential methods are available on the Query object itself, but too specific or legacy-ish methods are put here.
Query| Field Summary | |
|---|---|
static int |
OPERATOR_BETWEEN
|
static int |
OPERATOR_IN
|
static int |
OPERATOR_NULL
|
| Constructor Summary | |
|---|---|
Queries()
|
|
| Method Summary | |
|---|---|
static Constraint |
addConstraint(Query query,
Constraint newConstraint)
Adds a Constraint to the already present constraint (with AND). |
static Constraint |
addConstraints(Query query,
String constraints)
Adds a 'legacy' constraint to the query, i.e. |
static List<StepField> |
addFields(Query query,
String fields)
Adds a number of fields. |
static List<Step> |
addPath(Query query,
String path,
String searchDirs)
Adds path of steps to an existing query. |
static NodeQuery |
addRelationFields(NodeQuery q,
String role,
String relationFields,
String sortOrders)
|
static Query |
addSortedFields(Query q)
Make sure all sorted fields are queried |
static List<SortOrder> |
addSortOrders(Query query,
String sorted,
String directions)
Adds sort orders to the query, using two strings. |
static void |
addStartNodes(Query query,
String startNodes)
Add startNodes to the first step with the correct type to the given query. |
static NodeList |
addToResult(Query q,
Node n)
Explores a query object, and creates a certain new relation object, which would make the given node appear in the query's result. |
protected static Object |
aggregate(Query query,
StepField field,
int type)
|
static int |
applyConstraints(Query q,
Step step,
Node n)
|
protected static Node |
clusterNode(Relation relation,
String relationAlias,
Node node)
Given a 'relation' node |
static int |
compare(Node node1,
Node node2,
List<SortOrder> sortOrders)
Does a field-by-field compare of two Node objects, on the fields used to order the nodes. |
static int |
compare(Node node1,
Node node2,
SortOrder sortOrder)
Compare tho nodes, with a SortOrder. |
static int |
compare(Object value,
Object value2,
SortOrder sortOrder)
|
static Constraint |
copyConstraint(Constraint c,
Step sourceStep,
Query query,
Step step)
Takes a Constraint of a query, and takes al constraints on 'sourceStep' of it, and copies those Constraints to the given step of the receiving query. |
static void |
copySortOrders(List<SortOrder> sortOrders,
Step sourceStep,
Query query,
Step step)
Copies SortOrders to a given step of another query. |
static int |
count(Query query)
Takes the query, and does a count with the same constraints (so ignoring 'offset' and 'max') |
static Constraint |
createAgeConstraint(NodeQuery q,
int minAge,
int maxAge)
As createAgeConstraint(Query, Step, int int, int), but NodeQuery's have an exceptional
step, which can be taken as the default. |
static Constraint |
createAgeConstraint(Query query,
Step step,
int minAge,
int maxAge)
Create a constraint for the query which limits to results to nodes of a certain age, based on its number and the 'daymarkers' table. |
static Constraint |
createConstraint(Query query,
String fieldName,
int operator,
Object value)
Defaulting version of createConstraint(Query, String, int, Object, Object, boolean, int). |
static Constraint |
createConstraint(Query query,
String fieldName,
int operator,
Object value,
Object value2,
boolean caseSensitive)
Defaulting version of createConstraint(Query, String, int, Object, Object, boolean, int). |
static Constraint |
createConstraint(Query query,
String fieldName,
int operator,
Object originalValue,
Object value2,
boolean caseSensitive,
int datePart)
Creates a constraint smartly, depending on the type of the field, the value is cast to the right type, and the right type of constraint is created. |
static Constraint |
createMakeEmptyConstraint(Query q)
Creates a constraint that would make the result of the query q empty. |
static NodeQuery |
createNodeQuery(Node node)
Returns the NodeQuery returning the given Node. |
static Query |
createQuery(Cloud cloud,
String startNodes,
String nodePath,
String fields,
String constraints,
String orderby,
String directions,
String searchDir,
boolean distinct)
Creates a Query object using arguments for Cloud.getList(String, String, String, String, String, String, String, boolean)
(this function is of course implemented using this utility). |
static NodeQuery |
createRelatedNodesQuery(Node node,
NodeManager otherNodeManager,
String role,
String direction)
Returns a query to find the nodes related to the given node. |
static NodeQuery |
createRelationNodesQuery(Node node,
NodeManager otherNodeManager,
String role,
String direction)
Returns a query to find the relations nodes of the given node. |
static NodeQuery |
createRelationNodesQuery(Node node,
Node otherNode,
String role,
String direction)
Returns a query to find the relations nodes between two given nodes. |
static Query |
fixQuery(Query query,
Map<Integer,Integer> resolution)
Fixes the nodes of the steps of query. |
static Comparator<Node> |
getComparator(Query q)
Returns a Node comparator associated with the SortOrders of the given Query (See SearchQuery.getSortOrders()). |
protected static Object |
getCompareValue(int fieldType,
int operator,
Object value)
Used in implementation of createConstraint |
protected static Object |
getCompareValue(int fieldType,
int operator,
Object value,
int datePart,
Cloud cloud)
Used in implementation of createConstraint |
static List<FieldConstraint> |
getConstraints(Constraint constraint,
Step step)
|
static int |
getDateTimePart(String s)
Creates a part constant for use by createConstraint |
protected static int |
getDayMark(Cloud cloud,
int age)
|
static String |
getFieldAlias(StepField sf)
Returns the string which must be used for Node.getValue(java.lang.String) in the result set of the
query of the given StepField. |
protected static Number |
getNumberValue(String stringValue)
Used in implementation of createConstraint |
static int |
getOperator(String s)
Creates a operator constant for use by createConstraint |
static NodeList |
getRelatedNodes(Node node,
NodeManager otherNodeManager,
String role,
String direction,
String relationFields,
String sortOrders)
Queries a list of cluster nodes, using a NodeQuery (so al fields of
one step are available), plus some fields of the relation step. |
static List<Node> |
getRelatedNodesInTransaction(Node startNode,
NodeQuery q)
Returns the related nodes of a certain node (defined by the query), including the one that where related to it in the current transaction. |
static NodeList |
getRelations(Query q,
Node n)
Explores a query object, returns the relations the node has within the query. |
static int |
getRelationStepDirection(String search)
Translates a string to a search direction constant. |
static int |
getSortOrder(String dir)
Converts a String to a SortOrder constant |
static Object |
getSortOrderFieldValue(Node node,
SortOrder sortOrder)
Obtains a value for the field of a sortorder from a given node. |
static Node |
getStartNode(NodeQuery nq,
Cloud cloud)
This method is the counterpart of setStartNode(org.mmbase.bridge.NodeQuery, org.mmbase.bridge.Node) and receives 'the' startnode from the Query (which may not be committed). |
static NodeQuery |
getSubQuery(Query q,
Node node,
int step)
Defaulting version getSubQuery(Query, Node, int, int). |
static NodeQuery |
getSubQuery(Query q,
Node node,
int elementStep,
int lastStep)
Given a Query, and Node, produces a new query, where the first part of the query is replaced by the Node. |
static Object |
max(Query query,
StepField field)
|
static Object |
min(Query query,
StepField field)
|
static boolean |
removeConstraint(Query q,
Constraint cons)
|
static String |
removeDigits(String complete)
Returns substring of given string without the leading digits (used in 'paths') |
static NodeList |
removeFromResult(Query q,
Node n)
Deletes the relations with a node from a queries resulting relations list. |
static int |
reorderResult(NodeQuery q,
List<Integer> desiredOrder)
Will 'reorder' the result of a query with a sort order. |
static Step |
searchStep(List<Step> steps,
String stepAlias)
Searches a list of Steps for a step with a certain name. |
static void |
setStartNode(NodeQuery query,
Node startNode)
This puts the node as 'startnode' in the query (propably a 'related nodes' query. |
static Query |
sortUniquely(Query q)
Add a sortorder (DESCENDING) on al the'number' fields of the query, on which there is not yet a sortorder. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int OPERATOR_BETWEEN
public static final int OPERATOR_IN
public static final int OPERATOR_NULL
| Constructor Detail |
|---|
public Queries()
| Method Detail |
|---|
public static int getRelationStepDirection(String search)
null then
'BOTH' is returned.
search - string representation of the searchdir constant
RelationStepThe same function, only with another return value if String is null
public static Query createQuery(Cloud cloud,
String startNodes,
String nodePath,
String fields,
String constraints,
String orderby,
String directions,
String searchDir,
boolean distinct)
Cloud.getList(String, String, String, String, String, String, String, boolean)
(this function is of course implemented using this utility). This is useful to convert (legacy) code which uses
getList, but you want to use new Query features without rewriting the complete thing.
It can also be simply handy to specify things as Strings.
cloud - startNodes - nodePath - fields - constraints - orderby - directions - searchDir - distinct -
public static Constraint addConstraints(Query query,
String constraints)
query - query to add constraint toconstraints - string representation of constraints
public static Constraint addConstraint(Query query,
Constraint newConstraint)
query - query to add the constraint tonewConstraint - constraint to add
public static List<FieldConstraint> getConstraints(Constraint constraint,
Step step)
public static boolean removeConstraint(Query q,
Constraint cons)
public static int getOperator(String s)
s - String representation of operator
createConstraint(Query, String, int, Object),
createConstraint(Query, String, int, Object, Object, boolean)public static int getDateTimePart(String s)
s - String representation of a datetime part
createConstraint(Query, String, int, Object, Object, boolean, int)
protected static Number getNumberValue(String stringValue)
throws BridgeException
stringValue - string representation of a number
BridgeException - when failed to convert the string
protected static Object getCompareValue(int fieldType,
int operator,
Object value)
fieldType - Field Type constant (@link Field)operator - Compare operatorvalue - value to convert
protected static Object getCompareValue(int fieldType,
int operator,
Object value,
int datePart,
Cloud cloud)
fieldType - Field Type constant (@link Field)operator - Compare operatorvalue - value to convertcloud - The cloud may be used to pass locale sensitive properties which may be needed for comparisons (locales, timezones)
public static Constraint createConstraint(Query query,
String fieldName,
int operator,
Object value)
createConstraint(Query, String, int, Object, Object, boolean, int).
Casesensitivity defaults to false, value2 to null (so 'BETWEEN' cannot be used), datePart set to -1 (so no date part comparison)
query - The query to create the constraint forfieldName - The field to create the constraint on (as a string, so it can include the step), e.g. 'news.number'operator - The operator to use. This constant can be produces from a string using getOperator(String).value - The value to compare with, which must be of the right type. If field is number it might also be an alias.
null it by chance the specified arguments did not lead to a new actual constraint (e.g. if value is an empty set)
public static Constraint createConstraint(Query query,
String fieldName,
int operator,
Object value,
Object value2,
boolean caseSensitive)
createConstraint(Query, String, int, Object, Object, boolean, int).
DatePart set to -1 (so no date part comparison)
query - The query to create the constraint forfieldName - The field to create the constraint on (as a string, so it can include the step), e.g. 'news.number'operator - The operator to use. This constant can be produces from a string using getOperator(String).value - The value to compare with, which must be of the right type. If field is number it might also be an alias.value2 - The other value (only relevant if operator is BETWEEN, the only terniary operator)caseSensitive - Whether it should happen case sensitively (not relevant for number fields)
null it by chance the specified arguments did not lead to a new actual constraint (e.g. if value is an empty set)
public static Constraint createConstraint(Query query,
String fieldName,
int operator,
Object originalValue,
Object value2,
boolean caseSensitive,
int datePart)
query - The query to create the constraint forfieldName - The field to create the constraint on (as a string, so it can include the step), e.g. 'news.number'operator - The operator to use. This constant can be produces from a string using getOperator(String).originalValue - The value to compare with, which must be of the right type. If field is number it might also be an alias.value2 - The other value (only relevant if operator is BETWEEN, the only terniary operator)caseSensitive - Whether it should happen case sensitively (not relevant for number fields)datePart - The part of a DATETIME value that is to be checked
null it by chance the specified arguments did not lead to a new actual constraint (e.g. if value is an empty set)
public static Constraint copyConstraint(Constraint c,
Step sourceStep,
Query query,
Step step)
c - The constrain to be copied (for example the result of sourceQuery.getConstraint()).sourceStep - The step in the 'source' query.query - The receiving querystep - The step of the receiving query which must 'receive' the sort orders.
IllegalArgumentException - If the given constraint is not compatible with the given step.
UnsupportedOperationException - If CompareFieldsConstraints or LegacyConstraints are encountered.Functions are similar
public static void copySortOrders(List<SortOrder> sortOrders,
Step sourceStep,
Query query,
Step step)
sortOrders - A list of SortOrders (for example the result of sourceQuery.getSortOrders()).sourceStep - The step in the 'source' query.query - The receiving querystep - The step of the receiving query which must 'receive' the sort orders.public static int getSortOrder(String dir)
dir - string representation of direction of sortorder
public static List<SortOrder> addSortOrders(Query query,
String sorted,
String directions)
query - query to add the sortorders tosorted - string with comma-separated fieldsdirections - string with comma-separated directions
public static String removeDigits(String complete)
complete - string with leading digits
public static List<Step> addPath(Query query,
String path,
String searchDirs)
query - extend this querypath - create steps from this pathsearchDirs - add steps with these relation directions
public static List<StepField> addFields(Query query,
String fields)
query - The query where the fields should be added tofields - a comma separated string of fields
public static void addStartNodes(Query query,
String startNodes)
query - query to add the startnodesstartNodes - start nodes(this is essentially a 'bridge' version of the startnodes part)public static int count(Query query)
query - query as base for the count
protected static Object aggregate(Query query,
StepField field,
int type)
public static Object min(Query query,
StepField field)
public static Object max(Query query,
StepField field)
public static Step searchStep(List<Step> steps,
String stepAlias)
steps - steps to search throughstepAlias - alias to search for
ClassCastException - if list does not contain only Stepspublic static NodeQuery createNodeQuery(Node node)
node - Node to create the query from
public static NodeQuery createRelatedNodesQuery(Node node,
NodeManager otherNodeManager,
String role,
String direction)
node - start nodeotherNodeManager - node manager on the other side of the relationrole - role of the relationdirection - direction of the relation
public static NodeQuery createRelationNodesQuery(Node node,
NodeManager otherNodeManager,
String role,
String direction)
node - start nodeotherNodeManager - node manager on the other side of the relationrole - role of the relationdirection - direction of the relation
public static NodeQuery createRelationNodesQuery(Node node,
Node otherNode,
String role,
String direction)
if (Queries.count(Queries.createRelationNodesQuery(node1, node2, "posrel", null)) > 0) {
..
}
node - start nodeotherNode - node on the other side of the relationrole - role of the relationdirection - direction of the relation
public static NodeList getRelatedNodes(Node node,
NodeManager otherNodeManager,
String role,
String direction,
String relationFields,
String sortOrders)
NodeQuery (so al fields of
one step are available), plus some fields of the relation step. The actual node can be got
from the node cache by doing a Node.getNodeValue(java.lang.String) with the NodeList.NODESTEP_PROPERTY property. The fields of the relation can be got by
prefixing their names by the role and a dot (as normal in multilevel results).
node - start nodeotherNodeManager - node manager on the other side of the relationrole - role of the relationdirection - direction of the relationrelationFields - Comma separated string of fields which must be queried from the relation stepsortOrders - Comma separated string of fields of sortorders, or the empty string or null
So, this methods is targeted at the use of 'posrel' and similar fields, because sorting on other fields isn't possible right now.
public static NodeQuery addRelationFields(NodeQuery q,
String role,
String relationFields,
String sortOrders)
public static Query sortUniquely(Query q)
q - query to change
public static Query addSortedFields(Query q)
public static Object getSortOrderFieldValue(Node node,
SortOrder sortOrder)
public static int compare(Node node1,
Node node2,
SortOrder sortOrder)
compare(Node, Node, List)
If node2 is only 'longer' then node1, but otherwise equal, then it is bigger.
public static int compare(Object value,
Object value2,
SortOrder sortOrder)
public static int compare(Node node1,
Node node2,
List<SortOrder> sortOrders)
public static Comparator<Node> getComparator(Query q)
SortOrders of the given Query (See SearchQuery.getSortOrders()).
public static NodeList addToResult(Query q,
Node n)
UnsupportedOperationException - If it cannot be determined how the node should be related.
NullPointerException - if q or n is null
public static int applyConstraints(Query q,
Step step,
Node n)
public static NodeList removeFromResult(Query q,
Node n)
q - query from which resulting list the node should be removed fromn - node to remove
UnsupportedOperationException - If it cannot be determined how the node should be related.
public static NodeList getRelations(Query q,
Node n)
q - query that constructs the listn - node to which relations are related
UnsupportedOperationException - If it cannot be determined how the node is related.
NullPointerException - if q or n is nullpublic static String getFieldAlias(StepField sf)
Node.getValue(java.lang.String) in the result set of the
query of the given StepField.
protected static int getDayMark(Cloud cloud,
int age)
public static Constraint createAgeConstraint(NodeQuery q,
int minAge,
int maxAge)
createAgeConstraint(Query, Step, int int, int), but NodeQuery's have an exceptional
step, which can be taken as the default.
public static Constraint createAgeConstraint(Query query,
Step step,
int minAge,
int maxAge)
minAge - Minimal age in days (or -1 if it does not matter)maxAge - Maximal age in days (or -1 if it does not matter)
nullpublic static Constraint createMakeEmptyConstraint(Query q)
public static Query fixQuery(Query query,
Map<Integer,Integer> resolution)
public static int reorderResult(NodeQuery q,
List<Integer> desiredOrder)
NodeQuery q = Queries.createRelatedNodesQuery(node, cloud.getNodeManager("news"), "posrel", "destination");
Queries.addRelationFields(q, "posrel", "pos", "UP");
Queries.reorderResult(q, nodeNumbers);
If the values of 'pos' are equal to start with, they will be fixed too, and will have an increasing order.
If all values are different already, values will simply be interchanged.
A test-case for this is in QueriesTest#reorderResult.
q - The query which defines the existing order. The cloud of this object will be used, unless this is a committed transaction, then the parent Cloud.getNonTransactionalCloud() will be used to create the sub-transction.desiredOrder - The node numbers of the nodes in the query result of q. These are the actual nodes, not the nodes which define the order (like the posrel)
0 if the list was correctly ordered allready.
protected static Node clusterNode(Relation relation,
String relationAlias,
Node node)
public static void setStartNode(NodeQuery query,
Node startNode)
Query.addNode(org.mmbase.storage.search.Step, org.mmbase.bridge.Node) method.
The information will be put in the query in another way then, so that at least getStartNode(org.mmbase.bridge.NodeQuery, org.mmbase.bridge.Cloud) will give the correct result.
If the Query object is changed such that it can contain uncommitted nodes, then this method can be made deprecated.
public static Node getStartNode(NodeQuery nq,
Cloud cloud)
setStartNode(org.mmbase.bridge.NodeQuery, org.mmbase.bridge.Node) and receives 'the' startnode from the Query (which may not be committed).
public static List<Node> getRelatedNodesInTransaction(Node startNode,
NodeQuery q)
public static NodeQuery getSubQuery(Query q,
Node node,
int step)
getSubQuery(Query, Node, int, int). The last argument is replaced with q.getSteps().size();
public static NodeQuery getSubQuery(Query q,
Node node,
int elementStep,
int lastStep)
q - The query to base the new querynode - The node to start the query with. If this is null then step must be 0, and the original query will be
returned, only converted to a NodeQuery for the first step.elementStep - The element step. The first non-relation step after the first step where the node is fixed.lastStep - On default the remainder of the query is copied. It you need to truncate it from the back to, use this argument.
ClassCastException - if step + 1 is not a relationstep. (This restriction may perhaps be removed).
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||