openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kwsut...@apache.org
Subject svn commit: r567928 - in /openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc: kernel/exps/ sql/
Date Tue, 21 Aug 2007 03:15:38 GMT
Author: kwsutter
Date: Mon Aug 20 20:15:37 2007
New Revision: 567928

URL: http://svn.apache.org/viewvc?rev=567928&view=rev
Log:
OPENJPA-321.  Committing Teresa's patch with a few minor formatting updates.  Thanks, Teresa.

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Param.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/StringLength.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToLowerCase.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToUpperCase.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Trim.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/UnaryOp.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractDB2Dictionary.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java?rev=567928&r1=567927&r2=567928&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
Mon Aug 20 20:15:37 2007
@@ -28,7 +28,7 @@
  *
  * @author Abe White
  */
-class Lit
+public class Lit
     extends Const
     implements Literal {
 

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Param.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Param.java?rev=567928&r1=567927&r2=567928&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Param.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Param.java
Mon Aug 20 20:15:37 2007
@@ -33,7 +33,7 @@
  *
  * @author Abe White
  */
-class Param
+public class Param
     extends Const
     implements Parameter {
 

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/StringLength.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/StringLength.java?rev=567928&r1=567927&r2=567928&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/StringLength.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/StringLength.java
Mon Aug 20 20:15:37 2007
@@ -54,7 +54,8 @@
         DBDictionary dict = ctx.store.getDBDictionary();
         String func = dict.stringLengthFunction;
         dict.assertSupport(func != null, "StringLengthFunction");
-
+        func = dict.getCastFunction(getValue(), func);
+        
         int idx = func.indexOf("{0}");
         buf.append(func.substring(0, idx));
         getValue().appendTo(sel, ctx, state, buf, index);

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToLowerCase.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToLowerCase.java?rev=567928&r1=567927&r2=567928&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToLowerCase.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToLowerCase.java
Mon Aug 20 20:15:37 2007
@@ -43,6 +43,7 @@
         DBDictionary dict = ctx.store.getDBDictionary();
         String func = dict.toLowerCaseFunction;
         dict.assertSupport(func != null, "ToLowerCaseFunction");
+        func = dict.getCastFunction(getValue(), func);
 
         int idx = func.indexOf("{0}");
         buf.append(func.substring(0, idx));

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToUpperCase.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToUpperCase.java?rev=567928&r1=567927&r2=567928&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToUpperCase.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToUpperCase.java
Mon Aug 20 20:15:37 2007
@@ -43,6 +43,7 @@
         DBDictionary dict = ctx.store.getDBDictionary();
         String func = dict.toUpperCaseFunction;
         dict.assertSupport(func != null, "ToUpperCaseFunction");
+        func = dict.getCastFunction(getValue(), func);
 
         int idx = func.indexOf("{0}");
         buf.append(func.substring(0, idx));

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Trim.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Trim.java?rev=567928&r1=567927&r2=567928&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Trim.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Trim.java
Mon Aug 20 20:15:37 2007
@@ -150,8 +150,9 @@
         } else {
             func = dict.trimTrailingFunction;
             dict.assertSupport(func != null, "TrimTrailingFunction");
-        }
-
+        }        
+        func = dict.getCastFunction(_val, func);
+        
         int fromPart = func.indexOf("{0}");
         int charPart = func.indexOf("{1}");
         if (charPart == -1)

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/UnaryOp.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/UnaryOp.java?rev=567928&r1=567927&r2=567928&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/UnaryOp.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/UnaryOp.java
Mon Aug 20 20:15:37 2007
@@ -125,6 +125,7 @@
         sql.append(getOperator());
         sql.append("(");
         _val.appendTo(sel, ctx, state, sql, 0);
+        sql.addCastForParam(getOperator(), _val);
         sql.append(")");
     }
 

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractDB2Dictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractDB2Dictionary.java?rev=567928&r1=567927&r2=567928&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractDB2Dictionary.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractDB2Dictionary.java
Mon Aug 20 20:15:37 2007
@@ -38,7 +38,7 @@
         // in string functions
         toUpperCaseFunction = "UPPER(CAST({0} AS VARCHAR(1000)))";
         toLowerCaseFunction = "LOWER(CAST({0} AS VARCHAR(1000)))";
-        stringLengthFunction = "LENGTH(CAST({0} AS VARCHAR(1000)))";
+        stringLengthFunction = "LENGTH({0})";
         concatenateFunction = "(CAST({0} AS VARCHAR(1000)))||"
             + "(CAST({1} AS VARCHAR(1000)))";
 

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java?rev=567928&r1=567927&r2=567928&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
Mon Aug 20 20:15:37 2007
@@ -25,14 +25,18 @@
 import java.sql.Types;
 import java.util.Arrays;
 import java.util.StringTokenizer;
+
 import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration;
+import org.apache.openjpa.jdbc.kernel.exps.FilterValue;
+import org.apache.openjpa.jdbc.kernel.exps.Lit;
+import org.apache.openjpa.jdbc.kernel.exps.Param;
+import org.apache.openjpa.jdbc.kernel.exps.Val;
 import org.apache.openjpa.jdbc.schema.Sequence;
+import org.apache.openjpa.kernel.Filters;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.meta.JavaTypes;
 import org.apache.openjpa.util.OpenJPAException;
 import org.apache.openjpa.util.UnsupportedException;
