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

Allow escaping of '?' using '??' in SQL expression #1935

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -1981,6 +1981,11 @@ public Expression sql(String sql, List arguments) {
int start = 0;
int index = sql.indexOf('?');
while (index != -1) {
// ? can be escaped as ?? to support ? as a native SQL operator (e.g. on Postgres)
if (index < sql.length() - 1 && sql.charAt(index + 1) == '?') {
index = sql.indexOf('?', index + 2);
continue;
}
pvarga88 marked this conversation as resolved.
Show resolved Hide resolved
v.add(sql.substring(start, index));
start = index + 1;
index = sql.indexOf('?', start);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.persistence.jpa.test.framework.Emf;
import org.eclipse.persistence.jpa.test.framework.EmfRunner;
import org.eclipse.persistence.jpa.test.framework.Property;
import org.eclipse.persistence.sessions.Session;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -254,4 +255,32 @@ public void testSelectJsonInWhereCondition() {
}
}

@Test
public void testEscapedQuestionMarkInSQLOperator() {
EntityManager em = emf.createEntityManager();

if (emf.unwrap(Session.class).getPlatform().isOracle()) {
JsonValue value = Json.createObjectBuilder()
.add("id", "1007")
.build();
try {
em.getTransaction().begin();
JsonEntity e = new JsonEntity(1007, value);
em.persist(e);
em.flush();
em.getTransaction().commit();
em.clear();

JsonEntity dbValue = em.createQuery(
"SELECT v.value FROM JsonEntity v WHERE SQL('JSON_EXISTS(?, ''$??(@.id == 1007)'')', v.value)", JsonEntity.class)
pvarga88 marked this conversation as resolved.
Show resolved Hide resolved
.getSingleResult();
Assert.assertEquals(value, dbValue.getValue());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
}
}
Loading