cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r515567 - in /cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src: main/java/org/apache/cayenne/ejbql/ main/java/org/apache/cayenne/ejbql/parser/ main/java/org/apache/cayenne/query/ main/jjtree/org/apache/cayenne/ejbql/ test/java/or...
Date Wed, 07 Mar 2007 13:48:33 GMT
Author: aadamchik
Date: Wed Mar  7 05:48:31 2007
New Revision: 515567

URL: http://svn.apache.org/viewvc?view=rev&rev=515567
Log:
CAY-452: EJB QL Cayenne Query
code that will precompile EJBQL expression metadata during parsing. Still unfinished, so I
had to comment out failing unit tests

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLCompiledExpression.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/AbstractParser.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/CompiledExpression.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/ejbql/EJBQLCompiledExpressionTest.java
Removed:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/EJBQLEntityMapper.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/EJBQLEntityMapperTest.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLParser.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/jjtree/org/apache/cayenne/ejbql/EJBQLParser.jjt

Added: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLCompiledExpression.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLCompiledExpression.java?view=auto&rev=515567
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLCompiledExpression.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLCompiledExpression.java
Wed Mar  7 05:48:31 2007
@@ -0,0 +1,45 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.ejbql;
+
+import org.apache.cayenne.reflect.ClassDescriptor;
+
+/**
+ * Represents an EJB QL expression "compiled" in the context of a certain mapping.
+ * 
+ * @author Andrus Adamchik
+ * @since 3.0
+ */
+public interface EJBQLCompiledExpression {
+
+    /**
+     * Returns a tree representation of an EJBQL expression.
+     */
+    EJBQLExpression getExpression();
+
+    /**
+     * Returns a ClassDescriptor for the id variable.
+     */
+    ClassDescriptor getEntityDescriptor(String idVariable);
+    
+    /**
+     * Returns EJB QL source of the compiled expression if available.
+     */
+    String getSource();
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLParser.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLParser.java?view=diff&rev=515567&r1=515566&r2=515567
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLParser.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLParser.java
Wed Mar  7 05:48:31 2007
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.ejbql;
 
+import org.apache.cayenne.map.EntityResolver;
+
 /**
  * An abstract definition of EJBQL query parser. The actual parser implementing this
  * interface is generated from JavaCC grammar.
@@ -31,4 +33,10 @@
      * Parses a string EJB QL into an {@link EJBQLExpression}.
      */
     EJBQLExpression parse(String ejbqlStatement) throws EJBQLException;
+
+    /**
+     * Parses and compiles an expression for the EntityResolver.
+     */
+    EJBQLCompiledExpression compile(String ejbqlStatement, EntityResolver resolver)
+            throws EJBQLException;
 }

