mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
#278 improved groupBy/having serialization
This commit is contained in:
parent
77d8f45470
commit
2eed073bb0
@ -122,9 +122,9 @@ public final class JDOQLSerializer extends SerializerBase<JDOQLSerializer> {
|
||||
for (Expression<?> arg : operation.getArgs()) {
|
||||
if (!arg.getType().equals(String.class)) {
|
||||
args.add(arg);
|
||||
}else if (arg instanceof Constant) {
|
||||
} else if (arg instanceof Constant) {
|
||||
args.add(regexToLike(arg.toString()));
|
||||
}else if (arg instanceof Operation) {
|
||||
} else if (arg instanceof Operation) {
|
||||
args.add(regexToLike((Operation)arg));
|
||||
} else {
|
||||
args.add(arg);
|
||||
@ -140,7 +140,7 @@ public final class JDOQLSerializer extends SerializerBase<JDOQLSerializer> {
|
||||
return ConstantImpl.create(str.replace(".*", "%").replace(".", "_"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
public void serialize(QueryMetadata metadata, boolean forCountRow, boolean subQuery) {
|
||||
List<? extends Expression<?>> select = metadata.getProjection();
|
||||
List<JoinExpression> joins = metadata.getJoins();
|
||||
@ -205,12 +205,9 @@ public final class JDOQLSerializer extends SerializerBase<JDOQLSerializer> {
|
||||
if (joins.size() > 1) {
|
||||
serializeVariables(joins);
|
||||
}
|
||||
|
||||
// parameters
|
||||
if (!getConstantToLabel().isEmpty()) {
|
||||
serializeParameters(metadata.getParams());
|
||||
}
|
||||
|
||||
|
||||
int position = getLength();
|
||||
|
||||
// group by
|
||||
if (!groupBy.isEmpty()) {
|
||||
append(GROUP_BY).handle(COMMA, groupBy);
|
||||
@ -241,7 +238,12 @@ public final class JDOQLSerializer extends SerializerBase<JDOQLSerializer> {
|
||||
Long offset = metadata.getModifiers().getOffset();
|
||||
serializeModifiers(limit, offset);
|
||||
}
|
||||
|
||||
|
||||
// parameters
|
||||
if (!getConstantToLabel().isEmpty()) {
|
||||
insert(position, serializeParameters(metadata.getParams()));
|
||||
}
|
||||
|
||||
constantToLabel.pop();
|
||||
|
||||
}
|
||||
@ -259,14 +261,15 @@ public final class JDOQLSerializer extends SerializerBase<JDOQLSerializer> {
|
||||
}
|
||||
}
|
||||
|
||||
private void serializeParameters(Map<ParamExpression<?>, Object> params) {
|
||||
append(PARAMETERS);
|
||||
private String serializeParameters(Map<ParamExpression<?>, Object> params) {
|
||||
StringBuilder b = new StringBuilder();
|
||||
b.append(PARAMETERS);
|
||||
boolean first = true;
|
||||
List<Map.Entry<Object, String>> entries = new ArrayList<Map.Entry<Object, String>>(getConstantToLabel().entrySet());
|
||||
Collections.sort(entries, comparator);
|
||||
for (Map.Entry<Object, String> entry : entries) {
|
||||
if (!first) {
|
||||
append(COMMA);
|
||||
b.append(COMMA);
|
||||
}
|
||||
if (Param.class.isInstance(entry.getKey())) {
|
||||
Object constant = params.get(entry.getKey());
|
||||
@ -274,17 +277,17 @@ public final class JDOQLSerializer extends SerializerBase<JDOQLSerializer> {
|
||||
throw new ParamNotSetException((Param<?>) entry.getKey());
|
||||
}
|
||||
constants.add(constant);
|
||||
append(((Param<?>)entry.getKey()).getType().getName());
|
||||
b.append(((Param<?>)entry.getKey()).getType().getName());
|
||||
} else {
|
||||
constants.add(entry.getKey());
|
||||
append(entry.getKey().getClass().getName());
|
||||
b.append(entry.getKey().getClass().getName());
|
||||
}
|
||||
append(" ").append(entry.getValue());
|
||||
b.append(" ").append(entry.getValue());
|
||||
first = false;
|
||||
}
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void serializeVariables(List<JoinExpression> joins) {
|
||||
append(VARIABLES);
|
||||
for (int i = 1; i < joins.size(); i++) {
|
||||
|
||||
@ -39,6 +39,16 @@ public class GroupByTest extends AbstractJDOTest {
|
||||
assertEquals(3, query().from(product).groupBy(product.description).list(product.description).size());
|
||||
assertEquals(3, query().from(product).groupBy(product.price).list(product.price).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Having() {
|
||||
assertEquals(3, query().from(product)
|
||||
.groupBy(product.description).having(product.description.ne("XXX"))
|
||||
.list(product.description).size());
|
||||
assertEquals(3, query().from(product)
|
||||
.groupBy(product.price).having(product.price.gt(0))
|
||||
.list(product.price).size());
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void doPersist() {
|
||||
|
||||
@ -21,9 +21,6 @@ import org.junit.Test;
|
||||
import com.mysema.query.jdo.models.fitness.QGym;
|
||||
import com.mysema.query.jdo.models.fitness.Wardrobe;
|
||||
|
||||
/**
|
||||
* Tests for JDOQL queries of collections and maps.
|
||||
*/
|
||||
public class ContainerTest extends AbstractTest{
|
||||
|
||||
private QGym gym = QGym.gym1;
|
||||
@ -35,9 +32,6 @@ public class ContainerTest extends AbstractTest{
|
||||
wrd.setModel("model");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests NOT contains in Map.values
|
||||
*/
|
||||
@Test
|
||||
public void NotContainsValuesInMapFields() {
|
||||
|
||||
@ -82,9 +76,6 @@ public class ContainerTest extends AbstractTest{
|
||||
.list(gym)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests NOT contains in Map.keys
|
||||
*/
|
||||
@Test
|
||||
public void NotContainsKeysInMapFields() {
|
||||
|
||||
@ -129,10 +120,8 @@ public class ContainerTest extends AbstractTest{
|
||||
.list(gym)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests NOT contains in Map.entry
|
||||
*/
|
||||
@Test public void NotContainsEntryInMapFields() {
|
||||
@Test
|
||||
public void NotContainsEntryInMapFields() {
|
||||
// NOTE : containsEntry is not supported in Querydsl
|
||||
|
||||
// "SELECT FROM org.jpox.samples.models.fitness.Gym "
|
||||
@ -149,9 +138,6 @@ public class ContainerTest extends AbstractTest{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests get
|
||||
*/
|
||||
@Test
|
||||
public void GetInMapFields() {
|
||||
|
||||
@ -167,9 +153,6 @@ public class ContainerTest extends AbstractTest{
|
||||
.where(gym.wardrobes.get(wrd.getModel()).eq(wrd)).list(gym)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests get method used in ordering
|
||||
*/
|
||||
@Test
|
||||
public void GetInOrderingInMapFields() {
|
||||
// "SELECT FROM org.jpox.samples.models.fitness.Gym "
|
||||
@ -177,6 +160,7 @@ public class ContainerTest extends AbstractTest{
|
||||
// .setOrdering("this.wardrobes.get(wrd.model).model ascending");
|
||||
assertEquals(
|
||||
"SELECT FROM com.mysema.query.jdo.models.fitness.Gym " +
|
||||
"PARAMETERS java.lang.String a1 " +
|
||||
"ORDER BY this.wardrobes.get(a1).model ASC",
|
||||
|
||||
serialize(query().from(gym)
|
||||
|
||||
@ -0,0 +1,26 @@
|
||||
package com.mysema.query.jdo.serialization;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.jdo.models.company.QEmployee;
|
||||
|
||||
public class GroupByTest extends AbstractTest {
|
||||
|
||||
@Test
|
||||
public void GroupBy() {
|
||||
QEmployee employee = QEmployee.employee;
|
||||
assertEquals(
|
||||
"SELECT FROM com.mysema.query.jdo.models.company.Employee "+
|
||||
"PARAMETERS java.lang.String a1 "+
|
||||
"GROUP BY this.emailAddress "+
|
||||
"HAVING this.emailAddress != a1",
|
||||
|
||||
serialize(query()
|
||||
.from(employee)
|
||||
.groupBy(employee.emailAddress).having(employee.emailAddress.ne("XXX"))
|
||||
.list(employee)));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user