openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mik...@apache.org
Subject svn commit: r747421 - in /openjpa/branches/1.0.x: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/ openjpa-persistence-jdbc/src/te...
Date Tue, 24 Feb 2009 16:37:44 GMT
Author: mikedd
Date: Tue Feb 24 16:37:41 2009
New Revision: 747421

URL: http://svn.apache.org/viewvc?rev=747421&view=rev
Log:
OPENJPA-917. Merging B.J.'s changes

Added:
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryProcedures.java
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Applicant.java
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/DerbyProcedureList.java
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/ProcedureList.java
Modified:
    openjpa/branches/1.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java

Added: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryProcedures.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryProcedures.java?rev=747421&view=auto
==============================================================================
--- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryProcedures.java
(added)
+++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryProcedures.java
Tue Feb 24 16:37:41 2009
@@ -0,0 +1,168 @@
+/*
+ * 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.query;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.sql.DerbyDictionary;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.persistence.jdbc.query.domain.Applicant;
+import org.apache.openjpa.persistence.jdbc.query.procedure.DerbyProcedureList;
+import org.apache.openjpa.persistence.jdbc.query.procedure.ProcedureList;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Tests that Native queries use only 1-based positional parameters and 
+ * disallows named parameters.
+ * 
+ * Originally reported in 
+ * <A HRE="http://issues.apache.org/jira/browse/OPENJPA-112>OPENJPA-112</A>
+ *  
+ * @author B.J. Reed
+ *
+ */
+public class TestNativeQueryProcedures extends SingleEMFTestCase {
+    ProcedureList procedureList = null;
+    
+    @Override
+    public void setUp() throws Exception {
+        super.setUp(Applicant.class, CLEAR_TABLES);
+
+        // Figure out which DB we have and get the proper DB Procedure List
+        OpenJPAEntityManagerFactorySPI ojpaEmf = 
+            (OpenJPAEntityManagerFactorySPI) emf;
+        JDBCConfiguration conf = (JDBCConfiguration) ojpaEmf.getConfiguration();
+        
+        if (conf.getDBDictionaryInstance() instanceof DerbyDictionary) {
+            procedureList = new DerbyProcedureList();
+        }
+
+        if (procedureList != null) {
+            EntityManager em = emf.createEntityManager();
+            List<String> createList = procedureList.getCreateProcedureList();
+            try {
+                for (String createProcedure : createList) {
+                    em.getTransaction().begin();
+                    Query query = em.createNativeQuery(createProcedure);
+                    query.executeUpdate();
+                    em.getTransaction().commit();
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+                em.getTransaction().commit();
+            }
+            em.clear();
+            em.close();
+        }
+    }
+
+    public void tearDown() throws Exception {
+        if (procedureList != null) {
+            EntityManager em = emf.createEntityManager();
+            List<String> dropList = procedureList.getDropProcedureList();
+            try {
+                for (String dropProcedure : dropList) {
+                    em.getTransaction().begin();
+                    Query query = em.createNativeQuery(dropProcedure);
+                    query.executeUpdate();
+                    em.getTransaction().commit();
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+                em.getTransaction().commit();
+            }
+            em.clear();
+            em.close();
+        }
+        super.tearDown();
+    }
+    
+    public void testNoReturnNoParamProcedure() {
+        if (procedureList != null) {
+            EntityManager em = emf.createEntityManager();
+
+            Applicant applicant1 = new Applicant();
+            applicant1.setName("Charlie");
+            Applicant applicant2 = new Applicant();
+            applicant2.setName("Snoopy");
+
+            em.getTransaction().begin();
+            em.persist(applicant1);
+            em.persist(applicant2);
+            em.getTransaction().commit();
+
+            String sql = procedureList.callAddXToCharlie();
+
+            // query.getSingleResult() and query.getResultList() both throw an
+            // exception: Statement.executeQuery() cannot be called with a
+            // statement that returns a row count
+            try {
+                em.getTransaction().begin();
+                Query query = em.createNativeQuery(sql);
+                query.getSingleResult();
+                em.getTransaction().commit();
+                fail("Expected exception. getSingleResult() with no returns "+ 
+                    "should fail.");
+            } catch (Exception e) {
+                //Expected exception
+                em.getTransaction().rollback();
+            }
+            try {
+                em.getTransaction().begin();
+                Query query = em.createNativeQuery(sql);
+                query.getResultList();
+                em.getTransaction().commit();
+                fail("Expected exception. getResultList() with no returns " + 
+                    "should fail.");
+            } catch (Exception e) {
+                //Expected exception
+                em.getTransaction().rollback();
+            }
+
+            // This one should work properly
+            try {
+                em.getTransaction().begin();
+                Query query = em.createNativeQuery(sql);
+                query.executeUpdate();
+                em.getTransaction().commit();
+            } catch (Exception e) {
+                fail("Caught unexpected exception executing stored procedure: "
+                    + e.getMessage());
+                em.getTransaction().commit();
+            }
+        
+            em.clear();
+            em.close();
+            em = emf.createEntityManager();
+            applicant1 = em.find(Applicant.class, applicant1.getId());
+            applicant2 = em.find(Applicant.class, applicant2.getId());
+
+            // verify one changed and one didn't
+            assertEquals("Charliex", applicant1.getName());
+            assertEquals("Snoopy", applicant2.getName());
+        
+            em.clear();
+            em.close();
+        }
+    }
+}
\ No newline at end of file

Added: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Applicant.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Applicant.java?rev=747421&view=auto
==============================================================================
--- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Applicant.java
(added)
+++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Applicant.java
Tue Feb 24 16:37:41 2009
@@ -0,0 +1,50 @@
+/*
+ * 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.query.domain;
+
+import javax.persistence.*;
+
+/**
+ * Simple persistent entity as a target of uni-directional one-to-one 
+ * association.
+ * 
+ * @author Pinaki Poddar
+ *
+ */
+@Entity
+public class Applicant {
+	@Id
+	@GeneratedValue
+	private long id;
+
+	private String name;
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public long getId() {
+		return id;
+	}
+
+}

Added: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/DerbyProcedureList.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/DerbyProcedureList.java?rev=747421&view=auto
==============================================================================
--- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/DerbyProcedureList.java
(added)
+++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/DerbyProcedureList.java
Tue Feb 24 16:37:41 2009
@@ -0,0 +1,66 @@
+
+/*
+ * 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.query.procedure;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.util.ArrayList;
+import java.util.List;
+
+/*
+ * holds the stored procedures that will be used by test cases
+ */
+public class DerbyProcedureList extends ProcedureList {
+
+    public List<String> getCreateProcedureList () {
+        ArrayList<String> retList = new ArrayList<String>();
+
+        retList.add ("create procedure ADD_X_TO_CHARLIE () " +
+                     "PARAMETER STYLE JAVA LANGUAGE JAVA MODIFIES SQL DATA " +
+                     "EXTERNAL NAME 'org.apache.openjpa.persistence.jdbc." + 
+                     "query.procedure.DerbyProcedureList.addXToCharlie'");
+        
+
+        return retList;
+    }
+
+    public List<String> getDropProcedureList () {
+        ArrayList<String> retList = new ArrayList<String>();
+
+        retList.add ("drop procedure ADD_X_TO_CHARLIE");
+
+        return retList;
+    }
+
+    public String callAddXToCharlie () {
+        return "{ call ADD_X_TO_CHARLIE () }";
+    }
+
+    public static void addXToCharlie () throws Exception {
+        Connection conn = DriverManager.getConnection(
+            "jdbc:default:connection");
+        PreparedStatement ps1 = conn.prepareStatement("update APPLICANT set " + 
+            "name = 'Charliex' where name = 'Charlie'");
+        ps1.executeUpdate();
+
+        conn.close();
+    }
+}

Added: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/ProcedureList.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/ProcedureList.java?rev=747421&view=auto
==============================================================================
--- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/ProcedureList.java
(added)
+++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/ProcedureList.java
Tue Feb 24 16:37:41 2009
@@ -0,0 +1,41 @@
+/*
+ * 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.query.procedure;
+
+import java.util.List;
+
+/*
+ * holds the stored procedures that will be used by test cases
+ */
+public abstract class ProcedureList {
+
+    abstract public List<String> getCreateProcedureList ();
+
+    abstract public List<String> getDropProcedureList ();
+
+    abstract public String callAddXToCharlie ();
+
+    // This method should also be overriden, but it needs to be static so 
+    // that it can be called as a stored procedure
+    public static void addXToCharlie () throws Exception {
+        Exception e = new Exception ("Method not implemented by inheriting " + 
+            "class");
+        throw e;
+    }
+}

Modified: openjpa/branches/1.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java?rev=747421&r1=747420&r2=747421&view=diff
==============================================================================
--- openjpa/branches/1.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
(original)
+++ openjpa/branches/1.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
Tue Feb 24 16:37:41 2009
@@ -39,6 +39,7 @@
 import org.apache.openjpa.kernel.DelegatingQuery;
 import org.apache.openjpa.kernel.DelegatingResultList;
 import org.apache.openjpa.kernel.Filters;
+import org.apache.openjpa.kernel.QueryLanguages;
 import org.apache.openjpa.kernel.QueryOperations;
 import org.apache.openjpa.kernel.exps.AggregateListener;
 import org.apache.openjpa.kernel.exps.FilterListener;
@@ -224,7 +225,7 @@
     }
 
     private Object execute() {
-        if (_query.getOperation() != QueryOperations.OP_SELECT)
+        if (! isNative() && _query.getOperation() != QueryOperations.OP_SELECT)
             throw new InvalidStateException(_loc.get("not-select-query",
                 _query.getQueryString()), null, null, false);
 
@@ -485,6 +486,10 @@
             _query.unlock();
         }
     }
+    
+    public boolean isNative() {
+        return QueryLanguages.LANG_SQL.equals(getLanguage());
+    }
 
     public boolean hasPositionalParameters() {
         return _positional != null;



Mime
View raw message