openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r835257 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/kernel/TestUpdateWithSubSelect.java
Date Thu, 12 Nov 2009 06:35:32 GMT
Author: ppoddar
Date: Thu Nov 12 06:35:31 2009
New Revision: 835257

URL: http://svn.apache.org/viewvc?rev=835257&view=rev
Log:
OPENJPA-459: Bulk update using a join instead of a subselect

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/kernel/TestUpdateWithSubSelect.java
  (with props)
Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?rev=835257&r1=835256&r2=835257&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
Thu Nov 12 06:35:31 2009
@@ -1986,7 +1986,8 @@
         // (SELECT 1 FROM TABLE t0 WHERE t0.ID = TABLE.ID); also, some
         // databases do not allow aliases in delete statements, which
         // also causes us to use a subselect
-        if (sel.getTableAliases().size() == 1 && supportsSubselect
+        Set<String> selectedTables = getSelectTableAliases(sel);
+        if (selectedTables.size() == 1 && supportsSubselect
             && allowsAliasInBulkClause) {
             SQLBuffer from;
             if (sel.getFromSelect() != null)
@@ -2068,6 +2069,16 @@
         }
         return sql;
     }
+    
+    Set<String> getSelectTableAliases(Select sel) {
+        Set<String> result = new HashSet<String>();
+        List<String> selects = sel.getIdentifierAliases();
+        for (String s : selects) {
+            String tableAlias = s.substring(0, s.indexOf('.'));
+            result.add(tableAlias);
+        }
+        return result;
+    }
 
     protected SQLBuffer getDeleteTargets(Select sel) {
       SQLBuffer deleteTargets = new SQLBuffer(this);

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/kernel/TestUpdateWithSubSelect.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/kernel/TestUpdateWithSubSelect.java?rev=835257&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/kernel/TestUpdateWithSubSelect.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/kernel/TestUpdateWithSubSelect.java
Thu Nov 12 06:35:31 2009
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.jdbc.kernel;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.sql.DBDictionary;
+import org.apache.openjpa.persistence.jdbc.sqlcache.Address;
+import org.apache.openjpa.persistence.jdbc.sqlcache.Person;
+import org.apache.openjpa.persistence.test.SQLListenerTestCase;
+
+/**
+ * Tests bulk update with join instead of subselect.
+ * Reported in <A HREF="https://issues.apache.org/jira/browse/OPENJPA-459">OPENJPA-459</A>
+ * that update in MySQL fails when the UPDATE SQL included a SubSelect because of
+ * limitation mentioned in <A HREF="http://lists.mysql.com/mysql/185623">MySQL forum</A>.
+ * <br>
+ * The solution is {@link DBDictionary#toBulkOperation()} modified to accommodate
+ * scenarios that selects from a single table but may involve joins of multiple tables.
+ *  
+ * @author Pinaki Poddar
+ *
+ */
+public class TestUpdateWithSubSelect extends SQLListenerTestCase {
+
+    public void setUp() throws Exception {
+        super.setUp(CLEAR_TABLES, Person.class, Address.class);
+        DBDictionary dict = ((JDBCConfiguration)emf.getConfiguration()).getDBDictionaryInstance();
+        setTestsDisabled(!dict.supportsSubselect || !dict.allowsAliasInBulkClause);
+        getLog().trace(this + " is disabled because " + dict.getClass().getSimpleName() +

+          " either both or one of supportsSubselect and allowsAliasInBulkClause is false");
+    }
+    
+    /**
+     * Tests that a bulk update issues a single SQL and that uses a join. 
+     */
+    public void testUpdateBySubSelect() {
+        String jpql = "UPDATE Person p SET p.age = :age WHERE p.address.city = :city";
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        sql.clear();
+        em.createQuery(jpql)
+          .setParameter("age", (short)25)
+          .setParameter("city", "SFO")
+          .executeUpdate();
+        em.getTransaction().commit();
+        assertEquals(1,sql.size());
+        String sqlString = sql.get(0).toUpperCase().trim();
+        assertTrue(sqlString.startsWith("UPDATE"));
+        assertTrue(sqlString.indexOf("JOIN") != -1);
+    }
+
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/kernel/TestUpdateWithSubSelect.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message