cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r546624 - in /cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src: main/java/org/apache/cayenne/access/jdbc/ main/java/org/apache/cayenne/ejbql/ main/java/org/apache/cayenne/ejbql/parser/ main/java/org/apache/cayenne/query/ test/jav...
Date Tue, 12 Jun 2007 20:27:53 GMT
Author: aadamchik
Date: Tue Jun 12 13:27:51 2007
New Revision: 546624

URL: http://svn.apache.org/viewvc?view=rev&rev=546624
Log:
CAY-803: EJBQL Parameters support
(positional parameters)

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLAction.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLDelegatingVisitor.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLPositionalInputParameter.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLQueryTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslatorTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLAction.java?view=diff&rev=546624&r1=546623&r2=546624
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLAction.java
Tue Jun 12 13:27:51 2007
@@ -52,9 +52,12 @@
 
     public void performAction(Connection connection, OperationObserver observer)
             throws SQLException, Exception {
-        EJBQLCompiledExpression compiledExpression = query.getExpression(getEntityResolver());
-        final EJBQLTranslationContext context = new EJBQLTranslationContext(compiledExpression);
-        
+        EJBQLCompiledExpression compiledExpression = query
+                .getExpression(getEntityResolver());
+        final EJBQLTranslationContext context = new EJBQLTranslationContext(
+                compiledExpression,
+                query.getParameters());
+
         compiledExpression.getExpression().visit(new EJBQLBaseVisitor(false) {
 
             public boolean visitSelect(EJBQLExpression expression, int finishedChildIndex)
{

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java?view=diff&rev=546624&r1=546623&r2=546624
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
Tue Jun 12 13:27:51 2007
@@ -24,6 +24,7 @@
 import org.apache.cayenne.ejbql.EJBQLException;
 import org.apache.cayenne.ejbql.EJBQLExpression;
 import org.apache.cayenne.ejbql.parser.EJBQLPath;
+import org.apache.cayenne.ejbql.parser.EJBQLPositionalInputParameter;
 
 /**
  * @since 3.0
@@ -248,6 +249,13 @@
 
     public boolean visitPatternValue(EJBQLExpression expression) {
         // TODO: andrus 3/25/2007 - implement me
+        return true;
+    }
+
+    public boolean visitPositionalInputParameter(EJBQLPositionalInputParameter expression)
{
+
+        String parameter = context.bindPositionalParameter(expression.getPosition());
+        context.append('$').append(parameter);
         return true;
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java?view=diff&rev=546624&r1=546623&r2=546624
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
Tue Jun 12 13:27:51 2007
@@ -41,11 +41,13 @@
     private EJBQLCompiledExpression compiledExpression;
     private Map attributes;
     private Map reusableJoins;
+    private Map parameters;
 
-    EJBQLTranslationContext(EJBQLCompiledExpression compiledExpression) {
+    EJBQLTranslationContext(EJBQLCompiledExpression compiledExpression, Map parameters) {
         this.compiledExpression = compiledExpression;
         this.mainBuffer = new StringBuffer();
         this.currentBuffer = mainBuffer;
+        this.parameters = parameters;
     }
 
     SQLTemplate getQuery() {
@@ -135,6 +137,14 @@
 
     EJBQLCompiledExpression getCompiledExpression() {
         return compiledExpression;
+    }
+    
+    String bindPositionalParameter(int position) {
+        return bindParameter(parameters.get(new Integer(position)));
+    }
+    
+    String bindNamedParameter(String name) {
+        return bindParameter(parameters.get(name));
     }
 
     /**

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java?view=diff&rev=546624&r1=546623&r2=546624
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java
Tue Jun 12 13:27:51 2007
@@ -21,6 +21,7 @@
 import org.apache.cayenne.ejbql.parser.EJBQLFromItem;
 import org.apache.cayenne.ejbql.parser.EJBQLJoin;
 import org.apache.cayenne.ejbql.parser.EJBQLPath;
+import org.apache.cayenne.ejbql.parser.EJBQLPositionalInputParameter;
 
 /**
  * A noop implementation of the EJBQL visitor that returns same preset boolean value from
@@ -301,7 +302,7 @@
         return continueFlag;
     }
 
-    public boolean visitPositionalInputParameter(EJBQLExpression expression) {
+    public boolean visitPositionalInputParameter(EJBQLPositionalInputParameter expression)
{
         return continueFlag;
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLDelegatingVisitor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLDelegatingVisitor.java?view=diff&rev=546624&r1=546623&r2=546624
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLDelegatingVisitor.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLDelegatingVisitor.java
Tue Jun 12 13:27:51 2007
@@ -21,6 +21,7 @@
 import org.apache.cayenne.ejbql.parser.EJBQLFromItem;
 import org.apache.cayenne.ejbql.parser.EJBQLJoin;
 import org.apache.cayenne.ejbql.parser.EJBQLPath;
+import org.apache.cayenne.ejbql.parser.EJBQLPositionalInputParameter;
 
 /**
  * A base implementation of the EJBQLExpressionVisitor that implements all methods to
@@ -368,7 +369,7 @@
         return delegate != null ? delegate.visitPatternValue(expression) : continueFlag;
     }
 
-    public boolean visitPositionalInputParameter(EJBQLExpression expression) {
+    public boolean visitPositionalInputParameter(EJBQLPositionalInputParameter expression)
{
         return delegate != null
                 ? delegate.visitPositionalInputParameter(expression)
                 : continueFlag;

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java?view=diff&rev=546624&r1=546623&r2=546624
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java
Tue Jun 12 13:27:51 2007
@@ -21,6 +21,7 @@
 import org.apache.cayenne.ejbql.parser.EJBQLFromItem;
 import org.apache.cayenne.ejbql.parser.EJBQLJoin;
 import org.apache.cayenne.ejbql.parser.EJBQLPath;
+import org.apache.cayenne.ejbql.parser.EJBQLPositionalInputParameter;
 
 /**
  * A visitor interface to inspect the EJBQL expression tree.
@@ -286,7 +287,7 @@
 
     boolean visitPatternValue(EJBQLExpression expression);
 
-    boolean visitPositionalInputParameter(EJBQLExpression expression);
+    boolean visitPositionalInputParameter(EJBQLPositionalInputParameter expression);
 
     /**
      * Called on visiting "select" and also after visiting every expression child.

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLPositionalInputParameter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLPositionalInputParameter.java?view=diff&rev=546624&r1=546623&r2=546624
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLPositionalInputParameter.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLPositionalInputParameter.java
Tue Jun 12 13:27:51 2007
@@ -33,4 +33,8 @@
     protected boolean visitNode(EJBQLExpressionVisitor visitor) {
         return visitor.visitPositionalInputParameter(this);
     }
+
+    public int getPosition() {
+        return getText() != null ? Integer.parseInt(getText()) : -1;
+    }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java?view=diff&rev=546624&r1=546623&r2=546624
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java
Tue Jun 12 13:27:51 2007
@@ -18,6 +18,10 @@
  ****************************************************************/
 package org.apache.cayenne.query;
 
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.ejbql.EJBQLCompiledExpression;
 import org.apache.cayenne.ejbql.EJBQLException;
@@ -35,6 +39,7 @@
 
     protected String name;
     protected String ejbqlStatement;
+    protected Map parameters;
 
     protected transient EJBQLCompiledExpression expression;
     EJBQLQueryMetadata metadata = new EJBQLQueryMetadata();
@@ -90,5 +95,56 @@
 
     public void setName(String name) {
         this.name = name;
+    }
+
+    /**
+     * Returns unmodifiable map of combined named and positional parameters. Positional
+     * parameter keys are Integers, while named parameter keys are strings.
+     */
+    public Map getParameters() {
+        return parameters != null
+                ? Collections.unmodifiableMap(parameters)
+                : Collections.EMPTY_MAP;
+    }
+
+    /**
+     * Sets a named query parameter value.
+     */
+    public void setParameter(String name, Object object) {
+
+        // do a minimal sanity check
+        if (name == null || name.length() < 1) {
+            throw new IllegalArgumentException("Null or empty parameter name");
+        }
+
+        // TODO: andrus, 6/12/2007 - validate against available query parameters - JPA
+        // spec requires it.
+
+        if (parameters == null) {
+            parameters = new HashMap();
+        }
+
+        parameters.put(name, object);
+    }
+
+    /**
+     * Sets a positional query parameter value. Note that parameter indexes are starting
+     * from 1.
+     */
+    public void setParameter(int position, Object object) {
+
+        if (position < 1) {
+            throw new IllegalArgumentException("Parameter position must be >= 1: "
+                    + position);
+        }
+
+        // TODO: andrus, 6/12/2007 - validate against available query parameters - JPA
+        // spec requires it.
+
+        if (parameters == null) {
+            parameters = new HashMap();
+        }
+
+        parameters.put(new Integer(position), object);
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLQueryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLQueryTest.java?view=diff&rev=546624&r1=546623&r2=546624
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLQueryTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLQueryTest.java
Tue Jun 12 13:27:51 2007
@@ -180,4 +180,15 @@
         List ps = createDataContext().performQuery(query);
         assertEquals(2, ps.size());
     }
+    
+    public void testSelectFromWhereDecimalNumberPositional() throws Exception {
+        createTestData("prepare");
+
+        String ejbql = "select P from Painting P WHERE p.estimatedPrice <= ?1";
+        EJBQLQuery query = new EJBQLQuery(ejbql);
+        query.setParameter(1, new BigDecimal(5000.00));
+
+        List ps = createDataContext().performQuery(query);
+        assertEquals(2, ps.size());
+    }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslatorTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslatorTest.java?view=diff&rev=546624&r1=546623&r2=546624
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslatorTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslatorTest.java
Tue Jun 12 13:27:51 2007
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.access.jdbc;
 
+import java.util.Collections;
+
 import org.apache.cayenne.ejbql.EJBQLCompiledExpression;
 import org.apache.cayenne.ejbql.EJBQLParser;
 import org.apache.cayenne.ejbql.EJBQLParserFactory;
@@ -31,7 +33,9 @@
         EJBQLCompiledExpression select = parser.compile(ejbql, getDomain()
                 .getEntityResolver());
 
-        EJBQLTranslationContext tr = new EJBQLTranslationContext(select);
+        EJBQLTranslationContext tr = new EJBQLTranslationContext(
+                select,
+                Collections.EMPTY_MAP);
         select.getExpression().visit(new EJBQLSelectTranslator(tr));
         return tr.getQuery();
     }
@@ -208,17 +212,15 @@
                 + "NOT LIKE #bind('Stuff' 'VARCHAR')"));
     }
 
