MCPcopy
hub / github.com/ponyorm/pony / construct_sql_ast

Method construct_sql_ast

pony/orm/sqltranslation.py:671–785  ·  view source on GitHub ↗
(translator, limit=None, offset=None, distinct=None,
                          aggr_func_name=None, aggr_func_distinct=None, sep=None,
                          for_update=False, nowait=False, skip_locked=False, is_not_null_checks=False)

Source from the content-addressed store, hash-verified

669
670 return [ 'SELECT', select_ast, from_ast, where_ast ] + other_ast
671 def construct_sql_ast(translator, limit=None, offset=None, distinct=None,
672 aggr_func_name=None, aggr_func_distinct=None, sep=None,
673 for_update=False, nowait=False, skip_locked=False, is_not_null_checks=False):
674 attr_offsets = None
675 if distinct is None:
676 if not translator.order:
677 distinct = translator.distinct
678 ast_transformer = lambda ast: ast
679 if for_update:
680 sql_ast = [ 'SELECT_FOR_UPDATE', nowait, skip_locked ]
681 translator.query_result_is_cacheable = False
682 else: sql_ast = [ 'SELECT' ]
683
684 select_ast = [ 'DISTINCT' if distinct else 'ALL' ] + translator.expr_columns
685 if aggr_func_name:
686 expr_type = translator.expr_type
687 if isinstance(expr_type, EntityMeta):
688 if aggr_func_name == 'GROUP_CONCAT':
689 if expr_type._pk_is_composite_:
690 throw(TypeError, "`group_concat` cannot be used with entity with composite primary key")
691 elif aggr_func_name != 'COUNT': throw(TypeError,
692 'Attribute should be specified for %r aggregate function' % aggr_func_name.lower())
693 elif isinstance(expr_type, tuple):
694 if aggr_func_name != 'COUNT': throw(TypeError,
695 'Single attribute should be specified for %r aggregate function' % aggr_func_name.lower())
696 else:
697 if aggr_func_name in ('SUM', 'AVG') and expr_type not in numeric_types:
698 throw(TypeError, '%r is valid for numeric attributes only' % aggr_func_name.lower())
699 assert len(translator.expr_columns) == 1
700 aggr_ast = None
701 if translator.groupby_monads or (
702 aggr_func_name == 'COUNT' and distinct
703 and isinstance(translator.expr_type, EntityMeta)
704 and len(translator.expr_columns) > 1):
705 outer_alias = 't'
706 if aggr_func_name == 'COUNT' and not aggr_func_distinct:
707 outer_aggr_ast = [ 'COUNT', None ]
708 else:
709 assert len(translator.expr_columns) == 1
710 expr_ast = translator.expr_columns[0]
711 if expr_ast[0] == 'COLUMN':
712 outer_alias, column_name = expr_ast[1:]
713 outer_aggr_ast = [aggr_func_name, aggr_func_distinct, ['COLUMN', outer_alias, column_name]]
714 if aggr_func_name == 'GROUP_CONCAT' and sep is not None:
715 outer_aggr_ast.append(['VALUE', sep])
716 else:
717 select_ast = [ 'DISTINCT' if distinct else 'ALL' ] + [ [ 'AS', expr_ast, 'expr' ] ]
718 outer_aggr_ast = [ aggr_func_name, aggr_func_distinct, [ 'COLUMN', 't', 'expr' ] ]
719 if aggr_func_name == 'GROUP_CONCAT' and sep is not None:
720 outer_aggr_ast.append(['VALUE', sep])
721 def ast_transformer(ast):
722 return [ 'SELECT', [ 'AGGREGATES', outer_aggr_ast ],
723 [ 'FROM', [ outer_alias, 'SELECT', ast[1:] ] ] ]
724 else:
725 if aggr_func_name == 'COUNT':
726 if isinstance(expr_type, (tuple, EntityMeta)) and not distinct and not aggr_func_distinct:
727 aggr_ast = [ 'COUNT', aggr_func_distinct ]
728 else:

Callers 2

Calls 6

throwFunction · 0.90
combine_limit_and_offsetFunction · 0.85
appendMethod · 0.80
extendMethod · 0.80
getsqlMethod · 0.45

Tested by

no test coverage detected