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;
|
|
|
|
|
|
|
|
public class SQLOrderBy {
|
|
|
|
|
|
|
|
private List<OBField> orderByFields = new ArrayList<>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construit une nouvelle clause ORDER BY
|
|
|
|
*/
|
|
|
|
public SQLOrderBy() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
2017-09-15 01:33:45 +02:00
|
|
|
public SQLOrderBy add(SQLField<?, ?> field, Direction d) {
|
2017-09-13 01:05:10 +02:00
|
|
|
orderByFields.add(new OBField(field, d));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ajoute un champ dans la clause ORDER BY en construction,
|
|
|
|
* avec comme ordre de tri croissant ASC par défaut
|
|
|
|
*
|
|
|
|
* @param field le champ SQL à ordonner dans l'ordre croissant ASC
|
|
|
|
* @return l'objet courant (permet de chainer les ajouts de champs)
|
|
|
|
*/
|
2017-09-15 01:33:45 +02:00
|
|
|
public SQLOrderBy add(SQLField<?, ?> field) {
|
|
|
|
return add(field, Direction.ASC);
|
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 {
|
|
|
|
public final SQLField<?, ?> field;
|
|
|
|
public final Direction direction;
|
|
|
|
|
|
|
|
public OBField(SQLField<?, ?> f, Direction d) {
|
|
|
|
field = f;
|
|
|
|
direction = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum Direction {
|
|
|
|
ASC, DESC;
|
|
|
|
}
|
|
|
|
}
|