-    public void testSelectFromWhereRelationshipPropertyPath() {
-        SQLTemplate query = translateSelect("select p from Painting p where p.toArtist.artistName
= 'AA2'");
+    public void testSelectPositionalParameters() {
+        SQLTemplate query = translateSelect("select a from Artist a where a.artistName =
?1 or a.artistName = ?2");
         String sql = query.getDefaultTemplate();
 
-        System.out.println("SQL: " + sql);
-
         assertTrue(sql, sql.startsWith("SELECT "));
-        assertTrue(sql, sql.endsWith(" WHERE t1.ARTIST_NAME #bindEqual('AA2' 'VARCHAR')"));
-        // TODO: andrus, 3/25/2007 - implement joins support
-        // assertEquals(" FROM PAINTING t0 JOIN ARTIST t1 ON (t0.ARTIST_ID =
-        // t1.ARTIST_ID)", query.getParameters().get("from0"));
+        assertTrue(
+                sql,
+                sql
+                        .endsWith("t0.ARTIST_NAME #bindEqual($id1) OR t0.ARTIST_NAME #bindEqual($id2)"));
     }
 
     private int countDelimiters(String string, String delim, int fromIndex) {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java?view=diff&rev=546624&r1=546623&r2=546624
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java
Tue Jun 12 13:27:51 2007
@@ -18,11 +18,25 @@
  ****************************************************************/
 package org.apache.cayenne.query;
 
+import java.util.Map;
+
 import org.apache.cayenne.ejbql.EJBQLCompiledExpression;
 import org.apache.cayenne.map.EntityResolver;
 import org.apache.cayenne.unit.CayenneCase;
 
 public class EJBQLQueryTest extends CayenneCase {
+
+    public void testParameters() {
+        String ejbql = "select a FROM Artist a WHERE a.artistName = ?1 OR a.artistName =
:name";
+        EJBQLQuery query = new EJBQLQuery(ejbql);
+        query.setParameter(1, "X");
+        query.setParameter("name", "Y");
+
+        Map parameters = query.getParameters();
+        assertEquals(2, parameters.size());
+        assertEquals("X", parameters.get(new Integer(1)));
+        assertEquals("Y", parameters.get("name"));
+    }
 
     public void testGetExpression() {
         String ejbql = "select a FROM Artist a";



Mime
View raw message