All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.chengyuxing.sql.dsl.clause.OrderBy Maven / Gradle / Ivy

Go to download

Light wrapper of JDBC, support ddl, dml, query, plsql/procedure/function, transaction and manage sql file.

There is a newer version: 9.0.2
Show newest version
package com.github.chengyuxing.sql.dsl.clause;

import com.github.chengyuxing.common.tuple.Pair;
import com.github.chengyuxing.sql.dsl.types.FieldReference;
import com.github.chengyuxing.sql.dsl.types.OrderByType;
import org.jetbrains.annotations.NotNull;

import java.util.*;

/**
 * Order by clause.
 *
 * @param  entity type
 */
public abstract class OrderBy extends ColumnHelper {
    protected Set> orders = new LinkedHashSet<>();

    /**
     * Construct a new Order by builder.
     *
     * @param clazz entity type
     */
    public OrderBy(@NotNull Class clazz) {
        super(clazz);
    }

    /**
     * Construct a new Order by builder with initial Order by builder.
     *
     * @param clazz entity class
     * @param other order by builder
     */
    public OrderBy(@NotNull Class clazz, @NotNull OrderBy other) {
        super(clazz);
        this.orders = other.orders;
    }

    /**
     * Order by.
     *
     * @param column column
     * @param order  order by type
     * @return order by builder
     */
    public OrderBy by(@NotNull String column, OrderByType order) {
        orders.add(Pair.of(column, order));
        return this;
    }

    /**
     * Order by.
     *
     * @param column column
     * @param order  order by type
     * @return order by builder
     */
    public OrderBy by(FieldReference column, OrderByType order) {
        return by(getColumnName(column), order);
    }

    /**
     * {@code asc}
     *
     * @param column column
     * @return order by builder
     */
    public OrderBy asc(FieldReference column) {
        orders.add(Pair.of(getColumnName(column), OrderByType.ASC));
        return this;
    }

    /**
     * {@code desc}
     *
     * @param column column
     * @return order by builder
     */
    public OrderBy desc(FieldReference column) {
        orders.add(Pair.of(getColumnName(column), OrderByType.DESC));
        return this;
    }

    protected final String build() {
        if (orders.isEmpty()) {
            return "";
        }
        StringJoiner joiner = new StringJoiner(", ");
        for (Pair order : orders) {
            if (isIllegalColumn(order.getItem1())) {
                throw new IllegalArgumentException("unexpected column: '" + order.getItem1() + "' on order by: " + order);
            }
            joiner.add(order.getItem1() + " " + order.getItem2().name().toLowerCase());
        }
        return "\norder by " + joiner;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy