示例#1
0
    private Expression GetEntity(bool isLite, IColumn column, Type entityType, CachedTableConstructor constructor)
    {
        Expression id = constructor.GetTupleProperty(column);

        var pk = CachedTableConstructor.WrapPrimaryKey(id);

        CachedTableConstructor typeConstructor = CacheLogic.GetCacheType(entityType) == CacheType.Cached ?
                                                 CacheLogic.GetCachedTable(entityType).Constructor :
                                                 constructor.cachedTable.SubTables !.SingleEx(a => a.ParentColumn == column).Constructor;

        return(new CachedEntityExpression(pk, entityType, typeConstructor, null, null));
    }
示例#2
0
    private Expression GetField(Field field, CachedTableConstructor constructor, Expression?previousPrimaryKey)
    {
        if (field is FieldValue)
        {
            var value = constructor.GetTupleProperty((IColumn)field);
            return(value.Type == field.FieldType ? value : Expression.Convert(value, field.FieldType));
        }

        if (field is FieldEnum)
        {
            return(Expression.Convert(constructor.GetTupleProperty((IColumn)field), field.FieldType));
        }

        if (field is FieldPrimaryKey)
        {
            return(constructor.GetTupleProperty((IColumn)field));
        }

        if (field is IFieldReference)
        {
            bool isLite = ((IFieldReference)field).IsLite;

            if (field is FieldReference)
            {
                IColumn column = (IColumn)field;

                return(GetEntity(isLite, column, field.FieldType.CleanType(), constructor));
            }

            if (field is FieldImplementedBy ib)
            {
                var nullRef = Expression.Constant(null, field.FieldType);
                var call    = ib.ImplementationColumns.Aggregate((Expression)nullRef, (acum, kvp) =>
                {
                    IColumn column = (IColumn)kvp.Value;

                    var entity = GetEntity(isLite, column, kvp.Key, constructor);

                    return(Expression.Condition(Expression.NotEqual(constructor.GetTupleProperty(column), Expression.Constant(column.Type)),
                                                Expression.Convert(entity, field.FieldType),
                                                acum));
                });

                return(call);
            }

            if (field is FieldImplementedByAll)
            {
                throw new NotImplementedException("FieldImplementedByAll not supported in cached ToString");
            }
        }

        if (field is FieldEmbedded fe)
        {
            return(new CachedEntityExpression(previousPrimaryKey !, fe.FieldType, constructor, fe, null));
        }

        if (field is FieldMixin fm)
        {
            return(new CachedEntityExpression(previousPrimaryKey !, fm.FieldType, constructor, null, fm));
        }

        if (field is FieldMList)
        {
            throw new NotImplementedException("FieldMList not supported in cached ToString");
        }

        throw new InvalidOperationException("Unexpected {0}".FormatWith(field.GetType().Name));
    }