OPENJPA-338. Committing Teresa's patch for correcting CASTing with DB2.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@570240 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Kevin W. Sutter 2007-08-27 20:51:49 +00:00
parent 0a8239e6a7
commit 953df22c39
2 changed files with 52 additions and 0 deletions

View File

@ -696,4 +696,45 @@ public class DB2Dictionary
} }
buf.append(") - 1)"); 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);
}
}
}
} }

View File

@ -606,6 +606,17 @@ public final class SQLBuffer
} }
} }
/**
* 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. * Represents a subselect.
*/ */