2019-10-26 23:15:49 +02:00
|
|
|
package fr.pandacube.util.orm;
|
2017-09-13 01:05:10 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-03-05 20:04:34 +01:00
|
|
|
public class SQLOrderBy<E extends SQLElement<E>> {
|
2017-09-13 01:05:10 +02:00
|
|
|
|
|
|
|
private List<OBField> orderByFields = new ArrayList<>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construit une nouvelle clause ORDER BY
|
|
|
|
*/
|
2020-02-27 02:06:38 +01:00
|
|
|
private SQLOrderBy() {}
|
2017-09-13 01:05:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ajoute un champ dans la clause ORDER BY en construction
|
|
|
|
*
|
|
|
|
* @param field le champ SQL à ordonner
|
|
|
|
* @param d le sens de tri (croissant ASC ou décroissant DESC)
|
|
|
|
* @return l'objet courant (permet de chainer les ajouts de champs)
|
|
|
|
*/
|
2020-03-05 20:04:34 +01:00
|
|
|
private SQLOrderBy<E> add(SQLField<E, ?> field, Direction d) {
|
2017-09-13 01:05:10 +02:00
|
|
|
orderByFields.add(new OBField(field, d));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-27 02:06:38 +01:00
|
|
|
* Ajoute un champ dans la clause ORDER BY en construction avec pour direction ASC
|
2017-09-13 01:05:10 +02:00
|
|
|
*
|
2020-02-27 02:06:38 +01:00
|
|
|
* @param field le champ SQL à ordonner
|
2017-09-13 01:05:10 +02:00
|
|
|
* @return l'objet courant (permet de chainer les ajouts de champs)
|
|
|
|
*/
|
2020-03-05 20:04:34 +01:00
|
|
|
public SQLOrderBy<E> thenAsc(SQLField<E, ?> field) {
|
2017-09-15 01:33:45 +02:00
|
|
|
return add(field, Direction.ASC);
|
2017-09-13 01:05:10 +02:00
|
|
|
}
|
|
|
|
|
2020-02-27 02:06:38 +01:00
|
|
|
/**
|
|
|
|
* Ajoute un champ dans la clause ORDER BY en construction avec pour direction DESC
|
|
|
|
*
|
|
|
|
* @param field le champ SQL à ordonner
|
|
|
|
* @return l'objet courant (permet de chainer les ajouts de champs)
|
|
|
|
*/
|
2020-03-05 20:04:34 +01:00
|
|
|
public SQLOrderBy<E> thenDesc(SQLField<E, ?> field) {
|
2020-02-27 02:06:38 +01:00
|
|
|
return add(field, Direction.DESC);
|
|
|
|
}
|
|
|
|
|
2017-09-13 01:05:10 +02:00
|
|
|
/* package */ String toSQL() {
|
|
|
|
String ret = "";
|
|
|
|
boolean first = true;
|
|
|
|
for (OBField f : orderByFields) {
|
|
|
|
if (!first) ret += ", ";
|
|
|
|
first = false;
|
2019-07-18 17:37:27 +02:00
|
|
|
ret += "`" + f.field.getName() + "` " + f.direction.name();
|
2017-09-13 01:05:10 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return toSQL();
|
|
|
|
}
|
|
|
|
|
|
|
|
private class OBField {
|
2020-03-05 20:04:34 +01:00
|
|
|
public final SQLField<E, ?> field;
|
2017-09-13 01:05:10 +02:00
|
|
|
public final Direction direction;
|
|
|
|
|
2020-03-05 20:04:34 +01:00
|
|
|
public OBField(SQLField<E, ?> f, Direction d) {
|
2017-09-13 01:05:10 +02:00
|
|
|
field = f;
|
|
|
|
direction = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-27 02:06:38 +01:00
|
|
|
private enum Direction {
|
2017-09-13 01:05:10 +02:00
|
|
|
ASC, DESC;
|
|
|
|
}
|
2020-02-27 02:06:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-03-05 20:04:34 +01:00
|
|
|
public static <E extends SQLElement<E>> SQLOrderBy<E> asc(SQLField<E, ?> field) {
|
|
|
|
return new SQLOrderBy<E>().thenAsc(field);
|
2020-02-27 02:06:38 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:04:34 +01:00
|
|
|
public static <E extends SQLElement<E>> SQLOrderBy<E> desc(SQLField<E, ?> field) {
|
|
|
|
return new SQLOrderBy<E>().thenDesc(field);
|
2020-02-27 02:06:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-13 01:05:10 +02:00
|
|
|
}
|