Package pyperry :: Module relation :: Class Relation
[frames] | no frames]

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

Plural query methods

Finder methods

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.

Instance Methods
 
__init__(self, klass)
Set klass this relation object is mapped to
source code
 
__getattr__(self, key)
Delegate missing attributes to appropriate places
source code
 
__iter__(self) source code
 
__getitem__(self, index) source code
 
__len__(self) 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
 
query(self)
Return the query dictionary.
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
 
includes_value(self)
Combines arguments passed to includes into a single dict to support nested includes.
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
 
create_singular_method(self, key)
Create a default singular method with the given key.
source code
 
create_plural_method(self, key)
Create a default plural method with the given key.
source code
 
clone(self) source code
 
reset(self) source code
 
__repr__(self)
repr(x)
source code
 
fresh(self, value=True) source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables
  singular_query_methods = ['limit', 'offset', 'from', 'sql', 'f...
  plural_query_methods = ['select', 'group', 'order', 'joins', '...
  aliases = {'conditions': 'where', 'from_': 'from'}
Properties

Inherited from object: __class__

Method Details

__init__(self, klass)
(Constructor)

source code 

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.

query(self)

source code 

Return the query dictionary. This is used to form the dictionary of values used in the fetch_records call.

includes_value(self)

source code 

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': {}
   }

modifiers(self, value)

source code 

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_singular_method(self, key)

source code 

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_plural_method(self, key)

source code 

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__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)

Class Variable Details

singular_query_methods

Value:
['limit', 'offset', 'from', 'sql', 'fresh']

plural_query_methods

Value:
['select', 'group', 'order', 'joins', 'includes', 'where', 'having']