cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r549810 - in /cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src: main/java/org/apache/cayenne/access/ main/java/org/apache/cayenne/query/ main/java/org/apache/cayenne/remote/ test/java/org/apache/cayenne/ test/java/org/apache/caye...
Date Fri, 22 Jun 2007 13:07:58 GMT
Author: aadamchik
Date: Fri Jun 22 06:07:56 2007
New Revision: 549810

URL: http://svn.apache.org/viewvc?view=rev&rev=549810
Log:
ITS-809 Support for explicit mapping of SQL results with SQLResultSetMapping

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLResultSetMapping.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQuery.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQueryAction.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/DefaultQueryMetadata.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadata.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadataWrapper.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplateMetadata.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/remote/IncrementalQuery.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/remote/RangeQuery.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/DataObjectUtilsTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/MockQueryMetadata.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/DataObjectUtilsTest.xml
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextSQLTemplateTest.xml

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQuery.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQuery.java?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQuery.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQuery.java
Fri Jun 22 06:07:56 2007
@@ -29,6 +29,7 @@
 import org.apache.cayenne.query.QueryRouter;
 import org.apache.cayenne.query.SQLAction;
 import org.apache.cayenne.query.SQLActionVisitor;
+import org.apache.cayenne.query.SQLResultSetMapping;
 import org.apache.cayenne.reflect.ClassDescriptor;
 
 /**
@@ -46,6 +47,10 @@
 
     public QueryMetadata getMetaData(EntityResolver resolver) {
         return this;
+    }
+    
+    public SQLResultSetMapping getResultSetMapping() {
+        return null;
     }
 
     public String getName() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQueryAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQueryAction.java?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQueryAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQueryAction.java
Fri Jun 22 06:07:56 2007
@@ -48,6 +48,7 @@
 import org.apache.cayenne.query.QueryRouter;
 import org.apache.cayenne.query.RefreshQuery;
 import org.apache.cayenne.query.RelationshipQuery;
+import org.apache.cayenne.query.SQLResultSetMapping;
 import org.apache.cayenne.reflect.ClassDescriptor;
 import org.apache.cayenne.util.GenericResponse;
 import org.apache.cayenne.util.ListResponse;
@@ -121,7 +122,10 @@
         }
 
         if (!noObjectConversion) {
-            interceptObjectConversion();
+
+            if (interceptMappedConversion() != DONE) {
+                interceptObjectConversion();
+            }
         }
 
         return response;
@@ -455,6 +459,66 @@
                 }
             }
         }
+    }
+
+    private boolean interceptMappedConversion() {
+        SQLResultSetMapping rsMapping = metadata.getResultSetMapping();
+
+        if (rsMapping == null) {
+            return !DONE;
+        }
+
+        List mainRows = response.firstList();
+        if (mainRows != null && !mainRows.isEmpty()) {
+
+            Collection columns = rsMapping.getColumnResults();
+            if (columns.isEmpty()) {
+                throw new CayenneRuntimeException(
+                        "Invalid result set mapping, no columns mapped.");
+            }
+
+            Object[] columnsArray = columns.toArray();
+            int rowsLen = mainRows.size();
+            int rowWidth = columnsArray.length;
+
+            List objects = new ArrayList(rowsLen);
+
+            // add scalars to the result
+            if (rowWidth == 1) {
+
+                for (int i = 0; i < rowsLen; i++) {
+                    Map row = (Map) mainRows.get(i);
+                    objects.add(row.get(columnsArray[0]));
+                }
+            }
+            // add Object[]'s to the result
+            else {
+                for (int i = 0; i < rowsLen; i++) {
+                    Map row = (Map) mainRows.get(i);
+                    Object[] rowDecoded = new Object[rowWidth];
+
+                    for (int j = 0; j < rowWidth; j++) {
+                        rowDecoded[j] = row.get(columnsArray[j]);
+                    }
+
+                    objects.add(rowDecoded);
+                }
+            }
+
+            if (response instanceof GenericResponse) {
+                ((GenericResponse) response).replaceResult(mainRows, objects);
+            }
+            else if (response instanceof ListResponse) {
+                this.response = new ListResponse(objects);
+            }
+            else {
+                throw new IllegalStateException("Unknown response object: "
+                        + this.response);
+            }
+
+        }
+
+        return DONE;
     }
 
     public void route(QueryEngine engine, Query query, Query substitutedQuery) {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java
Fri Jun 22 06:07:56 2007
@@ -51,6 +51,7 @@
 import org.apache.cayenne.query.PrefetchTreeNode;
 import org.apache.cayenne.query.QueryMetadata;
 import org.apache.cayenne.query.RefreshQuery;
+import org.apache.cayenne.query.SQLResultSetMapping;
 import org.apache.cayenne.reflect.AttributeProperty;
 import org.apache.cayenne.reflect.ClassDescriptor;
 import org.apache.cayenne.reflect.PropertyVisitor;
@@ -1097,6 +1098,10 @@
 
         public String getCacheKey() {
             return cacheKey;
+        }
+        
+        public SQLResultSetMapping getResultSetMapping() {
+            return null;
         }
 
         public String[] getCacheGroups() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java
Fri Jun 22 06:07:56 2007
@@ -288,6 +288,15 @@
     public ClassDescriptor getClassDescriptor() {
         return classDescriptor;
     }
+    
+    /**
+     * Always returns null, as this is not supported for most classic queries.
+     * 
+     * @since 3.0
+     */
+    public SQLResultSetMapping getResultSetMapping() {
+        return null;
+    }
 
     /**
      * @since 1.2

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/DefaultQueryMetadata.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/DefaultQueryMetadata.java?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/DefaultQueryMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/DefaultQueryMetadata.java
Fri Jun 22 06:07:56 2007
@@ -54,6 +54,13 @@
 
         return null;
     }
+    
+    /**
+     * @since 3.0
+     */
+    public SQLResultSetMapping getResultSetMapping() {
+        return null;
+    }
 
     public DbEntity getDbEntity() {
         return null;

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadata.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadata.java?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadata.java
Fri Jun 22 06:07:56 2007
@@ -94,7 +94,7 @@
     public static final boolean RESOLVING_INHERITED_DEFAULT = true;
 
     public static final String CACHE_POLICY_PROPERTY = "cayenne.GenericSelectQuery.cachePolicy";
-    
+
     /**
      * @since 3.0
      */
@@ -106,7 +106,7 @@
      * @since 3.0
      */
     ClassDescriptor getClassDescriptor();
-    
+
     /**
      * Returns an ObjEntity associated with a query or null if no such entity exists.
      */
@@ -201,4 +201,12 @@
      * are configured.
      */
     PrefetchTreeNode getPrefetchTree();
+
+    /**
+     * Returns a mapping of the result set data rows. Can be null. If not null overrides
+     * prefetching and data row settings.
+     * 
+     * @since 3.0
+     */
+    SQLResultSetMapping getResultSetMapping();
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadataWrapper.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadataWrapper.java?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadataWrapper.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadataWrapper.java
Fri Jun 22 06:07:56 2007
@@ -61,6 +61,13 @@
     boolean overrideExists(String key) {
         return overrides != null && overrides.containsKey(key);
     }
+    
+    /**
+     * @since 3.0
+     */
+    public SQLResultSetMapping getResultSetMapping() {
+        return info.getResultSetMapping();
+    }
 
     public DataMap getDataMap() {
         return info.getDataMap();

Added: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLResultSetMapping.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLResultSetMapping.java?view=auto&rev=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLResultSetMapping.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLResultSetMapping.java
Fri Jun 22 06:07:56 2007
@@ -0,0 +1,73 @@
+/*****************************************************************
+ *   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.query;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * A metadata object that defines how a DataRow can be converted to result objects. This
+ * object provides mapping in a JPA-compilant manner, i.e. the DataRow is mapped either to
+ * a single Object or an Object[]. Each object (single result object or an array element
+ * object) can be a scalar or a Persistent object.
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+// TODO: andrus, 6/22/2007 - support entity results mapping.
+public class SQLResultSetMapping {
+
+    protected String name;
+    protected Collection columnResults;
+
+    public SQLResultSetMapping() {
+
+    }
+
+    public SQLResultSetMapping(String name) {
+        this.name = name;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * Returns a collection of mapped columns.
+     */
+    public Collection getColumnResults() {
+        return columnResults != null ? columnResults : Collections.EMPTY_LIST;
+    }
+
+    /**
+     * Adds a result set column name to the mapping.
+     */
+    public void addColumnResult(String column) {
+        if (columnResults == null) {
+            columnResults = new ArrayList(3);
+        }
+
+        columnResults.add(column);
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java
Fri Jun 22 06:07:56 2007
@@ -48,7 +48,7 @@
  * </p>
  * 
  * <pre>
- *    SELECT ID, NAME FROM SOME_TABLE WHERE NAME LIKE $a
+ *          SELECT ID, NAME FROM SOME_TABLE WHERE NAME LIKE $a
  * </pre>
  * 
  * <p>
@@ -593,5 +593,16 @@
      */
     public void setColumnNamesCapitalization(String columnNameCapitalization) {
         this.columnNamesCapitalization = columnNameCapitalization;
+    }
+
+    /**
+     * Sets an optional explicit mapping of the result set. If result set mapping is
+     * specified, the result of SQLTemplate may not be a normal list of Persistent objects
+     * or DataRows, instead it will follow the {@link SQLResultSetMapping} rules.
+     * 
+     * @since 3.0
+     */
+    public void setResultSetMapping(SQLResultSetMapping resultSetMapping) {
+        selectInfo.setResultSetMapping(resultSetMapping);
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplateMetadata.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplateMetadata.java?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplateMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplateMetadata.java
Fri Jun 22 06:07:56 2007
@@ -32,6 +32,16 @@
  * @author Andrus Adamchik
  */
 class SQLTemplateMetadata extends BaseQueryMetadata {
+    
+    private SQLResultSetMapping resultSetMapping;
+    
+    void setResultSetMapping(SQLResultSetMapping resultSetMapping) {
+        this.resultSetMapping = resultSetMapping;
+    }
+    
+    public SQLResultSetMapping getResultSetMapping() {
+        return resultSetMapping;
+    }
 
     boolean resolve(Object root, EntityResolver resolver, SQLTemplate query) {
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/remote/IncrementalQuery.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/remote/IncrementalQuery.java?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/remote/IncrementalQuery.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/remote/IncrementalQuery.java
Fri Jun 22 06:07:56 2007
@@ -30,6 +30,7 @@
 import org.apache.cayenne.query.QueryRouter;
 import org.apache.cayenne.query.SQLAction;
 import org.apache.cayenne.query.SQLActionVisitor;
+import org.apache.cayenne.query.SQLResultSetMapping;
 import org.apache.cayenne.reflect.ClassDescriptor;
 
 /**
@@ -62,6 +63,10 @@
 
             public String getCacheKey() {
                 return cacheKey;
+            }
+            
+            public SQLResultSetMapping getResultSetMapping() {
+                return metadata.getResultSetMapping();
             }
             
             public String[] getCacheGroups() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/remote/RangeQuery.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/remote/RangeQuery.java?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/remote/RangeQuery.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/remote/RangeQuery.java
Fri Jun 22 06:07:56 2007
@@ -30,6 +30,7 @@
 import org.apache.cayenne.query.QueryRouter;
 import org.apache.cayenne.query.SQLAction;
 import org.apache.cayenne.query.SQLActionVisitor;
+import org.apache.cayenne.query.SQLResultSetMapping;
 import org.apache.cayenne.reflect.ClassDescriptor;
 
 /**
@@ -67,6 +68,10 @@
 
     public QueryMetadata getMetaData(EntityResolver resolver) {
         return new QueryMetadata() {
+            
+            public SQLResultSetMapping getResultSetMapping() {
+                return null;
+            }
 
             public String getCacheKey() {
                 return cacheKey;

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/DataObjectUtilsTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/DataObjectUtilsTest.java?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/DataObjectUtilsTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/DataObjectUtilsTest.java
Fri Jun 22 06:07:56 2007
@@ -28,7 +28,10 @@
 import org.apache.art.CharPkTestEntity;
 import org.apache.art.CompoundPkTestEntity;
 import org.apache.cayenne.access.DataContext;
+import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.query.ObjectIdQuery;
+import org.apache.cayenne.query.SQLResultSetMapping;
+import org.apache.cayenne.query.SQLTemplate;
 import org.apache.cayenne.query.SelectQuery;
 import org.apache.cayenne.unit.CayenneCase;
 
@@ -40,6 +43,26 @@
     protected void setUp() throws Exception {
         super.setUp();
         deleteTestData();
+    }
+
+    public void testScalarObjectForQuery() throws Exception {
+        createTestData("testScalarObjectForQuery");
+        DataContext context = createDataContext();
+
+        String sql = "SELECT count(1) AS C FROM ARTIST";
+
+        DataMap map = getDomain().getMap("testmap");
+        SQLTemplate query = new SQLTemplate(map, sql);
+        query.setColumnNamesCapitalization(SQLTemplate.UPPERCASE_COLUMN_NAMES);
+
+        SQLResultSetMapping rsMap = new SQLResultSetMapping();
+        rsMap.addColumnResult("C");
+        query.setResultSetMapping(rsMap);
+
+        Object object = DataObjectUtils.objectForQuery(context, query);
+        assertNotNull(object);
+        assertTrue(object instanceof Number);
+        assertEquals(2, ((Number) object).intValue());
     }
 
     public void testObjectForQuery() throws Exception {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java
Fri Jun 22 06:07:56 2007
@@ -31,6 +31,8 @@
 import org.apache.cayenne.DataObjectUtils;
 import org.apache.cayenne.DataRow;
 import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.query.SQLResultSetMapping;
 import org.apache.cayenne.query.SQLTemplate;
 import org.apache.cayenne.unit.CayenneCase;
 
@@ -47,6 +49,55 @@
         context = createDataContext();
     }
 
+    public void testSQLResultSetMappingScalar() throws Exception {
+        createTestData("testSQLResultSetMappingScalar");
+        
+        String sql = "SELECT count(1) AS C FROM ARTIST";
+        
+        DataMap map = getDomain().getMap("testmap");
+        SQLTemplate query = new SQLTemplate(map, sql);
+        query.setColumnNamesCapitalization(SQLTemplate.UPPERCASE_COLUMN_NAMES);
+        
+        SQLResultSetMapping rsMap = new SQLResultSetMapping();
+        rsMap.addColumnResult("C");
+        query.setResultSetMapping(rsMap);
+        
+        List objects = createDataContext().performQuery(query);
+        assertEquals(1, objects.size());
+        
+        Object o = objects.get(0);
+        assertTrue(o instanceof Number);
+        assertEquals(4, ((Number) o).intValue());
+    }
+    
+    public void testSQLResultSetMappingScalarArray() throws Exception {
+        createTestData("testSQLResultSetMappingScalar");
+        
+        String sql = "SELECT count(1) AS C, 77 AS D FROM ARTIST";
+        
+        DataMap map = getDomain().getMap("testmap");
+        SQLTemplate query = new SQLTemplate(map, sql);
+        query.setColumnNamesCapitalization(SQLTemplate.UPPERCASE_COLUMN_NAMES);
+        
+        SQLResultSetMapping rsMap = new SQLResultSetMapping();
+        rsMap.addColumnResult("C");
+        rsMap.addColumnResult("D");
+        query.setResultSetMapping(rsMap);
+        
+        List objects = createDataContext().performQuery(query);
+        assertEquals(1, objects.size());
+        
+        Object o = objects.get(0);
+        assertTrue(o instanceof Object[]);
+        
+        Object[] row = (Object[]) o;
+        assertEquals(2, row.length);
+        
+        assertEquals(4, ((Number) row[0]).intValue());
+        assertEquals(77, ((Number) row[1]).intValue());
+    }
+
+
     public void testColumnNamesCapitalization() throws Exception {
         getAccessStack().createTestData(DataContextCase.class, "testArtists", null);
 
@@ -197,7 +248,6 @@
         assertEquals(33002, DataObjectUtils.intPKForObject(p));
     }
 
-    
     public void testBindObjectEqualNull() throws Exception {
         createTestData("prepare");
 
@@ -262,7 +312,7 @@
         CompoundFkTestEntity p = (CompoundFkTestEntity) objects.get(0);
         assertEquals(33001, DataObjectUtils.intPKForObject(p));
     }
-    
+
     public void testBindObjectNotEqualCompound() throws Exception {
         createTestData("testBindObjectEqualCompound");
 
@@ -289,7 +339,6 @@
         CompoundFkTestEntity p = (CompoundFkTestEntity) objects.get(0);
         assertEquals(33002, DataObjectUtils.intPKForObject(p));
     }
-
 
     public void testBindObjectNotEqualNull() throws Exception {
         createTestData("prepare");

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/MockQueryMetadata.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/MockQueryMetadata.java?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/MockQueryMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/query/MockQueryMetadata.java
Fri Jun 22 06:07:56 2007
@@ -26,6 +26,10 @@
 import org.apache.cayenne.reflect.ClassDescriptor;
 
 public class MockQueryMetadata implements QueryMetadata {
+    
+    public SQLResultSetMapping getResultSetMapping() {
+        return null;
+    }
 
     public ObjEntity getObjEntity() {
         return null;

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/DataObjectUtilsTest.xml
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/DataObjectUtilsTest.xml?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/DataObjectUtilsTest.xml
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/DataObjectUtilsTest.xml
Fri Jun 22 06:07:56 2007
@@ -39,7 +39,16 @@
 	
 	<!-- ======================================= -->
 	<!-- Data Sets -->
-	<!-- ======================================= -->	
+	<!-- ======================================= -->
+	<bean id="testScalarObjectForQuery" class="java.util.ArrayList">
+		<constructor-arg>
+			<list>
+				<ref bean="A1"/>
+				<ref bean="A2"/>
+			</list>
+		</constructor-arg>
+	</bean>
+		
 	<bean id="testCompoundPKForObject" class="java.util.ArrayList">
 		<constructor-arg>
 			<list>

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextSQLTemplateTest.xml
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextSQLTemplateTest.xml?view=diff&rev=549810&r1=549809&r2=549810
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextSQLTemplateTest.xml
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextSQLTemplateTest.xml
Fri Jun 22 06:07:56 2007
@@ -112,4 +112,15 @@
 			</list>
 		</constructor-arg>
 	</bean>
+	
+	<bean id="testSQLResultSetMappingScalar" class="java.util.ArrayList">
+		<constructor-arg>
+			<list>
+				<ref bean="A1"/>
+				<ref bean="A2"/>
+				<ref bean="A3"/>
+				<ref bean="A4"/>
+			</list>
+		</constructor-arg>
+	</bean>
 </beans>



Mime
View raw message