openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hthom...@apache.org
Subject svn commit: r1436150 - in /openjpa/branches/2.1.x: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence/src/m...
Date Mon, 21 Jan 2013 03:16:21 GMT
Author: hthomann
Date: Mon Jan 21 03:16:21 2013
New Revision: 1436150

URL: http://svn.apache.org/viewvc?rev=1436150&view=rev
Log:
OPENJPA-2324: Option to express literal in query string directly into generate SQL - applied
Albert's patch to 2.1.x

Modified:
    openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
    openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
    openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryHints.java
    openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/HintHandler.java
    openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java

Modified: openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java?rev=1436150&r1=1436149&r2=1436150&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
(original)
+++ openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
Mon Jan 21 03:16:21 2013
@@ -22,6 +22,7 @@ import org.apache.openjpa.jdbc.sql.Raw;
 import org.apache.openjpa.jdbc.sql.SQLBuffer;
 import org.apache.openjpa.jdbc.sql.Select;
 import org.apache.openjpa.kernel.Filters;
+import org.apache.openjpa.kernel.QueryHints;
 import org.apache.openjpa.kernel.exps.Literal;
 
 /**
@@ -141,6 +142,9 @@ public class Lit
                 _rawVal = lstate.sqlValue;
             }
         }
-        sql.appendValue(lstate.sqlValue, lstate.getColumn(index));
+        Object useLiteral = ctx.fetch.getHint(QueryHints.HINT_USE_LITERAL_IN_SQL);
+//        useLiteral = true;
+        boolean useParamToken = useLiteral != null ? !(Boolean)useLiteral : true; 
+        sql.appendValue(lstate.sqlValue, lstate.getColumn(index), null, useParamToken);
     }
 }

Modified: openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java?rev=1436150&r1=1436149&r2=1436150&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
(original)
+++ openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
Mon Jan 21 03:16:21 2013
@@ -37,6 +37,7 @@ import org.apache.openjpa.jdbc.kernel.ex
 import org.apache.openjpa.jdbc.schema.Column;
 import org.apache.openjpa.jdbc.schema.Sequence;
 import org.apache.openjpa.jdbc.schema.Table;
+import org.apache.openjpa.kernel.Filters;
 import org.apache.openjpa.kernel.exps.Parameter;
 
 
@@ -269,7 +270,7 @@ public final class SQLBuffer
     public SQLBuffer appendValue(Object o) {
         return appendValue(o, null);
     }
-    
+
     /**
      * Append a system inserted parameter value for a specific column.
      */
