def _build_query(self, table, tree, visitor):
kwargs = {}<EOL>index = None<EOL>if tree.using:<EOL><INDENT>index_name = kwargs["<STR_LIT:index>"] = tree.using[<NUM_LIT:1>]<EOL>index = table.get_index(index_name)<EOL><DEDENT>if tree.where:<EOL><INDENT>constraints = ConstraintExpression.from_where(tree.where)<EOL>possible_hash = constraints.possible_hash_fields()<EOL>possible_range = constraints.possible_range_fields()<EOL>if index is None:<EOL><INDENT>indexes = table.get_matching_indexes(possible_hash, possible_range)<EOL>if not indexes:<EOL><INDENT>action = "<STR_LIT>"<EOL>kwargs["<STR_LIT>"] = constraints.build(visitor)<EOL>kwargs["<STR_LIT>"] = visitor.expression_values<EOL>kwargs["<STR_LIT>"] = visitor.attribute_names<EOL><DEDENT>elif len(indexes) == <NUM_LIT:1>:<EOL><INDENT>index = indexes[<NUM_LIT:0>]<EOL>action = "<STR_LIT>"<EOL>add_query_kwargs(kwargs, visitor, constraints, index)<EOL><DEDENT>else:<EOL><INDENT>names = "<STR_LIT:U+002CU+0020>".join([index.name for index in indexes])<EOL>raise SyntaxError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT:%s>" % names<EOL>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if index.hash_key in possible_hash:<EOL><INDENT>action = "<STR_LIT>"<EOL>add_query_kwargs(kwargs, visitor, constraints, index)<EOL><DEDENT>else:<EOL><INDENT>action = "<STR_LIT>"<EOL>if not index.scannable:<EOL><INDENT>raise SyntaxError("<STR_LIT>" % index_name)<EOL><DEDENT>kwargs["<STR_LIT>"] = constraints.build(visitor)<EOL>kwargs["<STR_LIT>"] = visitor.expression_values<EOL>kwargs["<STR_LIT>"] = visitor.attribute_names<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>action = "<STR_LIT>"<EOL><DEDENT>return [action, kwargs, index]<EOL>
Build a scan/query from a statement