Ejemplo n.º 1
0
def class_for_table(selectable, **mapper_kwargs):
    selectable = expression._selectable(selectable)
    mapname = 'Mapped' + _selectable_name(selectable)
    if isinstance(mapname, unicode):
        engine_encoding = selectable.metadata.bind.dialect.encoding
        mapname = mapname.encode(engine_encoding)
    if isinstance(selectable, Table):
        klass = TableClassType(mapname, (object, ), {})
    else:
        klass = SelectableClassType(mapname, (object, ), {})

    def __cmp__(self, o):
        L = self.__class__.c.keys()
        L.sort()
        t1 = [getattr(self, k) for k in L]
        try:
            t2 = [getattr(o, k) for k in L]
        except AttributeError:
            raise TypeError('unable to compare with %s' % o.__class__)
        return cmp(t1, t2)

    def __repr__(self):
        L = [
            "%s=%r" % (key, getattr(self, key, ''))
            for key in self.__class__.c.keys()
        ]
        return '%s(%s)' % (self.__class__.__name__, ','.join(L))

    for m in ['__cmp__', '__repr__']:
        setattr(klass, m, eval(m))
    klass._table = selectable
    klass.c = expression.ColumnCollection()
    mappr = mapper(klass,
                   selectable,
                   extension=Session.extension,
                   allow_null_pks=_is_outer_join(selectable),
                   **mapper_kwargs)

    for k in mappr.iterate_properties:
        klass.c[k.key] = k.columns[0]

    klass._query = Session.query_property()
    return klass
Ejemplo n.º 2
0
def class_for_table(selectable, **mapper_kwargs):
    selectable = expression._selectable(selectable)
    mapname = 'Mapped' + _selectable_name(selectable)
    if isinstance(mapname, unicode): 
        engine_encoding = selectable.metadata.bind.dialect.encoding 
        mapname = mapname.encode(engine_encoding)
    if isinstance(selectable, Table):
        klass = TableClassType(mapname, (object,), {})
    else:
        klass = SelectableClassType(mapname, (object,), {})
    
    def __cmp__(self, o):
        L = self.__class__.c.keys()
        L.sort()
        t1 = [getattr(self, k) for k in L]
        try:
            t2 = [getattr(o, k) for k in L]
        except AttributeError:
            raise TypeError('unable to compare with %s' % o.__class__)
        return cmp(t1, t2)

    def __repr__(self):
        L = ["%s=%r" % (key, getattr(self, key, ''))
             for key in self.__class__.c.keys()]
        return '%s(%s)' % (self.__class__.__name__, ','.join(L))

    for m in ['__cmp__', '__repr__']:
        setattr(klass, m, eval(m))
    klass._table = selectable
    klass.c = expression.ColumnCollection()
    mappr = mapper(klass,
                   selectable,
                   extension=Session.extension,
                   allow_null_pks=_is_outer_join(selectable),
                   **mapper_kwargs)
                   
    for k in mappr.iterate_properties:
        klass.c[k.key] = k.columns[0]

    klass._query = Session.query_property()
    return klass
Ejemplo n.º 3
0
def class_for_table(selectable, **mapper_kwargs):
    selectable = expression._selectable(selectable)
    mapname = 'Mapped' + _selectable_name(selectable)
    if isinstance(selectable, Table):
        klass = TableClassType(mapname, (object,), {})
    else:
        klass = SelectableClassType(mapname, (object,), {})

    def __cmp__(self, o):
        L = self.__class__.c.keys()
        L.sort()
        t1 = [getattr(self, k) for k in L]
        try:
            t2 = [getattr(o, k) for k in L]
        except AttributeError:
            raise TypeError('unable to compare with %s' % o.__class__)
        return cmp(t1, t2)

    def __repr__(self):
        import locale
        encoding = locale.getdefaultlocale()[1] or 'ascii'
        L = []
        for k in self.__class__.c.keys():
            value = getattr(self, k, '')
            if isinstance(value, unicode):
                value = value.encode(encoding)
            L.append("%s=%r" % (k, value))
        return '%s(%s)' % (self.__class__.__name__, ','.join(L))

    for m in ['__cmp__', '__repr__']:
        setattr(klass, m, eval(m))
    klass._table = selectable
    mappr = mapper(klass,
                   selectable,
                   extension=objectstore.mapper_extension,
                   allow_null_pks=_is_outer_join(selectable),
                   **mapper_kwargs)
    klass._query = Query(mappr)
    return klass
Ejemplo n.º 4
0
def class_for_table(selectable, **mapper_kwargs):
    selectable = expression._selectable(selectable)
    mapname = str('Mapped' + _selectable_name(selectable))
    if isinstance(selectable, Table):
        klass = TableClassType(mapname, (object, ), {})
    else:
        klass = SelectableClassType(mapname, (object, ), {})

    def __cmp__(self, o):
        L = self.__class__.c.keys()
        L.sort()
        t1 = [getattr(self, k) for k in L]
        try:
            t2 = [getattr(o, k) for k in L]
        except AttributeError:
            raise TypeError('unable to compare with %s' % o.__class__)
        return cmp(t1, t2)

    def __repr__(self):
        import locale
        encoding = locale.getdefaultlocale()[1] or 'ascii'
        L = []
        for k in self.__class__.c.keys():
            value = getattr(self, k, '')
            if isinstance(value, unicode):
                value = value.encode(encoding)
            L.append("%s=%r" % (k, value))
        return '%s(%s)' % (self.__class__.__name__, ','.join(L))

    for m in ['__cmp__', '__repr__']:
        setattr(klass, m, eval(m))
    klass._table = selectable
    mappr = mapper(klass,
                   selectable,
                   extension=objectstore.mapper_extension,
                   allow_null_pks=_is_outer_join(selectable),
                   **mapper_kwargs)
    klass._query = Query(mappr)
    return klass
Ejemplo n.º 5
0
 def with_labels(self, item):
     # TODO give meaningful aliases
     return self.map(expression._selectable(item).select(use_labels=True).alias('foo'))
Ejemplo n.º 6
0
 def with_labels(self, item):
     # TODO give meaningful aliases
     return self.map(
         expression._selectable(item).select(use_labels=True).alias('foo'))