openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hthom...@apache.org
Subject svn commit: r1436149 - in /openjpa/branches/1.2.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/
Date Mon, 21 Jan 2013 03:13:08 GMT
Author: hthomann
Date: Mon Jan 21 03:13:08 2013
New Revision: 1436149

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

Modified:
    openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
    openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
    openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java
    openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryHints.java

Modified: openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java?rev=1436149&r1=1436148&r2=1436149&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
(original)
+++ openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
Mon Jan 21 03:13:08 2013
@@ -21,6 +21,7 @@ package org.apache.openjpa.jdbc.kernel.e
 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;
 
 /**
@@ -98,7 +99,12 @@ public class Lit
         if (lstate.otherLength > 1)
             sql.appendValue(((Object[]) lstate.sqlValue)[index], 
                 lstate.getColumn(index));
-        else
-            sql.appendValue(lstate.sqlValue, lstate.getColumn(index));
+        else {
+            Object useLiteral = ctx.fetch.getHint(QueryHints.HINT_USE_LITERAL_IN_SQL);
+            useLiteral = Filters.convert(useLiteral, Boolean.class);
+//            useLiteral = true;
+            boolean useParamToken = useLiteral != null ? !(((Boolean)useLiteral).booleanValue())
: true; 
+            sql.appendValue(lstate.sqlValue, lstate.getColumn(index), useParamToken);
+        }
     }
 }

Modified: openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java?rev=1436149&r1=1436148&r2=1436149&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
(original)
+++ openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
Mon Jan 21 03:13:08 2013
@@ -36,6 +36,8 @@ import org.apache.openjpa.jdbc.schema.Co
 import org.apache.openjpa.jdbc.schema.ForeignKey;
 import org.apache.openjpa.jdbc.schema.Sequence;
 import org.apache.openjpa.jdbc.schema.Table;
+import org.apache.openjpa.kernel.Filters;
+
 import serp.util.Numbers;
 
 /**
@@ -230,26 +232,46 @@ public final class SQLBuffer
      * Append a parameter value for a specific column.
      */
     public SQLBuffer appendValue(Object o, Column col) {
+        return appendValue(o, col, true);
+    }
+
+    public SQLBuffer appendValue(Object o, Column col, boolean useParamToken) {
         if (o == null)
             _sql.append("NULL");
         else if (o instanceof Raw)
             _sql.append(o.toString());
         else {
-            _sql.append(PARAMETER_TOKEN);
+            Class type = Filters.wrap(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 (col != null && _cols == null) {
+                    _cols = new ArrayList();
+                    while (_cols.size() < _params.size())
+                        _cols.add(null);
+                }
 
-            // 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 (col != null && _cols == null) {
-                _cols = new ArrayList();
-                while (_cols.size() < _params.size())
-                    _cols.add(null);
+                _params.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());
+                }
             }
 
-            _params.add(o);
-            if (_cols != null)
-                _cols.add(col);
             if (col == null)
                 return this;
             boolean isFK = false;
@@ -269,6 +291,17 @@ public final class SQLBuffer
         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;
+        return ret;
+    }
+
     /**
      * Append a parameter value.
      */

Modified: openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java?rev=1436149&r1=1436148&r2=1436149&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java
(original)
+++ openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java
Mon Jan 21 03:13:08 2013
@@ -253,6 +253,8 @@ public class Filters {
         if (!num) {
             if (type == String.class)
                 return o.toString();
+            else if (type == Boolean.class && o instanceof String)
+                return Boolean.valueOf(o.toString());
             else if (type == Character.class) {
                 String str = o.toString();
                 if (str != null && str.length() == 1)

Modified: openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryHints.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryHints.java?rev=1436149&r1=1436148&r2=1436149&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryHints.java
(original)
+++ openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryHints.java
Mon Jan 21 03:13:08 2013
@@ -28,4 +28,10 @@ public interface QueryHints {
      */
     public static final String HINT_RESULT_COUNT =
         "openjpa.hint.OptimizeResultCount";
+
+    /**
+     * 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";
 }



Mime
View raw message