rum.query

class rum.query.Query(expr=None, sort=None, limit=None, offset=None, resource=None, join=())

A base query object.

This object serves as a layer to decouple a query from the web in the form of query-string args into an internal object which rum.interfaces.IRepository use to filter the collection of objects.

It is the responsability of the rum.interfaces.IRepository to translate this object into a query the underlying data backend undersands.

Parameters

expr
An Expression object.
sort
A asc or a desc object or list of objects representing the sorting criteria for each attribute
limit
Tells the rum.interfaces.IRepository to return limit objects the most.
offset
Tells the rum.interfaces.IRepository to begin returning objects from this offset.
join
A list of names of field of class ToOneRelation, that should be joined

Examples:

>>> # A simple query: "name == Alberto"

>>> q = Query(eq('name', 'Alberto'))
>>> q
Query(eq('name', 'Alberto'), None, None, None, ())

>>> # Get a query string

>>> q.as_qs()
'q.o=eq&q.a=Alberto&q.c=name'

>>> # A query can be converted to a urlencodable dict and back

>>> Query.from_dict(q.as_dict())
Query(eq('name', 'Alberto'), None, None, None, ())

>>> # A more complex query: "name == 'Alberto' and (20 < age < 30)" with
>>> # sorting criteria, offset and limit

>>> q = Query(and_([eq('name', 'Alberto'), or_([gt('age', 20), lt('age', 30)])]), [desc('join_date')], 10, 10)
>>> q
Query(and_([eq('name', 'Alberto'), or_([gt('age', 20), lt('age', 30)])]), [desc('join_date')], 10, 10, ())
>>> Query.from_dict(q.as_dict())
Query(and_([eq('name', 'Alberto'), or_([gt('age', 20), lt('age', 30)])]), [desc('join_date')], 10, 10, ())
add_criteria(expr)

“ands” an expression to this Query’s expression returning a new Query.

as_dict()

Builds up a dictionary from a Query object.

This condition always holds True q == Query.from_dict(q.as_dict()):

>>> q = Query(eq('dummy', 2))
>>> Query.from_dict(q.as_dict())
Query(eq('dummy', 2), None, None, None, ())
classmethod from_dict(d)

Builds up a Query object from a dictionary

class rum.query.not_(col, arg=None)

Negates arg1

class rum.query.and_(col, arg=None)

A boolean AND of all arguments

class rum.query.gt(col, arg=None)

arg1 is greater than arg2

class rum.query.null(col, arg=None)

arg1 is null

class rum.query.eq(col, arg=None)

arg1 is equal to

class rum.query.in_(col, arg=None)

arg1 is in arg2 (which should be a sequence)

class rum.query.gte(col, arg=None)

arg1 is greater than or equal arg2

class rum.query.contains(col, arg=None)

arg1 contains arg2

class rum.query.lt(col, arg=None)

arg1 is less than arg2

class rum.query.neq(col, arg=None)

arg 1 is not equal to

class rum.query.startswith(col, arg=None)

arg1 starts with arg2

class rum.query.or_(col, arg=None)

A boolean OR of all arguments

class rum.query.notnull(col, arg=None)

arg1 is not null

class rum.query.Expression(col, arg=None)

This is a basic expression to build up queries.

class rum.query.desc(col, arg=None)

Descending ordering criteria.

Example, order in descending order by attribute age:

>>> desc('age')
desc('age')
class rum.query.asc(col, arg=None)

Ascending ordering criteria.

Example, order in ascending order by attribute name:

>>> asc('name')
asc('name')
class rum.query.endswith(col, arg=None)

arg1 ends with arg2

class rum.query.lte(col, arg=None)

arg1 is less than or equal arg2

Previous topic

rum.repository

Next topic

rum.view

This Page