use of com.infiniteautomation.mango.db.query.SortOption in project ma-core-public by infiniteautomation.
the class QueryModel method toString.
public String toString() {
StringBuilder builder = new StringBuilder();
for (QueryComparison c : andComparisons) {
builder.append(c.toString());
builder.append(" AND ");
}
for (QueryComparison c : orComparisons) {
builder.append(c.toString());
builder.append(" OR ");
}
if (sort.size() > 0) {
builder.append(" SORT ON ");
for (SortOption o : sort) {
builder.append(o.getAttribute());
if (o.isDesc()) {
builder.append(" DESC ");
} else {
builder.append(" ASC ");
}
}
}
if (limit != null) {
builder.append(" LIMIT ");
builder.append(limit);
}
return builder.toString();
}
use of com.infiniteautomation.mango.db.query.SortOption in project ma-core-public by infiniteautomation.
the class RQLToObjectQuery method applySort.
/**
* We just keep a reference to our comparators to sort later
*
* @param node
* @param data
* @return
*/
private void applySort(ASTNode node, T data) {
if (node.getArgumentsSize() == 0)
return;
boolean descending = false;
SortOption sort;
SortOptionComparator<T> compare;
for (Object arg : node) {
String prop = (String) arg;
if (prop.startsWith("-")) {
descending = true;
prop = prop.substring(1);
} else if (prop.startsWith("+")) {
prop = prop.substring(1);
descending = false;
}
sort = new SortOption(prop, descending);
compare = new SortOptionComparator<T>(sort);
this.compares.add(compare);
}
return;
}
use of com.infiniteautomation.mango.db.query.SortOption in project ma-core-public by infiniteautomation.
the class AbstractDao method applySort.
/**
* Apply The Sort to the Query
* @param sql
* @param sort
* @param selectArgs
* @return
*/
protected String applySort(String sql, List<SortOption> sort, List<Object> selectArgs) {
// always sort so that the offset/limit work as intended
if (sort == null)
sort = new ArrayList<SortOption>();
if (sort.isEmpty())
sort.add(new SortOption("id", false));
int i = 0;
Set<String> properties = this.propertyTypeMap.keySet();
for (SortOption option : sort) {
String prop = option.getAttribute();
boolean mapped = false;
if (!comparatorMap.containsKey(prop)) {
// Don't allow sorting on values that have a comparator
if (propertiesMap.containsKey(prop)) {
IntStringPair pair = propertiesMap.get(prop);
prop = pair.getValue();
PropertyArguments args = propertyArgumentsMap.get(option.getAttribute());
if (args != null) {
Collections.addAll(selectArgs, args.getArguments());
}
mapped = true;
}
if (mapped || properties.contains(prop)) {
sql += i++ == 0 ? " ORDER BY " : ", ";
if (mapped)
sql += prop;
else
sql += this.tablePrefix + prop;
if (option.isDesc()) {
sql += " DESC";
}
}
}
}
return sql;
}
use of com.infiniteautomation.mango.db.query.SortOption in project ma-core-public by infiniteautomation.
the class DeltamationCommon method beanSort.
/**
* Sorts a list of Bean objects by multiple SortOptions
* @param list
* @param sort
*/
@SuppressWarnings("unchecked")
public static void beanSort(List<?> list, SortOption... sort) {
ComparatorChain cc = new ComparatorChain();
// always sort so that the offset/limit work as intended
if (sort.length == 0) {
sort = new SortOption[] { new SortOption("id", false) };
}
// TODO catch exceptions?
NullComparator nullComparator = new NullComparator();
for (SortOption option : sort) {
if (option == null)
continue;
cc.addComparator(new BeanComparator(option.getAttribute(), nullComparator), option.isDesc());
}
Collections.sort(list, cc);
}
use of com.infiniteautomation.mango.db.query.SortOption in project ma-core-public by infiniteautomation.
the class RQLToObjectListQuery method applySort.
/**
* @param node
* @param data
* @return
*/
private List<T> applySort(ASTNode node, List<T> data) {
if (node.getArgumentsSize() == 0)
return data;
List<T> sorted = new ArrayList<T>(data);
boolean descending = false;
SortOption sort;
SortOptionComparator<Object> compare;
for (Object arg : node) {
String prop = (String) arg;
if (prop.startsWith("-")) {
descending = true;
prop = prop.substring(1);
} else if (prop.startsWith("+")) {
prop = prop.substring(1);
descending = false;
}
sort = new SortOption(prop, descending);
compare = new SortOptionComparator<Object>(sort);
Collections.sort(sorted, compare);
}
return sorted;
}
Aggregations