Search in sources :

Example 1 with SQLGenerationVisitor

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

the class BaseBatchTranslator method doTranslate.

/**
 * This method applies {@link org.apache.cayenne.access.translator.select.BaseSQLTreeProcessor} to the
 * provided SQL tree node and generates SQL string from it.
 *
 * @param nodeBuilder SQL tree node builder
 * @return SQL string
 */
protected String doTranslate(NodeBuilder nodeBuilder) {
    Node node = nodeBuilder.build();
    // convert to database flavour
    node = context.getAdapter().getSqlTreeProcessor().process(node);
    // generate SQL
    SQLGenerationVisitor visitor = new SQLGenerationVisitor(new DefaultQuotingAppendable(context));
    node.visit(visitor);
    bindings = context.getBindings().toArray(new DbAttributeBinding[0]);
    return visitor.getSQLString();
}
Also used : Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) SQLGenerationVisitor(org.apache.cayenne.access.sqlbuilder.SQLGenerationVisitor) DefaultQuotingAppendable(org.apache.cayenne.access.translator.select.DefaultQuotingAppendable)

Example 2 with SQLGenerationVisitor

use of org.apache.cayenne.access.sqlbuilder.SQLGenerationVisitor 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 3 with SQLGenerationVisitor

use of org.apache.cayenne.access.sqlbuilder.SQLGenerationVisitor 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)

Example 4 with SQLGenerationVisitor

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

the class SQLGenerationStage method perform.

@Override
public void perform(TranslatorContext context) {
    if (context.isSkipSQLGeneration()) {
        return;
    }
    // Build final SQL tree
    Node node = context.getSelectBuilder().build();
    // convert to database flavour
    node = context.getAdapter().getSqlTreeProcessor().process(node);
    // generate SQL
    SQLGenerationVisitor visitor = new SQLGenerationVisitor(new DefaultQuotingAppendable(context));
    node.visit(visitor);
    context.setFinalSQL(visitor.getSQLString());
}
Also used : Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) SQLGenerationVisitor(org.apache.cayenne.access.sqlbuilder.SQLGenerationVisitor)

Aggregations

SQLGenerationVisitor (org.apache.cayenne.access.sqlbuilder.SQLGenerationVisitor)4 Node (org.apache.cayenne.access.sqlbuilder.sqltree.Node)3 StringBuilderAppendable (org.apache.cayenne.access.sqlbuilder.StringBuilderAppendable)2 Test (org.junit.Test)2 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)1 DefaultQuotingAppendable (org.apache.cayenne.access.translator.select.DefaultQuotingAppendable)1 CompoundFkTestEntity (org.apache.cayenne.testdo.compound.CompoundFkTestEntity)1 CompoundPkTestEntity (org.apache.cayenne.testdo.compound.CompoundPkTestEntity)1