def _class_for_table(session, engine, selectable, base_cls=object, **mapper_kwargs): selectable = expression._clause_element_as_expr(selectable) mapname = 'Mapped' + _selectable_name(selectable) # Py2K if isinstance(mapname, unicode): engine_encoding = engine.dialect.encoding mapname = mapname.encode(engine_encoding) # end Py2K if isinstance(selectable, Table): klass = TableClassType(mapname, (base_cls, ), {}) else: klass = SelectableClassType(mapname, (base_cls, ), {}) def _compare(self, o): L = list(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 t1, t2 # python2/python3 compatible system of # __cmp__ - __lt__ + __eq__ def __lt__(self, o): t1, t2 = _compare(self, o) return t1 < t2 def __eq__(self, o): t1, t2 = _compare(self, o) return 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 ['__eq__', '__repr__', '__lt__']: setattr(klass, m, eval(m)) klass._table = selectable klass.c = expression.ColumnCollection() mappr = mapper(klass, selectable, extension=AutoAdd(session), **mapper_kwargs) for k in mappr.iterate_properties: klass.c[k.key] = k.columns[0] klass._query = session.query_property() return klass
def _class_for_table(session, engine, selectable, base_cls, mapper_kwargs): selectable = expression._clause_element_as_expr(selectable) mapname = _selectable_name(selectable) # Py2K if isinstance(mapname, unicode): engine_encoding = engine.dialect.encoding mapname = mapname.encode(engine_encoding) # end Py2K if isinstance(selectable, Table): klass = TableClassType(mapname, (base_cls,), {}) else: klass = SelectableClassType(mapname, (base_cls,), {}) def _compare(self, o): L = list(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 t1, t2 # python2/python3 compatible system of # __cmp__ - __lt__ + __eq__ def __lt__(self, o): t1, t2 = _compare(self, o) return t1 < t2 def __eq__(self, o): t1, t2 = _compare(self, o) return 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)) def __getitem__(self, key): return self._query[key] for m in ['__eq__', '__repr__', '__lt__', '__getitem__']: setattr(klass, m, eval(m)) klass._table = selectable klass.c = expression.ColumnCollection() mappr = mapper(klass, selectable, extension=AutoAdd(session), **mapper_kwargs) for k in mappr.iterate_properties: klass.c[k.key] = k.columns[0] klass._query = session.query_property() return klass
def parse(f,q): ce = expression._clause_element_as_expr(q) try: columns = ce._raw_columns except: columns = ce.columns._data.values() fe = [] tokens = whereExpression.parseString(f) for t in tokens: pass print tokens
def class_for_table(selectable, **mapper_kwargs): selectable = expression._clause_element_as_expr(selectable) mapname = 'Mapped' + _selectable_name(selectable) # Py2K if isinstance(mapname, unicode): engine_encoding = selectable.metadata.bind.dialect.encoding mapname = mapname.encode(engine_encoding) # end Py2K 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
def class_for_table(selectable, **mapper_kwargs): selectable = expression._clause_element_as_expr(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
def with_labels(self, selectable, base=None, **mapper_args): """Map a selectable directly, wrapping the selectable in a subquery with labels. The class and its mapping are not cached and will be discarded once dereferenced (as of 0.6.6). :param selectable: an :func:`.expression.select` construct. :param base: a Python class which will be used as the base for the mapped class. If ``None``, the "base" argument specified by this :class:`.SQLSoup` instance's constructor will be used, which defaults to ``object``. :param mapper_args: Dictionary of arguments which will be passed directly to :func:`.orm.mapper`. """ # TODO give meaningful aliases return self.map(expression._clause_element_as_expr(selectable).select( use_labels=True).alias('foo'), base=base, **mapper_args)
def with_labels(self, selectable, base=None, **mapper_args): """Map a selectable directly, wrapping the selectable in a subquery with labels. The class and its mapping are not cached and will be discarded once dereferenced (as of 0.6.6). :param selectable: an :func:`.expression.select` construct. :param base: a Python class which will be used as the base for the mapped class. If ``None``, the "base" argument specified by this :class:`.SqlSoup` instance's constructor will be used, which defaults to ``object``. :param mapper_args: Dictionary of arguments which will be passed directly to :func:`.orm.mapper`. """ # TODO give meaningful aliases return self.map( expression._clause_element_as_expr(selectable). select(use_labels=True). alias('foo'), base=base, **mapper_args)
def with_labels(self, item): # TODO give meaningful aliases return self.map( expression._clause_element_as_expr(item).select( use_labels=True).alias('foo'))
def __init__(self, element): self.element = _clause_element_as_expr(element)
def with_labels(self, item): # TODO give meaningful aliases return self.map(expression._clause_element_as_expr(item).select(use_labels=True).alias('foo'))
def __init__(self, *elements): self.elements = [_clause_element_as_expr(e) for e in elements]