本文整理汇总了Python中query.Query.select_clause方法的典型用法代码示例。如果您正苦于以下问题:Python Query.select_clause方法的具体用法?Python Query.select_clause怎么用?Python Query.select_clause使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类query.Query
的用法示例。
在下文中一共展示了Query.select_clause方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convert_correlated_collection_to_inline_view
# 需要导入模块: from query import Query [as 别名]
# 或者: from query.Query import select_clause [as 别名]
def convert_correlated_collection_to_inline_view(self, original_collection):
'''Converts the given collection to an inline view. The collection must be correlated,
ie. it must have an aliased ancestor. This function is able to handle cases where
there are unaliased collections between the given collection and the aliased
ancestor.
For example,
SELECT ...
FROM customer t1 INNER JOIN t1.orders.lineitem
should be converted to:
SELECT ...
FROM customer t1 INNER JOIN LATERAL (
SELECT
t2.*
FROM
customer.orders tmp_alias_1
INNER JOIN customer.orders.lineitems t2 ON ([tmp_alias_1 is parent of t2])
WHERE
[t1 is parent of tmp_alias_1]) t2 ON True
This function does not add a where clause to the inline view in order to connect
the first table expression in the from clause with it's parent. This is done in
flatten_from_clause.
'''
def replace_col(container, new_col):
for i, col in enumerate(container._cols):
if col.name == new_col.name:
container._cols[i] = new_col
new_col.owner = container
return
def create_inner_join(parent, child):
predicate = self.create_join_predicate(parent, child)
join_clause = JoinClause('INNER', child)
join_clause.boolean_expr = predicate
self.for_flattening.add(join_clause)
return join_clause
query = Query()
query.select_clause = SelectClause(None)
query.select_clause.star_prefix = original_collection.alias
# Create a list containing original_collection along with all of it's unaliased
# ancestors
cur = original_collection
all_from_elements = []
while True:
all_from_elements.append(cur)
if cur.owner.alias:
break
else:
cur = cur.owner
all_from_elements.reverse()
num_collections = sum(1 for e in all_from_elements if isinstance(e, CollectionColumn))
if num_collections == 1:
query.from_clause = FromClause(original_collection)
elif num_collections > 1:
# Add multiple elements to the from clause which are joined together.
for i in range(len(all_from_elements)):
if isinstance(all_from_elements[i], CollectionColumn):
first_collection = deepcopy(all_from_elements[i])
first_collection.alias = self.get_tmp_alias()
all_from_elements = all_from_elements[i + 1:]
break
query.from_clause = FromClause(first_collection)
prev = first_collection
cur = first_collection
for table_expr in all_from_elements[:-1]:
cur = cur.get_col_by_name(table_expr.name)
if isinstance(cur, CollectionColumn):
join_clause = create_inner_join(prev, cur)
join_clause.table_expr.alias = self.get_tmp_alias()
query.from_clause.join_clauses.append(join_clause)
prev = cur
# The last original_collection to be the original one.
replace_col(cur, original_collection)
join_clause = create_inner_join(prev, original_collection)
query.from_clause.join_clauses.append(join_clause)
else:
# num_collections < 1
assert False
inline_view = InlineView(query)
inline_view.alias = original_collection.alias
return inline_view