PandaLib/pandalib-db/src/main/java/fr/pandacube/lib/db/SQLOrderBy.java

94 lines
2.4 KiB
Java
Raw Normal View History

package fr.pandacube.lib.db;
2021-03-21 12:41:43 +01:00
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
2021-03-21 12:41:43 +01:00
2022-08-01 22:21:04 +02:00
/**
* A SQL {@code ORDER BY} expression builder.
* @param <E> the table type.
*/
2021-03-21 12:41:43 +01:00
public class SQLOrderBy<E extends SQLElement<E>> {
2022-08-01 22:21:04 +02:00
/**
* Creates a new SQL {@code ORDER BY} expression builder with the provided field to sort in the ascending order.
* @param field le field to order.
* @return a new SQL {@code ORDER BY} expression builder.
* @param <E> the type of the table declaring the field.
*/
public static <E extends SQLElement<E>> SQLOrderBy<E> asc(SQLField<E, ?> field) {
return new SQLOrderBy<E>().thenAsc(field);
}
/**
* Creates a new SQL {@code ORDER BY} expression builder with the provided field to sort in the descending order.
* @param field le field to order.
* @return a new SQL {@code ORDER BY} expression builder.
* @param <E> the type of the table declaring the field.
*/
public static <E extends SQLElement<E>> SQLOrderBy<E> desc(SQLField<E, ?> field) {
return new SQLOrderBy<E>().thenDesc(field);
}
private final List<OBField<E>> orderByFields = new ArrayList<>();
private SQLOrderBy() {}
private SQLOrderBy<E> add(SQLField<E, ?> field, Direction d) {
orderByFields.add(new OBField<>(field, d));
return this;
}
/**
* Adds the provided field to sort in the ascending order, in this {@code ORDER BY} expression builder.
* @param field le field to order.
* @return this.
*/
public SQLOrderBy<E> thenAsc(SQLField<E, ?> field) {
return add(field, Direction.ASC);
}
/**
* Adds the provided field to sort in the descending order, in this {@code ORDER BY} expression builder.
* @param field le field to order.
* @return this.
*/
public SQLOrderBy<E> thenDesc(SQLField<E, ?> field) {
return add(field, Direction.DESC);
}
/* package */ String toSQL() {
return orderByFields.stream()
.map(f -> "`" + f.field.getName() + "` " + f.direction.name())
.collect(Collectors.joining(", "));
}
@Override
public String toString() {
return toSQL();
}
private record OBField<E extends SQLElement<E>>(SQLField<E, ?> field, Direction direction) { }
private enum Direction {
ASC, DESC
}
2021-03-21 12:41:43 +01:00
}