@@ -286,45 +287,78 @@ public final class SQLBuffer
      * @param userParam if non-null, designates a 'user' parameter.
      */
     public SQLBuffer appendValue(Object o, Column col, Parameter userParam) {
+        return appendValue(o, col, userParam, true);
+    }
+
+    public SQLBuffer appendValue(Object o, Column col, Parameter userParam, boolean useParamToken)
{
         if (o == null)
             _sql.append("NULL");
         else if (o instanceof Raw)
             _sql.append(o.toString());
         else {
-            _sql.append(PARAMETER_TOKEN);
-
-            // initialize param and col lists; we hold off on col list until
-            // we get the first non-null col
-            if (_params == null)
-                _params = new ArrayList();
-            if (_userParams == null)
-                _userParams = new ArrayList();
-            if (col != null && _cols == null) {
-                _cols = new ArrayList();
-                while (_cols.size() < _params.size())
-                    _cols.add(null);
-            }
+            Class<?> type = Filters.wrap(o.getClass());
+//            System.out.println("======1> o="+o+", cls="+o.getClass());
+            if (useParamToken || !validParamLiteralType(type)) {
+                _sql.append(PARAMETER_TOKEN);
+
+                // initialize param and col lists; we hold off on col list until
+                // we get the first non-null col
+                if (_params == null)
+                    _params = new ArrayList();
+                if (_userParams == null)
+                    _userParams = new ArrayList();
+                if (col != null && _cols == null) {
+                    _cols = new ArrayList();
+                    while (_cols.size() < _params.size())
+                        _cols.add(null);
+                }
 
-            _params.add(o);
-            if (userParam != null) {
-                Object param = userParam;
-                if (userParam instanceof CollectionParam)
-                    param = ((CollectionParam) userParam).clone();
-                _userParams.add(param);
-                if (_userIndex == null)
-                    _userIndex = new ArrayList();
-                int index = _params.size()-1;
-                _userIndex.add(index);
-                _userIndex.add(param);
+                _params.add(o);
+                if (userParam != null) {
+                    Object param = userParam;
+                    if (userParam instanceof CollectionParam)
+                        param = ((CollectionParam) userParam).clone();
+                    _userParams.add(param);
+                    if (_userIndex == null)
+                        _userIndex = new ArrayList();
+                    int index = _params.size()-1;
+                    _userIndex.add(index);
+                    _userIndex.add(param);
+                }
+                else
+                    _userParams.add(o);
+                if (_cols != null)
+                    _cols.add(col);
+            } else {
+                if (type == String.class) {
+                    _sql.append("'" + o.toString().replace("'", "''") + "'");
+                } else if ( type == Character.class ) {
+                    if (_dict.storeCharsAsNumbers) {
+                        _sql.append(Integer.toString(((Character)o).charValue()));
+                    } else {
+                        _sql.append("'" + o.toString().replace("'", "''") + "'");
+                    }
+                } else {
+                    _sql.append(o.toString());
+                }
+//                System.out.println("======3> _sql="+_sql.toString());
             }
-            else
-                _userParams.add(o);
-            if (_cols != null)
-                _cols.add(col);
         }
         return this;
     }
 
+    private boolean validParamLiteralType(Class<?> type) {
+        boolean ret = type == String.class
+                || type == Integer.class 
+                || type == Character.class 
+                || type == Boolean.class
+                || type == Short.class
+                || type == Long.class
+                || type == Byte.class;
+//        System.out.println("======2> type="+type+", ret="+ret);
+        return ret;
+    }
+
     /**
      * Append a parameter value.
      */

Modified: openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryHints.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryHints.java?rev=1436150&r1=1436149&r2=1436150&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryHints.java
(original)
+++ openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryHints.java
Mon Jan 21 03:16:21 2013
@@ -102,4 +102,10 @@ public interface QueryHints {
      * it binds to.
      */
     public static final String HINT_RELAX_BIND_PARAM_TYPE_CHECK = "openjpa.hint.RelaxParameterTypeChecking";
+
+    /**
+     * A boolean directive to generate literal directly into the SQL statement instead of
using position parameter,
+     * if possible.
+     */
+    public static final String HINT_USE_LITERAL_IN_SQL = "openjpa.hint.UseLiteralInSQL";
 }

Modified: openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/HintHandler.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/HintHandler.java?rev=1436150&r1=1436149&r2=1436150&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/HintHandler.java
(original)
+++ openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/HintHandler.java
Mon Jan 21 03:16:21 2013
@@ -19,8 +19,6 @@
 package org.apache.openjpa.persistence;
 
 import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
@@ -177,6 +175,9 @@ public class HintHandler  {
         } else if (QueryHints.HINT_IGNORE_PREPARED_QUERY.equals(key)) {
             plan.setHint(key, Filters.convert(value, Boolean.class));
             owner.ignorePreparedQuery();
+        } else if (QueryHints.HINT_USE_LITERAL_IN_SQL.equals(key)) {
+            Boolean convertedValue = (Boolean)Filters.convert(value, Boolean.class);
+            plan.setHint(key, convertedValue);
         } else { // default 
             plan.setHint(key, value);
         }

Modified: openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java?rev=1436150&r1=1436149&r2=1436150&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
(original)
+++ openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
Mon Jan 21 03:16:21 2013
@@ -146,6 +146,7 @@ public class PersistenceProductDerivatio
         _hints.add(QueryHints.HINT_RECACHE_FINDER);
         _hints.add(QueryHints.HINT_RESULT_COUNT);
         _hints.add(QueryHints.HINT_SUBCLASSES);
+        _hints.add(QueryHints.HINT_USE_LITERAL_IN_SQL);
 
         _hints = Collections.unmodifiableSet(_hints);
     }



Mime
View raw message