openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r764094 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/
Date Fri, 10 Apr 2009 22:57:00 GMT
Author: faywang
Date: Fri Apr 10 22:57:00 2009
New Revision: 764094

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

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/GeneralCaseExpression.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/JDBCExpressionFactory.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SimpleCaseExpression.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/GeneralCaseExpression.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/GeneralCaseExpression.java?rev=764094&r1=764093&r2=764094&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/GeneralCaseExpression.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/GeneralCaseExpression.java
Fri Apr 10 22:57:00 2009
@@ -143,9 +143,10 @@
         for (int i = 0; i < _exp.length; i++) {   
             BinaryOpExpState bstate = (BinaryOpExpState) gstate.states[i];
             ((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) {

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=764094&r1=764093&r2=764094&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 22:57:00 2009
@@ -447,8 +447,8 @@
                 else
                     value.append("0");
             } else if (lit.getParseType() == Literal.TYPE_ENUM) {
-                value.append("'").append(((Enum) lit.getValue()).name()).
-                    append("'");
+                lit.setRaw(true);
+                return val;
             } else
                 value.append(lit.getValue().toString());
             lit.setValue(new Raw(value.toString()));

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java?rev=764094&r1=764093&r2=764094&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Lit.java
Fri Apr 10 22:57:00 2009
@@ -18,6 +18,7 @@
  */
 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.Select;
 import org.apache.openjpa.kernel.Filters;
@@ -34,6 +35,7 @@
 
     private Object _val;
     private int _ptype;
+    private boolean _isRaw;
 
     /**
      * Constructor. Supply literal value.
@@ -66,6 +68,14 @@
     public Object getValue(Object[] params) {
         return getValue();
     }
+    
+    public boolean isRaw() {
+        return _isRaw;
+    }
+    
+    public void setRaw(boolean isRaw) {
+        _isRaw = isRaw;
+    }
 
     public ExpState initialize(Select sel, ExpContext ctx, int flags) {
         return new LitExpState();
@@ -98,7 +108,21 @@
         if (lstate.otherLength > 1)
             sql.appendValue(((Object[]) lstate.sqlValue)[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));
+        }
     }
 }

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SimpleCaseExpression.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SimpleCaseExpression.java?rev=764094&r1=764093&r2=764094&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SimpleCaseExpression.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SimpleCaseExpression.java
Fri Apr 10 22:57:00 2009
@@ -177,7 +177,7 @@
             ((WhenScalar) _exp[i]).getVal1().calculateValue(sel, ctx,
                 bstate.state1, null, null);
             ((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,
             otherState);

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=764094&r1=764093&r2=764094&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 22:57:00 2009
@@ -175,7 +175,6 @@
             " ELSE e.age + 0 " +
             " END AS cage " +
             " FROM CompUser e ORDER BY cage";
-        
         List rs = em.createQuery(query).getResultList();
 
         String update = "UPDATE CompUser e SET e.age = " +
@@ -185,7 +184,6 @@
             "END";
 
         int result = em.createQuery(update).executeUpdate();
-
         assertEquals("the result is not 6", 6, result);
         
         String query2 = "SELECT e.name, e.age+1 as cage, " +
@@ -208,13 +206,46 @@
             " 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);
+        /*
+        // 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);
         endEm(em);
     }
 



Mime
View raw message