org.mmbase.bridge
Interface Query

All Superinterfaces:
Cacheable, Cloneable, SearchQuery
All Known Subinterfaces:
NodeQuery
All Known Implementing Classes:
BasicNodeQuery, BasicQuery

public interface Query
extends SearchQuery, Cacheable, Cloneable

Representation of a (database) query. It is modifiable for use by bridge-users.

Since:
MMBase-1.7
Version:
$Id: Query.java,v 1.39 2006/07/03 20:49:29 michiel Exp $
Author:
Michiel Meeuwissen, Pierre van Rooden
See Also:
Queries

Field Summary
 
Fields inherited from interface org.mmbase.storage.search.SearchQuery
DEFAULT_MAX_NUMBER, DEFAULT_OFFSET
 
Method Summary
 AggregatedField addAggregatedField(Step step, Field field, int aggregationType)
          Add an aggregated field to a step
 StepField addField(Step step, Field field)
          Adds a field to a step.
 StepField addField(String field)
          Adds a field by string
 void addNode(Step s, int number)
           
 void addNode(Step s, Node node)
          Adds a node to a step.
 RelationStep addRelationStep(NodeManager otherManager)
          If you need to add a 'related' NodeManager without specifying a role/searchDir simply use these addRelationStep.
 RelationStep addRelationStep(NodeManager nodeManager, String role, String searchDir)
          Adds new Relation step to the query.
 SortOrder addSortOrder(StepField f, int direction)
          Defaulting version of addSortOrder(StepField, int, boolean) (sorting case insensitively).
 SortOrder addSortOrder(StepField f, int direction, boolean caseSensitive)
          Adds an order on a certain field.
 Step addStep(NodeManager nodeManager)
          Adds a NodeManager to this Query.
 Query aggregatingClone()
          Creates an unused aggregate clone of this query.
 Object clone()
          Create an (unused) clone
 Query cloneWithoutFields()
          Clones this object, only without the fields
 CompositeConstraint createConstraint(Constraint c1, int op, Constraint c2)
          Combines two Constraints to one new one, using a boolean operator (AND or OR).
 FieldNullConstraint createConstraint(StepField f)
          Create a contraint (for use with this Query object).
 FieldValueConstraint createConstraint(StepField f, int op, Object v)
          Create a contraint (for use with this Query object).
 FieldValueConstraint createConstraint(StepField f, int op, Object v, int part)
          Create a contraint (for use with this Query object).
 CompareFieldsConstraint createConstraint(StepField f, int op, StepField v)
          Create a contraint (for use with this Query object).
 FieldValueConstraint createConstraint(StepField f, Object v)
          Create a contraint (for use with this Query object).
 FieldValueBetweenConstraint createConstraint(StepField f, Object o1, Object o2)
          Create a contraint (for use with this Query object).
 FieldValueInConstraint createConstraint(StepField f, SortedSet v)
          Create a contraint (for use with this Query object).
 LegacyConstraint createConstraint(String s)
          Create a contraint (for use with this Query object).
 StepField createStepField(Step step, Field field)
          Creates a StepField object withouth adding it (needed for aggregated queries).
 StepField createStepField(Step step, String fieldName)
          Creates a StepField object withouth adding it (needed for aggregated queries).
 StepField createStepField(String fieldIdentifer)
          Creates the step field for the given name.
 Constraint getCleanConstraint()
          Gets the 'clean' constraint on this query.
 Cloud getCloud()
          Returns the Cloud for which this Query was defined.
 NodeList getList()
          Executes the query and returns the resulting node list.
 Step getStep(String stepAlias)
          Returns the step with given alias, or null if it is not present
 boolean isAggregating()
          Whether this query is 'aggregating'.
 boolean isDistinct()
          Tests if the search request is to return distinct results.
 boolean isUsed()
          Whether this query was used or not.
 boolean markUsed()
          Mark this query 'used'.
 void removeFields()
          Removes all fields from the Query object.
 void setAlias(Step step, String alias)
          Sets the alias to the given step.
 FieldConstraint setCaseSensitive(FieldConstraint constraint, boolean sensitive)
          Changes the given constraint's 'case sensitivity' (if applicable).
 void setConstraint(Constraint c)
          The (composite) constraint can actually be set into the query with this method.
 Query setDistinct(boolean distinct)
          Specifies wether the query result must contain only 'distinct' results.
 Constraint setInverse(Constraint c, boolean i)
          Changes the given constraint's 'inverse' (if applicable).
 Query setMaxNumber(int maxNumber)
          Limits the query-result to maxNumber records.
 Query setOffset(int offset)
          Offsets the query-result with offset records.
 String toSql()
          Shows the query in a human-readable SQL form.
 
