OPENJPA-1024: support enum literal in case expression

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@764094 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2009-04-10 22:57:00 +00:00
parent 69d01f2813
commit d00cf9d468
5 changed files with 66 additions and 10 deletions

View File

@ -143,9 +143,10 @@ public class GeneralCaseExpression
for (int i = 0; i < _exp.length; i++) { for (int i = 0; i < _exp.length; i++) {
BinaryOpExpState bstate = (BinaryOpExpState) gstate.states[i]; BinaryOpExpState bstate = (BinaryOpExpState) gstate.states[i];
((WhenCondition) _exp[i]).getVal().calculateValue(sel, ctx, ((WhenCondition) _exp[i]).getVal().calculateValue(sel, ctx,
bstate.state2, null, null); bstate.state2, other, otherState);
} }
_val.calculateValue(sel, ctx, gstate.states[_exp.length], null, null); _val.calculateValue(sel, ctx, gstate.states[_exp.length], other,
otherState);
} }
public void groupBy(Select sel, ExpContext ctx, ExpState state) { public void groupBy(Select sel, ExpContext ctx, ExpState state) {

View File

@ -447,8 +447,8 @@ public class JDBCExpressionFactory
else else
value.append("0"); value.append("0");
} else if (lit.getParseType() == Literal.TYPE_ENUM) { } else if (lit.getParseType() == Literal.TYPE_ENUM) {
value.append("'").append(((Enum) lit.getValue()).name()). lit.setRaw(true);
append("'"); return val;
} else } else
value.append(lit.getValue().toString()); value.append(lit.getValue().toString());
lit.setValue(new Raw(value.toString())); lit.setValue(new Raw(value.toString()));

View File

@ -18,6 +18,7 @@
*/ */
package org.apache.openjpa.jdbc.kernel.exps; package org.apache.openjpa.jdbc.kernel.exps;
import org.apache.openjpa.jdbc.sql.Raw;
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;
@ -34,6 +35,7 @@ public class Lit
private Object _val; private Object _val;
private int _ptype; private int _ptype;
private boolean _isRaw;
/** /**
* Constructor. Supply literal value. * Constructor. Supply literal value.
@ -67,6 +69,14 @@ public class Lit
return getValue(); return getValue();
} }
public boolean isRaw() {
return _isRaw;
}
public void setRaw(boolean isRaw) {
_isRaw = isRaw;
}
public ExpState initialize(Select sel, ExpContext ctx, int flags) { public ExpState initialize(Select sel, ExpContext ctx, int flags) {
return new LitExpState(); return new LitExpState();
} }
@ -98,7 +108,21 @@ 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 {
if (getParseType() == Literal.TYPE_ENUM && _isRaw) {
StringBuilder value = new StringBuilder();
boolean isOrdinal = false;
if (lstate.sqlValue instanceof Integer)
isOrdinal = true;
if (!isOrdinal)
value.append("'");
value.append(lstate.sqlValue);
if (!isOrdinal)
value.append("'");
lstate.sqlValue = new Raw(value.toString());
setValue(lstate.sqlValue);
}
sql.appendValue(lstate.sqlValue, lstate.getColumn(index)); sql.appendValue(lstate.sqlValue, lstate.getColumn(index));
} }
} }
}

View File

@ -177,7 +177,7 @@ public class SimpleCaseExpression
((WhenScalar) _exp[i]).getVal1().calculateValue(sel, ctx, ((WhenScalar) _exp[i]).getVal1().calculateValue(sel, ctx,
bstate.state1, null, null); bstate.state1, null, null);
((WhenScalar) _exp[i]).getVal2().calculateValue(sel, ctx, ((WhenScalar) _exp[i]).getVal2().calculateValue(sel, ctx,
bstate.state2, null, null); bstate.state2, other, otherState);
} }
_val.calculateValue(sel, ctx, cstate.states[_exp.length+1], other, _val.calculateValue(sel, ctx, cstate.states[_exp.length+1], other,
otherState); otherState);

View File

@ -175,7 +175,6 @@ public class TestJPQLScalarExpressions extends AbstractTestCase {
" ELSE e.age + 0 " + " ELSE e.age + 0 " +
" END AS cage " + " END AS cage " +
" FROM CompUser e ORDER BY cage"; " FROM CompUser e ORDER BY cage";
List rs = em.createQuery(query).getResultList(); List rs = em.createQuery(query).getResultList();
String update = "UPDATE CompUser e SET e.age = " + String update = "UPDATE CompUser e SET e.age = " +
@ -185,7 +184,6 @@ public class TestJPQLScalarExpressions extends AbstractTestCase {
"END"; "END";
int result = em.createQuery(update).executeUpdate(); int result = em.createQuery(update).executeUpdate();
assertEquals("the result is not 6", 6, result); assertEquals("the result is not 6", 6, result);
String query2 = "SELECT e.name, e.age+1 as cage, " + String query2 = "SELECT e.name, e.age+1 as cage, " +
@ -208,12 +206,45 @@ public class TestJPQLScalarExpressions extends AbstractTestCase {
" ELSE " + " ELSE " +
"org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.EXCELLENT" + "org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.EXCELLENT" +
" END FROM CompUser e ORDER BY e.age"; " END FROM CompUser e ORDER BY e.age";
List rs3 = em.createQuery(query3).getResultList(); List rs3 = em.createQuery(query3).getResultList();
Object[] result3 = (Object[]) rs3.get(0); Object[] result3 = (Object[]) rs3.get(0);
assertEquals("the name is not Jacob", "Jacob", result3[0]); assertEquals("the name is not Jacob", "Jacob", result3[0]);
assertEquals("the credit rating is not 'POOR'", "POOR", result3[1]); assertEquals("the credit rating is not 'POOR'", "POOR", result3[1]);
/*
// this jpql fail with NPE in Derby. It works with DB2
String update2 = "update CompUser c set c.creditRating = " +
" CASE WHEN c.name ='Jacob' THEN " +
"org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.POOR" +
" WHEN c.name = 'Ugo' THEN " +
"org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.GOOD " +
" ELSE " +
"org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.EXCELLENT " +
" END ";
*/
String update2 = "update CompUser c set c.creditRating = " +
" CASE WHEN c.age > 30 THEN " +
"org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.POOR" +
" WHEN c.age < 15 THEN " +
"org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.GOOD " +
" ELSE " +
"org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.EXCELLENT " +
" END ";
result = em.createQuery(update2).executeUpdate();
assertEquals("the result is not 6", 6, result);
String update3 = "update CompUser c set c.creditRating = " +
" CASE c.age WHEN 35 THEN " +
"org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.POOR" +
" WHEN 11 THEN " +
"org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.GOOD " +
" ELSE " +
"org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.EXCELLENT " +
" END ";
result = em.createQuery(update3).executeUpdate();
assertEquals("the result is not 6", 6, result);
endTx(em); endTx(em);
endEm(em); endEm(em);
} }