Subsections

4 pyorq -- The base PyORQ package

4.1 pyorq.ptype -- Persistent objects

class ptype( name, bases, attributes)
This is the metaclass for persistent classes.

The purpose of ptype is to register newly defined classes with the database and to delegate object instantiation to the database. ptype derives from the build in type.

The constructor performs bookkeeping operations on persistent properties, and registers the class with the database.

__call__( *args, **kwargs)
This method delegates object instantiation to the database

The following methods on ptype behave as if they are class-methods on pobject (see below).

all_sublasses( )
Recursively generate the class and all of its subclasses

make_new_instance( *args, **kwargs)
Create a new instance, passing args and kwargs to the constructor

make_old_instance( oid)
Create a 'bare' instance, bypassing the object constructor, for an object that will be retrieved from the database

class pobject
This is be the base class for all persistent classes.

A persistent class should:

  1. be a subclass of pobject
  2. define an attribute database, referring to a database interface
  3. define one or more persistent attributes.

__metaclass__
The metaclass of all persistent classes is ptype (q.v.).
commit( )
Commit the persistent object to the database

First, recursively, commit all objects that the object refers to, then commit the object itself.

4.2 ptype.pprop -- Persistent properties

class pprop
The base class for all persistent properties

All persistent properties have the following attributes

name
The attribute name that refers to the property. This value is set upon initialization of the class by the metaclass.

key
The key that is used to store the value of the property in the instance __dict__. This value is set upon initialization of the class by the metaclass.

ptype
The type of the persistent property. Assignment of a value val is type checked using isinstance(val, ptype)

default
The default value of the persistent property. The property getter returns the default, if the values is not found in the instance __dict__.

class pval( default=None)
The base class for persistent (atomic) values

__get__( instance, cls=None)
The getter.

If called as instance.attr, return the value of the property. If called as cls.attr, return a prel.value_attr object.

The following classed derive from pval:

Property  ptype  default 
pint int 0
pfloat float 0.0
pstr str ''
pdate datetime.date datetime.date(1,1,1)
ptime datetime.time datetime.time()
pdatetime datetime.datetime datetime.datetime(1,1,1)

class pref( cls, default=None)
A reference to another persistent class cls.

__get__( instance, cls=None)
The getter.

If called as instance.attr, return the value of the property. If the value is a tuple (cls, oid) then it first instantiates the object using cls(oid=oid).

If called as cls.attr, return a prel.ref_attr object.

4.3 ptype.prel -- Relations

This module implements the relational algebra for PyORQ.

Ths module is used internally by PyORQ, You don't need this information to work with queries

A more or less formal definition is given by the following grammar:

relation   := rel_not | rel_and | rel_or | comparions
rel_not    := '~' relation
rel_and    := relation '&' relation
rel_or     := relation '|' relation
comparison := expr cmp_op expr
cmp_op     := '==' | '!=' | '<' | '<=' | '>' | '>>'
expression := min_expr | bin_expr | term
min_expr   := '-' expr
bin_expr   := expr bin_op expr
bin_op     := '+' | '-' | '*' | '/'
term       := value_attr | ref_attr | int | long | float | str ...

All classes in this module define the following methods:

py_repr( )
Build a representation that can be evaluated by python. Used by the nodb() interface to build a list comprehension. that should produce the same result as an SQL query

sql_repr( db)
Build a representation that can be used in a WHERE clause. The db-argument is used to call back into the database interface to build representations for builtin values

free_variable( )
Returns the free variable of the relation or the expression

updated_bound_variables( d)
Used to find the join clauses necessary to build the cross reference between tables. A query of the form A.b.c.d == 3, produces a bound-variable dict {('_x', 'b'): A.b.ptype, ('_x', 'b', 'c'): A.b.ptype.c.ptype} This dictionary is used to produce aliases and join clauses.

The module defines the following classes

class expression( )
Expression object define arithmetic operations that return new expression object. Expression object define comparison operators that return relation objects.

class relation( )
Relation objects define logical operators (using the bitwise operators) that return new relation objects.

__iter__( )
Yield all instances that satisfy the relation.

The iterator loops over all subclasses of the free variable of the relation. The database evaluates the relation for each subclass

The following classes are derived from expression.

class value_attr( parent, prop)
A reference to a persistent value.

This object is returned by the getter of property prop if the attribute is accessed as a class attribute. In this case parent refers to the class. The object may also be returned by ref_attr.__getattr__(attr) if attr is as persistent value of the referred property. In this case the parent will be the ref_attr object.

class ref_attr( parent, prop)
A reference to a persistent class.

This object is returned by the getter of property prop if the attribute is accessed as a class attribute. In this case parent refers to the class. The object may also be returned by ref_attr.__getattr__(attr) if attr is as persistent reference of the referred property. In this case the parent will be the ref_attr object.

__getattr__( attr)
Used to build chains of references to references.

If attr is a persistent value of the persistent class that ref_attr refers to, return a new value_attr object. If it is a persistent ref, return a ref_attr object. Otherwise, raise an AttributeError.

Comparisons of ref_attr objects with instances implies identity comparison.

__eq__( other)
Returns a comp_is object.
__ne__( other)
Returns a comp_not_is object.

class term( value)
An expression object, containing a value of one of the built in Python types.

class expr_min( arg)
Produced by -arg

class expr_add( lhs, rhs)
Produced by lhs + rhs

class expr_sub( lhs, rhs)
Produced by lhs - rhs

class expr_mul( lhs, rhs)
Produced by lhs * rhs

class expr_div( lhs, rhs)
Produced by lhs / rhs

The following classed are derived from relation.

class comp_eq( lhs, rhs)
Produced by lhs == rhs
class comp_ne( lhs, rhs)
Produced by lhs != rhs
class comp_ge( lhs, rhs)
Produced by lhs >= rhs
class comp_gt( lhs, rhs)
Produced by lhs > rhs
class comp_le( lhs, rhs)
Produced by lhs <= rhs
class comp_lt( lhs, rhs)
Produced by lhs < rhs
class comp_is( lhs, rhs)
Produced by lhs == rhs if lhs or rhs is a ref_attr
class comp_not_is( lhs, rhs)
Produced by lhs != rhs if lhs or rhs is a ref_attr

class rel_and( lhs, rhs)
Produced by lhs & rhs
class rel_or( lhs, rhs)
Produced by lhs | rhs

class rel_not( arg)
Produced by ~arg