def __new__(cls, name, bases, dict): ret = type.__new__(cls, name, bases, dict) if name != "dbobject": if not hasattr(ret, "__relation__") or getattr(ret.__relation__, "__autocreated__", False): # __relation__ which are set by this procedure # are overwritten with one that uses the current class' # name, considering the __schema__ class variable. schema = getattr(ret, "__schema__", None) ret.__relation__ = sql.relation(name, schema) ret.__relation__.__autocreated__ = True elif type(ret.__relation__) == StringType: schema = getattr(ret, "__schema__", None) ret.__relation__ = sql.relation(ret.__relation__, schema) elif type(ret.__relation__) == UnicodeType: raise TypeError("Unicode is not allowed as SQL identifyer") elif isinstance(ret.__relation__, sql.relation): pass else: msg = "Relation name must be a string or an " + "sql.relation() instance, not %s (%s)" raise TypeError(msg % (repr(type(ret.__relation__)), repr(ret.__relation__))) if not hasattr(ret, "__view__") or getattr(ret.__view__, "__autocreated__", False): ret.__view__ = ret.__relation__ ret.__view__.__autocreated__ = True elif type(ret.__view__) == StringType: schema = getattr(ret, "__schema__", None) ret.__view__ = sql.relation(ret.__view__, schema) elif type(ret.__view__) == UnicodeType: raise TypeError("Unicode is not allowed as SQL identifyer") elif isinstance(ret.__view__, sql.relation): pass else: msg = ( "Relation name for the __view__ " "must be a string or an " "sql.relation() instance, not %s (%s)" ) raise TypeError(msg % (repr(type(ret.__view__)), repr(ret.__view__))) # Initialize the dbproperties for attr_name, property in dict.items(): if isinstance(property, datatype): property.__init_dbclass__(ret, attr_name) # Add (=inherit) db-properties from our parent classes for base in bases: for attr_name, property in base.__dict__.items(): if isinstance(property, (datatype,)): property_cpy = copy.copy(property) if hasattr(property_cpy, "__init_dbclass__"): property_cpy.__init_dbclass__(ret, attr_name) setattr(ret, attr_name, property_cpy) return ret
def __init__(self, child_class, link_relation, parent_own_key=None, parent_link_column=None, child_own_key=None, child_link_column=None, title=None): """ @param parent_own_key: The attribute in the parent dbclass that is referred to by the link table. Defaults to the primary key. @param parent_link_column: The column name(!) in the link table referring to parent_own_key. Defaults to <parent class name>_<primary key column>. @param child_own_key: The attribute in the child dbclass that is referred to by the link table. Defaults to the primary key. @param child_link_column: The column name(!) in the link table referring to child_own_key. Defaults to <child class name>_<child key column>. """ relationship.__init__(self, child_class, None, None, title, False) if isinstance(link_relation, sql.relation): self.link_relation = link_relation else: self.link_relation = sql.relation(link_relation) self._parent_own_key = parent_own_key if isinstance(parent_link_column, sql.column): self._parent_link_column = parent_link_column.column else: self._parent_link_column = parent_link_column self._child_own_key = child_own_key if isinstance(child_link_column, sql.column): self._child_link_column = child_link_column.column else: self._child_link_column = child_link_column