OPENJPA-2324: Option to express literal in query string directly into generate SQL - applied Albert's patch to 1.0.x

git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/1.0.x@1436118 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Heath Thomann 2013-01-21 02:32:24 +00:00
parent aa3bf7eaab
commit 8f53fb2fd8
4 changed files with 64 additions and 15 deletions

View File

@ -21,6 +21,7 @@ package org.apache.openjpa.jdbc.kernel.exps;
import org.apache.openjpa.jdbc.sql.SQLBuffer; import org.apache.openjpa.jdbc.sql.SQLBuffer;
import org.apache.openjpa.jdbc.sql.Select; import org.apache.openjpa.jdbc.sql.Select;
import org.apache.openjpa.kernel.Filters; import org.apache.openjpa.kernel.Filters;
import org.apache.openjpa.kernel.QueryHints;
import org.apache.openjpa.kernel.exps.Literal; import org.apache.openjpa.kernel.exps.Literal;
/** /**
@ -98,7 +99,12 @@ public class Lit
if (lstate.otherLength > 1) if (lstate.otherLength > 1)
sql.appendValue(((Object[]) lstate.sqlValue)[index], sql.appendValue(((Object[]) lstate.sqlValue)[index],
lstate.getColumn(index)); lstate.getColumn(index));
else else {
sql.appendValue(lstate.sqlValue, lstate.getColumn(index)); 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);
}
} }
} }

View File

@ -35,6 +35,8 @@ import org.apache.openjpa.jdbc.kernel.exps.Val;
import org.apache.openjpa.jdbc.schema.Column; import org.apache.openjpa.jdbc.schema.Column;
import org.apache.openjpa.jdbc.schema.Sequence; import org.apache.openjpa.jdbc.schema.Sequence;
import org.apache.openjpa.jdbc.schema.Table; import org.apache.openjpa.jdbc.schema.Table;
import org.apache.openjpa.kernel.Filters;
import serp.util.Numbers; import serp.util.Numbers;
/** /**
@ -229,30 +231,63 @@ public final class SQLBuffer
* Append a parameter value for a specific column. * Append a parameter value for a specific column.
*/ */
public SQLBuffer appendValue(Object o, Column col) { public SQLBuffer appendValue(Object o, Column col) {
return appendValue(o, col, true);
}
public SQLBuffer appendValue(Object o, Column col, boolean useParamToken) {
if (o == null) if (o == null)
_sql.append("NULL"); _sql.append("NULL");
else if (o instanceof Raw) else if (o instanceof Raw)
_sql.append(o.toString()); _sql.append(o.toString());
else { else {
_sql.append(PARAMETER_TOKEN); Class type = Filters.wrap(o.getClass());
// System.out.println("======1> o="+o+", cls="+o.getClass());
if (useParamToken || !validParamLiteralType(type)) {
_sql.append(PARAMETER_TOKEN);
// initialize param and col lists; we hold off on col list until // initialize param and col lists; we hold off on col list until
// we get the first non-null col // we get the first non-null col
if (_params == null) if (_params == null)
_params = new ArrayList(); _params = new ArrayList();
if (col != null && _cols == null) { if (col != null && _cols == null) {
_cols = new ArrayList(); _cols = new ArrayList();
while (_cols.size() < _params.size()) while (_cols.size() < _params.size())
_cols.add(null); _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());
}
// System.out.println("======3> _sql="+_sql.toString());
} }
_params.add(o);
if (_cols != null)
_cols.add(col);
} }
return this; 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;
// System.out.println("======2> type="+type+", ret="+ret);
return ret;
}
/** /**
* Append a parameter value. * Append a parameter value.
*/ */

View File

@ -253,6 +253,8 @@ public class Filters {
if (!num) { if (!num) {
if (type == String.class) if (type == String.class)
return o.toString(); return o.toString();
else if (type == Boolean.class && o instanceof String)
return Boolean.valueOf(o.toString());
else if (type == Character.class) { else if (type == Character.class) {
String str = o.toString(); String str = o.toString();
if (str != null && str.length() == 1) if (str != null && str.length() == 1)

View File

@ -28,4 +28,10 @@ public interface QueryHints {
*/ */
public static final String HINT_RESULT_COUNT = public static final String HINT_RESULT_COUNT =
"openjpa.hint.OptimizeResultCount"; "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";
} }