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

Class sqldict

source code

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


This datatype represents a relationship between relations in which
the child relation proveies a number of values for a parent
relation indexed by the parent's primary key and a unique key for
each of the entries. In the parent dbobject these key/value pairs
appear as a dict.

Like this:

  CREATE user (
     id INTEGER,
     ... some more fields ...
  );

  CREATE user_info (
     user_id INTEGER REFERENCES user(id),
     key VARCHAR(100),
     value TEXT,

     PRIMARY KEY(user_id, key)
  );

The PRIMARY KEY clause is not strictly necessary, but it will make
sure every user_id/key pair only appears once. You may also want to
create an index over the user_id column, because queries will
usually call for all the child rows associated with the parent.

An appropriate dbclass would look like this::

  class user(dbobject):
      id = integer()
      info = sqldict('user_info', varchar(column=key),
                     Unicode(column=value))

  result = ds.select(user)
  me = result.next()
  me.info['firstname'] = 'Diedrich'
  me.info['lastname'] = 'Vorberg'

  if me.info['age'] > 30:
      print 'Consider suicide!'

As for the sqltuple the whole thing only works on single-column
primary keys.



Nested Classes [hide private]
  sqldict_dict

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

Inherited from _container: __convert__, __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_key_column, child_value_column, 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)

__init_dbclass__(self, dbclass, attribute_name)

source code 
None
Overrides: _container.__init_dbclass__

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

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_dict)

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)