Author: kwsutter Date: Mon Aug 27 13:51:49 2007 New Revision: 570240 URL: http://svn.apache.org/viewvc?rev=570240&view=rev Log: OPENJPA-338. Committing Teresa's patch for correcting CASTing with DB2. Modified: 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/SQLBuffer.java 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=570240&r1=570239&r2=570240&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 27 13:51:49 2007 @@ -696,4 +696,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/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=570240&r1=570239&r2=570240&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 27 13:51:49 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 {