<commit_before>package com.avairebot.orion.database.grammar.mysql;

import com.avairebot.orion.contracts.database.grammar.SelectGrammar;
import com.avairebot.orion.database.query.*;

import java.util.List;

public class Select extends SelectGrammar {
    @Override
    public String format(QueryBuilder builder) {
        buildColumns(builder);

        buildJoins(builder);

        buildWhereClause(builder);

        return finalize(builder);
    }

    private void buildColumns(QueryBuilder builder) {
        if (builder.getColumns().size() == 1) {
            String column = builder.getColumns().get(0);

            if (column.equals("*")) {
                query += "*";
            } else if (column.startsWith("RAW:")) {
                query += column.substring(4);
            }
        } else {
            builder.getColumns().stream().forEach((column) -> {
                query += formatField(column) + ", ";
            });

            removeLast(2);
        }

        query += String.format(" FROM %s ", formatField(builder.getTable()));
    }

    private void buildJoins(QueryBuilder builder) {
        List<JoinClause> joins = builder.getJoins();

        for (JoinClause join : joins) {
            if (join.clauses.isEmpty()) {
                continue;
            }

            addPart(String.format(" %s JOIN %s ON ", join.type.toUpperCase(), formatField(join.table)));

            int orderLength = 0;

            for (Clause clause : join.clauses) {
                String string = String.format(" %s %s %s",
                        formatField(clause.getOne()), clause.getIdentifier(), formatField((String) clause.getTwo())
                );

                if (clause.getOrder() == null) {
                    clause.setOrder(OperatorType.AND);
                }

                String operator = clause.getOrder().getOperator();

                orderLength = operator.length() + 2;
                addPart(String.format(string + " %s ", operator));
            }

            if (orderLength > 0) {
                removeLast(orderLength);
            }
        }
    }

    @Override
    protected String finalize(QueryBuilder builder) {
        if (!builder.getOrder().isEmpty()) {
            addPart(" ORDER BY ");

            for (QueryOrder order : builder.getOrder()) {
                if (order.getField() == null) {
                    continue;
                }

                if (order.isRawSQL()) {
                    addPart(" %s , ", order.getField());

                    continue;
                }

                if (order.getType() != null) {
                    addPart(" %s %s, ", formatField(order.getField()), order.getType().toUpperCase());
                }
            }

            removeLast(2);
        }

        if (builder.getTake() <= 0) {
            return query.trim() + ";";
        }

        if (builder.getTake() > 0) {
            addPart(String.format(" LIMIT %d", builder.getTake()));

            // The skip clause is placed inside the limit statement because LIMIT is 
            // required for the OFFSET to be regonized by the SQL server, placing
            // it outside will throw a Syntex Exception due to the missing limit.
            if (builder.getSkip() > 0) {
                addPart(String.format(" OFFSET %d", builder.getSkip()));
            }
        }

        addPart(";");

        return query;
    }
}<commit_msg>Add support for single select colums in MySQl select grammar<commit_after>package com.avairebot.orion.database.grammar.mysql;

import com.avairebot.orion.contracts.database.grammar.SelectGrammar;
import com.avairebot.orion.database.query.*;

import java.util.List;

public class Select extends SelectGrammar {
    @Override
    public String format(QueryBuilder builder) {
        buildColumns(builder);

        buildJoins(builder);

        buildWhereClause(builder);

        return finalize(builder);
    }

    private void buildColumns(QueryBuilder builder) {
        if (builder.getColumns().isEmpty() || builder.getColumns().size() == 1 && builder.getColumns().get(0).equals("*")) {
            query += String.format("* FROM %s ", formatField(builder.getTable()));
            return;
        }

        builder.getColumns().stream().forEach((column) -> {
            if (column.startsWith("RAW:")) {
                query += String.format("%s, ", column.substring(4));
                return;
            }
            query += formatField(column) + ", ";
        });

        removeLast(2);

        query += String.format(" FROM %s ", formatField(builder.getTable()));
    }

    private void buildJoins(QueryBuilder builder) {
        List<JoinClause> joins = builder.getJoins();

        for (JoinClause join : joins) {
            if (join.clauses.isEmpty()) {
                continue;
            }

            addPart(String.format(" %s JOIN %s ON ", join.type.toUpperCase(), formatField(join.table)));

            int orderLength = 0;

            for (Clause clause : join.clauses) {
                String string = String.format(" %s %s %s",
                        formatField(clause.getOne()), clause.getIdentifier(), formatField((String) clause.getTwo())
                );

                if (clause.getOrder() == null) {
                    clause.setOrder(OperatorType.AND);
                }

                String operator = clause.getOrder().getOperator();

                orderLength = operator.length() + 2;
                addPart(String.format(string + " %s ", operator));
            }

            if (orderLength > 0) {
                removeLast(orderLength);
            }
        }
    }

    @Override
    protected String finalize(QueryBuilder builder) {
        if (!builder.getOrder().isEmpty()) {
            addPart(" ORDER BY ");

            for (QueryOrder order : builder.getOrder()) {
                if (order.getField() == null) {
                    continue;
                }

                if (order.isRawSQL()) {
                    addPart(" %s , ", order.getField());

                    continue;
                }

                if (order.getType() != null) {
                    addPart(" %s %s, ", formatField(order.getField()), order.getType().toUpperCase());
                }
            }

            removeLast(2);
        }

        if (builder.getTake() <= 0) {
            return query.trim() + ";";
        }

        if (builder.getTake() > 0) {
            addPart(String.format(" LIMIT %d", builder.getTake()));

            // The skip clause is placed inside the limit statement because LIMIT is
            // required for the OFFSET to be regonized by the SQL server, placing
            // it outside will throw a Syntex Exception due to the missing limit.
            if (builder.getSkip() > 0) {
                addPart(String.format(" OFFSET %d", builder.getSkip()));
            }
        }

        addPart(";");

        return query;
    }
}
{'fraction_non_alphanumeric': 0.12880091795754448, 'fraction_numerical': 0.003155479059093517, 'mean_word_length': 1.6473044798785117, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3835846', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1778, 'n_words': 405}