Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] ID(this) is not accepted as an argument to a function - bugfix + tests #2287

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ public static Test suite() {
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testSelectQueryImplicitThisVariableInAggregateFunctionPath"));
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testSelectQueryImplicitThisVariableInArithmeticExpression"));
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testSelectQueryImplicitThisVariableAndEnum"));
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testSelectQueryIdFunctionNestedInArithmeticFunction_Where"));
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testSelectQueryIdFunctionNestedInStringFunction_Where"));
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testUpdateImplicitVariableInArithmeticExpression"));
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testDeleteQueryLengthInExpressionOnLeft"));
suite.addTest(new JUnitJPQLJakartaDataNoAliasTest("testDeleteQueryLengthInExpressionOnRight"));
Expand Down Expand Up @@ -382,6 +384,33 @@ public void testSelectQueryImplicitThisVariableAndEnum() {
assertEquals(ROOMS[1], rooms.get(0));
}

// Covers https://github.com/eclipse-ee4j/eclipselink/issues/2286
public void testSelectQueryIdFunctionNestedInArithmeticFunction_Where() {
final String typeName = getPlatform().isMySQL() ? "UNSIGNED": "INTEGER";
resetRooms();
Room room = getEntityManagerFactory().callInTransaction(em -> em.createQuery(
"SELECT this " +
"FROM Room " +
"WHERE ABS(CAST(ID(this) AS " + typeName + "))= :idParam",
Room.class)
.setParameter("idParam", ROOMS[1].getId())
.getSingleResult());
assertEquals(ROOMS[1], room);
}

// Covers https://github.com/eclipse-ee4j/eclipselink/issues/2286
public void testSelectQueryIdFunctionNestedInStringFunction_Where() {
resetRooms();
Room room = getEntityManagerFactory().callInTransaction(em -> em.createQuery(
"SELECT this " +
"FROM Room " +
"WHERE LOWER(CAST(ID(this) AS CHAR))= :idParam",
Room.class)
.setParameter("idParam", String.valueOf(ROOMS[1].getId()))
.getSingleResult());
assertEquals(ROOMS[1], room);
}

public void testUpdateImplicitVariableInArithmeticExpression() {
resetRooms();
int numberOfChanges = getEntityManagerFactory().callInTransaction(em -> em.createQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,14 @@ protected void initializeBNFs() {
// ID function
addChildBNF(SelectExpressionBNF.ID, IdExpressionBNF.ID);
addChildBNF(ComparisonExpressionBNF.ID, IdExpressionBNF.ID);
addChildBNF(ScalarExpressionBNF.ID, IdExpressionBNF.ID);
addChildBNF(IdExpressionBNF.ID, GeneralIdentificationVariableBNF.ID);
addChildBNF(IdExpressionBNF.ID, SingleValuedObjectPathExpressionBNF.ID);

// VERSION function
addChildBNF(SelectExpressionBNF.ID, VersionExpressionBNF.ID);
addChildBNF(ComparisonExpressionBNF.ID, VersionExpressionBNF.ID);
addChildBNF(ScalarExpressionBNF.ID, VersionExpressionBNF.ID);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,28 @@ public static String query_IdFunction_Where() {
return "SELECT c FROM Customer c WHERE ID(c) = 1";
}

public static String query_IdFunctionNestedInArithmeticFunction_Where() {
return "SELECT this FROM Customer this WHERE ABS(CAST(ID(this) AS INTEGER)) = 1";
}

public static String query_IdFunctionNestedInStringFunction_Where() {
return "SELECT this FROM Customer this WHERE LOWER(CAST(ID(this) AS STRING)) = 'abc'";
}

public static String query_VersionFunction_Select01() {
return "SELECT VERSION(c) FROM Customer c";
}

public static String query_VersionFunction_Where() {
return "SELECT c FROM Customer c WHERE VERSION(c) = 1";
}

public static String query_VersionFunctionNestedInArithmeticFunction_Where() {
return "SELECT this FROM Customer this WHERE ABS(CAST(VERSION(this) AS INTEGER)) = 1";
}

public static String query_VersionFunctionNestedInStringFunction_Where() {
return "SELECT this FROM Customer this WHERE LOWER(CAST(VERSION(this) AS STRING)) = '1'";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import static org.eclipse.persistence.jpa.tests.jpql.JPQLQueries3_2.query_ConcatPipes_Where;
import static org.eclipse.persistence.jpa.tests.jpql.JPQLQueries3_2.query_IdFunction_Select01;
import static org.eclipse.persistence.jpa.tests.jpql.JPQLQueries3_2.query_IdFunction_Where;
import static org.eclipse.persistence.jpa.tests.jpql.JPQLQueries3_2.query_IdFunctionNestedInArithmeticFunction_Where;
import static org.eclipse.persistence.jpa.tests.jpql.JPQLQueries3_2.query_IdFunctionNestedInStringFunction_Where;
import static org.eclipse.persistence.jpa.tests.jpql.JPQLQueries3_2.query_LeftFunction_Select01;
import static org.eclipse.persistence.jpa.tests.jpql.JPQLQueries3_2.query_LeftFunction_Select02;
import static org.eclipse.persistence.jpa.tests.jpql.JPQLQueries3_2.query_LeftFunction_Select03;
Expand All @@ -39,7 +41,11 @@
import static org.eclipse.persistence.jpa.tests.jpql.JPQLQueries3_2.query_RightFunction_Where;
import static org.eclipse.persistence.jpa.tests.jpql.JPQLQueries3_2.query_VersionFunction_Select01;
import static org.eclipse.persistence.jpa.tests.jpql.JPQLQueries3_2.query_VersionFunction_Where;
import static org.eclipse.persistence.jpa.tests.jpql.JPQLQueries3_2.query_VersionFunctionNestedInArithmeticFunction_Where;
import static org.eclipse.persistence.jpa.tests.jpql.JPQLQueries3_2.query_VersionFunctionNestedInStringFunction_Where;
import static org.eclipse.persistence.jpa.tests.jpql.JPQLQueries3_2.query_Union01;
import static org.eclipse.persistence.jpa.tests.jpql.parser.JPQLParserTester.abs;
import static org.eclipse.persistence.jpa.tests.jpql.parser.JPQLParserTester.castAs;
import static org.eclipse.persistence.jpa.tests.jpql.parser.JPQLParserTester.concatPipes;
import static org.eclipse.persistence.jpa.tests.jpql.parser.JPQLParserTester.count;
import static org.eclipse.persistence.jpa.tests.jpql.parser.JPQLParserTester.except;
Expand All @@ -50,6 +56,7 @@
import static org.eclipse.persistence.jpa.tests.jpql.parser.JPQLParserTester.intersect;
import static org.eclipse.persistence.jpa.tests.jpql.parser.JPQLParserTester.left;
import static org.eclipse.persistence.jpa.tests.jpql.parser.JPQLParserTester.leftJoin;
import static org.eclipse.persistence.jpa.tests.jpql.parser.JPQLParserTester.lower;
import static org.eclipse.persistence.jpa.tests.jpql.parser.JPQLParserTester.numeric;
import static org.eclipse.persistence.jpa.tests.jpql.parser.JPQLParserTester.path;
import static org.eclipse.persistence.jpa.tests.jpql.parser.JPQLParserTester.replace;
Expand Down Expand Up @@ -412,6 +419,34 @@ public final void test_Query_IdFunction_Where() {
testQuery(query_IdFunction_Where(), selectStatement);
}

@Test
public final void test_Query_IdFunctionNestedInArithmeticFunction_Where() {
//SELECT this
// FROM Customer this
// WHERE ABS(CAST(ID(this) AS INTEGER)) = 1"

ExpressionTester selectStatement = selectStatement(
select(variable("this")),
from("Customer", "this"),
where(abs(castAs(id("this"), "INTEGER")).equal(numeric(1))));

testQuery(query_IdFunctionNestedInArithmeticFunction_Where(), selectStatement);
}

@Test
public final void test_Query_IdFunctionNestedInStringFunction_Where() {
// SELECT this
// FROM Customer this
// WHERE LOWER(CAST(ID(this) AS STRING)) = 'a'

ExpressionTester selectStatement = selectStatement(
select(variable("this")),
from("Customer", "this"),
where(lower(castAs(id("this"), "STRING")).equal(string("'abc'"))));

testQuery(query_IdFunctionNestedInStringFunction_Where(), selectStatement);
}

@Test
public final void test_Query_VersionFunction_Select01() {
// select version(c)
Expand All @@ -437,4 +472,32 @@ public final void test_Query_VersionFunction_Where() {

testQuery(query_VersionFunction_Where(), selectStatement);
}

@Test
public final void test_Query_VersionFunctionNestedInArithmeticFunction_Where() {
//SELECT this
// FROM Customer this
// WHERE ABS(CAST(VERSION(this) AS INTEGER)) = 1"

ExpressionTester selectStatement = selectStatement(
select(variable("this")),
from("Customer", "this"),
where(abs(castAs(version("this"), "INTEGER")).equal(numeric(1))));

testQuery(query_VersionFunctionNestedInArithmeticFunction_Where(), selectStatement);
}

@Test
public final void test_Query_VersionFunctionNestedInStringFunction_Where() {
// SELECT this
// FROM Customer this
// WHERE LOWER(CAST(VERSION(this) AS STRING)) = '1'

ExpressionTester selectStatement = selectStatement(
select(variable("this")),
from("Customer", "this"),
where(lower(castAs(version("this"), "STRING")).equal(string("'1'"))));

testQuery(query_VersionFunctionNestedInStringFunction_Where(), selectStatement);
}
}
Loading