mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-16 21:01:10 +08:00
Add prototype of functionality #582
This commit is contained in:
parent
7079eeff9c
commit
f0d9ca2eeb
@ -0,0 +1,56 @@
|
||||
package com.mysema.query.jpa;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.mysema.query.types.EntityPath;
|
||||
import com.mysema.query.types.path.PathBuilder;
|
||||
|
||||
public class OrderHelper {
|
||||
|
||||
private static final Pattern DOT = Pattern.compile("\\.");
|
||||
|
||||
public static PathBuilder<?> join(JPACommonQuery<?> query, PathBuilder<?> builder, Map<String, PathBuilder<?>> joins, String path) {
|
||||
PathBuilder<?> rv = joins.get(path);
|
||||
if (rv == null) {
|
||||
if (path.contains(".")) {
|
||||
String[] tokens = DOT.split(path);
|
||||
String[] parent = new String[tokens.length - 1];
|
||||
System.arraycopy(tokens, 0, parent, 0, tokens.length - 1);
|
||||
String parentKey = StringUtils.join(parent, ".");
|
||||
builder = join(query, builder, joins, parentKey);
|
||||
rv = new PathBuilder(Object.class, StringUtils.join(tokens, "_"));
|
||||
query.leftJoin((EntityPath)builder.get(tokens[tokens.length - 1]), rv);
|
||||
} else {
|
||||
rv = new PathBuilder(Object.class, path);
|
||||
query.leftJoin((EntityPath)builder.get(path), rv);
|
||||
}
|
||||
joins.put(path, rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static void orderBy(JPACommonQuery<?> query, EntityPath<?> entity, List<String> order) {
|
||||
PathBuilder<?> builder = new PathBuilder(entity.getType(), entity.getMetadata());
|
||||
Map<String, PathBuilder<?>> joins = Maps.newHashMap();
|
||||
|
||||
for (String entry : order) {
|
||||
String[] tokens = DOT.split(entry);
|
||||
if (tokens.length > 1) {
|
||||
String[] parent = new String[tokens.length - 1];
|
||||
System.arraycopy(tokens, 0, parent, 0, tokens.length - 1);
|
||||
PathBuilder<?> parentAlias = join(query, builder, joins, StringUtils.join(parent, "."));
|
||||
query.orderBy(parentAlias.getString(tokens[tokens.length - 1]).asc());
|
||||
} else {
|
||||
query.orderBy(builder.getString(tokens[0]).asc());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,41 @@
|
||||
package com.mysema.query.jpa;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.mysema.query.types.path.PathBuilder;
|
||||
|
||||
public class OrderHelperTest {
|
||||
|
||||
@Test
|
||||
public void Order() {
|
||||
PathBuilder<Object> entity = new PathBuilder<Object>(Object.class, "project");
|
||||
List<String> order = Lists.newArrayList();
|
||||
order.add("customer.name");
|
||||
order.add("department.superior.name");
|
||||
order.add("customer.company.name");
|
||||
order.add("previousProject.customer.company.name");
|
||||
order.add("department.name");
|
||||
|
||||
JPASubQuery query = new JPASubQuery();
|
||||
query.from(entity);
|
||||
OrderHelper.orderBy(query, entity, order);
|
||||
assertEquals("select project\n" +
|
||||
"from Object project\n" +
|
||||
" left join project.customer as customer\n" +
|
||||
" left join project.department as department\n" +
|
||||
" left join department.superior as department_superior\n" +
|
||||
" left join customer.company as customer_company\n" +
|
||||
" left join project.previousProject as previousProject\n" +
|
||||
" left join previousProject.customer as previousProject_customer\n" +
|
||||
" left join previousProject_customer.company as previousProject_customer_company\n" +
|
||||
"order by customer.name asc, department_superior.name asc, customer_company.name asc," +
|
||||
" previousProject_customer_company.name asc, department.name asc",
|
||||
query.toString());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user