def __init_dbclass__(self, dbclass, attribute_name): datatype.__init_dbclass__(self, dbclass, attribute_name) if type(self.postfixes) != DictType: p = {} for postfix in self.postfixes: if type(postfix) != StringType: raise TypeError("All postfixes must be strings!") if p.has_key(postfix): raise ValueError("The members of the postfixes " "parameter must be unique.") p[postfix] = sql.column("%s_%s" % ( attribute_name, postfix, )) self.postfixes = p else: for postfix, column in self.postfixes: if not isinstance(column, sql.column): self.postfixes[postfix] = sql.column(column) for postfix, column in self.postfixes.items(): attr_name = " %s_%s" % ( attribute_name, repr(postfix), ) dt = self.inside_datatype(column=column, title=self.title, validators=self.validators, has_default=self.has_default) setattr(dbclass, attr_name, dt) dt.__init_dbclass__(dbclass, attr_name) self.inside_dbproperty_names[postfix] = attr_name
def select_expression(self, dbclass, full_column_names): """ The expression this function returns is used to SELECT this column from the database. """ if full_column_names: return sql.column(self.column.name(), dbclass.__view__, self.column.quote()) else: return self.column
def where(self): """ Return the WHERE clause that selects the child objects for the given parent. The clause will also include the condition to limit the JOIN to the appropriate rows. """ parent_where = sql.where( sql.column(self.relationship.parent_link_column(self.dbobj), relation=self.relationship.link_relation), " = ", self.dbobj.__primary_key__.sql_literal()) join_where = sql.where( sql.column(self.child_class().__primary_key__, self.child_class().__view__), " = ", sql.column(self.relationship.child_link_column(), self.relationship.link_relation)) return sql.where.and_(join_where, parent_where)
def __init_dbclass__(self, dbclass, attribute_name): if self.column is None and self.foreign_key is None: column_name = "%s_%s" % ( self.child_class.__name__, self.child_class.__primary_key__) # A class' __primary_key__ attr is always a string! self.column = sql.column(column_name) datatype.__init_dbclass__(self, dbclass, attribute_name) if self.foreign_key is not None: self.column = None
def __init_dbclass__(self, dbclass, attribute_name): """ This methods gets called by dbobject's metaclass. It supplies the db property with info about the class it belongs to and its attribute name. """ self.dbclass = dbclass self.attribute_name = attribute_name # The actual data attribute's names start with a space character # to avoid name clashes. if self.column is None: self.column = sql.column(attribute_name) self._data_attribute_name = " %s" % str(self.column) if self.title is None: self.title = unicode(self.attribute_name, "ascii")
def __init__(self, column=None, title=None, validators=(), has_default=False): """ @param column: A t4.orm.sql column instance or a string containing a SQL column name pointing to the column this property is responsible for. Defaults to the column with the same name as the attribute. @param title: The title of this column used in dialogs, tables and validator error messages (among other things). This must be a unicode object or None. @param validators: A sequence of objects of validators.validator children. (A single validator is ok, too) @param has_default: Boolean property that determines whether this dbproperty is retrieved from the database after the dbobject has been INSERTed. (So has_default referrs to the SQL column really). """ global _property_counter if type(column) == StringType or isinstance(column, sql.quotes): self.column = sql.column(column) elif isinstance(column, sql.column): self.column = column elif column is None: self.column = None else: raise TypeError("Column must either be a string or an sql.column"+\ " instance, not %s (%s)" % ( repr(type(column)), repr(column),) ) self.title = title if isinstance(validators, validator): self.validators = ( validators, ) else: self.validators = tuple(validators) self.has_default = has_default self.index = _property_counter _property_counter += 1