cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r398249 - in /incubator/cayenne/jpa/trunk: cayenne-jpa-annotations-example/src/main/java/org/apache/cayenne/jpa/example/entity/ cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/ cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/
Date Sat, 29 Apr 2006 22:29:19 GMT
Author: aadamchik
Date: Sat Apr 29 15:29:13 2006
New Revision: 398249

URL: http://svn.apache.org/viewcvs?rev=398249&view=rev
Log:
implemented SelectQuery with qualifier and parameters support

Modified:
    incubator/cayenne/jpa/trunk/cayenne-jpa-annotations-example/src/main/java/org/apache/cayenne/jpa/example/entity/Department.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaIndirectQuery.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaSQLTemplate.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaSelectQuery.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/QueryHints.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaDataSourceFactory.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaQuery.java

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa-annotations-example/src/main/java/org/apache/cayenne/jpa/example/entity/Department.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa-annotations-example/src/main/java/org/apache/cayenne/jpa/example/entity/Department.java?rev=398249&r1=398248&r2=398249&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa-annotations-example/src/main/java/org/apache/cayenne/jpa/example/entity/Department.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa-annotations-example/src/main/java/org/apache/cayenne/jpa/example/entity/Department.java
Sat Apr 29 15:29:13 2006
@@ -25,7 +25,8 @@
 
 @Entity
 @NamedQuery(name = "DepartmentWithName", query = "delete from department", hints = {
-    @QueryHint(name = QueryHints.QUERY_TYPE_HINT, value = QueryHints.SELECT_QUERY)
+        @QueryHint(name = QueryHints.QUERY_TYPE_HINT, value = QueryHints.SELECT_QUERY),
+        @QueryHint(name = QueryHints.QUALIFIER_HINT, value = "name likeIgnoreCase $name")
 })
 public class Department {
 

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaIndirectQuery.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaIndirectQuery.java?rev=398249&r1=398248&r2=398249&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaIndirectQuery.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaIndirectQuery.java
Sat Apr 29 15:29:13 2006
@@ -15,21 +15,45 @@
  */
 package org.apache.cayenne.jpa.bridge;
 
+import java.util.Map;
+
+import org.apache.cayenne.jpa.JpaProviderException;
 import org.apache.cayenne.jpa.map.JpaNamedQuery;
 import org.objectstyle.cayenne.map.DataMap;
 import org.objectstyle.cayenne.map.ObjEntity;
 import org.objectstyle.cayenne.query.IndirectQuery;
+import org.objectstyle.cayenne.query.ParameterizedQuery;
+import org.objectstyle.cayenne.query.Query;
 
 /**
  * A superclass of indirect queries that map JPA to Cayenne queries.
  * 
  * @author Andrus Adamchik
  */
-public abstract class JpaIndirectQuery extends IndirectQuery {
+public abstract class JpaIndirectQuery extends IndirectQuery implements
+        ParameterizedQuery {
 
     protected JpaNamedQuery jpaQuery;
     protected DataMap parentMap;
     protected ObjEntity parentEntity;
+    protected Map parameters;
+
+    public Query createQuery(Map parameters) {
+        JpaIndirectQuery clone;
+        try {
+            clone = (JpaIndirectQuery) getClass().newInstance();
+        }
+        catch (Exception e) {
+            throw new JpaProviderException("Error cloning a query", e);
+        }
+
+        clone.setJpaQuery(jpaQuery);
+        clone.setParentEntity(parentEntity);
+        clone.setParentMap(parentMap);
+        clone.parameters = parameters;
+
+        return clone;
+    }
 
     public JpaNamedQuery getJpaQuery() {
         return jpaQuery;

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaSQLTemplate.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaSQLTemplate.java?rev=398249&r1=398248&r2=398249&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaSQLTemplate.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaSQLTemplate.java
Sat Apr 29 15:29:13 2006
@@ -15,6 +15,8 @@
  */
 package org.apache.cayenne.jpa.bridge;
 
+import java.util.Collections;
+
 import org.objectstyle.cayenne.CayenneRuntimeException;
 import org.objectstyle.cayenne.map.EntityResolver;
 import org.objectstyle.cayenne.query.Query;
@@ -46,6 +48,9 @@
             throw new CayenneRuntimeException("Unknown query root. Name: " + getName());
         }
 
-        return query;
+        return query.queryWithParameters(parameters != null
+                ? parameters
+                : Collections.EMPTY_MAP);
     }
+
 }

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaSelectQuery.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaSelectQuery.java?rev=398249&r1=398248&r2=398249&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaSelectQuery.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/JpaSelectQuery.java
Sat Apr 29 15:29:13 2006
@@ -15,8 +15,14 @@
  */
 package org.apache.cayenne.jpa.bridge;
 
+import java.util.Collections;
+
+import org.apache.cayenne.jpa.map.JpaQueryHint;
+import org.objectstyle.cayenne.CayenneRuntimeException;
+import org.objectstyle.cayenne.exp.Expression;
 import org.objectstyle.cayenne.map.EntityResolver;
 import org.objectstyle.cayenne.query.Query;
+import org.objectstyle.cayenne.query.SelectQuery;
 
 /**
  * An indirect query that resolves to Cayenne SelectQuery using information from JPA query
@@ -26,8 +32,28 @@
  */
 public class JpaSelectQuery extends JpaIndirectQuery {
 
+    /**
+     * Creates a SelectQuery using query hints.
+     */
     @Override
     protected Query createReplacementQuery(EntityResolver resolver) {
-        throw new UnsupportedOperationException("TODO");
+        SelectQuery query = new SelectQuery();
+
+        if (parentEntity != null) {
+            query.setRoot(parentEntity);
+        }
+        else {
+            throw new CayenneRuntimeException("No parent entity. Query name: "
+                    + getName());
+        }
+
+        JpaQueryHint qualifierHint = jpaQuery.getHint(QueryHints.QUALIFIER_HINT);
+        if (qualifierHint != null) {
+            query.setQualifier(Expression.fromString(qualifierHint.getValue()));
+        }
+
+        return query.queryWithParameters(parameters != null
+                ? parameters
+                : Collections.EMPTY_MAP);
     }
 }

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/QueryHints.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/QueryHints.java?rev=398249&r1=398248&r2=398249&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/QueryHints.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/QueryHints.java
Sat Apr 29 15:29:13 2006
@@ -15,7 +15,6 @@
  */
 package org.apache.cayenne.jpa.bridge;
 
-
 public interface QueryHints {
 
     public static final String QUERY_TYPE_HINT = "cayenne.query.type";
@@ -26,4 +25,6 @@
     public static final String SELECT_QUERY = "org.apache.cayenne.jpa.bridge.JpaSelectQuery";
     public static final String PROCEDURE_QUERY = "org.apache.cayenne.jpa.bridge.JpaProcedureQuery";
     public static final String SQL_TEMPLATE_QUERY = "org.apache.cayenne.jpa.bridge.JpaSQLTemplate";
+
+    public static final String QUALIFIER_HINT = "cayenne.query.qualifier";
 }

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaDataSourceFactory.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaDataSourceFactory.java?rev=398249&r1=398248&r2=398249&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaDataSourceFactory.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaDataSourceFactory.java
Sat Apr 29 15:29:13 2006
@@ -62,8 +62,8 @@
 public class CjpaDataSourceFactory extends JndiJpaDataSourceFactory {
 
     public static final String DATA_SOURCE_PREFIX = "CayenneDataSource.";
-    public static final String MIN_CONNECTIONS_SUFFIX = "minConnections";
-    public static final String MAX_CONNECTIONS_SUFFIX = "maxConnections";
+    public static final String MIN_CONNECTIONS_SUFFIX = "jdbc.minConnections";
+    public static final String MAX_CONNECTIONS_SUFFIX = "jdbc.maxConnections";
 
     static String getPropertyName(String dataSourceName, String suffix) {
         return DATA_SOURCE_PREFIX + dataSourceName + "." + suffix;

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaQuery.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaQuery.java?rev=398249&r1=398248&r2=398249&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaQuery.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/cspi/CjpaQuery.java
Sat Apr 29 15:29:13 2006
@@ -39,13 +39,11 @@
 public class CjpaQuery implements Query {
 
     protected Map<String, Object> parameters = new HashMap<String, Object>();
-
-    protected org.objectstyle.cayenne.query.Query cquery;
-
-    protected DataContext ctxt;
+    protected org.objectstyle.cayenne.query.Query cayenneQuery;
+    protected DataContext context;
 
     public CjpaQuery(DataContext ctxt) {
-        this.ctxt = ctxt;
+        this.context = ctxt;
     }
 
     /**
@@ -54,10 +52,11 @@
      * @param ctxt
      * @param name
      */
-    public CjpaQuery(DataContext ctxt, String name) {
-        this(ctxt);
+    public CjpaQuery(DataContext context, String name) {
+        this(context);
 
-        org.objectstyle.cayenne.query.Query q = ctxt.getEntityResolver().lookupQuery(name);
+        org.objectstyle.cayenne.query.Query q = context.getEntityResolver().lookupQuery(
+                name);
 
         if (q == null) {
             throw new IllegalArgumentException("Non-existing query: " + name);
@@ -67,11 +66,11 @@
     }
 
     protected void setQuery(org.objectstyle.cayenne.query.Query q) {
-        this.cquery = q;
+        this.cayenneQuery = q;
     }
 
     protected org.objectstyle.cayenne.query.Query getQuery() {
-        return cquery;
+        return cayenneQuery;
     }
 
     /**
@@ -79,35 +78,32 @@
      */
     private org.objectstyle.cayenne.query.Query queryWithParameters() {
         if (parameters.size() == 0) {
-            return cquery;
+            return cayenneQuery;
         }
 
-        return ((ParameterizedQuery) cquery).createQuery(parameters);
+        return ((ParameterizedQuery) cayenneQuery).createQuery(parameters);
     }
 
     /**
      * Execute a SELECT query and return the query results as a List.
      * 
      * @return a list of the results
-     * @throws IllegalStateException
-     *             if called for an EJB QL UPDATE or DELETE statement
+     * @throws IllegalStateException if called for an EJB QL UPDATE or DELETE statement
      */
     public List getResultList() {
-        return ctxt.performQuery(queryWithParameters());
+        return context.performQuery(queryWithParameters());
     }
 
     /**
      * Execute an update or delete statement.
      * 
      * @return the number of entities updated or deleted
-     * @throws IllegalStateException
-     *             if called for an EJB QL SELECT statement
-     * @throws TransactionRequiredException
-     *             if there is no transaction
+     * @throws IllegalStateException if called for an EJB QL SELECT statement
+     * @throws TransactionRequiredException if there is no transaction
      */
     public int executeUpdate() {
         // TODO: check transaction
-        int[] res = ctxt.performNonSelectingQuery(queryWithParameters());
+        int[] res = context.performNonSelectingQuery(queryWithParameters());
         int num = 0;
         for (int i = 0; i < res.length; i++) {
             num = num + res[i];
@@ -119,12 +115,9 @@
      * Execute a SELECT query that returns a single result.
      * 
      * @return the result
-     * @throws NoResultException
-     *             if there is no result
-     * @throws NonUniqueResultException
-     *             if more than one result
-     * @throws IllegalStateException
-     *             if called for an EJB QL UPDATE or DELETE statement
+     * @throws NoResultException if there is no result
+     * @throws NonUniqueResultException if more than one result
+     * @throws IllegalStateException if called for an EJB QL UPDATE or DELETE statement
      */
     public Object getSingleResult() {
         List rows = getResultList();
@@ -143,8 +136,7 @@
      * 
      * @param maxResult
      * @return the same query instance
-     * @throws IllegalArgumentException
-     *             if argument is negative
+     * @throws IllegalArgumentException if argument is negative
      */
     public Query setMaxResults(int maxResult) {
         if (maxResult < 0) {
@@ -154,9 +146,11 @@
         // TODO: use QueryMetadata?
         if (getQuery() instanceof SelectQuery) {
             ((SelectQuery) getQuery()).setFetchLimit(maxResult);
-        } else if (getQuery() instanceof SQLTemplate) {
+        }
+        else if (getQuery() instanceof SQLTemplate) {
             ((SQLTemplate) getQuery()).setFetchLimit(maxResult);
-        } else if (getQuery() instanceof ProcedureQuery) {
+        }
+        else if (getQuery() instanceof ProcedureQuery) {
             ((ProcedureQuery) getQuery()).setFetchLimit(maxResult);
         }
 
@@ -164,14 +158,14 @@
     }
 
     /**
-     * Set an implementation-specific hint. If the hint name is not recognized,
-     * it is silently ignored.
+     * Set an implementation-specific hint. If the hint name is not recognized, it is
+     * silently ignored.
      * 
      * @param hintName
      * @param value
      * @return the same query instance
-     * @throws IllegalArgumentException
-     *             if the second argument is not valid for the implementation
+     * @throws IllegalArgumentException if the second argument is not valid for the
+     *             implementation
      */
     public Query setHint(String hintName, Object value) {
         return this;
@@ -180,15 +174,14 @@
     /**
      * Set the position of the first result to retrieve.
      * 
-     * @param start
-     *            position of the first result, numbered from 0
+     * @param start position of the first result, numbered from 0
      * @return the same query instance
-     * @throws IllegalArgumentException
-     *             if argument is negative
+     * @throws IllegalArgumentException if argument is negative
      */
     public Query setFirstResult(int startPosition) {
         if (startPosition < 0) {
-            throw new IllegalArgumentException("Invalid first result value: " + startPosition);
+            throw new IllegalArgumentException("Invalid first result value: "
+                    + startPosition);
         }
         // TODO: support in core like fetchLimit?
         // TODO: hack a temp solution here based on sub-list?
@@ -198,16 +191,14 @@
     /**
      * Bind an argument to a named parameter.
      * 
-     * @param name
-     *            the parameter name
+     * @param name the parameter name
      * @param value
      * @return the same query instance
-     * @throws IllegalArgumentException
-     *             if parameter name does not correspond to parameter in query
-     *             string or argument is of incorrect type
+     * @throws IllegalArgumentException if parameter name does not correspond to parameter
+     *             in query string or argument is of incorrect type
      */
     public Query setParameter(String name, Object value) {
-        if (!(cquery instanceof ParameterizedQuery)) {
+        if (!(cayenneQuery instanceof ParameterizedQuery)) {
             throw new IllegalArgumentException("query does not accept parameters");
         }
 
@@ -225,9 +216,8 @@
      * @param value
      * @param temporalType
      * @return the same query instance
-     * @throws IllegalArgumentException
-     *             if parameter name does not correspond to parameter in query
-     *             string
+     * @throws IllegalArgumentException if parameter name does not correspond to parameter
+     *             in query string
      */
     public Query setParameter(String name, Date value, TemporalType temporalType) {
         // handled by cayenne.
@@ -241,9 +231,8 @@
      * @param value
      * @param temporalType
      * @return the same query instance
-     * @throws IllegalArgumentException
-     *             if parameter name does not correspond to parameter in query
-     *             string
+     * @throws IllegalArgumentException if parameter name does not correspond to parameter
+     *             in query string
      */
     public Query setParameter(String name, Calendar value, TemporalType temporalType) {
         // handled by cayenne.
@@ -256,9 +245,8 @@
      * @param position
      * @param value
      * @return the same query instance
-     * @throws IllegalArgumentException
-     *             if position does not correspond to positional parameter of
-     *             query or argument is of incorrect type
+     * @throws IllegalArgumentException if position does not correspond to positional
+     *             parameter of query or argument is of incorrect type
      */
     public Query setParameter(int position, Object value) {
         // TODO: implement
@@ -272,9 +260,8 @@
      * @param value
      * @param temporalType
      * @return the same query instance
-     * @throws IllegalArgumentException
-     *             if position does not correspond to positional parameter of
-     *             query
+     * @throws IllegalArgumentException if position does not correspond to positional
+     *             parameter of query
      */
     public Query setParameter(int position, Date value, TemporalType temporalType) {
         // handled by cayenne.
@@ -288,9 +275,8 @@
      * @param value
      * @param temporalType
      * @return the same query instance
-     * @throws IllegalArgumentException
-     *             if position does not correspond to positional parameter of
-     *             query
+     * @throws IllegalArgumentException if position does not correspond to positional
+     *             parameter of query
      */
     public Query setParameter(int position, Calendar value, TemporalType temporalType) {
         // handled by cayenne.



Mime
View raw message