Package orm2 :: Module keys :: Class key
[hide private]
[frames] | no frames]

Class key

source code

Known Subclasses:
foreign_key, primary_key

This class manages keys of dbobjects (not dbclasses! The object must be initialized for this to work). It is instantiated by dbobject.__init__(), among others, using the __primary_key__ attribute to set it up.

There are a number of methods in this class which come in pairs: a singular one and a plural one. attribute() for instance returns returns the name of the dbobj's key attribute, if, and only if, the key is a single column key. If the key has multiple column, it will raise an exception. This is true also for column() and value(). The plural form of these functions is more generic: it will return a generator(!) yielding the requested objects, a generator with only one element for single column keys, a generator with several element for multiple column keys.

I know the way this is imlemented below duplicates a lot of code and could be optimized. But since this would make the code much harder to understand and these functions are not likely to change in principle, I've decided to write it this way.

Instance Methods [hide private]
  __init__(self, dbobj, *key_attributes)
  isset(self)
  attribute_name(self)
  attribute_names(self)
  attribute(self)
  attributes(self)
  column(self)
  columns(self)
  value(self)
  values(self)
  sql_literal(self)
  sql_literals(self)
  _where(self, columns)
  where(self)
  __eq__(self, other)
  make_tuple(self, t)

Method Details [hide private]

__init__(self, dbobj, *key_attributes)
(Constructor)

source code 
Parameters:
  • dbobj - Dbobj that this key belongs to. (If you pass a dbclass instead of a dbobj, all functionality will work that doesn't depend on actual data).
  • key_attributes - Those attribute(s) the key consists of

isset(self)

source code 
Returns:
True, if all attributes that are needed for this key are set within the dbobj.

attribute_name(self)

source code 
Returns:
An string containing the name of the attribute managing the key column.
Raises:

attribute_names(self)

source code 
Returns:
A tuple of strings naming the db attributes managing the key columns.

attribute(self)

source code 
Returns:
An datatype instance managing the key attribute.
Raises:

attributes(self)

source code 
Returns:
A generator yielding the datatype instances that comprise the key

column(self)

source code 
Returns:
An sql.column instance indicating the key's column.
Raises:

columns(self)

source code 
Returns:
A tuple of sql.column instances that comprise the key

value(self)

source code 
Returns:
The value of the key as a Python data object
Raises:

values(self)

source code 
Returns:
The value of the key as Python tuple

sql_literal(self)

source code 
Returns:
The sql_literal of the key as a Python data object
Raises:

sql_literals(self)

source code 
Returns:
A generator yielding the SQL literals of this key as strings.

_where(self, columns)

source code 
None

where(self)

source code 
Returns:
sql.where() instance representing a where clause that refers to this key

__eq__(self, other)
(Equality operator)

source code 
Returns:
True - if the other refers to the same key (maybe different tables!) as this key, otherwise, you guessed it, False. Order of attributes (i.e. columns) does matter, as it does in SQL!

make_tuple(self, t)

source code 
None