OPENJPA-1024: add enum literal support for case expression

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@763860 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2009-04-10 02:51:31 +00:00
parent 9d5066a225
commit 16c8b6d2dc
5 changed files with 49 additions and 5 deletions

View File

@ -446,8 +446,10 @@ public class JDBCExpressionFactory
value.append("1");
else
value.append("0");
}
else
} else if (lit.getParseType() == Literal.TYPE_ENUM) {
value.append("'").append(((Enum) lit.getValue()).name()).
append("'");
} else
value.append(lit.getValue().toString());
lit.setValue(new Raw(value.toString()));
return lit;
@ -470,6 +472,7 @@ public class JDBCExpressionFactory
Exp[] exps = new Exp[exp.length];
for (int i = 0; i < exp.length; i++)
exps[i] = (Exp) exp[i];
val = getLiteralRawString(val);
return new GeneralCaseExpression(exps, (Val) val);
}

View File

@ -33,6 +33,7 @@ public interface Literal
public static final int TYPE_STRING = 3;
public static final int TYPE_SQ_STRING = 4; // single-quoted string
public static final int TYPE_CLASS = 5;
public static final int TYPE_ENUM = 6;
/**
* The value of this literal.

View File

@ -1560,11 +1560,11 @@ public class JPQLExpressionBuilder
Class c = resolver.classForName(className, null);
if (c != null) {
String fieldName = lastChild(node).text;
int type = (c.isEnum() ? Literal.TYPE_ENUM : Literal.TYPE_UNKNOWN);
try {
Field field = c.getField(fieldName);
Object value = field.get(null);
return factory.newLiteral(value, Literal.TYPE_UNKNOWN);
return factory.newLiteral(value, type);
} catch (NoSuchFieldException nsfe) {
if (node.inEnumPath)
throw parseException(EX_USER, "no-field",

View File

@ -48,7 +48,11 @@ public class CompUser
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int userid;
@Enumerated
@Basic
private CreditRating creditRating;
public CompUser(){}
public CompUser(String name, String cName, Address address, int age)
@ -95,4 +99,13 @@ public class CompUser
this.age = age;
}
public CreditRating getRating() {
return creditRating;
}
public void setRating(CreditRating rating) {
this.creditRating = rating;
}
public enum CreditRating { POOR, GOOD, EXCELLENT };
}

View File

@ -187,6 +187,33 @@ public class TestJPQLScalarExpressions extends AbstractTestCase {
int result = em.createQuery(update).executeUpdate();
assertEquals("the result is not 6", 6, result);
String query2 = "SELECT e.name, e.age+1 as cage, " +
"CASE WHEN e.address.country = 'USA' " +
" THEN 'United-States' " +
" ELSE 'Non United-States' END as d2," +
" e.address.country " +
" FROM CompUser e ORDER BY cage, d2 DESC";
List rs2 = em.createQuery(query2).getResultList();
Object[] result2 = (Object[]) rs2.get(rs2.size()-1);
assertEquals("the name is not seetha", "Seetha", result2[0]);
assertEquals("the country is not 'Non United-States'",
"Non United-States", result2[2]);
String query3 = " select e.name, " +
"CASE WHEN e.age = 11 THEN " +
"org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.POOR" +
" WHEN e.age = 35 THEN " +
"org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.GOOD" +
" ELSE " +
"org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.EXCELLENT" +
" END FROM CompUser e ORDER BY e.age";
List rs3 = em.createQuery(query3).getResultList();
Object[] result3 = (Object[]) rs3.get(0);
assertEquals("the name is not Jacob", "Jacob", result3[0]);
assertEquals("the credit rating is not 'POOR'", "POOR", result3[1]);
endTx(em);
endEm(em);
}