Methods inherited from interface org.mmbase.storage.search.SearchQuery
equals, getConstraint, getFields, getMaxNumber, getOffset, getSortOrders, getSteps, hashCode, toString
 
Methods inherited from interface org.mmbase.bridge.Cacheable
getCachePolicy, setCachePolicy
 

Method Detail

getCloud

public Cloud getCloud()
Returns the Cloud for which this Query was defined.

Returns:
Cloud

isAggregating

public boolean isAggregating()
Whether this query is 'aggregating'. You can only use 'addAggregatedField' on aggregating querys.

Specified by:
isAggregating in interface SearchQuery
Returns:
true if the query is aggregating
To Do:
Should this not appear in SearchQuery itself? Or should there be an AggregatingQuery interface? It is now used in BasicCloud.getList.

addStep

public Step addStep(NodeManager nodeManager)
Adds a NodeManager to this Query. This can normally be done only once. After that you need to use (one of the) 'addRelationStep'.

Parameters:
nodeManager - The nodeManager associated with the step.
Returns:
The 'step' wrapping the NodeManager.
Throws:
IllegalArgumentException - when an invalid argument is supplied.
See Also:
addRelationStep(NodeManager)

setAlias

public void setAlias(Step step,
                     String alias)
Sets the alias to the given step.

Parameters:
step - step to add the alias for
alias - The alias which must be given to the step. If it is "" an alias should be generated. 'null' removes the alias.

getStep

public Step getStep(String stepAlias)
Returns the step with given alias, or null if it is not present

Parameters:
stepAlias - Alias for the step (may also be tableName, in which case the first step for this table is returned)
Returns:
step with given alias

addRelationStep

public RelationStep addRelationStep(NodeManager nodeManager,
                                    String role,
                                    String searchDir)
Adds new Relation step to the query. Adds the next step as well, it can be retrieved by calling getNext() on the relationstep, and cast to Step.

Parameters:
nodeManager - node manager on the other side of the relation
role - role of a relation
searchDir - the direction of the relation
Returns:
new Relation step
Throws:
IllegalArgumentException - when an invalid argument is supplied.
IllegalStateException - when there is no previous step.

addRelationStep

public RelationStep addRelationStep(NodeManager otherManager)
If you need to add a 'related' NodeManager without specifying a role/searchDir simply use these addRelationStep.

Parameters:
otherManager - node manager on the other side of the relation
Returns:
new Relation step

addField

public StepField addField(Step step,
                          Field field)
Adds a field to a step.

Parameters:
step - step to add field to
field - field to add
Returns:
new StepField

addField

public StepField addField(String field)
Adds a field by string

Parameters:
field - field to add
Returns:
new StepField

removeFields

public void removeFields()
Removes all fields from the Query object.


createStepField

public StepField createStepField(Step step,
                                 Field field)
Creates a StepField object withouth adding it (needed for aggregated queries).

Parameters:
step - step to create StepField from
field - field to create StepField from
Returns:
new StepField

createStepField

public StepField createStepField(Step step,
                                 String fieldName)
Creates a StepField object withouth adding it (needed for aggregated queries).

Parameters:
step - step to create StepField from
fieldName - name of field to create StepField from
Returns:
new StepField