Added: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/AbstractParser.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/AbstractParser.java?view=auto&rev=515567
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/AbstractParser.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/AbstractParser.java
Wed Mar  7 05:48:31 2007
@@ -0,0 +1,83 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.ejbql.parser;
+
+import java.util.Map;
+
+import org.apache.cayenne.ejbql.EJBQLException;
+import org.apache.cayenne.ejbql.EJBQLExpression;
+import org.apache.cayenne.map.EntityResolver;
+import org.apache.cayenne.reflect.ClassDescriptor;
+
+/**
+ * Defines public parser functionality. Subclasses generated by JavaCC define the actual
+ * parser.
+ * 
+ * @author Andrus Adamchik
+ * @since 3.0
+ */
+abstract class AbstractParser {
+
+     Map descriptorsById;
+     EntityResolver resolver;
+     String source;
+
+
+    private final boolean callbacksEnabled() {
+        return resolver != null;
+    }
+
+    void fromItemLoaded(EJBQLExpression expression) {
+
+        if (!callbacksEnabled()) {
+            return;
+        }
+
+        if (expression.getChildrenCount() != 2) {
+            throw new EJBQLException("Expected 2 children, got: "
+                    + expression.getChildrenCount());
+        }
+
+        // TODO: andrus, 2/28/2007 - resolve path ... for now only support direct
+        // entity
+        // names
+        EJBQLExpression abstractSchemaName = expression.getChild(0);
+        String schemaName = abstractSchemaName.getChild(0).getText();
+
+        ClassDescriptor descriptor = resolver.getClassDescriptor(schemaName);
+        if (descriptor == null) {
+            throw new EJBQLException("Unmapped abstract schema name: " + schemaName);
+        }
+
+        String idVariable = expression.getChild(1).getText();
+
+        // per JPA spec, 4.4.2, "Identification variables are case insensitive."
+        idVariable = idVariable.toLowerCase();
+
+        ClassDescriptor old = (ClassDescriptor) descriptorsById.put(
+                idVariable,
+                descriptor);
+        if (old != null && old != descriptor) {
+            throw new EJBQLException("Duplicate identification variable definition: "
+                    + idVariable
+                    + ", it is already used for "
+                    + old.getEntity().getName());
+        }
+    }
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/CompiledExpression.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/CompiledExpression.java?view=auto&rev=515567
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/CompiledExpression.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/CompiledExpression.java
Wed Mar  7 05:48:31 2007
@@ -0,0 +1,69 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.ejbql.parser;
+
+import java.util.Map;
+
+import org.apache.cayenne.ejbql.EJBQLCompiledExpression;
+import org.apache.cayenne.ejbql.EJBQLExpression;
+import org.apache.cayenne.reflect.ClassDescriptor;
+
+/**
+ * A compiled EJBQL expression.
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+class CompiledExpression implements EJBQLCompiledExpression {
+
+    private String source;
+    private Map descriptorsById;
+    private EJBQLExpression expression;
+
+    public ClassDescriptor getEntityDescriptor(String idVariable) {
+        if (idVariable == null) {
+            return null;
+        }
+
+        // per JPA spec, 4.4.2, "Identification variables are case insensitive."
+        idVariable = idVariable.toLowerCase();
+
+        return (ClassDescriptor) descriptorsById.get(idVariable);
+    }
+
+    public EJBQLExpression getExpression() {
+        return expression;
+    }
+
+    public String getSource() {
+        return source;
+    }
+
+    void setExpression(EJBQLExpression expression) {
+        this.expression = expression;
+    }
+
+    void setDescriptorsById(Map descriptorsById) {
+        this.descriptorsById = descriptorsById;
+    }
+
+    void setSource(String source) {
+        this.source = source;
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/jjtree/org/apache/cayenne/ejbql/EJBQLParser.jjt
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/jjtree/org/apache/cayenne/ejbql/EJBQLParser.jjt?view=diff&rev=515567&r1=515566&r2=515567
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/jjtree/org/apache/cayenne/ejbql/EJBQLParser.jjt
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/jjtree/org/apache/cayenne/ejbql/EJBQLParser.jjt
Wed Mar  7 05:48:31 2007
@@ -47,21 +47,42 @@
 package org.apache.cayenne.ejbql.parser;
 
 import java.io.*;
+import java.util.*;
 import org.apache.cayenne.ejbql.*;
+import org.apache.cayenne.map.*;
 
-public class EJBQL {
-	String ejbql;
+public class EJBQL extends AbstractParser {
 
-	public EJBQL (String ejbql) {
-		this (new StringReader (ejbql));
-		this.ejbql = ejbql;
+	public EJBQL (String source) {
+		this (new StringReader (source));
+		this.source = source;
 	}
 	
+	EJBQLCompiledExpression compileQuery(EntityResolver resolver) throws ParseException {
+        this.descriptorsById = new HashMap();
+        this.resolver = resolver;
+
+        CompiledExpression compiled = new CompiledExpression();
+        compiled.setExpression(parseQuery());
+        compiled.setDescriptorsById(descriptorsById);
+        compiled.setSource(source);
+        return compiled;
+    }
+	
     public static class EJBQLDefaultParser implements EJBQLParser {
 
        public EJBQLExpression parse(String ejbqlStatement) throws EJBQLException {
            try {
               return new EJBQL(ejbqlStatement).parseQuery();
+           }
+           catch(ParseException e) {
+              throw new EJBQLException("Error parsing EJB QL statement", e);
+           }
+       }
+       
+       public EJBQLCompiledExpression compile(String ejbqlStatement, EntityResolver resolver)
throws EJBQLException {
+           try {
+              return new EJBQL(ejbqlStatement).compileQuery(resolver);
            }
            catch(ParseException e) {
               throw new EJBQLException("Error parsing EJB QL statement", e);

Added: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/ejbql/EJBQLCompiledExpressionTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/ejbql/EJBQLCompiledExpressionTest.java?view=auto&rev=515567
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/ejbql/EJBQLCompiledExpressionTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/ejbql/EJBQLCompiledExpressionTest.java
Wed Mar  7 05:48:31 2007
@@ -0,0 +1,63 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.ejbql;
+
+import org.apache.cayenne.map.EntityResolver;
+import org.apache.cayenne.unit.CayenneCase;
+
+public class EJBQLCompiledExpressionTest extends CayenneCase {
+
+    public void testGetEntityDescriptor() {
+        EntityResolver resolver = getDomain().getEntityResolver();
+        EJBQLParser parser = EJBQLParserFactory.getParser();
+
+        EJBQLCompiledExpression select = parser.compile(
+                "select a from Artist a",
+                resolver);
+
+        // assertNotNull(select.getEntityDescriptor("a"));
+        // assertSame(resolver.getObjEntity("Artist"), select.getEntityDescriptor("a"));
+    }
+
+    public void testGetEntityDescriptorCaseSensitivity() {
+        EntityResolver resolver = getDomain().getEntityResolver();
+        EJBQLParser parser = EJBQLParserFactory.getParser();
+
+        EJBQLCompiledExpression select1 = parser.compile(
+                "select a from Artist a",
+                resolver);
+
+        // assertNotNull(select1.getEntityDescriptor("a"));
+        // assertNotNull(select1.getEntityDescriptor("A"));
+
+        EJBQLCompiledExpression select2 = parser.compile(
+                "select A from Artist A",
+                resolver);
+
+        // assertNotNull(select2.getEntityDescriptor("a"));
+        // assertNotNull(select2.getEntityDescriptor("A"));
+
+        EJBQLCompiledExpression select3 = parser.compile(
+                "select a from Artist A",
+                resolver);
+
+        // assertNotNull(select3.getEntityDescriptor("a"));
+        // assertNotNull(select3.getEntityDescriptor("A"));
+    }
+}



Mime
View raw message