#462 Improve case-when-then serialization

This commit is contained in:
Timo Westkämper 2013-07-27 23:20:28 +03:00
parent 7efecd3c43
commit 41271c9455
5 changed files with 67 additions and 22 deletions

View File

@ -220,8 +220,8 @@ public class Templates {
add(PathType.ARRAYVALUE_CONSTANT, "{0}[{1s}]"); // serialized constant
// case
add(Ops.CASE, "case {0} end");
add(Ops.CASE_WHEN, "when {0} then {1} {2}");
add(Ops.CASE, "case {0} end", 0);
add(Ops.CASE_WHEN, "when {0} then {1} {2}", 0);
add(Ops.CASE_ELSE, "else {0}");
// case for

View File

@ -417,6 +417,11 @@ public abstract class AbstractStandardTest {
query().from(cat).list(new QTuple(cat.id, Expressions.constantAs("abc", new StringPath("const"))));
}
@Test
public void Constant2() {
query().from(cat).map(cat.id, Expressions.constant("name"));
}
@Test
public void DistinctResults() {
System.out.println("-- list results");
@ -611,6 +616,14 @@ public abstract class AbstractStandardTest {
query().from(cat).list(cat.name.when("Bob").then(1).otherwise(2));
}
@Test
public void Case2() {
query().from(cat)
.list(Expressions.cases().when(cat.toes.eq(2)).then(cat.id.multiply(2))
.when(cat.toes.eq(3)).then(cat.id.multiply(3))
.otherwise(4));
}
@Test
public void Cast() {
query().from(cat).list(cat.bodyWeight.castToNum(Integer.class));

View File

@ -4,6 +4,7 @@ import static org.junit.Assert.assertNotNull;
import javax.persistence.EntityManager;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
@ -23,40 +24,46 @@ import com.mysema.testutil.Performance;
public class QueryPerformanceTest {
private static final int iterations = 1000;
private EntityManager entityManager;
@BeforeClass
public static void setUpClass() {
Mode.mode.set("h2perf");
Mode.target.set(Target.H2);
}
@AfterClass
public static void tearDownClass() {
Mode.mode.remove();
Mode.target.remove();
}
private JPAQuery query() {
return new JPAQuery(entityManager);
}
@Before
public void setUp() {
if (query().from(QCat.cat).notExists()) {
for (int i = 0; i < iterations; i++) {
entityManager.persist(new Cat(String.valueOf(i), i + 100));
}
entityManager.flush();
}
}
entityManager.flush();
}
}
@Test
public void ById_Raw() {
long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
Cat cat = (Cat)entityManager.createQuery("select cat from Cat cat where id = ?")
.setParameter(1, i + 100).getSingleResult();
assertNotNull(cat);
assertNotNull(cat);
}
System.err.println("by id - raw" + (System.currentTimeMillis() - start));
}
@Test
public void ById_Qdsl() {
long start = System.currentTimeMillis();
@ -67,7 +74,7 @@ public class QueryPerformanceTest {
}
System.err.println("by id - dsl" + (System.currentTimeMillis() - start));
}
@Test
public void ById_TwoCols_Raw() {
long start = System.currentTimeMillis();
@ -75,11 +82,11 @@ public class QueryPerformanceTest {
Object[] row = (Object[])entityManager.createQuery(
"select cat.id, cat.name from Cat cat where id = ?")
.setParameter(1, i + 100).getSingleResult();
assertNotNull(row);
assertNotNull(row);
}
System.err.println("by id - 2 cols - raw" + (System.currentTimeMillis() - start));
}
@Test
public void ById_TwoCols_Qdsl() {
long start = System.currentTimeMillis();
@ -90,11 +97,11 @@ public class QueryPerformanceTest {
}
System.err.println("by id - 2 cols - dsl" + (System.currentTimeMillis() - start));
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
}

View File

@ -23,13 +23,38 @@ import com.mysema.query.QueryMetadata;
import com.mysema.query.domain.QCat;
import com.mysema.query.jpa.domain.Location;
import com.mysema.query.jpa.domain.QEmployee;
import com.mysema.query.support.Expressions;
import com.mysema.query.types.EntityPath;
import com.mysema.query.types.Expression;
import com.mysema.query.types.path.EntityPathBase;
import com.mysema.query.types.path.NumberPath;
import com.mysema.query.types.path.StringPath;
public class JPQLSerializerTest {
@Test
public void Case() {
QCat cat = QCat.cat;
JPQLSerializer serializer = new JPQLSerializer(HQLTemplates.DEFAULT);
Expression<?> expr = Expressions.cases().when(cat.toes.eq(2)).then(2)
.when(cat.toes.eq(3)).then(3)
.otherwise(4);
serializer.handle(expr);
assertEquals("case when (cat.toes = ?1) then ?1 when (cat.toes = ?2) then ?2 else ?3 end", serializer.toString());
}
@Test
public void Case2() {
QCat cat = QCat.cat;
JPQLSerializer serializer = new JPQLSerializer(HQLTemplates.DEFAULT);
Expression<?> expr = Expressions.cases().when(cat.toes.eq(2)).then(cat.id.multiply(2))
.when(cat.toes.eq(3)).then(cat.id.multiply(3))
.otherwise(4);
serializer.handle(expr);
assertEquals("case when (cat.toes = ?1) then (cat.id * ?1) when (cat.toes = ?2) then (cat.id * ?2) else ?3 end", serializer.toString());
}
@Test
public void FromWithCustomEntityName() {
JPQLSerializer serializer = new JPQLSerializer(HQLTemplates.DEFAULT);

View File

@ -1,6 +1,6 @@
package com.mysema.query.suites;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@ -20,8 +20,8 @@ import com.mysema.query.jpa.SerializationBase;
SerializationBase.class})
public abstract class AbstractJPASuite {
@BeforeClass
public static void tearDown() throws Exception {
@AfterClass
public static void tearDownClas() throws Exception {
Mode.mode.remove();
Mode.target.remove();
}