Package orm2 :: Module containers :: Class sqltuple
[hide private]
[frames] | no frames]

Class sqltuple

source code

    object --+            
             |            
      property --+        
                 |        
datatypes.datatype --+    
                     |    
            _container --+
                         |
                        sqltuple

This datatype represents a relationship between relations in which the child relation provides a number of values for a parent relation indexed by the parent's primary key. In the parent dbobject these values appear as a tuple.

Like this:
  CREATE parent (
      id INTEGER,
      ... some more fields ...
  );

  CREATE child (
      parent_id INTEGER NOT NULL REFERENCES parent(id),
      info TEXT
  );
An appropriate dbclass would look like this:
  class parent(dbobject):
      id = integer()
      info = sqltuple('child', text(), 'id', 'parent_id')

  result = ds.select(parent)
  p = result.next()
  p.info = ( "One", "Two", "Three", )

An sqltuple is not mutable (i.e. a tuple and not a list), so you can't set any member of the tupe as in t[3] = 'Hallo'. To append a value you must say dbobj.tpl += ( "Hallo", ). The program will do the right thing and only create as many INSERT statements as strings added. For all other operations all rows referenced by the parent's key will be DELETEd and INSERTed again.

The sqltuple dbattribute may be set to any iterable that yields values of the appropriate type. It will always return a Python tuple.

The orderby parameter allows you to specify an SQL clause that will determine the order of the child table's rows that are returned. It may be None. Orderby must be an instance of sql.orderby.

An sqltuple cannot be None, just an empty tuple.

Instance Methods [hide private]
  __init__(self, child_relation, child_column, orderby=None, child_key=None, title=None)
@param child_relation: sql.relation object or string, indicating the name of the dependent relation.
  __get__(self, dbobj, owner="What??")
See the Python Language Reference, chapter 3.3.2.2 for details on how this works.
  __set__(self, dbobj, new_values)
Set the attribute managed by this datatype class on instance to value.

Inherited from _container: __convert__, __init_dbclass__, __select_after_insert__, __select_this_column__, __set_from_result__, child_where, sql_literal

Inherited from datatypes.datatype: __delete__, __setattr__, add_widget, check_dbobj, data_attribute_name, isset, widget_specs

Inherited from property: __getattribute__, __new__

Inherited from object: __delattr__, __hash__, __reduce__, __reduce_ex__, __repr__, __str__


Class Variables [hide private]

Inherited from datatypes.datatype: python_class, sql_literal_class

Inherited from property: fdel, fget, fset


Properties [hide private]

Inherited from object: __class__


Method Details [hide private]

__init__(self, child_relation, child_column, orderby=None, child_key=None, title=None)
(Constructor)

source code 

@param child_relation: sql.relation object or string, indicating
   the name of the dependent relation.
@param child_column: It's datatype (datatype object). The datatype's
   column parameter may be used as well as teh validators. Title and
   has_default will be ignored. The column name defaults to the
   container's attribute_name in the parent dbclass.
@param child_key: A string indicating the column in the child
   relation that is used in the foreign key to point to the parent.
   
The the docstrings of the actual implementations for examples,
that's going to make it clearer.

As a sidenote: This only works for a single-column reference
key from the parent class to the child class. It would be
possible implementing this for multiple column keys using an
anonymous dbclass, but it's just soooo darn complicated! So I
thought to myself that orm2 is complecated enough...

Overrides: _container.__init__
(inherited documentation)

__get__(self, dbobj, owner="What??")

source code 
See the Python Language Reference, chapter 3.3.2.2 for details on how this works. Be sure to be in a relaxed, ready-for-hard-figuring mood.
Overrides: datatypes.datatype.__get__
(inherited documentation)

__set__(self, dbobj, new_values)

source code 
Set the attribute managed by this datatype class on instance to value. This will be called by Python on attribute assignment. The __set_from_result__ method does the same thing for data retrieved from the RDBMS. See below.
Overrides: datatypes.datatype.__set__
(inherited documentation)