createStepField

public StepField createStepField(String fieldIdentifer)
Creates the step field for the given name. For a NodeQuery the arguments is simply the name of the field. For a 'normal' query, it should be prefixed by the (automatic) alias of the Step.

Parameters:
fieldIdentifer - field identifier to create StepField from
Returns:
new StepField

addAggregatedField

public AggregatedField addAggregatedField(Step step,
                                          Field field,
                                          int aggregationType)
Add an aggregated field to a step

Parameters:
step - step to add field to
field - field to add
aggregationType - Type of aggregation
Returns:
new AggregatedField

setDistinct

public Query setDistinct(boolean distinct)
Specifies wether the query result must contain only 'distinct' results.

Parameters:
distinct - 'distinct' results
Returns:
Query
See Also:
BasicSearchQuery.setDistinct(boolean), isDistinct()

isDistinct

public boolean isDistinct()
Description copied from interface: SearchQuery
Tests if the search request is to return distinct results. In that case duplicate nodes will be removed from the result.

This corresponds to the use of "DISTINCT" in SQL SELECT-syntax .

Specified by:
isDistinct in interface SearchQuery
See Also:
SearchQuery.isDistinct()

setMaxNumber

public Query setMaxNumber(int maxNumber)
Limits the query-result to maxNumber records.

Parameters:
maxNumber - max number of results
Returns:
Query
See Also:
BasicSearchQuery.setMaxNumber(int)

setOffset

public Query setOffset(int offset)
Offsets the query-result with offset records.

Parameters:
offset - offset in results
Returns:
Query
See Also:
BasicSearchQuery.setOffset(int)

getCleanConstraint

public Constraint getCleanConstraint()
Gets the 'clean' constraint on this query. I.e. the constraint which were automaticly added because of security are stripped away, and it is garanteed that you get back what you put in. It is adviced that you use this in stead of SearchQuery#getConstraint, because that function is used by the Query handlers, which do need the security constraints. But otherwise you don't want to see those.

Returns:
Constraint
Since:
MMBase-1.7.1

createConstraint

public LegacyConstraint createConstraint(String s)
Create a contraint (for use with this Query object). The argument is a string, as also can be used as an argument of the 'non-query' getList. This should be considered legacy.

Parameters:
s - String with LegacyConstraint
Returns:
LegacyConstraint
See Also:
Cloud.getList(String startNodes, String nodePath, String fields, String constraints, String orderby, String directions, String searchDir, boolean distinct), NodeManager.getList(String constraints, String orderby, String directions)

createConstraint

public FieldNullConstraint createConstraint(StepField f)
Create a contraint (for use with this Query object). The given field must be 'null'.

Parameters:
f - Stepfield
Returns:
FieldNullConstraint

createConstraint

public FieldValueConstraint createConstraint(StepField f,
                                             Object v)
Create a contraint (for use with this Query object). The given field must equal the given value 'v'.

Parameters:
f - field
v - value
Returns:
FieldValueConstraint

createConstraint

public FieldValueConstraint createConstraint(StepField f,
                                             int op,
                                             Object v)
Create a contraint (for use with this Query object). The given field and the given value 'v', combined with given operator must evaluate to true.

Parameters:
f - field
op - operator
v - value
Returns:
FieldValueConstraint

createConstraint

public FieldValueConstraint createConstraint(StepField f,
                                             int op,
                                             Object v,
                                             int part)
Create a contraint (for use with this Query object). The given date field and the given value 'v', combined with given operator must evaluate to true for the specified date part.

Parameters:
f - field
op - operator
v - value
part - part of the date value
Returns:
FieldValueConstraint

createConstraint

public CompareFieldsConstraint createConstraint(StepField f,
                                                int op,
                                                StepField v)
Create a contraint (for use with this Query object). The two given fields , combined with given operator must evaluate to true.

Parameters:
f - field
op - operator
v - value
Returns:
CompareFieldsConstraint

