Class Relation
source code
object --+
|
Relation
Relations
The Relation
class represents an abstract query for a
data store. It provides a set of query methods and a set of finder
methods that allow you to build and execute a query respectively.
While the method names and terminology used in this class are
representative of SQL queries, the resulting query may be used for
non-SQL data stores given that an appropriate adapter has been written
for that data store.
Method delgation
pyperry.Base delegates any calls to query methods or
finder methods to a pre-initialized Relation
class. That
means that if you have a Person model, instead of having to write
Relation(Person).order('last_name')
to create a
relation, you can simply write
Person.order('last_name')
.
Method chaining
All query methods can be chained. That means that every query
method returns a new Relation
instance that is a copy of
the old relation relation merged with the result of calling the
current query method. This saves a lot of typing when writing longer
queries like
Person.order('last_name').limit(10).offset(100).where(age=24).all()
Once you call one of the finder methods, the query gets executed and
the result of that query is returned, which breaks the method
chain.
Query methods
There are two kinds of query methods: singular and plural.
Singular query methods only store one value in the underlying
relation, so succesive calls to a singular query method overwrite the
old value with the new value. Plural query methods may have multiple
values, so successive calls to a plural query method append or merge
the old value with the new value so that all values are present in
the resulting query. Please note that not all query methods will
apply to all data stores.
Singular query methods
-
limit: (int) limit the number of records returned by the
data store to the value specified
-
offset: (int) exclude the first N records from the
result where N is the value passed to offset
-
from: (string) specify the source of the records within
the data store, such as a table name in a SQL database
-
from_: alias of
from
Plural query methods
-
select: (string) only include the given attributes in
the resulting records
-
where: (string, dict) specify conditions that the
records must meet to be included in the result
-
order: (string) order the records by the given values
-
joins: (string) a full SQL join clause
-
includes: (string, dict) eager load any associations matching the given
values. Include values may be nested in a dict.
-
conditions: alias of
where
-
group: (string) group the records by the given values
-
having: (string) specify conditions that apply only to
the group values
-
modifiers: (dict) include any additional information in
the relation. Modfiers allow you to include data in your
queries that may be useful to a processor or middleware you write. The
modifiers value is not included in the dictionary returned by
the query method, so the modifiers will not be
passed on to the data store.
Finder methods
-
first: return all records represented by the
current relation
-
all: return only the first record represented
by the currennt relation
Finder options
The finder methods will also accept a dictionary or keyword
arguments that specify a query without you having to actually call the
query methods. The keys should be named the same as the corresponding
query methods and the values should be the same values you would
normally pass to those query methods. For example, the following to
queries are equivalent:
Person.order('last_name').limit(10).all()
Person.all({'order': 'last_name', 'limit': 10})
Some other methods also accept finder options as a dictionary or
keyword arguments such as the scope
method and association
methods on pyperry.Base.
|
|
|
__getattr__(self,
key)
Delegate missing attributes to appropriate places |
source code
|
|
|
|
|
|
|
|
|
first(self,
options={ } ,
**kwargs)
Apply a limit scope of 1 and return the resulting singular value |
source code
|
|
|
all(self,
options={ } ,
**kwargs)
Apply any finder options passed and execute the query returning the
list of records |
source code
|
|
|
find(self,
pks_or_mode,
options={ } ,
**kwargs)
Returns a record or list of records matching the primary key or array
of primary keys given as its first argument. |
source code
|
|
|
apply_finder_options(self,
options={ } ,
**kwargs)
Apply given dictionary as finder options returning a new relation |
source code
|
|
|
merge(self,
relation)
Merge given relation onto self returning a new relation |
source code
|
|
|
|
|
fetch_records(self)
Perform the query and return the resulting list (aliased as list) |
source code
|
|
|
list(self)
Perform the query and return the resulting list (aliased as list) |
source code
|
|
|
|
|
modifiers(self,
value)
A pseudo query method used to store additional data on a relation. |
source code
|
|
|
modifiers_value(self)
Returns the combined dict of all values passed to the modifers
method. |
source code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Inherited from object :
__delattr__ ,
__format__ ,
__getattribute__ ,
__hash__ ,
__new__ ,
__reduce__ ,
__reduce_ex__ ,
__setattr__ ,
__sizeof__ ,
__str__ ,
__subclasshook__
|
Inherited from object :
__class__
|
Set klass this relation object is mapped to
- Overrides:
object.__init__
|
find(self,
pks_or_mode,
options={ } ,
**kwargs)
| source code
|
Returns a record or list of records matching the primary key or array
of primary keys given as its first argument. If the first argument is one
of 'first' or 'all', the first or all finder
methods respectively are called with the given finder options from the
second argument.
|
Return the query dictionary. This is used to form the dictionary of
values used in the fetch_records call.
|
Combines arguments passed to includes into a single dict to support
nested includes.
For example, the following query:
r = relation.includes('foo')
r = r.includes({'bar': 'baz'})
r = r.includes('boo', {'bar': 'biz'})
will result in an includes dict like this:
{
'foo': {},
'bar': {'biz': {}, 'baz': {}},
'boo': {}
}
|
A pseudo query method used to store additional data on a relation.
modifiers expects its value to be either a dict or a lambda that
returns a dict. Successive calls to modifiers will 'merge' the values it
receives into a single dict. The modifiers method behaves almost
identical to a plural query method. You can even use the modifiers method
in your scopes. The only difference is that the modifiers value is not
included in the dict returned by the query() method. The purpose of
having a modifiers query method is to include additional data in the
query that may be of interest to middlewares or adapters but is not
inherent to the query itself.
|
Create a default singular method with the given key. For special
functionality you can create a explicit method that will shadow this
implementation. These methods will be created dynamically at
runtime.
|
Create a default plural method with the given key. For special
functionality you can create a explicit method that will shadow this
implementation. These methods will be created dynamically at
runtime.
|
repr(x)
- Overrides:
object.__repr__
- (inherited documentation)
|
singular_query_methods
- Value:
[ ' limit ' , ' offset ' , ' from ' , ' sql ' , ' fresh ' ]
|
|
plural_query_methods
- Value:
[ ' select ' , ' group ' , ' order ' , ' joins ' , ' includes ' , ' where ' , ' having ' ]
|
|