cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r620111 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query: EntityResult.java FieldResult.java
Date Sat, 09 Feb 2008 14:00:48 GMT
Author: aadamchik
Date: Sat Feb  9 06:00:47 2008
New Revision: 620111

URL: http://svn.apache.org/viewvc?rev=620111&view=rev
Log:
CAY-839 Support combination of Persistent objects and scalars in query results

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResult.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/FieldResult.java

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResult.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResult.java?rev=620111&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResult.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EntityResult.java
Sat Feb  9 06:00:47 2008
@@ -0,0 +1,127 @@
+/*****************************************************************
+ *   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.Collections;
+import java.util.List;
+
+import org.apache.cayenne.map.EntityResolver;
+import org.apache.cayenne.map.ObjAttribute;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.reflect.ClassDescriptor;
+
+/**
+ * A metadata object that provides mapping of a set of result columns to an ObjEntity.
+ * Used by {@link SQLResultSetMapping}.
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+public class EntityResult {
+
+    protected String entityName;
+    protected Class<?> entityClass;
+    protected List<FieldResult> fields;
+
+    public EntityResult(Class<?> entityClass) {
+        this.entityClass = entityClass;
+    }
+
+    public EntityResult(String entityName) {
+        this.entityName = entityName;
+    }
+    
+    public ClassDescriptor getClassDescriptor(EntityResolver resolver) {
+        return resolver.getClassDescriptor(getEntity(resolver).getName());
+    }
+
+    /**
+     * Returns entity result fields nrmalized to represent DbAttributes.
+     */
+    public FieldResult[] getDbFields(EntityResolver resolver) {
+        FieldResult[] fields = (this.fields != null)
+                ? new FieldResult[this.fields.size()]
+                : new FieldResult[0];
+        ObjEntity entity = null;
+
+        for (int i = 0; i < fields.length; i++) {
+            FieldResult field = this.fields.get(i);
+
+            if (!field.isDbAttribute()) {
+                if (entity == null) {
+                    entity = getEntity(resolver);
+                }
+
+                ObjAttribute attribute = (ObjAttribute) entity.getAttribute(field
+                        .getAttributeName());
+
+                // TODO: andrus 2/8/2008 - flattened attributes support
+                field = new FieldResult(
+                        attribute.getDbAttributeName(),
+                        field.getColumn(),
+                        true);
+            }
+
+            fields[i] = field;
+        }
+
+        return fields;
+    }
+
+    public ObjEntity getEntity(EntityResolver resolver) {
+        if (entityName != null) {
+            return resolver.getObjEntity(entityName);
+        }
+        else if (entityClass != null) {
+            return resolver.lookupObjEntity(entityClass);
+        }
+        else {
+            throw new IllegalStateException("Both entity name and class are null");
+        }
+    }
+
+    public void addObjectField(String attributeName, String column) {
+        addField(new FieldResult(attributeName, column, false));
+    }
+
+    public void addDbField(String dbAttributeName, String column) {
+        addField(new FieldResult(dbAttributeName, column, true));
+    }
+
+    void addField(FieldResult field) {
+        if (fields == null) {
+            fields = new ArrayList<FieldResult>();
+        }
+
+        fields.add(field);
+    }
+
+    public String getEntityName() {
+        return entityName;
+    }
+
+    public Class<?> getEntityClass() {
+        return entityClass;
+    }
+
+    public List<FieldResult> getFields() {
+        return fields != null ? fields : Collections.EMPTY_LIST;
+    }
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/FieldResult.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/FieldResult.java?rev=620111&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/FieldResult.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/FieldResult.java
Sat Feb  9 06:00:47 2008
@@ -0,0 +1,52 @@
+/*****************************************************************
+ *   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;
+
+/**
+ * A metadata object that maps a result set column to an ObjAttribute or DbAttribute. Used
+ * by {@link EntityResult}.
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+public class FieldResult {
+
+    protected String attributeName;
+    protected String column;
+    protected boolean dbAttribute;
+
+    public FieldResult(String attributeName, String column, boolean dbAttribute) {
+        this.attributeName = attributeName;
+        this.column = column;
+        this.dbAttribute = dbAttribute;
+    }
+
+    public String getAttributeName() {
+        return attributeName;
+    }
+
+    public String getColumn() {
+        return column;
+    }
+
+    public boolean isDbAttribute() {
+        return dbAttribute;
+    }
+
+}



Mime
View raw message