Search in sources :

Example 1 with StringBuilderAppendable

use of org.apache.cayenne.access.sqlbuilder.StringBuilderAppendable in project cayenne by apache.

the class QualifierTranslatorTest method translateComplexAnd.

@Test
public void translateComplexAnd() {
    Node and = translate("a < 2 and b in (5,6) and b = 7");
    assertNotNull(and);
    assertThat(and, instanceOf(OpExpressionNode.class));
    assertEquals("AND", ((OpExpressionNode) and).getOp());
    assertEquals(3, and.getChildrenCount());
    SQLGenerationVisitor visitor = new SQLGenerationVisitor(new StringBuilderAppendable());
    and.visit(visitor);
    assertEquals(" ( t0.a < 2 ) AND t0.b IN ( 5, 6) AND ( t0.b = 7 )", visitor.getSQLString());
}
Also used : StringBuilderAppendable(org.apache.cayenne.access.sqlbuilder.StringBuilderAppendable) SQLGenerationVisitor(org.apache.cayenne.access.sqlbuilder.SQLGenerationVisitor) Test(org.junit.Test)

Example 2 with StringBuilderAppendable

use of org.apache.cayenne.access.sqlbuilder.StringBuilderAppendable in project cayenne by apache.

the class QualifierTranslatorIT method testCompoundPK.

@Test
public void testCompoundPK() {
    CompoundPkTestEntity testEntity = ObjectSelect.query(CompoundPkTestEntity.class).selectFirst(context);
    assertNotNull(testEntity);
    ObjectSelect<CompoundFkTestEntity> query = ObjectSelect.query(CompoundFkTestEntity.class).where(CompoundFkTestEntity.TO_COMPOUND_PK.eq(testEntity)).and(CompoundFkTestEntity.NAME.like("test%")).and(CompoundFkTestEntity.NAME.contains("a"));
    DefaultSelectTranslator translator = new DefaultSelectTranslator(query, runtime.getDataDomain().getDefaultNode().getAdapter(), context.getEntityResolver());
    QualifierTranslator qualifierTranslator = translator.getContext().getQualifierTranslator();
    Node node = qualifierTranslator.translate(query.getWhere());
    SQLGenerationVisitor visitor = new SQLGenerationVisitor(new StringBuilderAppendable());
    node.visit(visitor);
    assertEquals(" ( ( ( t0.F_KEY1 = 'PK1' ) AND ( t0.F_KEY2 = 'PK2' ) ) AND t0.NAME LIKE 'test%' ) AND t0.NAME LIKE '%a%'", visitor.getSQLString());
}
Also used : CompoundPkTestEntity(org.apache.cayenne.testdo.compound.CompoundPkTestEntity) StringBuilderAppendable(org.apache.cayenne.access.sqlbuilder.StringBuilderAppendable) Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) SQLGenerationVisitor(org.apache.cayenne.access.sqlbuilder.SQLGenerationVisitor) CompoundFkTestEntity(org.apache.cayenne.testdo.compound.CompoundFkTestEntity) Test(org.junit.Test)

Aggregations

SQLGenerationVisitor (org.apache.cayenne.access.sqlbuilder.SQLGenerationVisitor)2 StringBuilderAppendable (org.apache.cayenne.access.sqlbuilder.StringBuilderAppendable)2 Test (org.junit.Test)2 Node (org.apache.cayenne.access.sqlbuilder.sqltree.Node)1 CompoundFkTestEntity (org.apache.cayenne.testdo.compound.CompoundFkTestEntity)1 CompoundPkTestEntity (org.apache.cayenne.testdo.compound.CompoundPkTestEntity)1