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();
}
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());
}
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());
}
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());
}
Aggregations