openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r739784 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/o...
Date Sun, 01 Feb 2009 16:50:45 GMT
Author: ppoddar
Date: Sun Feb  1 16:50:45 2009
New Revision: 739784

URL: http://svn.apache.org/viewvc?rev=739784&view=rev
Log:
OPENJPA-703: A separate executor for Prepared SQL.

Added:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedSQLStoreQuery.java
Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PreparedQuery.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java?rev=739784&r1=739783&r2=739784&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
Sun Feb  1 16:50:45 2009
@@ -794,9 +794,10 @@
         ExpressionParser ep = QueryLanguages.parserForLanguage(language);
         if (ep != null)
             return new JDBCStoreQuery(this, ep);
-        if (QueryLanguages.LANG_SQL.equals(language)
-         || QueryLanguages.LANG_PREPARED_SQL.equals(language))
+        if (QueryLanguages.LANG_SQL.equals(language))
             return new SQLStoreQuery(this);
+        if (QueryLanguages.LANG_PREPARED_SQL.equals(language))
+            return new PreparedSQLStoreQuery(this);
         return null;
     }
 

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java?rev=739784&r1=739783&r2=739784&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
Sun Feb  1 16:50:45 2009
@@ -32,6 +32,7 @@
 import org.apache.openjpa.kernel.Broker;
 import org.apache.openjpa.kernel.PreparedQuery;
 import org.apache.openjpa.kernel.Query;
+import org.apache.openjpa.kernel.QueryLanguages;
 import org.apache.openjpa.lib.rop.ResultList;
 import org.apache.openjpa.util.ImplHelper;
 import org.apache.openjpa.util.InternalException;
@@ -88,6 +89,10 @@
         return _id;
     }
     
