cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r722024 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne: access/ access/jdbc/ map/ query/ reflect/
Date Mon, 01 Dec 2008 10:06:11 GMT
Author: aadamchik
Date: Mon Dec  1 02:06:04 2008
New Revision: 722024

URL: http://svn.apache.org/viewvc?rev=722024&view=rev
Log:
CAY-1140 Store ObjEntity name in the DataRow

implementing result set processing for array result sets at the row reader level

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/InheritanceAwareEntityRowReader.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultScalarResultMetadata.java
      - copied, changed from r722021, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResultMetadata.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/ScalarResultMetadata.java
      - copied, changed from r722021, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResultMetadata.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQueryAction.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectResolver.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EntityRowReader.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/JDBCResultIterator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/ScalarRowReader.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/EntityResult.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultEntityResultMetadata.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultResultSetMetadata.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResultMetadata.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SQLResultSetMetadata.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorResultMetadata.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQueryAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQueryAction.java?rev=722024&r1=722023&r2=722024&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQueryAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataDomainQueryAction.java
Mon Dec  1 02:06:04 2008
@@ -642,18 +642,24 @@
                 PrefetchTreeNode prefetchTree,
                 List<Object[]> rows,
                 int position) {
+
+            int len = rows.size();
+            List<DataRow> rowsColumn = new ArrayList<DataRow>(len);
+            for (int i = 0; i < len; i++) {
+                rowsColumn.add((DataRow) rows.get(i)[position]);
+            }
             List<Persistent> objects;
 
             // take a shortcut when no prefetches exist...
             if (prefetchTree == null) {
                 objects = new ObjectResolver(context, descriptor)
-                        .synchronizedObjectsFromDataRows(rows, position);
+                        .synchronizedObjectsFromDataRows(rowsColumn);
             }
             else {
                 ObjectTreeResolver resolver = new ObjectTreeResolver(context, metadata);
                 objects = resolver.synchronizedObjectsFromDataRows(
                         prefetchTree,
-                        rows,
+                        rowsColumn,
                         prefetchResultsByPath);
             }
             return objects;
@@ -686,7 +692,7 @@
 
                 Object[] row = mainRows.get(j);
                 for (int i = 0; i < entityPositions.length; i++) {
-                    row[i] = resultLists[i].get(j);
+                    row[entityPositions[i]] = resultLists[i].get(j);
                 }
             }
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectResolver.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectResolver.java?rev=722024&r1=722023&r2=722024&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectResolver.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectResolver.java
Mon Dec  1 02:06:04 2008
@@ -95,12 +95,6 @@
         }
     }
 
-    List<Persistent> synchronizedObjectsFromDataRows(List<Object[]> rows, int
position) {
-        synchronized (context.getObjectStore()) {
-            return objectsFromDataRows(rows, position);
-        }
-    }
-
     /**
      * Converts rows to objects.
      * <p>
@@ -131,29 +125,6 @@
         return results;
     }
 
-    List<Persistent> objectsFromDataRows(List<Object[]> rows, int position) {
-        if (rows == null || rows.size() == 0) {
-            return new ArrayList<Persistent>(1);
-        }
-
-        List<Persistent> results = new ArrayList<Persistent>(rows.size());
-
-        for (Object[] row : rows) {
-            Persistent object = objectFromDataRow((DataRow) row[position]);
-
-            if (object == null) {
-                throw new CayenneRuntimeException("Can't build Object from row: " + row[position]);
-            }
-
-            results.add(object);
-        }
-
-        // now deal with snapshots
-        cache.snapshotsUpdatedForObjects(results, rows);
-
-        return results;
-    }
-
     /**
      * Processes a list of rows for a result set that has objects related to a set of
      * parent objects via some relationship defined in PrefetchProcessorNode parameter.

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java?rev=722024&r1=722023&r2=722024&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
Mon Dec  1 02:06:04 2008
@@ -400,7 +400,7 @@
                     "No result set mapping exists for expression, can't map column aliases");
         }
 
-        return resultSetMetadata.getScalarSegment(resultDescriptorPosition++);
+        return resultSetMetadata.getScalarSegment(resultDescriptorPosition++).getColumn();
     }
 
     boolean isAppendingResultColumns() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EntityRowReader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EntityRowReader.java?rev=722024&r1=722023&r2=722024&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EntityRowReader.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EntityRowReader.java
Mon Dec  1 02:06:04 2008
@@ -19,63 +19,78 @@
 package org.apache.cayenne.access.jdbc;
 
 import java.sql.ResultSet;
-import java.util.Map;
 
 import org.apache.cayenne.CayenneException;
 import org.apache.cayenne.DataRow;
+import org.apache.cayenne.access.types.ExtendedType;
 import org.apache.cayenne.query.EntityResultMetadata;
-import org.apache.cayenne.query.QueryMetadata;
+import org.apache.cayenne.reflect.ClassDescriptor;
 import org.apache.cayenne.util.Util;
 
 /**
  * @since 3.0
  */
