Search in sources :

Example 1 with FunctionRenderContext

use of com.blazebit.persistence.spi.FunctionRenderContext in project blaze-persistence by Blazebit.

the class SubqueryTest method testSubqueryCorrelatesMacro.

@Test
public // Test for #421
void testSubqueryCorrelatesMacro() {
    CriteriaBuilder<Document> crit = cbf.create(em, Document.class, "d");
    crit.registerMacro("test", new JpqlMacro() {

        @Override
        public void render(FunctionRenderContext context) {
            context.addChunk("d.");
            context.addArgument(0);
        }
    });
    crit.where("owner").in().from("TEST(people)").where("partnerDocument").eqExpression("d").end();
    String peopleCorrelation = correlationPath(Document.class, "d.people", "people", "id = d.id");
    String where = " WHERE ";
    String peopleCorrelationWhere = "";
    int idx;
    if ((idx = peopleCorrelation.indexOf(where)) != -1) {
        peopleCorrelationWhere = peopleCorrelation.substring(idx + where.length());
        peopleCorrelation = peopleCorrelation.substring(0, idx);
    }
    String expectedQuery = "SELECT d FROM Document d WHERE d.owner IN (SELECT people FROM " + peopleCorrelation + " WHERE ";
    if (!peopleCorrelationWhere.isEmpty()) {
        expectedQuery += peopleCorrelationWhere + " AND ";
    }
    expectedQuery += "people.partnerDocument = d)";
    assertEquals(expectedQuery, crit.getQueryString());
    crit.getResultList();
}
Also used : JpqlMacro(com.blazebit.persistence.spi.JpqlMacro) Document(com.blazebit.persistence.testsuite.entity.Document) FunctionRenderContext(com.blazebit.persistence.spi.FunctionRenderContext) Test(org.junit.Test)

Example 2 with FunctionRenderContext

use of com.blazebit.persistence.spi.FunctionRenderContext in project blaze-persistence by Blazebit.

the class JpqlMacroTest method testScopedMacro.

@Test
public void testScopedMacro() {
    CriteriaBuilder<Document> cb = cbf.create(em, Document.class, "d");
    verifyException(cb, SyntaxErrorException.class, r -> r.where("ID(d)"));
    cb.registerMacro("id", new JpqlMacro() {

        @Override
        public void render(FunctionRenderContext context) {
            context.addArgument(0);
            context.addChunk(".id");
        }
    });
    cb.where("ID(d)").eq(1);
    String expected = "SELECT d FROM Document d WHERE d.id = :param_0";
    assertEquals(expected, cb.getQueryString());
    CriteriaBuilder<Document> newCb = cbf.create(em, Document.class, "d");
    verifyException(newCb, SyntaxErrorException.class, r -> r.where("ID(d)"));
}
Also used : JpqlMacro(com.blazebit.persistence.spi.JpqlMacro) Document(com.blazebit.persistence.testsuite.entity.Document) FunctionRenderContext(com.blazebit.persistence.spi.FunctionRenderContext) Test(org.junit.Test)

Aggregations

FunctionRenderContext (com.blazebit.persistence.spi.FunctionRenderContext)2 JpqlMacro (com.blazebit.persistence.spi.JpqlMacro)2 Document (com.blazebit.persistence.testsuite.entity.Document)2 Test (org.junit.Test)2