Beispiel #1
0
    def _get_base_sql_ast(self, modifier=None, entities=None):
        entities = self._entities if entities is None else entities

        if self._join:
            table_section = [
                'JOIN', ['TABLE', self.table_name],
                ['TABLE', self._join._get_table_name()]
            ]
        elif self._left_join:
            table_section = [
                'LEFT JOIN', ['TABLE', self.table_name],
                ['TABLE', self._left_join._get_table_name()]
            ]
        elif self._right_join:
            table_section = [
                'RIGHT JOIN', ['TABLE', self.table_name],
                ['TABLE', self._right_join._get_table_name()]
            ]
        else:
            table_section = ['TABLE', self.table_name]

        rev_alias_mapping = {}
        table_section = _detect_table_alias(
            table_section, rev_alias_mapping=rev_alias_mapping)

        alias_mapping = {v: k for k, v in sorted(iteritems(rev_alias_mapping))}

        with table_alias_mapping_context(alias_mapping):
            select_ast = [
                'SERIES',
            ] + [
                e.get_sql_ast() if hasattr(e, 'get_sql_ast') else e
                for e in entities
            ]
            if len(select_ast) == 2 and select_ast[1][0] == 'SERIES':
                select_ast = select_ast[1]
            if modifier is not None:
                select_ast = ['MODIFIER', modifier, select_ast]

        sql_ast = ['SELECT']
        sql_ast.append(select_ast)
        sql_ast.append(['FROM', table_section])
        return sql_ast, alias_mapping
Beispiel #2
0
    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
Beispiel #3
0
 def get_primitive_sql_ast(self, base_sql_ast=None, alias_mapping=None):
     if base_sql_ast is None:
         base_sql_ast, alias_mapping = self._get_base_sql_ast()
     alias_mapping = alias_mapping or {}
     with table_alias_mapping_context(alias_mapping):
         return self._get_primitive_sql_ast(base_sql_ast)