From ac939f7b5f23e8ef73e37c8baa1b0b23c5c4edce Mon Sep 17 00:00:00 2001 From: Fay Wang Date: Wed, 2 Sep 2009 01:46:54 +0000 Subject: [PATCH] OPENJPA-1281: fix reparameter when PreparedQueryCache is on git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@810331 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/jdbc/sql/SQLBuffer.java | 14 +++++++ .../openjpa/persistence/jdbc/sqlcache/CD.java | 10 +++++ .../jdbc/sqlcache/TestPreparedQueryCache.java | 37 +++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java index fc443356c..df3d45870 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java @@ -166,6 +166,20 @@ public final class SQLBuffer _userIndex.add(newIndex); _userIndex.add(userParam); } + } else { + if (_userIndex != null) { + List userIndex = new ArrayList(); + for (int i = 0; i < _userIndex.size(); i+=2) { + int oldIndex = ((Integer)_userIndex.get(i)).intValue(); + Object userParam = _userIndex.get(i+1); + if (oldIndex >= paramIndex) + userIndex.add(oldIndex + paramIndex); + else + userIndex.add(oldIndex); + userIndex.add(userParam); + } + _userIndex = userIndex; + } } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CD.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CD.java index 9cb9f99f8..06c16e758 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CD.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CD.java @@ -28,6 +28,8 @@ import javax.persistence.ManyToOne; public class CD extends Merchandise { private String label; + private int status; + @ManyToOne private Singer singer; @@ -55,4 +57,12 @@ public class CD extends Merchandise { this.singer = singer; singer.addCd(this); } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java index 3eb9e16be..5a4184add 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java @@ -702,6 +702,43 @@ public class TestPreparedQueryCache extends TestCase { assertNotNull(book2.getAuthors()); assertFalse(book2.getAuthors().isEmpty()); } + + public void testQueryWithUserDefinedAndInternalParamtersInSubquery() { + String jpql = "Select a From Address a Where Not Exists (" + + " Select s.id From Singer As s Where " + + " s.address = a And " + + " Not (" + + " (s.firstName = :firstName) " + + " Or " + + " (" + + " (" + + " exists (select c.id from CD c where c.singer = s and c.status = 1) And " + + " s.lastName = :lastName" + + " ) " + + " Or " + + " (" + + " not exists (Select c.id from CD c where c.singer = s and c.status = 2)" + + " )" + + " )" + + " )" + + " )"; + + Query jQ = em.createQuery(jpql); + jQ.setParameter("lastName", "LastName"); + jQ.setParameter("firstName", "FirstName"); + List jList = jQ.getResultList(); + + Query jQ1 = em.createQuery(jpql); + jQ1.setParameter("lastName", "LastName1"); + jQ1.setParameter("firstName", "FirstName1"); + try { + List jList1 = jQ1.getResultList(); + } catch (Exception e) { + fail("Fail to execute again - Parameters are messed up:" + e.getMessage()); + } + } + + PreparedQueryCache getPreparedQueryCache() { return emf.getConfiguration().getQuerySQLCacheInstance(); }