org.mmbase.bridge.util
Class Queries

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

public abstract class Queries
extends java.lang.Object

This class contains various utility methods for manipulating and creating query objecs. 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.32.2.7 2004/07/23 19:34:45 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, java.lang.String constraints)
          Adds a 'legacy' constraint to the query, i.e.
static java.util.List addFields(Query query, java.lang.String fields)
          Adds a number of fields.
static java.util.List addPath(Query query, java.lang.String path, java.lang.String searchDirs)
          Adds path of steps to an existing query.
static java.util.List addSortOrders(Query query, java.lang.String sorted, java.lang.String directions)
          Adds sort orders to the query, using two strings.
static void addStartNodes(Query query, java.lang.String startNodes)
          Add startNodes to the first step with the correct type to the given query.
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(java.util.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, java.lang.String fieldName, int operator, java.lang.Object value)
          Defaulting version of createConstraint(Query, String, int, Object, Object, boolean).
static Constraint createConstraint(Query query, java.lang.String fieldName, int operator, java.lang.Object value, java.lang.Object value2, boolean caseSensitive)
          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, java.lang.String startNodes, java.lang.String nodePath, java.lang.String fields, java.lang.String constraints, java.lang.String orderby, java.lang.String directions, java.lang.String searchDir, boolean distinct)
          Creates a Query object using arguments for Cloud.getList(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, boolean) (this function is of course implemented using this utility).
static NodeQuery createRelatedNodesQuery(Node node, NodeManager otherNodeManager, java.lang.String role, java.lang.String direction)
          Returns a query to find the nodes related to the given node.
static NodeQuery createRelationNodesQuery(Node node, NodeManager otherNodeManager, java.lang.String role, java.lang.String direction)
          Returns a query to find the relations nodes of the given node.
protected static java.lang.Object getCompareValue(int fieldType, int operator, java.lang.Object value)
          Used in implementation of createConstraint
protected static java.lang.Number getNumberValue(java.lang.String stringValue)
          Used in implementation of createConstraint
static int getOperator(java.lang.String s)
          Creates a operator constant for use by createConstraint
static int getRelationStepDirection(java.lang.String search)
          Translates a string to a search direction constant.
static int getSortOrder(java.lang.String dir)
          Converts a String to a SortOrder constant
protected static java.lang.String removeDigits(java.lang.String complete)
          Returns substring of given string without the leading digits (used in 'paths')
static Step searchStep(java.util.List steps, java.lang.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(java.lang.String search)
Translates a string to a search direction constant. If the string is null then 'BOTH' is returned.

See Also:
The same function, only with another return value if String is null

createQuery

public static Query createQuery(Cloud cloud,
                                java.lang.String startNodes,
                                java.lang.String nodePath,
                                java.lang.String fields,
                                java.lang.String constraints,
                                java.lang.String orderby,
                                java.lang.String directions,
                                java.lang.String searchDir,
                                boolean distinct)
Creates a Query object using arguments for Cloud.getList(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.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.


addConstraints

public static Constraint addConstraints(Query query,
                                        java.lang.String constraints)
Adds a 'legacy' constraint to the query, i.e. constraint(s) represented by a string. Alreading existing constraints remain ('AND' is used)

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

Returns:
The new constraint.

getOperator

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

See Also:
createConstraint(org.mmbase.bridge.Query, java.lang.String, int, java.lang.Object)

getNumberValue

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

Throws:
BridgeException

getCompareValue

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


createConstraint

public static Constraint createConstraint(Query query,
                                          java.lang.String fieldName,
                                          int operator,
                                          java.lang.Object value)
Defaulting version of createConstraint(Query, String, int, Object, Object, boolean). Casesensitivity defaults to false, value2 to null (so 'BETWEEN' cannot be used).


createConstraint

public static Constraint createConstraint(Query query,
                                          java.lang.String fieldName,
                                          int operator,
                                          java.lang.Object value,
                                          java.lang.Object value2,
                                          boolean caseSensitive)
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)
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:
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:
java.lang.IllegalArgumentException - If the given constraint is not compatible with the given step.
UnsuporteOperationException - If CompareFieldsConstraints or LegacyConstraints are encountered.
Since:
MMBase-1.7.1
See Also:
Functions are similar

copySortOrders

public static void copySortOrders(java.util.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(java.lang.String dir)
Converts a String to a SortOrder constant

Since:
MMBase-1.7.1

addSortOrders

public static java.util.List addSortOrders(Query query,
                                           java.lang.String sorted,
                                           java.lang.String directions)
Adds sort orders to the query, using two strings. Like in 'getList' of Cloud. Several tag-attributes need this.

Returns:
The new sort orders
To Do:
implement for normal query.

removeDigits

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


addPath

public static java.util.List addPath(Query query,
                                     java.lang.String path,
                                     java.lang.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.

Returns:
The new steps.

addFields

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

Returns:
The new stepfields

addStartNodes

public static void addStartNodes(Query query,
                                 java.lang.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.

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')


searchStep

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

Returns:
The Step if found, otherwise null
Throws:
java.lang.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.

Returns:
A new NodeQuery object

createRelatedNodesQuery

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

Returns:
A new NodeQuery object

createRelationNodesQuery

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

Returns:
A new NodeQuery object

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.

Returns:
The changed Query


MMBase build 1.7.1.20041002