org.mmbase.bridge.util
Class Queries

java.lang.Object
  extended byorg.mmbase.bridge.util.Queries

public abstract class Queries
extends Object

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.

Since:
MMBase-1.7
Version:
$Id: Queries.java,v 1.74 2006/06/06 19:58:21 michiel Exp $
Author:
Michiel Meeuwissen
See Also:
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 addFields(Query query, String fields)
          Adds a number of fields.
static List 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 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.
protected static Object aggregate(Query query, StepField field, int type)
           
static int compare(Node node1, Node node2, List 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 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 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 value, Object value2, boolean caseSensitive, int datePart)
          Creates a constraint smartly, depending on the type of the field, the value is casted to the right type, and the right type of constraint is created.
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.
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)
          Used in implementation of createConstraint
static int getDateTimePart(String s)
          Creates a part constant for use by createConstraint
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 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 void main(String[] argv)
           
static Object max(Query query, StepField field)
           
static Object min(Query query, StepField field)
           
protected static String removeDigits(String complete)
          Returns substring of given string without the leading digits (used in 'paths')
static Step searchStep(List steps, String stepAlias)
          Searches a list of Steps for a step with a certain name.
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

OPERATOR_BETWEEN

public static final int OPERATOR_BETWEEN
See Also:
Constant Field Values

OPERATOR_IN

public static final int OPERATOR_IN
See Also:
Constant Field Values

OPERATOR_NULL

public static final int OPERATOR_NULL
See Also:
Constant Field Values
Constructor Detail

Queries

public Queries()
Method Detail

getRelationStepDirection

public static int getRelationStepDirection(String search)
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
Returns:
Searchdir constant (@link RelationStep)
See Also:
The same function, only with another return value if String is null

createQuery

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(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.

Parameters:
cloud -
startNodes -
nodePath -
fields -
constraints -
orderby -
directions -
searchDir -
distinct -
Returns:
New query object
To Do:
Should this method be part of Cloud itself?

addConstraints

public static Constraint addConstraints(Query query,
                                        String constraints)
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
constraints - string representation of constraints
Returns:
The new constraint, or null if nothing changed added.

addConstraint

public static Constraint addConstraint(Query query,
                                       Constraint newConstraint)
Adds a Constraint to the already present constraint (with AND).

Parameters:
query - query to add the constraint to
newConstraint - constraint to add
Returns:
The new constraint.

getOperator

public static int getOperator(String s)
Creates a operator constant for use by createConstraint

Parameters:
s - String representation of operator
Returns:
FieldCompareConstraint operator constant
See Also:
createConstraint(Query, String, int, Object), createConstraint(Query, String, int, Object, Object, boolean)

getDateTimePart

public static int getDateTimePart(String s)
Creates a part constant for use by createConstraint

Parameters:
s - String representation of a datetime part
Returns:
FieldValueDateConstraint part constant
See Also:
createConstraint(Query, String, int, Object, Object, boolean, int)

getNumberValue

protected static Number getNumberValue(String stringValue)
                                throws BridgeException
Used in implementation of createConstraint

Parameters:
stringValue - string representation of a number
Returns:
Number object
Throws:
BridgeException - when failed to convert the string

getCompareValue

protected static Object getCompareValue(int fieldType,
                                        int operator,
                                        Object value)
Used in implementation of createConstraint

Parameters:
fieldType - Field Type constant (@link Field)
operator - Compare operator
value - value to convert
Returns:
new Compare value

getCompareValue

protected static Object getCompareValue(int fieldType,
                                        int operator,
                                        Object value,
                                        int datePart)
Used in implementation of createConstraint

Parameters:
fieldType - Field Type constant (@link Field)
operator - Compare operator
value - value to convert
Returns:
new Compare value

createConstraint

public static Constraint createConstraint(Query query,
                                          String fieldName,
                                          int operator,
                                          Object value)
Defaulting version of 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
fieldName - 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.
Returns:
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)
Defaulting version of 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
fieldName - 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)
Returns:
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,
                                          int datePart)
Creates a constraint smartly, depending on the type of the field, the value is casted 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
fieldName - 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)
datePart - The part of a DATETIME value that is to be checked
Returns:
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)

copyConstraint

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. 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()).
sourceStep - The step in the 'source' query.
query - The receiving query
step - The step of the receiving query which must 'receive' the sort orders.
Returns:
The new constraint or null
Throws:
IllegalArgumentException - If the given constraint is not compatible with the given step.
UnsupportedOperationException - If CompareFieldsConstraints or LegacyConstraints are encountered.
Since:
MMBase-1.7.1
See Also:
Functions are similar

copySortOrders

