def post_BINARY_OPERATE(self, operator, left, right): left_sql_piece, left_params = self.translate(left) right_sql_piece, right_params = self.translate(right) ops = ('UNARY_OPERATE', 'BINARY_OPERATE') if is_sql_ast(left) and car(left) in ops: _cmp = compare_operator_precedence( left[1], operator ) if _cmp < 0: left_sql_piece = '({})'.format(left_sql_piece) # noqa pragma: no cover if is_sql_ast(right): right_car = car(right) if right_car in ops: _cmp = compare_operator_precedence( right[1], operator ) if _cmp < 0: right_sql_piece = '({})'.format(right_sql_piece) # noqa pragma: no cover elif right_car == 'SELECT': right_sql_piece = '({})'.format(right_sql_piece) return ( '{} {} {}'.format( left_sql_piece, operator, right_sql_piece ), left_params + right_params )
def translate(self, sql_ast): if not is_sql_ast(sql_ast): return sql_ast, [] head = car(sql_ast) tail = cdr(sql_ast) method_name = 'post_{}'.format('_'.join(head.split(' '))) method = getattr(self, method_name, None) if method is None: raise NotImplementedError(method_name) return method(*tail) # pylint: disable=not-callable
def translate(self, sql_ast: AST) -> Tuple[str, List]: if not is_sql_ast(sql_ast): raise ORMError(f'{friendly_repr(sql_ast)} is not a valid sql ast!') alias_mapping = None if not context.in_sql_translation: sql_ast, alias_mapping = detect_table_alias(sql_ast) head = car(sql_ast) tail = cdr(sql_ast) method_name = 'post_{}'.format('_'.join(head.split(' '))) method = getattr(self, method_name, None) if method is None: raise NotImplementedError(method_name) with in_sql_translation_context(): if alias_mapping: with table_alias_mapping_context(alias_mapping): return method(*tail) # pylint: disable=not-callable return method(*tail) # pylint: disable=not-callable