def test_joined_entity(self): query = self.session.query(self.TextItem).join( self.BlogPost, self.BlogPost.id == self.TextItem.id ) assert list(get_query_entities(query)) == [ self.TextItem, self.BlogPost ]
def test_joined_aliased_entity(self): alias = sa.orm.aliased(self.BlogPost) query = self.session.query(self.TextItem).join( alias, alias.id == self.TextItem.id ) assert list(get_query_entities(query)) == [self.TextItem, alias]
def test_with_subquery(self): number_of_articles = (sa.select([sa.func.count(self.Article.id)]).select_from(self.Article.__table__)).label( "number_of_articles" ) query = self.session.query(self.Article, number_of_articles) assert list(get_query_entities(query)) == [self.Article, number_of_articles]
def test_with_subquery(self): number_of_articles = (sa.select( [sa.func.count(self.Article.id)], ).select_from( self.Article.__table__)).label('number_of_articles') query = self.session.query(self.Article, number_of_articles) assert get_query_entities(query) == [self.Article, number_of_articles]
def test_joined_aliased_entity(self): alias = sa.orm.aliased(self.BlogPost) query = self.session.query(self.TextItem).join( alias, alias.id == self.TextItem.id ) assert get_query_entities(query) == [self.TextItem, alias]
def test_joined_entity(self, session, TextItem, BlogPost): query = session.query(TextItem).join( BlogPost, BlogPost.id == TextItem.id ) assert get_query_entities(query) == [ TextItem, sa.inspect(BlogPost) ]
def test_joined_entity(self): query = self.session.query(self.TextItem).join( self.BlogPost, self.BlogPost.id == self.TextItem.id ) assert list(get_query_entities(query)) == [ self.TextItem, sa.inspect(self.BlogPost) ]
def test_joined_aliased_entity(self, session, TextItem, BlogPost): alias = sa.orm.aliased(BlogPost) query = session.query(TextItem).join( alias, alias.id == TextItem.id ) assert get_query_entities(query) == [TextItem, alias]
def test_with_subquery(self, session, Article): number_of_articles = ( sa.select( [sa.func.count(Article.id)], ) .select_from( Article.__table__ ) ).label('number_of_articles') query = session.query(Article, number_of_articles) assert get_query_entities(query) == [ Article, number_of_articles ]
def test_aliased_entity(self, session, Article): alias = sa.orm.aliased(Article) query = session.query(alias) assert get_query_entities(query) == [alias]
def test_mapper(self): query = self.session.query(sa.inspect(self.TextItem)) assert list(get_query_entities(query)) == [sa.inspect(self.TextItem)]
def test_column(self): query = self.session.query(self.TextItem.__table__.c.id) assert get_query_entities(query) == [self.TextItem.__table__]
def test_column_entity_with_label(self): query = self.session.query(self.Article.id.label('id')) assert get_query_entities(query) == [self.Article]
def test_aliased_entity(self): alias = sa.orm.aliased(self.Article) query = self.session.query(alias) assert list(get_query_entities(query)) == [alias]
def test_entity(self): query = self.session.query(self.TextItem) assert get_query_entities(query) == [self.TextItem]
def test_column(self): query = self.session.query(self.TextItem.__table__.c.id) assert list(get_query_entities(query)) == [self.TextItem.__table__]
def test_mapper(self, session, TextItem): query = session.query(sa.inspect(TextItem)) assert get_query_entities(query) == [TextItem]
def test_entity(self): query = self.session.query(self.TextItem) assert list(get_query_entities(query)) == [self.TextItem]
def test_entity(self, session, TextItem): query = session.query(TextItem) assert get_query_entities(query) == [TextItem]
def test_aliased_selectable(self, session, TextItem, BlogPost): selectable = sa.orm.with_polymorphic(TextItem, [BlogPost]) query = session.query(selectable) assert get_query_entities(query) == [selectable]
def test_column(self, session, TextItem): query = session.query(TextItem.__table__.c.id) assert get_query_entities(query) == [TextItem.__table__]
def test_instrumented_attribute(self, session, TextItem): query = session.query(TextItem.id) assert get_query_entities(query) == [TextItem]
def test_instrumented_attribute(self): query = self.session.query(self.TextItem.id) assert list(get_query_entities(query)) == [sa.inspect(self.TextItem)]
def test_column_entity_with_label(self, session, Article): query = session.query(Article.id.label('id')) assert get_query_entities(query) == [Article]
def test_aliased_selectable(self): selectable = sa.orm.with_polymorphic(self.TextItem, [self.BlogPost]) query = self.session.query(selectable) assert list(get_query_entities(query)) == [selectable]
def test_column_entity_with_label(self): query = self.session.query(self.Article.id.label('id')) assert list(get_query_entities(query)) == [sa.inspect(self.Article)]
def test_mapper(self): query = self.session.query(sa.inspect(self.TextItem)) assert get_query_entities(query) == [self.TextItem]
def test_aliased_entity(self): alias = sa.orm.aliased(self.Article) query = self.session.query(alias) assert get_query_entities(query) == [alias]
def test_instrumented_attribute(self): query = self.session.query(self.TextItem.id) assert get_query_entities(query) == [self.TextItem]
def _build_criterion(query): main_decl_class = None decl_class_by_tablename = {} # get main decl class and aliases using in query for entity in sau.get_query_entities(query): if isinstance(entity, sa.orm.util.AliasedClass): name = sa.inspect(entity).name elif isinstance(entity, sa.orm.Mapper): continue else: name = sau.functions.get_mapper(entity).tables[0].name if main_decl_class is None: main_decl_class = entity decl_class_by_tablename[name] = entity # get other decl classes base = sau.functions.get_declarative_base(main_decl_class) for c in base._decl_class_registry.values(): if hasattr(c, '__tablename__' ) and c.__tablename__ not in decl_class_by_tablename: decl_class_by_tablename[c.__tablename__] = c def _criterion(attr, op, value): op = op.lower() if '.' in attr: (tablename, attr) = attr.split('.', maxsplit=1) if tablename in decl_class_by_tablename: class_attr = getattr(decl_class_by_tablename[tablename], attr, None) else: raise KeyError('not found declarative class: %s' % tablename) else: class_attr = getattr(main_decl_class, attr, None) if class_attr is None: raise AttributeError( 'main declarative class does not have attribute: %s' % attr) if op == '==': return class_attr == value elif op == '!=': return class_attr != value elif op == '>=': return class_attr >= value elif op == '>': return class_attr > value elif op == '<=': return class_attr <= value elif op == '<': return class_attr < value elif op == 'in': if not isinstance(value, list): value = [value] return class_attr.in_(value) elif op == '!in': if not isinstance(value, list): value = [value] return ~class_attr.in_(value) elif op == 'like': return class_attr.like(value) elif op == '!like': return ~class_attr.like(value) else: raise ValueError('invalid op: %s' % op) return _criterion
def test_aliased_selectable(self): selectable = sa.orm.with_polymorphic(self.TextItem, [self.BlogPost]) query = self.session.query(selectable) assert get_query_entities(query) == [selectable]
def test_joined_aliased_entity(self, session, TextItem, BlogPost): alias = sa.orm.aliased(BlogPost) query = session.query(TextItem).join(alias, alias.id == TextItem.id) assert get_query_entities(query) == [TextItem, alias]
def construct_filter(query, items, conjunction='and'): """ Construct a filter from a combination of filter items Args: query (sqlalchemy.orm.query.Query): The SQLAlchemy SQL ORM object items (list[str]): List of query expressions in form of "[KEY][OPERATOR][VALUE...]" conjunction (str): Combine the filter items using 'and' or 'or' Returns: sqlalchemy.orm.query.Query: A query with filter items applied Raises: KeyError: Raise if column given in filter expression does not exist """ # Find table model entities = sau.get_query_entities(query) if len(entities) > 1: logger.warning('Using first entity from search query ({})' .format(entities[0])) table = entities[0] base = sau.get_declarative_base(table) # Parse expressions items = [_convert_expression_operator(item) for item in items] exprs = [_parse_expression_filter(item) for item in items] filters = [] joins = set([]) for key, operator, value in exprs: # Preprocess case of linked table if '.' in key: _tablename, key = key.split('.', maxsplit=1) _table = _tablename_to_class(base, _tablename) joins.update([t for t in _link_path(TABLES_GRAPH, table, _table) if t is not table]) else: _table = table column = getattr(_table, key, None) if column is None: raise KeyError('Cannot construct filter: column "{}" does not ' 'exist'.format(key)) if operator.lower() == 'in': value = [convert_query_type(column, v) for v in value.replace(' ', ',').split(',') if v] filter_item = column.in_(value) else: attr = [pattern % operator for pattern in ('%s', '%s_', '__%s__') if hasattr(column, pattern % operator)] if not attr: raise KeyError('Cannot construct filter: column {} is not ' 'usable with "{}"'.format(key, operator)) if value.lower() in ('null', 'none', 'na', 'nan'): value = None else: value = convert_query_type(column, value) filter_item = getattr(column, attr[0])(value) filters.append(filter_item) for join_table in joins: query = query.join(join_table) if conjunction == 'or': return query.filter(sa.or_(*filters)) else: return query.filter(sa.and_(*filters))
def test_joined_entity(self, session, TextItem, BlogPost): query = session.query(TextItem).join(BlogPost, BlogPost.id == TextItem.id) assert get_query_entities(query) == [TextItem, sa.inspect(BlogPost)]