Skip to content

Instantly share code, notes, and snippets.

@abetkin
Created June 12, 2020 08:59
Show Gist options
  • Save abetkin/3681914df4015a10a83a77fc231e08f5 to your computer and use it in GitHub Desktop.
Save abetkin/3681914df4015a10a83a77fc231e08f5 to your computer and use it in GitHub Desktop.
def exec_query(query_def: str, globals, locals, left_join=False):
cells = None
tree = parse(query_def)
tree = tree.node.nodes[0].expr.code
tree, extractors = create_extractors(tree, globals, locals, special_functions, const_functions)
vars, vartypes = extract_vars(extractors, globals, locals, cells)
node = tree.quals[0].iter
varkey = node.src
origin = vars[varkey]
database = origin._database_
database.provider.normalize_vars(vars, vartypes)
translator_cls = database.provider.translator_cls
translator = translator_cls(tree, None, extractors, vars, vartypes.copy(), left_join=left_join)
sql_ast, attr_offsets = translator.construct_sql_ast()
sql, adapter = database.provider.ast2sql(sql_ast)
arguments = adapter(vars)
cursor = database._exec_sql(sql, arguments)
assert isinstance(translator.expr_type, EntityMeta)
entity = translator.expr_type
items = entity._fetch_objects(cursor, attr_offsets)
return items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment