| java.lang.Object org.mmbase.bridge.util.Queries
Queries | abstract public class Queries (Code) | | 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.
author: Michiel Meeuwissen version: $Id: Queries.java,v 1.96 2008/02/29 11:00:05 michiel Exp $ See Also: org.mmbase.bridge.Query since: MMBase-1.7 |
Method Summary | |
public static Constraint | addConstraint(Query query, Constraint newConstraint) Adds a Constraint to the already present constraint (with AND). | public static Constraint | addConstraints(Query query, String constraints) Adds a 'legacy' constraint to the query, i.e. | public static List<StepField> | addFields(Query query, String fields) Adds a number of fields. | public static List<Step> | addPath(Query query, String path, String searchDirs) Adds path of steps to an existing query. | public static NodeQuery | addRelationFields(NodeQuery q, String role, String relationFields, String sortOrders) | public static List<SortOrder> | addSortOrders(Query query, String sorted, String directions) Adds sort orders to the query, using two strings. | public static Query | addSortedFields(Query q) | public static void | addStartNodes(Query query, String startNodes) Add startNodes to the first step with the correct type to the given query. | protected static Object | aggregate(Query query, StepField field, int type) | public static int | compare(Node node1, Node node2, SortOrder sortOrder) Compare tho nodes, with a SortOrder. | public static int | compare(Object value, Object value2, SortOrder sortOrder) | public 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. | public 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.
Constraints on different steps then the given 'sourceStep' are ignored. | public static void | copySortOrders(List<SortOrder> sortOrders, Step sourceStep, Query query, Step step) Copies SortOrders to a given step of another query. | public static int | count(Query query) | public static Constraint | createConstraint(Query query, String fieldName, int operator, Object value) Defaulting version of
Queries.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)
Parameters: query - The query to create the constraint for Parameters: fieldName - The field to create the constraint on (as a string, so it can include the step), e.g. | public static Constraint | createConstraint(Query query, String fieldName, int operator, Object value, Object value2, boolean caseSensitive) Defaulting version of
Queries.createConstraint(Query,String,int,Object,Object,boolean,int) .
DatePart set to -1 (so no date part comparison)
Parameters: query - The query to create the constraint for Parameters: fieldName - The field to create the constraint on (as a string, so it can include the step), e.g. | public 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.
This is used in taglib implementation, but could be useful more generally.
Parameters: query - The query to create the constraint for Parameters: fieldName - The field to create the constraint on (as a string, so it can include the step), e.g. | public static NodeQuery | createNodeQuery(Node node) Returns the NodeQuery returning the given Node. | public 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(StringStringStringStringStringStringStringboolean) (this function is of course implemented using this utility). | public static NodeQuery | createRelatedNodesQuery(Node node, NodeManager otherNodeManager, String role, String direction) Returns a query to find the nodes related to the given node. | public static NodeQuery | createRelationNodesQuery(Node node, NodeManager otherNodeManager, String role, String direction) Returns a query to find the relations nodes of the given node. | public static NodeQuery | createRelationNodesQuery(Node node, Node otherNode, String role, String direction) Returns a query to find the relations nodes between two given nodes. | protected static Object | getCompareValue(int fieldType, int operator, Object value) | protected static Object | getCompareValue(int fieldType, int operator, Object value, int datePart, Cloud cloud) | public static int | getDateTimePart(String s) | protected static Number | getNumberValue(String stringValue) | public static int | getOperator(String s) | public static NodeList | getRelatedNodes(Node node, NodeManager otherNodeManager, String role, String direction, String relationFields, String sortOrders) Queries a list of cluster nodes, using a
org.mmbase.bridge.NodeQuery (so al fields of
one step are available), plus some fields of the relation step. | public static int | getRelationStepDirection(String search) Translates a string to a search direction constant. | public static int | getSortOrder(String dir) | public static Object | getSortOrderFieldValue(Node node, SortOrder sortOrder) Obtains a value for the field of a sortorder from a given node. | public static void | main(String[] argv) | public static Object | max(Query query, StepField field) | public static Object | min(Query query, StepField field) | public static String | removeDigits(String complete) | public static Step | searchStep(List<Step> steps, String stepAlias) Searches a list of Steps for a step with a certain name. | public 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. |
OPERATOR_BETWEEN | final public static int OPERATOR_BETWEEN(Code) | | |
OPERATOR_IN | final public static int OPERATOR_IN(Code) | | |
OPERATOR_NULL | final public static int OPERATOR_NULL(Code) | | |
addConstraint | public static Constraint addConstraint(Query query, Constraint newConstraint)(Code) | | Adds a Constraint to the already present constraint (with AND).
Parameters: query - query to add the constraint to Parameters: newConstraint - constraint to add The new constraint. |
addConstraints | public static Constraint addConstraints(Query query, String constraints)(Code) | | Adds a 'legacy' constraint to the query, i.e. constraint(s) represented
by a string. Alreading existing constraints remain ('AND' is used).
Parameters: query - query to add constraint to Parameters: constraints - string representation of constraints The new constraint, or null if nothing changed added. |
addFields | public static List<StepField> addFields(Query query, String fields)(Code) | | Adds a number of fields. Fields is represented as a comma separated string.
Parameters: query - The query where the fields should be added to Parameters: fields - a comma separated string of fields The new stepfields |
addPath | public static List<Step> addPath(Query query, String path, String searchDirs)(Code) | | Adds path of steps to an existing query. The query may contain steps already. Per step also
the 'search direction' may be specified.
Parameters: query - extend this query Parameters: path - create steps from this path Parameters: searchDirs - add steps with these relation directions The new steps. |
addSortOrders | public static List<SortOrder> addSortOrders(Query query, String sorted, String directions)(Code) | | Adds sort orders to the query, using two strings. Like in 'getList' of Cloud. Several tag-attributes need this.
Parameters: query - query to add the sortorders to Parameters: sorted - string with comma-separated fields Parameters: directions - string with comma-separated directions The new sort orders |
addSortedFields | public static Query addSortedFields(Query q)(Code) | | Make sure all sorted fields are queried
since: MMBase-1.8 |
addStartNodes | public static void addStartNodes(Query query, String startNodes)(Code) | | Add startNodes to the first step with the correct type to the given query. The nodes are identified
by a String, which could be prefixed with a step-alias, if you want to add the nodes to
another then this found step.
Furthermore may the nodes by identified by their alias, if they have one.
Parameters: query - query to add the startnodes Parameters: startNodes - start nodes See Also: org.mmbase.module.core.ClusterBuilder.getMultiLevelSearchQuery(ListListStringListStringListListint) See Also: (this is essentially a 'bridge' version of the startnodes part) |
compare | public static int compare(Node node1, Node node2, SortOrder sortOrder)(Code) | | Compare tho nodes, with a SortOrder. This determins where a certain node is smaller or bigger than a certain other node, with respect to some SortOrder.
This is used by
Queries.compare(Node,Node,List) If node2 is only 'longer' then node1, but otherwise equal, then it is bigger.
since: MMBase-1.8 |
compare | public static int compare(Node node1, Node node2, List<SortOrder> sortOrders)(Code) | | Does a field-by-field compare of two Node objects, on the fields used to order the nodes.
This is used to determine whether a node comes after or before another, in a certain query result.
-1 if node1 is smaller than node 2, 0 if both nodes are equals, and +1 is node 1 is greater than node 2. since: MMBase-1.8 |
copyConstraint | public static Constraint copyConstraint(Constraint c, Step sourceStep, Query query, Step step)(Code) | | 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.
Constraints on different steps then the given 'sourceStep' are ignored. CompositeConstraints
cause recursion and would work too (but same limitation are valid for the childs).
Parameters: c - The constrain to be copied (for example the result of sourceQuery.getConstraint()). Parameters: sourceStep - The step in the 'source' query. Parameters: query - The receiving query Parameters: step - The step of the receiving query which must 'receive' the sort orders. since: MMBase-1.7.1 See Also: org.mmbase.storage.search.implementation.BasicSearchQuery.copyConstraint See Also: Functions are similar throws: IllegalArgumentException - If the given constraint is not compatible with the given step. throws: UnsupportedOperationException - If CompareFieldsConstraints or LegacyConstraints are encountered. The new constraint or null |
copySortOrders | public static void copySortOrders(List<SortOrder> sortOrders, Step sourceStep, Query query, Step step)(Code) | | Copies SortOrders to a given step of another query. SortOrders which do not sort the given
'sourceStep' are ignored.
Parameters: sortOrders - A list of SortOrders (for example the result of sourceQuery.getSortOrders()). Parameters: sourceStep - The step in the 'source' query. Parameters: query - The receiving query Parameters: step - The step of the receiving query which must 'receive' the sort orders. since: MMBase-1.7.1 |
count | public static int count(Query query)(Code) | | Takes the query, and does a count with the same constraints (so ignoring 'offset' and 'max')
Parameters: query - query as base for the count number of results |
createConstraint | public static Constraint createConstraint(Query query, String fieldName, int operator, Object value)(Code) | | Defaulting version of
Queries.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)
Parameters: query - The query to create the constraint for Parameters: fieldName - The field to create the constraint on (as a string, so it can include the step), e.g. 'news.number' Parameters: operator - The operator to use. This constant can be produces from a string using Queries.getOperator(String). Parameters: value - The value to compare with, which must be of the right type. If field is number it might also be an alias. The new constraint, or null it by chance the specified arguments did not lead to a new actual constraint (e.g. if value is an empty set) |
createConstraint | public static Constraint createConstraint(Query query, String fieldName, int operator, Object value, Object value2, boolean caseSensitive)(Code) | | Defaulting version of
Queries.createConstraint(Query,String,int,Object,Object,boolean,int) .
DatePart set to -1 (so no date part comparison)
Parameters: query - The query to create the constraint for Parameters: fieldName - The field to create the constraint on (as a string, so it can include the step), e.g. 'news.number' Parameters: operator - The operator to use. This constant can be produces from a string using Queries.getOperator(String). Parameters: value - The value to compare with, which must be of the right type. If field is number it might also be an alias. Parameters: value2 - The other value (only relevant if operator is BETWEEN, the only terniary operator) Parameters: caseSensitive - Whether it should happen case sensitively (not relevant for number fields) The new constraint, or null it by chance the specified arguments did not lead to a new actual constraint (e.g. if value is an empty set) |
createConstraint | public static Constraint createConstraint(Query query, String fieldName, int operator, Object originalValue, Object value2, boolean caseSensitive, int datePart)(Code) | | 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.
This is used in taglib implementation, but could be useful more generally.
Parameters: query - The query to create the constraint for Parameters: fieldName - The field to create the constraint on (as a string, so it can include the step), e.g. 'news.number' Parameters: operator - The operator to use. This constant can be produces from a string using Queries.getOperator(String). Parameters: value - The value to compare with, which must be of the right type. If field is number it might also be an alias. Parameters: value2 - The other value (only relevant if operator is BETWEEN, the only terniary operator) Parameters: caseSensitive - Whether it should happen case sensitively (not relevant for number fields) Parameters: datePart - The part of a DATETIME value that is to be checked The new constraint, or null it by chance the specified arguments did not lead to a new actual constraint (e.g. if value is an empty set) |
createNodeQuery | public static NodeQuery createNodeQuery(Node node)(Code) | | Returns the NodeQuery returning the given Node. This query itself is not very useful, because
you already have its result (the node), but it is convenient as a base query for many other
goals.
If the node is uncommited, it cannot be queried, and the node query returning all nodes from
the currect type will be returned.
Parameters: node - Node to create the query from A new NodeQuery object |
createQuery | public static Query createQuery(Cloud cloud, String startNodes, String nodePath, String fields, String constraints, String orderby, String directions, String searchDir, boolean distinct)(Code) | | Creates a Query object using arguments for
Cloud.getList(StringStringStringStringStringStringStringboolean) (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.
Parameters: cloud - Parameters: startNodes - Parameters: nodePath - Parameters: fields - Parameters: constraints - Parameters: orderby - Parameters: directions - Parameters: searchDir - Parameters: distinct - New query object |
createRelatedNodesQuery | public static NodeQuery createRelatedNodesQuery(Node node, NodeManager otherNodeManager, String role, String direction)(Code) | | Returns a query to find the nodes related to the given node.
Parameters: node - start node Parameters: otherNodeManager - node manager on the other side of the relation Parameters: role - role of the relation Parameters: direction - direction of the relation A new NodeQuery object |
createRelationNodesQuery | public static NodeQuery createRelationNodesQuery(Node node, NodeManager otherNodeManager, String role, String direction)(Code) | | Returns a query to find the relations nodes of the given node.
Parameters: node - start node Parameters: otherNodeManager - node manager on the other side of the relation Parameters: role - role of the relation Parameters: direction - direction of the relation A new NodeQuery object |
createRelationNodesQuery | public static NodeQuery createRelationNodesQuery(Node node, Node otherNode, String role, String direction)(Code) | | Returns a query to find the relations nodes between two given nodes.
To test whether to nodes are related you can use e.g.:
if (Queries.count(Queries.createRelationNodesQuery(node1, node2, "posrel", null)) > 0) {
..
}
Parameters: node - start node Parameters: otherNode - node on the other side of the relation Parameters: role - role of the relation Parameters: direction - direction of the relation A new NodeQuery object since: MMBase-1.8 |
getCompareValue | protected static Object getCompareValue(int fieldType, int operator, Object value)(Code) | | Used in implementation of createConstraint
Parameters: fieldType - Field Type constant (@link Field) Parameters: operator - Compare operator Parameters: value - value to convert new Compare value |
getCompareValue | protected static Object getCompareValue(int fieldType, int operator, Object value, int datePart, Cloud cloud)(Code) | | Used in implementation of createConstraint
Parameters: fieldType - Field Type constant (@link Field) Parameters: operator - Compare operator Parameters: value - value to convert Parameters: cloud - The cloud may be used to pass locale sensitive properties which may be needed for comparisions (locales, timezones) new Compare value since: MMBase-1.8.2 |
getNumberValue | protected static Number getNumberValue(String stringValue) throws BridgeException(Code) | | Used in implementation of createConstraint
Parameters: stringValue - string representation of a number Number object throws: BridgeException - when failed to convert the string |
getRelatedNodes | public static NodeList getRelatedNodes(Node node, NodeManager otherNodeManager, String role, String direction, String relationFields, String sortOrders)(Code) | | Queries a list of cluster nodes, using a
org.mmbase.bridge.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
org.mmbase.bridge.Node.getNodeValue with the
org.mmbase.bridge.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).
Parameters: node - start node Parameters: otherNodeManager - node manager on the other side of the relation Parameters: role - role of the relation Parameters: direction - direction of the relation Parameters: relationFields - Comma separated string of fields which must be queried from the relation step Parameters: sortOrders - 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. since: MMBase-1.8 |
getRelationStepDirection | public static int getRelationStepDirection(String search)(Code) | | Translates a string to a search direction constant. If the string is null then
'BOTH' is returned.
Parameters: search - string representation of the searchdir constant Searchdir constant as in RelationStep See Also: ClusterBuilder.getSearchDir See Also: The same function, only with another return value if String is null |
getSortOrder | public static int getSortOrder(String dir)(Code) | | Converts a String to a SortOrder constant
Parameters: dir - string representation of direction of sortorder SortOrder constant since: MMBase-1.7.1 |
getSortOrderFieldValue | public static Object getSortOrderFieldValue(Node node, SortOrder sortOrder)(Code) | | Obtains a value for the field of a sortorder from a given node.
Used to set constraints based on sortorder.
since: MMBase-1.8 |
removeDigits | public static String removeDigits(String complete)(Code) | | Returns substring of given string without the leading digits (used in 'paths')
Parameters: complete - string with leading digits string with digits removed |
searchStep | public static Step searchStep(List<Step> steps, String stepAlias)(Code) | | Searches a list of Steps for a step with a certain name. (alias or tableName)
Parameters: steps - steps to search through Parameters: stepAlias - alias to search for The Step if found, otherwise null throws: ClassCastException - if list does not contain only Steps |
sortUniquely | public static Query sortUniquely(Query q)(Code) | | Add a sortorder (DESCENDING) on al the'number' fields of the query, on which there is not yet a
sortorder. This ensures that the query result is ordered uniquely.
Parameters: q - query to change The changed Query |
|
|