+    public String getLanguage() {
+        return QueryLanguages.LANG_PREPARED_SQL;
+    }
+    
     /**
      * Get the original query string which is same as the identifier of this 
      * receiver.
@@ -207,13 +212,13 @@
     /**
      * Marks the positions of user parameters.
      * 
-     * @param list odd elements are numbers representing the position of a 
-     * user parameter in the _param list. Even elements are the user parameter
+     * @param list even elements are numbers representing the position of a 
+     * user parameter in the _param list. Odd elements are the user parameter
      * key. A user parameter key may appear more than once.
      */
     void setUserParameterPositions(List list) {
         _userParamPositions = new HashMap<Object, int[]>();
-        for (int i = 1; list != null && i < list.size(); i+=2) {
+        for (int i = 1; list != null && i < list.size(); i += 2) {
             Object key = list.get(i);
             int p = (Integer)list.get(i-1);
             int[] positions = _userParamPositions.get(key);
@@ -238,5 +243,4 @@
         return "PreparedQuery: [" + getOriginalQuery() + "] --> [" + 
                getTargetQuery() + "]";
     }
-
 }

Added: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedSQLStoreQuery.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedSQLStoreQuery.java?rev=739784&view=auto
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedSQLStoreQuery.java
(added)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedSQLStoreQuery.java
Sun Feb  1 16:50:45 2009
@@ -0,0 +1,106 @@
+/*
+ * 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.jdbc.kernel;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.sql.DBDictionary;
+import org.apache.openjpa.jdbc.sql.ResultSetResult;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.SQLExceptions;
+import org.apache.openjpa.kernel.StoreQuery;
+import org.apache.openjpa.lib.rop.RangeResultObjectProvider;
+import org.apache.openjpa.lib.rop.ResultObjectProvider;
+import org.apache.openjpa.meta.ClassMetaData;
+
+/**
+ * A executor for Prepared SQL Query.
+ * 
+ * @author Pinaki Poddar
+ *
+ */
+public class PreparedSQLStoreQuery extends SQLStoreQuery {
+    public PreparedSQLStoreQuery(JDBCStore store) {
+        super(store);
+    }
+    
+    public Executor newDataStoreExecutor(ClassMetaData meta,
+        boolean subclasses) {
+        return new PreparedSQLExecutor(this, meta);
+    }
+    
+    public static class PreparedSQLExecutor extends AbstractExecutor {
+        private final ClassMetaData _meta;
+        public PreparedSQLExecutor(PreparedSQLStoreQuery q, 
+            ClassMetaData candidate) {
+            _meta = candidate;
+        }
+               
+        public int getOperation(StoreQuery q) {
+            return OP_SELECT;
+        }
+
+        public ResultObjectProvider executeQuery(StoreQuery q,
+            Object[] params, Range range) {
+            JDBCStore store = ((SQLStoreQuery) q).getStore();
+            DBDictionary dict = store.getDBDictionary();
+
+            SQLBuffer buf = new SQLBuffer(dict)
+                .append(q.getContext().getQueryString());
+            Connection conn = store.getConnection();
+            JDBCFetchConfiguration fetch = (JDBCFetchConfiguration)
+                q.getContext().getFetchConfiguration();
+
+            ResultObjectProvider rop;
+            PreparedStatement stmnt = null;
+            try {
+                stmnt = !range.lrs
+                    ? buf.prepareStatement(conn)
+                    : buf.prepareStatement(conn, fetch, -1, -1);
+
+                int index = 0;
+                for (int i = 0; i < params.length; i++)
+                    dict.setUnknown(stmnt, ++index, params[i], null);
+
+                ResultSet rs = stmnt.executeQuery();
+                ResultSetResult res = new ResultSetResult(conn, stmnt, rs, 
+                    store);
+                if (q.getContext().getCandidateType() != null)
+                    rop = new GenericResultObjectProvider((ClassMapping) _meta,
+                        store, fetch, res);
+                else
+                    rop = new SQLProjectionResultObjectProvider(store, fetch,
+                        res, q.getContext().getResultType());
+            } catch (SQLException se) {
+                if (stmnt != null)
+                    try { stmnt.close(); } catch (SQLException se2) {}
+                try { conn.close(); } catch (SQLException se2) {}
+                throw SQLExceptions.getStore(se, dict);
+            }
+
+            if (range.start != 0 || range.end != Long.MAX_VALUE)
+                rop = new RangeResultObjectProvider(rop, range.start,range.end);
+            return rop;
+        }
+    }
+}

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java?rev=739784&r1=739783&r2=739784&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java Sun
Feb  1 16:50:45 2009
@@ -63,8 +63,8 @@
     private List _params = null;
     private List _cols = null;
     
-    // Odd element refers to an index of the _params list
-    // Even element refers to the user parameter key
+    // Even element refers to an index of the _params list
+    // Odd element refers to the user parameter key
     private List _userIndex = null;
     
     /**

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PreparedQuery.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PreparedQuery.java?rev=739784&r1=739783&r2=739784&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PreparedQuery.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PreparedQuery.java
Sun Feb  1 16:50:45 2009
@@ -65,6 +65,8 @@
      */
     public String getOriginalQuery();
     
+    public String getLanguage();
+    
     /**
      * Fill in the post-compilation state of the given Query. This must be
      * called when a original query is substituted by this receiver and hence 

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java?rev=739784&r1=739783&r2=739784&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
Sun Feb  1 16:50:45 2009
@@ -70,8 +70,8 @@
 	private Company IBM;
 	
 	public void setUp() throws Exception {
-		super.setUp(Company.class, Department.class, Employee.class,
-				Address.class, 
+		super.setUp(CLEAR_TABLES, Company.class, Department.class, 
+		    Employee.class, Address.class, 
 				"openjpa.QuerySQLCache", 
 				"true(excludes='select count(p) from Company p;select count(p) from Department p')");
 		createTestData();

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java?rev=739784&r1=739783&r2=739784&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
Sun Feb  1 16:50:45 2009
@@ -875,18 +875,15 @@
         assertNotCloseInvoked();
         try {
             String qid = query;
-            PreparedQuery cached = getPreparedQuery(qid);
-            if (cached != null) {
-                language = QueryLanguages.LANG_PREPARED_SQL;
-                query = cached.getTargetQuery();
-            }
-            org.apache.openjpa.kernel.Query q = _broker.newQuery(language, 
-                query);
+            PreparedQuery pq = getPreparedQuery(qid);
+            org.apache.openjpa.kernel.Query q = (pq == null)
+                ? _broker.newQuery(language, query)
+                : _broker.newQuery(pq.getLanguage(), pq.getTargetQuery());
             // have to validate JPQL according to spec
             if (JPQLParser.LANG_JPQL.equals(language))
                 q.compile(); 
-            if (cached != null) {
-                cached.setInto(q);
+            if (pq != null) {
+                pq.setInto(q);
             }
             return new QueryImpl(this, _ret, q).setId(qid);
         } catch (RuntimeException re) {
@@ -910,19 +907,15 @@
             QueryMetaData meta = _broker.getConfiguration().
                 getMetaDataRepositoryInstance().getQueryMetaData(null, name,
                 _broker.getClassLoader(), true);
-            String query = null;
-            String language = meta.getLanguage();
             String qid = meta.getQueryString();
             
-            PreparedQuery cached = getPreparedQuery(qid);
-            if (cached != null) {
-                language = QueryLanguages.LANG_PREPARED_SQL;
-                query = cached.getTargetQuery();
-            }
-            org.apache.openjpa.kernel.Query del = _broker.newQuery(language, 
-                query);
-            if (cached != null) {
-                cached.setInto(del);
+            PreparedQuery pq = getPreparedQuery(qid);
+            org.apache.openjpa.kernel.Query del = (pq == null)
+                ? _broker.newQuery(meta.getLanguage(), meta.getQueryString())
+                : _broker.newQuery(pq.getLanguage(), pq.getTargetQuery());
+            
+            if (pq != null) {
+                pq.setInto(del);
             } else {
                 meta.setInto(del);
             }



Mime
View raw message