-import org.apache.openjpa.kernel.Filters;
-import org.apache.openjpa.jdbc.kernel.exps.FilterValue;
 
 /**
  * Dictionary for IBM DB2 database.
@@ -612,5 +616,82 @@
             val.getFieldMapping().getColumns()[0])).
             append("/*[");
         val.appendTo(buf);        
+    }
+    
+    /**
+     * add CAST for a scalar function where operand is a param
+     * 
+     * @param func original string
+     * @param target substring to look for
+     * @param asString 
+     * @return updated string (func)
+     */
+    private String addCastAsString(String func, String target, 
+            String asString) {
+        String fstring = func;
+        if (func.indexOf(target) != -1)
+            fstring = func.replace(target, "CAST(" + target + asString);
+        return fstring;
+    }
+
+    /**
+     * add CAST for a function operator where operand is a param
+     * 
+     * @param func function name
+     * @param val type
+     * @return updated string (func)
+     */
+    public String addCastAsType(String func, Val val) {
+        String fstring = null;
+        String type = getTypeName(getJDBCType(JavaTypes.getTypeCode(val
+                .getType()), false));
+        fstring = "CAST(? AS " + type + ")";
+        return fstring;
+    }
+
+    /**
+     * Return the correct CAST function syntax
+     * 
+     * @param val operand of cast
+     * @param func original string
+     * @return a String with the correct CAST function syntax
+     */
+    public String getCastFunction(Val val, String func) {
+        if (val instanceof Lit || val instanceof Param)
+            if (func.indexOf("VARCHAR") == -1)
+                func = addCastAsString(func, "{0}", " AS VARCHAR(1000))");
+        return func;
+    }
+
+    public void indexOf(SQLBuffer buf, FilterValue str, FilterValue find,
+            FilterValue start) {
+        if (find.getValue() != null) { // non constants
+            buf.append("(LOCATE(CAST((");
+            find.appendTo(buf);
+            buf.append(") AS VARCHAR(1000)), ");
+        } else {
+            // this is a constant
+            buf.append("(LOCATE(");
+            find.appendTo(buf);
+            buf.append(", ");
+        }
+        if (str.getValue() != null) {
+            buf.append("CAST((");
+            str.appendTo(buf);
+            buf.append(") AS VARCHAR(1000))");
+        } else {
+            str.appendTo(buf);
+        }
+        if (start != null) {
+            if (start.getValue() == null) {
+                buf.append(", CAST((");
+                start.appendTo(buf);
+                buf.append(") AS INTEGER) + 1");
+            } else {
+                buf.append(", ");
+                start.appendTo(buf);
+            }
+        }
+        buf.append(") - 1)");
     }
 }

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?rev=567928&r1=567927&r2=567928&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
Mon Aug 20 20:15:37 2007
@@ -68,6 +68,8 @@
 import org.apache.openjpa.jdbc.kernel.exps.ExpContext;
 import org.apache.openjpa.jdbc.kernel.exps.ExpState;
 import org.apache.openjpa.jdbc.kernel.exps.FilterValue;
+import org.apache.openjpa.jdbc.kernel.exps.Lit;
+import org.apache.openjpa.jdbc.kernel.exps.Param;
 import org.apache.openjpa.jdbc.kernel.exps.Val;
 import org.apache.openjpa.jdbc.meta.ClassMapping;
 import org.apache.openjpa.jdbc.meta.FieldMapping;
@@ -92,7 +94,6 @@
 import org.apache.openjpa.lib.log.Log;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.lib.util.Localizer.Message;
-import org.apache.openjpa.meta.FieldMetaData;
 import org.apache.openjpa.meta.JavaTypes;
 import org.apache.openjpa.util.GeneralException;
 import org.apache.openjpa.util.OpenJPAException;
@@ -2541,6 +2542,18 @@
     protected void appendLength(SQLBuffer buf, int type) {        
     }
 
+    
+    /**
+     * add CAST for a function operator where operand is a param
+     * @param func  function name
+     * @param val 
+     * @return updated func
+     */
+    public String addCastAsType(String func, Val val) {
+        return null;
+    }    
+
+
     ///////////
     // DDL SQL
     ///////////
@@ -3869,4 +3882,16 @@
     public String getVersionColumn(Column column, String tableAlias) {
         return column.toString();
     }
+    
+    /**
+     * Attach CAST to the current function if necessary
+     * 
+     * @param val operand value
+     * @parma func the sql function statement
+     * @return a String with the correct CAST function syntax
+     */
+    public String getCastFunction(Val val, String func) {
+        return func;
+    }
+   
 }

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=567928&r1=567927&r2=567928&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 Mon
Aug 20 20:15:37 2007
@@ -31,6 +31,7 @@
 
 import org.apache.commons.lang.ObjectUtils;
 import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration;
+import org.apache.openjpa.jdbc.kernel.exps.Val;
 import org.apache.openjpa.jdbc.schema.Column;
 import org.apache.openjpa.jdbc.schema.Sequence;
 import org.apache.openjpa.jdbc.schema.Table;
@@ -590,6 +591,19 @@
         SQLBuffer buf = (SQLBuffer) other;
         return _sql.equals(buf._sql)
             && ObjectUtils.equals(_params, buf._params);
+    }
+
+    /**
+     * Replace SQL '?' with CAST string if required by DB platform
+     * @param oper
+     * @param val
+     */
+    public void addCastForParam(String oper, Val val) {
+        if (_sql.charAt(_sql.length() - 1) == '?') {
+            String castString = _dict.addCastAsType(oper, val);
+            if (castString != null)
+                _sql.replace(_sql.length() - 1, _sql.length(), castString);
+        }
     }
 
     /**



Mime
View raw message