public static void copySortOrders(List sortOrders,
                                  Step sourceStep,
                                  Query query,
                                  Step step)
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()).
sourceStep - The step in the 'source' query.
query - The receiving query
step - The step of the receiving query which must 'receive' the sort orders.
Since:
MMBase-1.7.1

getSortOrder

public static int getSortOrder(String dir)
Converts a String to a SortOrder constant

Parameters:
dir - string representation of direction of sortorder
Returns:
SortOrder constant
Since:
MMBase-1.7.1

addSortOrders

public static List addSortOrders(Query query,
                                 String sorted,
                                 String directions)
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
sorted - string with comma-separated fields
directions - string with comma-separated directions
Returns:
The new sort orders
To Do:
implement for normal query.

removeDigits

protected static String removeDigits(String complete)
Returns substring of given string without the leading digits (used in 'paths')

Parameters:
complete - string with leading digits
Returns:
string with digits removed

addPath

public static List addPath(Query query,
                           String path,
                           String searchDirs)
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
path - create steps from this path
searchDirs - add steps with these relation directions
Returns:
The new steps.

addFields

public static List addFields(Query query,
                             String fields)
Adds a number of fields. Fields is represented as a comma separated string.

Parameters:
query - The query where the fields should be added to
fields - a comma separated string of fields
Returns:
The new stepfields

addStartNodes

public static void addStartNodes(Query query,
                                 String startNodes)
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
startNodes - start nodes
See Also:
(this is essentially a 'bridge' version of the startnodes part)

count

public static int count(Query query)
Takes the query, and does a count with the same constraints (so ignoring 'offset' and 'max')

Parameters:
query - query as base for the count
Returns:
number of results

aggregate

protected static Object aggregate(Query query,
                                  StepField field,
                                  int type)
Since:
MMBase-1.8

min

public static Object min(Query query,
                         StepField field)
Since:
MMBase-1.8

max

public static Object max(Query query,
                         StepField field)
Since:
MMBase-1.8

searchStep

public static Step searchStep(List steps,
                              String stepAlias)
Searches a list of Steps for a step with a certain name. (alias or tableName)

Parameters:
steps - steps to search through
stepAlias - alias to search for
Returns:
The Step if found, otherwise null
Throws:
ClassCastException - if list does not contain only Steps

createNodeQuery

public static NodeQuery createNodeQuery(Node node)
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
Returns:
A new NodeQuery object

createRelatedNodesQuery

public static NodeQuery createRelatedNodesQuery(Node node,
                                                NodeManager otherNodeManager,
                                                String role,
                                                String direction)
Returns a query to find the nodes related to the given node.

Parameters:
node - start node
otherNodeManager - node manager on the other side of the relation
role - role of the relation
direction - direction of the relation
Returns:
A new NodeQuery object

createRelationNodesQuery

public static NodeQuery createRelationNodesQuery(Node node,
                                                 NodeManager otherNodeManager,
                                                 String role,
                                                 String direction)
Returns a query to find the relations nodes of the given node.

Parameters:
node - start node
otherNodeManager - node manager on the other side of the relation
role - role of the relation
direction - direction of the relation
Returns:
A new NodeQuery object

createRelationNodesQuery

public static NodeQuery createRelationNodesQuery(Node node,
                                                 Node otherNode,
                                                 String role,
                                                 String direction)
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
otherNode - node on the other side of the relation
role - role of the relation
direction - direction of the relation
Returns:
A new NodeQuery object
Since:
MMBase-1.8

getRelatedNodes

public 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. 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).

Parameters:
node - start node
otherNodeManager - node manager on the other side of the relation
role - role of the relation
direction - direction of the relation
relationFields - Comma separated string of fields which must be queried from the relation step
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
To Do:
EXPERIMENTAL

addRelationFields

public static NodeQuery addRelationFields(NodeQuery q,
                                          String role,
                                          String relationFields,
                                          String sortOrders)
Since:
MMBase-1.8

sortUniquely

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. This ensures that the query result is ordered uniquely.

Parameters:
q - query to change
Returns:
The changed Query

addSortedFields

public static Query addSortedFields(Query q)
Make sure all sorted fields are queried

Since:
MMBase-1.8

getSortOrderFieldValue

public static Object getSortOrderFieldValue(Node node,
                                            SortOrder sortOrder)
Obtains a value for the field of a sortorder from a given node. Used to set constraints based on sortorder.

Since:
MMBase-1.8

compare

public static int compare(Node node1,
                          Node node2,
                          SortOrder sortOrder)
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 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(Object value,
                          Object value2,
                          SortOrder sortOrder)
Since:
MMBase-1.8

compare

public static int compare(Node node1,
                          Node node2,
                          List sortOrders)
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.

Returns:
-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

main

public static void main(String[] argv)


MMBase build 1.8.1.20060716