Skip to content

Commit

Permalink
CAY-2871 QualifierTranslator breaks on a relationship with a compound FK
Browse files Browse the repository at this point in the history
  • Loading branch information
stariy95 committed Sep 4, 2024
1 parent 2ae50e6 commit a036d3f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ class QualifierTranslator implements TraversalHandler {
QualifierTranslator(TranslatorContext context) {
this.context = context;
this.pathTranslator = context.getPathTranslator();
this.expressionsToSkip = new HashSet<>();
// must use identity comparison for the node skipping, or it could skip the wrong node
// and mess everything up, see for example CAY-2871
this.expressionsToSkip = Collections.newSetFromMap(new IdentityHashMap<>());
this.nodeStack = new ArrayDeque<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

Expand All @@ -56,6 +58,7 @@ public void setUp() throws Exception {
TableHelper tCompoundPKTest = new TableHelper(dbHelper, "COMPOUND_PK_TEST");
tCompoundPKTest.setColumns("KEY1", "KEY2", "NAME");
tCompoundPKTest.insert("PK1", "PK2", "BBB");
tCompoundPKTest.insert("PK3", "PK4", "CCC");
}

@Test
Expand All @@ -81,4 +84,29 @@ public void testCompoundPK() {
assertEquals(" ( ( ( t0.F_KEY1 = 'PK1' ) AND ( t0.F_KEY2 = 'PK2' ) ) AND t0.NAME LIKE 'test%' ) AND t0.NAME LIKE '%a%'", visitor.getSQLString());
}

@Test
public void testMultipleCompoundPK() {
List<CompoundPkTestEntity> testEntity = ObjectSelect.query(CompoundPkTestEntity.class)
.limit(2)
.select(context);
assertNotNull(testEntity);
assertEquals(2, testEntity.size());

ObjectSelect<CompoundFkTestEntity> query = ObjectSelect.query(CompoundFkTestEntity.class)
.where(CompoundFkTestEntity.TO_COMPOUND_PK.eq(testEntity.get(0)))
.or(CompoundFkTestEntity.TO_COMPOUND_PK.eq(testEntity.get(1)));

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' ) ) OR ( ( t0.F_KEY1 = 'PK3' ) AND ( t0.F_KEY2 = 'PK4' ) )", visitor.getSQLString());
}

}

0 comments on commit a036d3f

Please sign in to comment.