

def generate_select(self, query, alias_map=None):
    model = query.model_class
    db = model._meta.database
    alias_map = self.calculate_alias_map(query, alias_map)
    if isinstance(query, CompoundSelect):
        clauses = [_StripParens(query)]
    else:
        if (not query._distinct):
            clauses = [SQL('SELECT')]
        else:
            clauses = [SQL('SELECT DISTINCT')]
            if (query._distinct not in (True, False)):
                clauses += [SQL('ON'), EnclosedClause(*query._distinct)]
        select_clause = Clause(*query._select)
        select_clause.glue = ', '
        clauses.extend((select_clause, SQL('FROM')))
        if (query._from is None):
            clauses.append(model.as_entity().alias(alias_map[model]))
        else:
            clauses.append(CommaClause(*query._from))
    if (query._windows is not None):
        clauses.append(SQL('WINDOW'))
        clauses.append(CommaClause(*[Clause(SQL(window._alias), SQL('AS'), window.__sql__()) for window in query._windows]))
    join_clauses = self.generate_joins(query._joins, model, alias_map)
    if join_clauses:
        clauses.extend(join_clauses)
    if (query._where is not None):
        clauses.extend([SQL('WHERE'), query._where])
    if query._group_by:
        clauses.extend([SQL('GROUP BY'), CommaClause(*query._group_by)])
    if query._having:
        clauses.extend([SQL('HAVING'), query._having])
    if query._order_by:
        clauses.extend([SQL('ORDER BY'), CommaClause(*query._order_by)])
    if ((query._limit is not None) or (query._offset and db.limit_max)):
        limit = (query._limit if (query._limit is not None) else db.limit_max)
        clauses.append(SQL(('LIMIT %s' % limit)))
    if (query._offset is not None):
        clauses.append(SQL(('OFFSET %s' % query._offset)))
    (for_update, no_wait) = query._for_update
    if for_update:
        stmt = ('FOR UPDATE NOWAIT' if no_wait else 'FOR UPDATE')
        clauses.append(SQL(stmt))
    return self.build_query(clauses, alias_map)

dataset/ETHPy150Open coleifer/peewee/peewee.py QueryCompiler.generate_select/original