createConstraint

public FieldValueBetweenConstraint createConstraint(StepField f,
                                                    Object o1,
                                                    Object o2)
Create a contraint (for use with this Query object). The given field must lie between the two given values.

Parameters:
f - field
o1 - value one
o2 - value two
Returns:
FieldValueBetweenConstraint

createConstraint

public FieldValueInConstraint createConstraint(StepField f,
                                               SortedSet v)
Create a contraint (for use with this Query object). The given field value must be contained by the given set of values. If the given set is empty, a FieldValueInConstraint will be constructed for the number field in stead ('number IN (-1)'), which ensures that also in that case the logical thing will happen. (' IN ()' fails in most databases).

Parameters:
f - field
v - value
Returns:
the new Constraint.

setCaseSensitive

public FieldConstraint setCaseSensitive(FieldConstraint constraint,
                                        boolean sensitive)
Changes the given constraint's 'case sensitivity' (if applicable). Default it is true.

Parameters:
constraint - constraint to change
sensitive - case sensitivity
Returns:
modified FieldConstraint

setInverse

public Constraint setInverse(Constraint c,
                             boolean i)
Changes the given constraint's 'inverse' (if applicable). Default it is (of course) false.

Parameters:
c - constraint
i - inverse
Returns:
Inversed contraint

createConstraint

public CompositeConstraint createConstraint(Constraint c1,
                                            int op,
                                            Constraint c2)
Combines two Constraints to one new one, using a boolean operator (AND or OR). Every new constraint must be combined with the ones you already have with such a new CompositeConstraint. If the first constraint is a composite constraint (with the same logical operator), then the second one will simply be added.

Parameters:
c1 - constraint one
op - operator (CompositeConstraint.LOGICAL_AND, CompositeConstraint.LOGICAL_OR)
c2 - constraint two
Returns:
a Composite constraint (might not be a new one)

setConstraint

public void setConstraint(Constraint c)
The (composite) constraint can actually be set into the query with this method.

Parameters:
c - constraint

addSortOrder

public SortOrder addSortOrder(StepField f,
                              int direction,
                              boolean caseSensitive)
Adds an order on a certain field.

Parameters:
f - field
direction - ASCENDING or DESCENDING
Returns:
new SortOrder
Since:
MMBase-1.8
See Also:
BasicSearchQuery.addSortOrder(org.mmbase.storage.search.StepField)

addSortOrder

public SortOrder addSortOrder(StepField f,
                              int direction)
Defaulting version of addSortOrder(StepField, int, boolean) (sorting case insensitively).


addNode

public void addNode(Step s,
                    Node node)
Adds a node to a step.

Parameters:
s - step
node - node to add

addNode

public void addNode(Step s,
                    int number)
Since:
MMBase-1.8

isUsed

public boolean isUsed()
Whether this query was used or not. If is was used, then you cannot modify it anymore (would kill caches, and references to 'original query' would get invalid)

Returns:
query already used or not

markUsed

public boolean markUsed()
Mark this query 'used'. It has to be copied first, if you want to add things to it.

Returns:
if this query is was used before this method call

clone

public Object clone()
Create an (unused) clone

Returns:
Cloned Query

cloneWithoutFields

public Query cloneWithoutFields()
Clones this object, only without the fields

Returns:
Cloned Query

aggregatingClone

public Query aggregatingClone()
Creates an unused aggregate clone of this query. If this query is not itself aggregated, all fields are removed (but the contraints on them remain), and you can add aggregated fields then.

Returns:
Cloned Query

getList

public NodeList getList()
Executes the query and returns the resulting node list.

Since:
MMBase-1.8

toSql

public String toSql()
Shows the query in a human-readable SQL form. This is probably not the query which will actually be sent to the database. This method is provided because 'toString' on a Query object is pretty complete, but pretty undigestable for mere mortals too.

Since:
MMBase-1.8


MMBase build 1.8.1.20060716