openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r763860 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/ openjpa-persistence-jdbc/...
Date Fri, 10 Apr 2009 02:51:32 GMT
Author: faywang
Date: Fri Apr 10 02:51:31 2009
New Revision: 763860

URL: http://svn.apache.org/viewvc?rev=763860&view=rev
Log:
OPENJPA-1024: add enum literal support for case expression

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/JDBCExpressionFactory.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/Literal.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/apps/CompUser.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/TestJPQLScalarExpressions.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/JDBCExpressionFactory.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/JDBCExpressionFactory.java?rev=763860&r1=763859&r2=763860&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/JDBCExpressionFactory.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/JDBCExpressionFactory.java
Fri Apr 10 02:51:31 2009
@@ -446,8 +446,10 @@
                     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 @@
         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);
     }
 

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/Literal.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/Literal.java?rev=763860&r1=763859&r2=763860&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/Literal.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/Literal.java
Fri Apr 10 02:51:31 2009
@@ -33,6 +33,7 @@
     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.

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java?rev=763860&r1=763859&r2=763860&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
Fri Apr 10 02:51:31 2009
@@ -1560,11 +1560,11 @@
         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",

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/apps/CompUser.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/apps/CompUser.java?rev=763860&r1=763859&r2=763860&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/apps/CompUser.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/apps/CompUser.java
Fri Apr 10 02:51:31 2009
@@ -48,7 +48,11 @@
 	@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 @@
 		this.age = age;
 	}
 
+    public CreditRating getRating() {
+        return creditRating;
+    }
+    
+    public void setRating(CreditRating rating) {
+        this.creditRating = rating;
+    }
+	
+    public enum CreditRating { POOR, GOOD, EXCELLENT };
 }

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/TestJPQLScalarExpressions.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/TestJPQLScalarExpressions.java?rev=763860&r1=763859&r2=763860&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/TestJPQLScalarExpressions.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/TestJPQLScalarExpressions.java
Fri Apr 10 02:51:31 2009
@@ -187,6 +187,33 @@
         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);
     }



Mime
View raw message