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.
This commit is contained in:
Andrzej Kapolka
2010-08-18 22:40:03 +00:00
parent 061219bc57
commit af0c636517
2 changed files with 22 additions and 6 deletions
@@ -132,10 +132,6 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
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<Void>
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<Void>
_builder.append(fieldComputed.fieldDefinition());
if (_enableAliasing) {
_builder.append(" as ");
appendIdentifier(field.name);
appendIdentifier(fm.getColumnName());
}
return;
}
@@ -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<T>
{
_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<? extends PersistentRecord> 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;
}