openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wisnes...@apache.org
Subject svn commit: r497605 - /incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/SQLStoreQuery.java
Date Thu, 18 Jan 2007 22:21:09 GMT
Author: wisneskid
Date: Thu Jan 18 14:21:08 2007
New Revision: 497605

URL: http://svn.apache.org/viewvc?view=rev&rev=497605
Log:
Support native queries making stored procedure calls, or insert, update, delete.
For native queries returning ResultSet should use getResultList or getSingleResult interfaces
on Query.
For native queries performing updates without returning any results use executeUpdate interface
on Query.

Modified:
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/SQLStoreQuery.java

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/SQLStoreQuery.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/SQLStoreQuery.java?view=diff&rev=497605&r1=497604&r2=497605
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/SQLStoreQuery.java
(original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/SQLStoreQuery.java
Thu Jan 18 14:21:08 2007
@@ -43,6 +43,7 @@
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.meta.ClassMetaData;
 import org.apache.openjpa.util.UserException;
+import serp.util.Numbers;
 
 /**
  * A SQL query.
@@ -158,6 +159,7 @@
 
         private final ClassMetaData _meta;
         private final boolean _select;
+        private final boolean _call;   // native call stored procedure
         private final QueryResultMapping _resultMapping;
 
         public SQLExecutor(SQLStoreQuery q, ClassMetaData candidate) {
@@ -180,6 +182,63 @@
                 throw new UserException(_loc.get("no-sql"));
             _select = sql.length() > 6
                 && sql.substring(0, 6).equalsIgnoreCase("select");
+            _call = sql.length() > 4
+                && sql.substring(0, 4).equalsIgnoreCase("call");
+        }
+
+        public int getOperation(StoreQuery q) {
+           return _select ? OP_SELECT : 
+                (q.getContext().getCandidateType() != null
+                        || q.getContext().getResultType() != null
+                        || q.getContext().getResultMappingName() != null 
+                        || q.getContext().getResultMappingScope() != null)
+                        ? OP_SELECT : OP_UPDATE;
+        }
+
+        public Number executeUpdate(StoreQuery q, Object[] params) {
+            JDBCStore store = ((SQLStoreQuery) q).getStore();
+            DBDictionary dict = store.getDBDictionary();
+            String sql = q.getContext().getQueryString();
+
+            List paramList;
+            if (params.length > 0) {
+                paramList = new ArrayList(Arrays.asList(params));
+                try {
+                    sql = substituteParams(sql, paramList);
+                } catch (IOException ioe) {
+                    throw new UserException(ioe);
+                }
+            } else
+                paramList = Collections.EMPTY_LIST;
+
+            SQLBuffer buf = new SQLBuffer(dict).append(sql);
+            Connection conn = store.getConnection();
+            JDBCFetchConfiguration fetch = (JDBCFetchConfiguration)
+                q.getContext().getFetchConfiguration();
+
+            PreparedStatement stmnt = null;
+            try {
+                stmnt = buf.prepareCall(conn);
+
+                int index = 0;
+                for (Iterator i = paramList.iterator(); i.hasNext();)
+                    dict.setUnknown(stmnt, ++index, i.next(), null);
+                
+                int count = 0;
+                if (_call && stmnt.execute() == false) {
+                    count = stmnt.getUpdateCount();
+                }
+                else {
+                    // native insert, update, delete
+                    count = stmnt.executeUpdate();
+                }
+                return Numbers.valueOf(count);
+            } catch (SQLException se) {
+                if (stmnt != null)
+                    try { stmnt.close(); } catch (SQLException se2) {}
+                try { conn.close(); } catch (SQLException se2) {}
+                throw SQLExceptions.getStore(se, dict);
+            }
         }
 
         public ResultObjectProvider executeQuery(StoreQuery q,



Mime
View raw message