cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1063284 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/access/jdbc/ main/java/org/apache/cayenne/access/trans/ main/java/org/apache/cayenne/ejbql/ main/java/org/apache/cayenne/ejbql/parser/ ...
Date Tue, 25 Jan 2011 14:07:42 GMT
Author: aadamchik
Date: Tue Jan 25 14:07:42 2011
New Revision: 1063284

URL: http://svn.apache.org/viewvc?rev=1063284&view=rev
Log:
CAY-1528 Undo CAY-1213

undoing r1001040

Removed:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLConstant.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/Compiler.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/SimpleNode.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/ConstQueryTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java?rev=1063284&r1=1063283&r2=1063284&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
Tue Jan 25 14:07:42 2011
@@ -32,7 +32,6 @@ import org.apache.cayenne.ejbql.EJBQLBas
 import org.apache.cayenne.ejbql.EJBQLException;
 import org.apache.cayenne.ejbql.EJBQLExpression;
 import org.apache.cayenne.ejbql.parser.AggregateConditionNode;
-import org.apache.cayenne.ejbql.parser.EJBQLConstant;
 import org.apache.cayenne.ejbql.parser.EJBQLDecimalLiteral;
 import org.apache.cayenne.ejbql.parser.EJBQLEquals;
 import org.apache.cayenne.ejbql.parser.EJBQLIdentificationVariable;
@@ -686,18 +685,6 @@ public class EJBQLConditionTranslator ex
     }
 
     @Override
