def _get_primitive_sql_ast(self, base_sql_ast):
    sql_ast = list(base_sql_ast)
    if (self._expression is not None):
        sql_ast.append(['WHERE', self._expression.get_sql_ast()])
    if ((self._having_expression is not None) and (not self._group_by)):
        group_by = []
        for entity in self._entities:
            if (entity is self._model_class):
                pk = self._model_class.get_singleness_pk_field()
                group_by.append(pk)
                break
            if isinstance(entity, Field):
                group_by.append(entity)
        self._group_by = group_by
    if self._group_by:
        entities = self._get_entities(self._group_by)
        field_names = {getattr(f, 'name', '') for f in entities}
        pk = self._model_class.get_singleness_pk_field()
        if (self._entities_contains(pk) and (pk.name not in field_names)):
            entities.append(pk)
        sql_ast.append(['GROUP BY', (['SERIES'] + [f.get_sql_ast() for f in entities])])
    if (self._having_expression is not None):
        sql_ast.append(['HAVING', self._having_expression.get_sql_ast()])
    if self._order_by:
        sql_ast.append(['ORDER BY', (['SERIES'] + [f.get_sql_ast() for f in self._order_by])])
    if (self._limit is not None):
        limit_section = ['LIMIT', None, ['VALUE', self._limit]]
        if ((self._offset is not None) and (self._offset != 0)):
            limit_section[1] = ['VALUE', self._offset]
        sql_ast.append(limit_section)
    if self._for_update:
        sql_ast.append(['FOR UPDATE'])
    return sql_ast
def _get_primitive_sql_ast(self, base_sql_ast):
    sql_ast = list(base_sql_ast)
    if (self._expression is not None):
        sql_ast.append(['WHERE', self._expression.get_sql_ast()])
    if ((self._having_expression is not None) and (not self._group_by)):
        group_by = []
        for entity in self._entities:
            if (entity is self._model_class):
                pk = self._model_class.get_singleness_pk_field()
                group_by.append(pk)
                break
            if isinstance(entity, Field):
                group_by.append(entity)
        self._group_by = group_by
    if self._group_by:
        entities = self._get_entities(self._group_by)
        field_names = {getattr(f, 'name', ) for f in entities}
        pk = self._model_class.get_singleness_pk_field()
        if (self._entities_contains(pk) and (pk.name not in field_names)):
            entities.append(pk)
        sql_ast.append(['GROUP BY', (['SERIES'] + [f.get_sql_ast() for f in entities])])
    if (self._having_expression is not None):
        sql_ast.append(['HAVING', self._having_expression.get_sql_ast()])
    if self._order_by:
        sql_ast.append(['ORDER BY', (['SERIES'] + [f.get_sql_ast() for f in self._order_by])])
    if (self._limit is not None):
        limit_section = ['LIMIT', None, ['VALUE', self._limit]]
        if ((self._offset is not None) and (self._offset != 0)):
            limit_section[1] = ['VALUE', self._offset]
        sql_ast.append(limit_section)
    if self._for_update:
        sql_ast.append(['FOR UPDATE'])
    return sql_ast