From af0c6365178c78adca05d05999af27f5393ebb1a Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 18 Aug 2010 22:40:03 +0000 Subject: [PATCH] More fixes for named columns. DepotMarshaller expects the names of the columns in the results to match the FieldMarshaller column names, so we have to use those rather than the field names. Also, shadowed fields need to use the column names of the fields that they're shadowing. --- .../samskivert/depot/impl/BuildVisitor.java | 10 +++++----- .../samskivert/depot/impl/FieldMarshaller.java | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/java/com/samskivert/depot/impl/BuildVisitor.java b/src/java/com/samskivert/depot/impl/BuildVisitor.java index ed51fad..56879a5 100644 --- a/src/java/com/samskivert/depot/impl/BuildVisitor.java +++ b/src/java/com/samskivert/depot/impl/BuildVisitor.java @@ -132,10 +132,6 @@ public abstract class BuildVisitor implements FragmentVisitor public Void visit (FieldDefinition definition) { definition.getDefinition().accept(this); - if (_enableAliasing) { - _builder.append(" as "); - appendIdentifier(definition.getField()); - } return null; } @@ -816,6 +812,10 @@ public abstract class BuildVisitor implements FragmentVisitor boolean saved = _enableOverrides; _enableOverrides = false; fieldDef.accept(this); + if (_enableAliasing) { + _builder.append(" as "); + appendIdentifier(fm.getColumnName()); + } _enableOverrides = saved; return; } @@ -844,7 +844,7 @@ public abstract class BuildVisitor implements FragmentVisitor _builder.append(fieldComputed.fieldDefinition()); if (_enableAliasing) { _builder.append(" as "); - appendIdentifier(field.name); + appendIdentifier(fm.getColumnName()); } return; } diff --git a/src/java/com/samskivert/depot/impl/FieldMarshaller.java b/src/java/com/samskivert/depot/impl/FieldMarshaller.java index c74d40a..ce0be21 100644 --- a/src/java/com/samskivert/depot/impl/FieldMarshaller.java +++ b/src/java/com/samskivert/depot/impl/FieldMarshaller.java @@ -34,6 +34,7 @@ import java.sql.Time; import java.sql.Timestamp; import com.samskivert.depot.DatabaseException; +import com.samskivert.depot.PersistentRecord; import com.samskivert.depot.Transformer; import com.samskivert.depot.annotation.Column; import com.samskivert.depot.annotation.Computed; @@ -216,15 +217,30 @@ public abstract class FieldMarshaller { _field = field; _columnName = field.getName(); + _computed = field.getAnnotation(Computed.class); Column column = _field.getAnnotation(Column.class); + if (column == null) { + // look for a column annotation on the shadowed field, if any + Computed dcomputed = (_computed == null) ? + field.getDeclaringClass().getAnnotation(Computed.class) : _computed; + if (dcomputed != null) { + Class sclass = dcomputed.shadowOf(); + if (sclass != null) { + try { + column = sclass.getField(field.getName()).getAnnotation(Column.class); + } catch (NoSuchFieldException e) { + // no problem; assume that it will be defined in the query + } + } + } + } if (column != null) { if (!StringUtil.isBlank(column.name())) { _columnName = column.name(); } } - _computed = field.getAnnotation(Computed.class); if (_computed != null) { return; }