-class EntityRowReader extends BaseRowReader<DataRow> {
+class EntityRowReader implements RowReader<DataRow> {
 
-    private int[] valueIndices;
+    private ExtendedType[] converters;
+    private String[] labels;
+    private int[] types;
+
+    String entityName;
     private int mapCapacity;
+    private int startIndex;
+
+    DataRowPostProcessor postProcessor;
+
+    EntityRowReader(RowDescriptor descriptor, EntityResultMetadata segmentMetadata) {
+
+        ClassDescriptor classDescriptor = segmentMetadata.getClassDescriptor();
 
-    EntityRowReader(RowDescriptor descriptor, QueryMetadata queryMetadata,
-            EntityResultMetadata entityResult) {
-        super(descriptor, queryMetadata);
+        if (classDescriptor != null) {
+            this.entityName = classDescriptor.getEntity().getName();
+        }
 
-        Map<String, String> fields = entityResult.getFields();
+        int segmentWidth = segmentMetadata.getFields().size();
 
-        this.mapCapacity = (int) Math.ceil((fields.size()) / 0.75);
-        this.valueIndices = new int[fields.size()];
+        this.startIndex = segmentMetadata.getColumnOffset();
+        this.converters = new ExtendedType[segmentWidth];
+        this.types = new int[segmentWidth];
+        this.labels = new String[segmentWidth];
 
+        ExtendedType[] converters = descriptor.getConverters();
         ColumnDescriptor[] columns = descriptor.getColumns();
-        for (int i = 0, j = 0; i < columns.length; i++) {
-            if (fields.containsKey(columns[i].getName())) {
-                valueIndices[j++] = i;
-            }
+
+        for (int i = 0; i < segmentWidth; i++) {
+            this.converters[i] = converters[startIndex + i];
+            types[i] = columns[startIndex + i].getJdbcType();
+
+            // query translator may change the order of fields compare to the entity
+            // result, so figure out DataRow labels by doing reverse lookup of
+            // RowDescriptor labels...
+            labels[i] = segmentMetadata.getColumnPath(columns[startIndex + i].getLabel());
         }
     }
 
-    @Override
+    public void setPostProcessor(DataRowPostProcessor postProcessor) {
+        this.postProcessor = postProcessor;
+    }
+
     public DataRow readRow(ResultSet resultSet) throws CayenneException {
 
         try {
             DataRow row = new DataRow(mapCapacity);
-            row.setEntityName(entityName);
-            int len = valueIndices.length;
+            int len = converters.length;
 
             for (int i = 0; i < len; i++) {
 
-                // dereference column index
-                int index = valueIndices[i];
-
                 // note: jdbc column indexes start from 1, not 0 as in arrays
-                Object val = converters[index].materializeObject(
-                        resultSet,
-                        index + 1,
-                        types[index]);
-                row.put(labels[index], val);
+                Object val = converters[i].materializeObject(resultSet, startIndex
+                        + i
+                        + 1, types[i]);
+                row.put(labels[i], val);
             }
 
-            if (postProcessor != null) {
-                postProcessor.postprocessRow(resultSet, row);
-            }
+            postprocessRow(resultSet, row);
 
             return row;
         }
@@ -88,4 +103,12 @@
                     .unwindException(otherex));
         }
     }
+
+    void postprocessRow(ResultSet resultSet, DataRow dataRow) throws Exception {
+        if (postProcessor != null) {
+            postProcessor.postprocessRow(resultSet, dataRow);
+        }
+
+        dataRow.setEntityName(entityName);
+    }
 }

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/InheritanceAwareEntityRowReader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/InheritanceAwareEntityRowReader.java?rev=722024&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/InheritanceAwareEntityRowReader.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/InheritanceAwareEntityRowReader.java
Mon Dec  1 02:06:04 2008
@@ -0,0 +1,53 @@
+/*****************************************************************
+ *   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.access.jdbc;
+
+import java.sql.ResultSet;
+
+import org.apache.cayenne.DataRow;
+import org.apache.cayenne.map.EntityInheritanceTree;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.query.EntityResultMetadata;
+
+/**
+ * @since 3.0
+ */
+class InheritanceAwareEntityRowReader extends EntityRowReader {
+
+    private EntityInheritanceTree entityInheritanceTree;
+
+    public InheritanceAwareEntityRowReader(RowDescriptor descriptor,
+            EntityResultMetadata segmentMetadata) {
+        super(descriptor, segmentMetadata);
+
+        entityInheritanceTree = segmentMetadata
+                .getClassDescriptor()
+                .getEntityInheritanceTree();
+    }
+
+    @Override
+    void postprocessRow(ResultSet resultSet, DataRow dataRow) throws Exception {
+        if (postProcessor != null) {
+            postProcessor.postprocessRow(resultSet, dataRow);
+        }
+
+        ObjEntity entity = entityInheritanceTree.entityMatchingRow(dataRow);
+        dataRow.setEntityName(entity != null ? entity.getName() : entityName);
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/JDBCResultIterator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/JDBCResultIterator.java?rev=722024&r1=722023&r2=722024&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/JDBCResultIterator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/JDBCResultIterator.java
Mon Dec  1 02:06:04 2008
@@ -32,6 +32,7 @@
 import org.apache.cayenne.DataRow;
 import org.apache.cayenne.access.ResultIterator;
 import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.query.EntityResultMetadata;
 import org.apache.cayenne.query.QueryMetadata;
 import org.apache.cayenne.query.SQLResultSetMetadata;
 
@@ -103,21 +104,19 @@
             else if (resultWidth == 1) {
 
                 if (entitySegments.length > 0) {
-                    return new EntityRowReader(descriptor, queryMetadata, rsMapping
-                            .getEntitySegment(entitySegments[0]));
+                    return createEntityRowReader(descriptor, rsMapping
+                            .getEntitySegment(0));
                 }
                 else {
-                    return new ScalarRowReader(descriptor, rsMapping
-                            .getScalarSegment(scalarSegments[0]));
+                    return new ScalarRowReader(descriptor, rsMapping.getScalarSegment(0));
                 }
             }
             else {
                 CompoundRowReader reader = new CompoundRowReader(resultWidth);
                 for (int i : entitySegments) {
-                    reader.addRowReader(i, new EntityRowReader(
-                            descriptor,
-                            queryMetadata,
-                            rsMapping.getEntitySegment(i)));
+
+                    reader.addRowReader(i, createEntityRowReader(descriptor, rsMapping
+                            .getEntitySegment(i)));
                 }
 
                 for (int i : scalarSegments) {
@@ -133,6 +132,19 @@
         }
     }
 
+    private RowReader<?> createEntityRowReader(
+            RowDescriptor descriptor,
+            EntityResultMetadata resultMetadata) {
+
+        if (resultMetadata.getClassDescriptor() != null
+                && resultMetadata.getClassDescriptor().getEntityInheritanceTree()
!= null) {
+            return new InheritanceAwareEntityRowReader(descriptor, resultMetadata);
+        }
+        else {
+            return new EntityRowReader(descriptor, resultMetadata);
+        }
+    }
+
     private RowReader<?> createFullRowReader(
             RowDescriptor descriptor,
             QueryMetadata queryMetadata) {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/ScalarRowReader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/ScalarRowReader.java?rev=722024&r1=722023&r2=722024&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/ScalarRowReader.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/ScalarRowReader.java
Mon Dec  1 02:06:04 2008
@@ -22,6 +22,7 @@
 
 import org.apache.cayenne.CayenneException;
 import org.apache.cayenne.access.types.ExtendedType;
+import org.apache.cayenne.query.ScalarResultMetadata;
 import org.apache.cayenne.util.Util;
 
 /**
@@ -33,18 +34,12 @@
     private int index;
     private int type;
 
-    ScalarRowReader(RowDescriptor descriptor, String columnName) {
-        ColumnDescriptor[] columns = descriptor.getColumns();
-        for (int i = 0; i < columns.length; i++) {
-            if (columnName.equals(columns[i].getLabel())) {
-
-                this.converter = descriptor.getConverters()[i];
-                this.type = columns[i].getJdbcType();
-                this.index = i + 1;
+    ScalarRowReader(RowDescriptor descriptor, ScalarResultMetadata segmentMetadata) {
 
-                break;
-            }
-        }
+        int scalarIndex = segmentMetadata.getColumnOffset();
+        this.converter = descriptor.getConverters()[scalarIndex];
+        this.type = descriptor.getColumns()[scalarIndex].getJdbcType();
+        this.index = scalarIndex + 1;
     }
 
     public Object readRow(ResultSet resultSet) throws CayenneException {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/EntityResult.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/EntityResult.java?rev=722024&r1=722023&r2=722024&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/EntityResult.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/EntityResult.java
Mon Dec  1 02:06:04 2008
@@ -28,7 +28,9 @@
 
 /**
  * A metadata object that provides mapping of a set of result columns to an ObjEntity.
- * Used by {@link SQLResult}.
+ * Used by {@link SQLResult}. Note that fields in the EntityResult are not required to
+ * follow the order of columns in the actual query, and can be added in the arbitrary
+ * order.
  * 
  * @since 3.0
  */

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultEntityResultMetadata.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultEntityResultMetadata.java?rev=722024&r1=722023&r2=722024&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultEntityResultMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultEntityResultMetadata.java
Mon Dec  1 02:06:04 2008
@@ -29,11 +29,13 @@
 
     private ClassDescriptor classDescriptor;
     private Map<String, String> fields;
+    private int offset;
 
     DefaultEntityResultMetadata(ClassDescriptor classDescriptor,
-            Map<String, String> fields) {
+            Map<String, String> fields, int offset) {
         this.classDescriptor = classDescriptor;
         this.fields = fields;
+        this.offset = offset;
     }
 
     public ClassDescriptor getClassDescriptor() {
@@ -43,4 +45,19 @@
     public Map<String, String> getFields() {
         return fields;
     }
+
+    public int getColumnOffset() {
+        return offset;
+    }
+
+    public String getColumnPath(String resultSetLabel) {
+
+        for (Map.Entry<String, String> entry : fields.entrySet()) {
+            if (resultSetLabel.equals(entry.getValue())) {
+                return entry.getKey();
+            }
+        }
+
+        return null;
+    }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultResultSetMetadata.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultResultSetMetadata.java?rev=722024&r1=722023&r2=722024&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultResultSetMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultResultSetMetadata.java
Mon Dec  1 02:06:04 2008
@@ -47,11 +47,14 @@
         int[] entitySegments = new int[descriptors.size()];
 
         int ss = 0, es = 0;
+        int offset = 0;
         for (int i = 0; i < descriptors.size(); i++) {
 
             Object descriptor = descriptors.get(i);
             if (descriptor instanceof String) {
-                segments.add(descriptor);
+                segments
+                        .add(new DefaultScalarResultMetadata((String) descriptor, offset));
+                offset = offset + 1;
                 scalarSegments[ss++] = i;
             }
             else if (descriptor instanceof EntityResult) {
@@ -66,7 +69,11 @@
                 }
 
                 ClassDescriptor classDescriptor = resolver.getClassDescriptor(entityName);
-                segments.add(new DefaultEntityResultMetadata(classDescriptor, fields));
+                segments.add(new DefaultEntityResultMetadata(
+                        classDescriptor,
+                        fields,
+                        offset));
+                offset = offset + fields.size();
                 entitySegments[es++] = i;
             }
             else {
@@ -100,11 +107,11 @@
         return entitySegments;
     }
 
-    public String getScalarSegment(int position) {
+    public ScalarResultMetadata getScalarSegment(int position) {
 
         Object result = segments.get(position);
-        if (result instanceof String) {
-            return (String) result;
+        if (result instanceof ScalarResultMetadata) {
+            return (ScalarResultMetadata) result;
         }
 
         throw new IllegalArgumentException("Segment at position "

Copied: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultScalarResultMetadata.java
(from r722021, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResultMetadata.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultScalarResultMetadata.java?p2=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultScalarResultMetadata.java&p1=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResultMetadata.java&r1=722021&r2=722024&rev=722024&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResultMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/DefaultScalarResultMetadata.java
Mon Dec  1 02:06:04 2008
@@ -18,19 +18,25 @@
  ****************************************************************/
 package org.apache.cayenne.query;
 
-import java.util.Map;
-
-import org.apache.cayenne.map.EntityResult;
-import org.apache.cayenne.reflect.ClassDescriptor;
-
 /**
- * A "compiled" version of a {@link EntityResult} descriptor.
- * 
  * @since 3.0
  */
-public interface EntityResultMetadata {
+class DefaultScalarResultMetadata implements ScalarResultMetadata {
+
+    private String column;
+    private int offset;
+
+    DefaultScalarResultMetadata(String column, int offset) {
+        this.column = column;
+        this.offset = offset;
+    }
+
+    public String getColumn() {
+        return column;
+    }
 
-    ClassDescriptor getClassDescriptor();
+    public int getColumnOffset() {
+        return offset;
+    }
 
-    Map<String, String> getFields();
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResultMetadata.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResultMetadata.java?rev=722024&r1=722023&r2=722024&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResultMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResultMetadata.java
Mon Dec  1 02:06:04 2008
@@ -32,5 +32,21 @@
 
     ClassDescriptor getClassDescriptor();
 
+    /**
+     * Returns a map of ResultSet labels keyed by column paths. Note that ordering of
+     * fields in the returned map is generally undefined and should not be relied upon
+     * when processing query result sets.
+     */
     Map<String, String> getFields();
+
+    /**
+     * Performs a reverse lookup of the column path for a given ResultSet label.
+     */
+    String getColumnPath(String resultSetLabel);
+
+    /**
+     * Returns a zero-based column index of the first column of this segment in the
+     * ResultSet.
+     */
+    int getColumnOffset();
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SQLResultSetMetadata.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SQLResultSetMetadata.java?rev=722024&r1=722023&r2=722024&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SQLResultSetMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SQLResultSetMetadata.java
Mon Dec  1 02:06:04 2008
@@ -38,5 +38,5 @@
 
     EntityResultMetadata getEntitySegment(int position);
 
-    String getScalarSegment(int position);
+    ScalarResultMetadata getScalarSegment(int position);
 }

Copied: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/ScalarResultMetadata.java
(from r722021, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResultMetadata.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/ScalarResultMetadata.java?p2=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/ScalarResultMetadata.java&p1=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResultMetadata.java&r1=722021&r2=722024&rev=722024&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResultMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/ScalarResultMetadata.java
Mon Dec  1 02:06:04 2008
@@ -18,19 +18,16 @@
  ****************************************************************/
 package org.apache.cayenne.query;
 
-import java.util.Map;
-
-import org.apache.cayenne.map.EntityResult;
-import org.apache.cayenne.reflect.ClassDescriptor;
-
 /**
- * A "compiled" version of a {@link EntityResult} descriptor.
- * 
  * @since 3.0
  */
-public interface EntityResultMetadata {
+public interface ScalarResultMetadata {
 
-    ClassDescriptor getClassDescriptor();
+    String getColumn();
 
-    Map<String, String> getFields();
+    /**
+     * Returns a zero-based column index of the first column of this segment in the
+     * ResultSet.
+     */
+    int getColumnOffset();
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorResultMetadata.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorResultMetadata.java?rev=722024&r1=722023&r2=722024&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorResultMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorResultMetadata.java
Mon Dec  1 02:06:04 2008
@@ -18,7 +18,7 @@
  ****************************************************************/
 package org.apache.cayenne.reflect;
 
-import java.util.LinkedHashMap;
+import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.cayenne.map.ObjAttribute;
@@ -32,12 +32,12 @@
 
     ClassDescriptor classDescriptor;
     Map<String, String> fields;
+    Map<String, String> reverseFields;
 
     PersistentDescriptorResultMetadata(ClassDescriptor classDescriptor) {
         this.classDescriptor = classDescriptor;
-
-        // preserve field addition order by using linked map.
-        this.fields = new LinkedHashMap<String, String>();
+        this.fields = new HashMap<String, String>();
+        this.reverseFields = new HashMap<String, String>();
     }
 
     public ClassDescriptor getClassDescriptor() {
@@ -48,11 +48,16 @@
         return fields;
     }
 
+    public String getColumnPath(String resultSetLabel) {
+        return reverseFields.get(resultSetLabel);
+    }
+
     void addObjectField(String attributeName, String column) {
         ObjEntity entity = classDescriptor.getEntity();
 
         ObjAttribute attribute = (ObjAttribute) entity.getAttribute(attributeName);
         fields.put(attribute.getDbAttributePath(), column);
+        reverseFields.put(column, attribute.getDbAttributePath());
     }
 
     /**
@@ -65,6 +70,7 @@
 
         ObjAttribute attribute = (ObjAttribute) entity.getAttribute(attributeName);
         fields.put(attribute.getDbAttributePath(), column);
+        reverseFields.put(column, attribute.getDbAttributePath());
     }
 
     /**
@@ -72,5 +78,11 @@
      */
     void addDbField(String dbAttributeName, String column) {
         fields.put(dbAttributeName, column);
+        reverseFields.put(column, dbAttributeName);
+    }
+
+    public int getColumnOffset() {
+        throw new UnsupportedOperationException(
+                "Column offset only makes sense in the context of a query");
     }
 }



Mime
View raw message