-    public boolean visitConst(EJBQLExpression expression) {
-        Object constValue = ((EJBQLConstant) expression).getValue();
-        String constBoundName = context.bindParameter(constValue);
-        context.append(" #bind($").append(constBoundName).append(")");
-
-        if (constValue instanceof Enum<?>) {
-            processEnumParameter(expression, constBoundName, (Enum<?>) constValue);
-        }
-        return true;
-    }
-
-    @Override
     public boolean visitIntegerLiteral(EJBQLIntegerLiteral expression) {
         if (expression.getText() == null) {
             context.append("null");
@@ -823,60 +810,6 @@ public class EJBQLConditionTranslator ex
         return false;
     }
 
-    private AttributeProperty getParentExpressionLastPathChildAttribute(
-            EJBQLExpression expression) {
-        Node parent = ((SimpleNode) expression).jjtGetParent();
-        EJBQLPathAnaliserTranslator translator = new EJBQLPathAnaliserTranslator(context);
-        parent.visit(translator);
-        translator.visitPath(parent, parent.getChildrenCount());
-
-        String id = translator.idPath;
-        if (id != null) {
-            ClassDescriptor descriptor = context.getEntityDescriptor(id);
-            if (descriptor == null) {
-                throw new EJBQLException("Unmapped id variable: " + id);
-            }
-            String pathChunk = translator.lastPathComponent;
-            Property property = descriptor.getProperty(pathChunk);
-            if (property instanceof AttributeProperty) {
-                return (AttributeProperty) property;
-            }
-        }
-        return null;
-    }
-
-    private Integer getParentExpressionSqlType(EJBQLExpression expression) {
-        AttributeProperty lastPathAttribute = getParentExpressionLastPathChildAttribute(expression);
-        return lastPathAttribute == null ? null : lastPathAttribute
-                .getAttribute()
-                .getDbAttribute()
-                .getType();
-    }
-
-    private String getParentExpressionSqlTypeName(EJBQLExpression expression) {
-        AttributeProperty lastPathAttribute = getParentExpressionLastPathChildAttribute(expression);
-        if (lastPathAttribute != null) {
-            String atrType = lastPathAttribute.getAttribute().getType();
-            return TypesMapping.getSqlNameByType(TypesMapping.getSqlTypeByJava(atrType));
-        }
-        return null;
-    }
-
-    private void processEnumParameter(
-            EJBQLExpression expression,
-            String boundName,
-            Enum<?> value) {
-        context.pushMarker("@processEnumParameter", true);
-        Integer sqlType = getParentExpressionSqlType(expression);
-        if (sqlType != null && TypesMapping.isNumeric(sqlType)) {
-            context.rebindParameter(boundName, value.ordinal());
-        }
-        else {
-            context.rebindParameter(boundName, value.name());
-        }
-        context.popMarker();
-    }
-
     private void processParameter(String boundName, EJBQLExpression expression) {
         Object object = context.getBoundParameter(boundName);
 
@@ -905,13 +838,38 @@ public class EJBQLConditionTranslator ex
 
         if (object != null) {
             context.append(" #bind($").append(boundName).append(")");
-            if (object instanceof Enum<?>) {
-                processEnumParameter(expression, boundName, (Enum<?>) object);
-            }
         }
         else {
+
+            String type = null;
+            Node parent = ((SimpleNode) expression).jjtGetParent();
+
             context.pushMarker("@processParameter", true);
-            String type = getParentExpressionSqlTypeName(expression);
+
+            EJBQLPathAnaliserTranslator translator = new EJBQLPathAnaliserTranslator(
+                    context);
+            parent.visit(translator);
+            translator.visitPath(parent, parent.getChildrenCount());
+
+            String id = translator.idPath;
+            if (id != null) {
+
+                ClassDescriptor descriptor = context.getEntityDescriptor(id);
+                if (descriptor == null) {
+                    throw new EJBQLException("Unmapped id variable: " + id);
+                }
+                String pathChunk = translator.lastPathComponent;
+
+                Property property = descriptor.getProperty(pathChunk);
+                if (property instanceof AttributeProperty) {
+                    String atrType = ((AttributeProperty) property)
+                            .getAttribute()
+                            .getType();
+
+                    type = TypesMapping.getSqlNameByType(TypesMapping
+                            .getSqlTypeByJava(atrType));
+                }
+            }
             context.popMarker();
 
             if (type == null) {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java?rev=1063284&r1=1063283&r2=1063284&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java
Tue Jan 25 14:07:42 2011
@@ -375,16 +375,34 @@ public class QualifierTranslator extends
     }
 
     private boolean tryAppendConst(String path, Expression parentNode) throws IOException
{
+
+        if (path.length() < 3) {
+            return false;
+        }
+
+        int lastDot = path.lastIndexOf('.');
+        if (lastDot <= 0 || lastDot == path.length() - 1) {
+            return false;
+        }
+
+        String constName = path.substring(lastDot + 1);
+        String className = path.substring(0, lastDot);
+        
         Object constValue;
         try {
-            constValue = Util.getClassFieldValue(path);
+            Class<?> klass = Util.getJavaClass(className);
+            Field constField = klass.getField(constName);
+            constValue = constField.get(null);
         }
-        catch (IllegalAccessException e) {
-            throw new ExpressionException("Can't access const field", e);
+        catch (ClassNotFoundException e) {
+            return false;
         }
-        if (constValue == null) {
+        catch (NoSuchFieldException e) {
             return false;
         }
+        catch (IllegalAccessException e) {
+            throw new ExpressionException("Can't access const field", e);
+        }
         appendLiteral(constValue, paramsDbType(parentNode), parentNode);
         return true;
     }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java?rev=1063284&r1=1063283&r2=1063284&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java
Tue Jan 25 14:07:42 2011
@@ -299,10 +299,6 @@ public class EJBQLBaseVisitor implements
         return continueFlag;
     }
 
-    public boolean visitConst(EJBQLExpression expression) {
-        return continueFlag;
-    }
-
     public boolean visitPatternValue(EJBQLExpression expression) {
         return continueFlag;
     }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java?rev=1063284&r1=1063283&r2=1063284&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java
Tue Jan 25 14:07:42 2011
@@ -275,12 +275,7 @@ public interface EJBQLExpressionVisitor 
      *            visited.
      */
     boolean visitPath(EJBQLExpression expression, int finishedChildIndex);
-
-    /**
-     * @since 3.1
-     */
-    boolean visitConst(EJBQLExpression expression);
-
+    
     boolean visitDbPath(EJBQLExpression expression, int finishedChildIndex);
 
     boolean visitPatternValue(EJBQLExpression expression);

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/Compiler.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/Compiler.java?rev=1063284&r1=1063283&r2=1063284&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/Compiler.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/Compiler.java
Tue Jan 25 14:07:42 2011
@@ -47,7 +47,6 @@ import org.apache.cayenne.reflect.Proper
 import org.apache.cayenne.reflect.PropertyVisitor;
 import org.apache.cayenne.reflect.ToManyProperty;
 import org.apache.cayenne.reflect.ToOneProperty;
-import org.apache.cayenne.util.Util;
 
 /**
  * Produces an {@link EJBQLCompiledExpression} out of an EJBQL expression tree.
@@ -67,7 +66,6 @@ class Compiler {
     private Collection<EJBQLPath> paths;
     private EJBQLExpressionVisitor fromItemVisitor;
     private EJBQLExpressionVisitor joinVisitor;
-    private EJBQLExpressionVisitor constTransformVisitor;
     private EJBQLExpressionVisitor pathVisitor;
     private EJBQLExpressionVisitor rootDescriptorVisitor;
     private List<Object> resultComponents;
@@ -82,7 +80,6 @@ class Compiler {
         this.fromItemVisitor = new FromItemVisitor();
         this.joinVisitor = new JoinVisitor();
         this.pathVisitor = new PathVisitor();
-        this.constTransformVisitor = new ConstTransformVisitor();
     }
 
     CompiledExpression compile(String source, EJBQLExpression parsed) {
@@ -444,7 +441,6 @@ class Compiler {
 
         @Override
         public boolean visitWhere(EJBQLExpression expression) {
-            expression.visit(constTransformVisitor);
             expression.visit(pathVisitor);
 
             // continue with children as there may be subselects with their own id
@@ -587,37 +583,6 @@ class Compiler {
         }
     }
 
-    class ConstTransformVisitor extends EJBQLBaseVisitor {
-
-        @Override
-        public boolean visitPath(EJBQLExpression expression, int finishedChildIndex) {
-            EJBQLPath pathExpression = (EJBQLPath) expression;
-            String path = pathExpression.getAbsolutePath();
-            Object constValue;
-            try {
-                constValue = Util.getClassFieldValue(path);
-            }
-            catch (IllegalAccessException e) {
-                throw new EJBQLException("Can't access const field", e);
-            }
-            if (constValue != null) {
-                transformNode(pathExpression, path, constValue);
-            }
-            return false;
-        }
-
-        private void transformNode(
-                EJBQLPath pathExpression,
-                String path,
-                Object constValue) {
-            EJBQLConstant constantExpression = new EJBQLConstant(
-                    pathExpression,
-                    path,
-                    constValue);
-            pathExpression.getParent().replaceChild(pathExpression, constantExpression);
-        }
-    }
-
     class PathVisitor extends EJBQLBaseVisitor {
 
         @Override

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/SimpleNode.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/SimpleNode.java?rev=1063284&r1=1063283&r2=1063284&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/SimpleNode.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/SimpleNode.java
Tue Jan 25 14:07:42 2011
@@ -92,18 +92,6 @@ public abstract class SimpleNode impleme
         return jjtGetNumChildren();
     }
 
-    public void replaceChild(SimpleNode oldChild, SimpleNode newChild) {
-        for (int i = 0; i < children.length; i++) {
-            if (children[i] == oldChild) {
-                children[i] = newChild;
-            }
-        }
-    }
-
-    public SimpleNode getParent() {
-        return parent;
-    }
-
     public String getName() {
         String className = getClass().getName();
         int i = className.lastIndexOf("EJBQL");

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java?rev=1063284&r1=1063283&r2=1063284&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java
Tue Jan 25 14:07:42 2011
@@ -34,7 +34,6 @@ import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.OutputStream;
 import java.io.Serializable;
-import java.lang.reflect.Field;
 import java.lang.reflect.Member;
 import java.lang.reflect.Modifier;
 import java.net.URI;
@@ -740,38 +739,6 @@ public class Util {
         }
     }
 
-    /**
-     * Returns constant field value by fully qualified path.
-     * 
-     * @return Field value or {@code null} if class or field specified in path was not
-     *         found.
-     * @since 3.1
-     */
-    public static Object getClassFieldValue(String path) throws IllegalAccessException {
-        if (path.length() < 3) {
-            return null;
-        }
-
-        int lastDot = path.lastIndexOf('.');
-        if (lastDot <= 0 || lastDot == path.length() - 1) {
-            return null;
-        }
-
-        String constName = path.substring(lastDot + 1);
-        String className = path.substring(0, lastDot);
-        try {
-            Class<?> klass = getJavaClass(className);
-            Field constField = klass.getField(constName);
-            return constField.get(null);
-        }
-        catch (ClassNotFoundException e) {
-            return null;
-        }
-        catch (NoSuchFieldException e) {
-            return null;
-        }
-    }
-
     static void setReverse(
             final Persistent sourceObject,
             String propertyName,

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/ConstQueryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/ConstQueryTest.java?rev=1063284&r1=1063283&r2=1063284&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/ConstQueryTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/ConstQueryTest.java
Tue Jan 25 14:07:42 2011
@@ -103,25 +103,4 @@ public class ConstQueryTest extends Serv
         assertEquals(1, users.size());
         assertEquals("org.apache.cayenne.testdo.consttest.Const1Type.ADMIN", ((Const1Entity)
users.get(0)).getName());
     }
-    
-    public void testSelectByEnumValueWithEJBQL() throws Exception {
-        createConst1EntityDataSet();
-
-        EJBQLQuery query = new EJBQLQuery(
-                "SELECT e FROM Const1Entity e WHERE e.type = :t");
-        query.setParameter("t", Const1Type.ORDINARY);
-        List entities = context.performQuery(query);
-        assertEquals(1, entities.size());
-        assertEquals("entity1", ((Const1Entity) entities.get(0)).getName());
-    }
-
-    public void testSelectByEnumValueSpecifiedAsConstantWithEJBQL() throws Exception {
-        createConst1EntityDataSet();
-
-        EJBQLQuery query = new EJBQLQuery(
-                "SELECT e FROM Const1Entity e WHERE e.type = org.apache.cayenne.testdo.consttest.Const1Type.ORDINARY");
-        List entities = context.performQuery(query);
-        assertEquals(1, entities.size());
-        assertEquals("entity1", ((Const1Entity) entities.get(0)).getName());
-    }
 }



Mime
View raw message