You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by mi...@apache.org on 2007/09/18 22:10:40 UTC

svn commit: r577038 - in /openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql: DB2Dictionary.java SQLBuffer.java

Author: mikedd
Date: Tue Sep 18 13:10:39 2007
New Revision: 577038

URL: http://svn.apache.org/viewvc?rev=577038&view=rev
Log:
OPENJPA-338 for 1.0.x. Committing Teresa's patch

Modified:
    openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
    openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java

Modified: openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java?rev=577038&r1=577037&r2=577038&view=diff
==============================================================================
--- openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java (original)
+++ openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java Tue Sep 18 13:10:39 2007
@@ -695,4 +695,45 @@
         }
         buf.append(") - 1)");
     }
+
+    /** 
+     * Cast the specified value to the specified type.
+     *
+     * @param buf the buffer to append the cast to
+     * @param val the value to cast
+     * @param type the type of the case, e.g. {@link Types#NUMERIC}
+     */
+    public void appendCast(SQLBuffer buf, FilterValue val, int type) {
+
+        // Convert the cast function: "CAST({0} AS {1})"
+        int firstParam = castFunction.indexOf("{0}");
+        String pre = castFunction.substring(0, firstParam); // "CAST("
+        String mid = castFunction.substring(firstParam + 3);
+        int secondParam = mid.indexOf("{1}");
+        String post;
+        if (secondParam > -1) {
+            post = mid.substring(secondParam + 3); // ")"
+            mid = mid.substring(0, secondParam); // " AS "
+        } else
+            post = "";
+
+        // No need to add CAST if the value is a constant
+        if (val instanceof Lit || val instanceof Param) {
+            buf.append(pre);
+            val.appendTo(buf);
+            buf.append(mid);
+            buf.append(getTypeName(type));
+            appendLength(buf, type);
+            buf.append(post);
+        } else {
+            val.appendTo(buf);
+            String sqlString = buf.getSQL(false);
+            if (sqlString.endsWith("?")) {
+                // case "(?" - convert to "CAST(? AS type"
+                String str = "CAST(? AS " + getTypeName(type) + ")";
+                buf.replaceSqlString(sqlString.length() - 1,
+                        sqlString.length(), str);
+            }
+        }
+    }
 }

Modified: openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java?rev=577038&r1=577037&r2=577038&view=diff
==============================================================================
--- openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java (original)
+++ openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java Tue Sep 18 13:10:39 2007
@@ -607,6 +607,17 @@
     }
 
     /**
+     * Replace current buffer string with the new string
+     * 
+     * @param start replace start position
+     * @param end replace end position
+     * @param newString
+     */
+    public void replaceSqlString(int start, int end, String newString) {
+        _sql.replace(start, end, newString);
+    }
+    
+    /**
      * Represents a subselect.
      */
     private static class Subselect {