Пример #1
0
        private static Expression GetUpdateExpressionByParameter(TranslateContext transContext, ParameterExpression parExp, LambdaExpression predicate, bool isAsync)
        {
            var metadata = EntityMetadataUnity.GetEntityMetadata(parExp.Type);
            var table    = new TableExpression(new TableAlias(), metadata.TableName, parExp.Type);

            return(new UpdateCommandExpression(table, predicate.Body, GetUpdateArguments(transContext, table, parExp), isAsync));
        }
Пример #2
0
        internal static Expression GetUpdateExpression(TranslateContext transContext, Expression instance, LambdaExpression predicate, bool isAsync)
        {
            if (instance is ParameterExpression)
            {
                return(GetUpdateExpressionByParameter(transContext, (ParameterExpression)instance, predicate, isAsync));
            }
            else if (instance is ConstantExpression)
            {
                return(GetUpdateExpressionByEntity(transContext, (ConstantExpression)instance, predicate, isAsync));
            }

            var lambda = instance as LambdaExpression;

            if (lambda == null && instance.NodeType == ExpressionType.Quote)
            {
                lambda = ((UnaryExpression)instance).Operand as LambdaExpression;
            }

            if (lambda != null)
            {
                return(GetUpdateExpressionByCalculator(transContext, lambda, predicate, isAsync));
            }

            return(null);
        }
Пример #3
0
        internal static Expression GetInsertExpression(TranslateContext transContext, Expression instance, bool isAsync)
        {
            var entityType = instance.Type;
            Func <TableExpression, Expression, IEnumerable <ColumnAssignment> > func;

            if (instance is ParameterExpression)
            {
                func = new Func <TableExpression, Expression, IEnumerable <ColumnAssignment> >((t, exp) => GetInsertArguments(transContext, t, exp.As <ParameterExpression>()));
            }
            else if (instance is ConstantExpression)
            {
                func = new Func <TableExpression, Expression, IEnumerable <ColumnAssignment> >((t, exp) => GetInsertArguments(transContext, t, exp.As <ConstantExpression>().Value as IEntity));
            }
            else
            {
                throw new ArgumentException("instance");
            }

            var metadata = EntityMetadataUnity.GetEntityMetadata(entityType);
            var table    = new TableExpression(new TableAlias(), metadata.TableName, entityType);

            return(new InsertCommandExpression(table, func(table, instance), isAsync)
            {
                WithAutoIncrement = !string.IsNullOrEmpty(transContext.SyntaxProvider.IdentitySelect) && HasAutoIncrement(instance.Type),
                WithGenerateValue = HasGenerateValue(instance.Type)
            });
        }
Пример #4
0
 private static IEnumerable <ColumnAssignment> GetUpdateArguments(TranslateContext transContext, TableExpression table, IEnumerable <MemberAssignment> bindings)
 {
     return(from m in bindings
            let property = PropertyUnity.GetProperty(table.Type, m.Member.Name)
                           where property != null
                           select new ColumnAssignment(
                (ColumnExpression)GetMemberExpression(transContext, table, property),
                m.Expression));
 }
Пример #5
0
        private static IEnumerable <ColumnAssignment> GetUpdateArguments(TranslateContext transContext, TableExpression table, IEntity entity)
        {
            var properties = GetModifiedProperties(entity).ToArray();

            return(properties
                   .Select(p => new ColumnAssignment(
                               (ColumnExpression)GetMemberExpression(transContext, table, p),
                               Expression.Constant(GetConvertableValue(entity, p))
                               )).ToList());
        }
Пример #6
0
        internal static Expression GetMemberExpression(TranslateContext transContext, Expression root, IProperty property)
        {
            if (property is RelationProperty relationProprety)
            {
                //所关联的实体类型
                var relMetadata = EntityMetadataUnity.GetEntityMetadata(relationProprety.RelationalType);
                var projection  = GetTableQuery(transContext, relMetadata, false, false);

                Expression parentExp = null, childExp = null;
                var        ship = RelationshipUnity.GetRelationship(relationProprety);

                if (ship.ThisType != relationProprety.EntityType)
                {
                    parentExp = projection.Projector;
                    childExp  = root;
                }
                else
                {
                    parentExp = root;
                    childExp  = projection.Projector;
                }

                var where = ship.Keys.Select(r =>
                                             GetMemberExpression(transContext, parentExp, r.ThisProperty).Equal(GetMemberExpression(transContext, childExp, r.OtherProperty)))
                            .Aggregate(Expression.And);

                var newAlias = new TableAlias();
                var pc       = ColumnProjector.ProjectColumns(CanBeColumnExpression, projection.Projector, null, newAlias, projection.Select.Alias);

                var aggregator = GetAggregator(property.Type, typeof(IEnumerable <>).MakeGenericType(pc.Projector.Type));
                var result     = new ProjectionExpression(
                    new SelectExpression(newAlias, pc.Columns, projection.Select, where),
                    pc.Projector, aggregator, projection.IsAsync, projection.IsNoTracking
                    );

                return(transContext.QueryPolicy.ApplyPolicy(result, property.Info.ReflectionInfo, ex => QueryBinder.Bind(transContext, ex)));
            }

            if (root is TableExpression table)
            {
                if (property is SubqueryProperty sqProperty)
                {
                    return(new SubqueryColumnExpression(property.Type, table.Alias, property.Info.FieldName, sqProperty.Subquery));
                }
                else if (property is ISavedProperty)
                {
                    return(new ColumnExpression(property.Type, table.Alias, property.Name, property.Info));
                }
            }

            return(QueryBinder.BindMember(root, property.Info.ReflectionInfo));
        }
Пример #7
0
        internal static Expression GetTypeProjection(TranslateContext transContext, Expression root, EntityMetadata entity)
        {
            var entityType = entity.EntityType;

            //获取实体中定义的所有依赖属性
            var properties = PropertyUnity.GetLoadedProperties(entityType);

            var bindings = from p in properties
                           let mbrExp = GetMemberExpression(transContext, root, p)
                                        where mbrExp != null
                                        select Expression.Bind(p.Info.ReflectionInfo, mbrExp);

            return(new EntityExpression(entity, Expression.MemberInit(Expression.New(entityType), bindings)));
        }
Пример #8
0
        private static IEnumerable <ColumnAssignment> GetUpdateArguments(TranslateContext transContext, TableExpression table, ParameterExpression parExp)
        {
            IEnumerable <IProperty> properties = null;

            if (transContext.TemporaryBag != null)
            {
                properties = GetModifiedProperties(table.Type, transContext.TemporaryBag);
            }

            return(from p in properties
                   select new ColumnAssignment(
                       (ColumnExpression)GetMemberExpression(transContext, table, p),
                       Expression.MakeMemberAccess(parExp, p.Info.ReflectionInfo)));
        }
Пример #9
0
        /// <summary>
        /// 返回插入具体实体时的 <see cref="ColumnAssignment"/> 集合。
        /// </summary>
        /// <param name="syntax"></param>
        /// <param name="table"></param>
        /// <param name="entity">具体的实体。</param>
        /// <returns></returns>
        private static IEnumerable <ColumnAssignment> GetInsertArguments(TranslateContext transContext, TableExpression table, IEntity entity)
        {
            var properties = GetModifiedProperties(entity);

            var assignments = properties
                              .Select(p => new ColumnAssignment(
                                          (ColumnExpression)GetMemberExpression(transContext, table, p),
                                          Expression.Constant(GetConvertableValue(entity, p))
                                          )).ToList();

            assignments.AddRange(GetAssignmentsForPrimaryKeys(transContext, table, null, entity));

            return(assignments);
        }
Пример #10
0
        private static Expression GetUpdateExpressionByEntity(TranslateContext transContext, ConstantExpression constant, LambdaExpression predicate, bool isAsync)
        {
            var entity   = constant.Value as IEntity;
            var metadata = EntityMetadataUnity.GetEntityMetadata(entity.EntityType);
            var table    = new TableExpression(new TableAlias(), metadata.TableName, metadata.EntityType);

            Expression where = null;

            if (predicate != null)
            {
                var row = GetTypeProjection(transContext, table, metadata);
                where = DbExpressionReplacer.Replace(predicate.Body, predicate.Parameters[0], row);
            }

            return(new UpdateCommandExpression(table, where, GetUpdateArguments(transContext, table, entity), isAsync));
        }
Пример #11
0
        internal static ProjectionExpression GetTableQuery(TranslateContext transContext, EntityMetadata entity, bool isNoTracking, bool isAsync)
        {
            var tableAlias  = new TableAlias();
            var selectAlias = new TableAlias();
            var entityType  = entity.EntityType;
            var table       = new TableExpression(tableAlias, entity.TableName, entityType);

            var projector = GetTypeProjection(transContext, table, entity);
            var pc        = ColumnProjector.ProjectColumns(CanBeColumnExpression, projector, null, selectAlias, tableAlias);

            var proj = new ProjectionExpression(
                new SelectExpression(selectAlias, pc.Columns, table, null),
                pc.Projector, isAsync, isNoTracking
                );

            return((ProjectionExpression)transContext.QueryPolicy.ApplyPolicy(proj, entityType, ex => QueryBinder.Bind(transContext, ex)));
        }
Пример #12
0
        internal static LambdaExpression GetPrimaryKeyExpression(TranslateContext transContext, ParameterExpression parExp)
        {
            var metadata    = EntityMetadataUnity.GetEntityMetadata(parExp.Type);
            var table       = new TableExpression(new TableAlias(), metadata.TableName, parExp.Type);
            var primaryKeys = PropertyUnity.GetPrimaryProperties(parExp.Type);

            if (!primaryKeys.Any())
            {
                throw new NotSupportedException(SR.GetString(SRKind.NotDefinedPrimaryKey));
            }

            var where = primaryKeys.Select(p =>
                                           GetMemberExpression(transContext, table, p).Equal(Expression.MakeMemberAccess(parExp, p.Info.ReflectionInfo)))
                        .Aggregate(Expression.Add);

            return(Expression.Lambda(where, parExp));
        }
Пример #13
0
        private static IEnumerable <ColumnAssignment> GetAssignmentsForPrimaryKeys(TranslateContext transContext, TableExpression table, ParameterExpression parExp, IEntity entity)
        {
            var entityType  = parExp != null ? parExp.Type : entity.EntityType;
            var assignments = new List <ColumnAssignment>();

            foreach (var p in PropertyUnity.GetPrimaryProperties(entityType))
            {
                var pvExp = GetPrimaryValueExpression(transContext.SyntaxProvider, table, parExp, entity, p);

                if (pvExp != null)
                {
                    var columnExp = (ColumnExpression)GetMemberExpression(transContext, table, p);
                    assignments.Add(new ColumnAssignment(columnExp, pvExp));
                }
            }

            return(assignments);
        }
Пример #14
0
        /// <summary>
        /// 返回插入具体实体时的 <see cref="ColumnAssignment"/> 集合。
        /// </summary>
        /// <param name="syntax"></param>
        /// <param name="table"></param>
        /// <param name="parExp"></param>
        /// <returns></returns>
        private static IEnumerable <ColumnAssignment> GetInsertArguments(TranslateContext transContext, TableExpression table, ParameterExpression parExp)
        {
            IEnumerable <IProperty> properties = null;

            if (transContext.TemporaryBag != null)
            {
                properties = GetModifiedProperties(table.Type, transContext.TemporaryBag);
            }

            var assignments = properties
                              .Select(p => new ColumnAssignment(
                                          (ColumnExpression)GetMemberExpression(transContext, table, p),
                                          Expression.MakeMemberAccess(parExp, p.Info.ReflectionInfo)
                                          )).ToList();

            assignments.AddRange(GetAssignmentsForPrimaryKeys(transContext, table, parExp, null));

            return(assignments);
        }
Пример #15
0
        internal static Expression GetLogicalDeleteExpression(TranslateContext transContext, EntityMetadata metadata, LambdaExpression predicate, bool isAsync)
        {
            var table = new TableExpression(new TableAlias(), metadata.TableName, metadata.EntityType);

            Expression where = null;

            var delflag     = (ColumnExpression)GetMemberExpression(transContext, table, metadata.DeleteProperty);
            var assignments = new List <ColumnAssignment>
            {
                new ColumnAssignment(delflag, Expression.Constant(true))
            };

            if (predicate != null)
            {
                var row = GetTypeProjection(transContext, table, metadata);
                where = DbExpressionReplacer.Replace(predicate.Body, predicate.Parameters[0], row);
            }

            return(new UpdateCommandExpression(table, where, assignments, isAsync));
        }
Пример #16
0
        internal static Expression GetDeleteExpression(TranslateContext transContext, EntityMetadata metadata, LambdaExpression predicate, bool replace, bool isAsync)
        {
            var table = new TableExpression(new TableAlias(), metadata.TableName, metadata.EntityType);

            Expression where = null;

            if (predicate != null)
            {
                if (replace)
                {
                    var row = GetTypeProjection(transContext, table, metadata);
                    where = DbExpressionReplacer.Replace(predicate.Body, predicate.Parameters[0], row);
                }
                else
                {
                    where = predicate.Body;
                }
            }

            return(new DeleteCommandExpression(table, where, isAsync));
        }
Пример #17
0
        private static Expression GetUpdateExpressionByCalculator(TranslateContext transContext, LambdaExpression lambda, LambdaExpression predicate, bool isAsync)
        {
            var initExp    = lambda.Body as MemberInitExpression;
            var newExp     = initExp.NewExpression;
            var entityType = newExp.Type;
            var metadata   = EntityMetadataUnity.GetEntityMetadata(entityType);
            var table      = new TableExpression(new TableAlias(), metadata.TableName, metadata.EntityType);

            Expression where = null;

            var row = GetTypeProjection(transContext, table, metadata);

            if (predicate != null)
            {
                where = DbExpressionReplacer.Replace(predicate.Body, predicate.Parameters[0], row);
            }

            var bindings = initExp.Bindings.Cast <MemberAssignment>().Select(m =>
                                                                             Expression.Bind(m.Member, DbExpressionReplacer.Replace(m.Expression, lambda.Parameters[0], row)));

            return(new UpdateCommandExpression(table, where, GetUpdateArguments(transContext, table, bindings), isAsync));
        }
 public override TranslatorBase CreateTranslator(TranslateContext transContext)
 {
     return(new PostgreSqlTranslator(transContext));
 }
        private Expression TranslateInternal(TranslateContext transContext, Expression expression)
        {
            var translation = QueryBinder.Bind(transContext, expression);

            translation = LogicalDeleteFlagRewriter.Rewrite(translation);
            translation = GlobalQueryPolicyRewriter.Rewrite(translation);
            translation = AggregateRewriter.Rewrite(translation);
            translation = UnusedColumnRemover.Remove(translation);
            translation = RedundantColumnRemover.Remove(translation);
            translation = RedundantSubqueryRemover.Remove(translation);
            translation = RedundantJoinRemover.Remove(translation);

            var bound = RelationshipBinder.Bind(transContext, translation);

            if (bound != translation)
            {
                translation = bound;
                translation = RedundantColumnRemover.Remove(translation);
                translation = RedundantJoinRemover.Remove(translation);
            }

            translation = ComparisonRewriter.Rewrite(translation);

            Expression rewritten;

            if (transContext.QueryPolicy != null)
            {
                rewritten = RelationshipIncluder.Include(transContext, translation);
                if (rewritten != translation)
                {
                    translation = rewritten;
                    translation = UnusedColumnRemover.Remove(translation);
                    translation = RedundantColumnRemover.Remove(translation);
                    translation = RedundantSubqueryRemover.Remove(translation);
                    translation = RedundantJoinRemover.Remove(translation);
                }
            }

            rewritten = SingletonProjectionRewriter.Rewrite(translation);
            if (rewritten != translation)
            {
                translation = rewritten;
                translation = UnusedColumnRemover.Remove(translation);
                translation = RedundantColumnRemover.Remove(translation);
                translation = RedundantSubqueryRemover.Remove(translation);
                translation = RedundantJoinRemover.Remove(translation);
            }

            rewritten = ClientJoinedProjectionRewriter.Rewrite(translation);
            if (rewritten != translation)
            {
                translation = rewritten;
                translation = UnusedColumnRemover.Remove(translation);
                translation = RedundantColumnRemover.Remove(translation);
                translation = RedundantSubqueryRemover.Remove(translation);
                translation = RedundantJoinRemover.Remove(translation);
            }

            translation = BuildExpression(translation);

            return(translation);
        }
Пример #20
0
 public override TranslatorBase CreateTranslator(TranslateContext transContext)
 {
     return(new FirebirdTranslator(transContext));
 }
Пример #21
0
 public static Expression Bind(TranslateContext transContext, Expression expression)
 {
     return(new RelationshipBinder {
         _transContext = transContext
     }.Visit(expression));
 }
Пример #22
0
 public SQLiteTranslator(TranslateContext transContext)
     : base(transContext)
 {
 }
Пример #23
0
 public AccessTranslator(TranslateContext transContext)
     : base(transContext)
 {
 }
Пример #24
0
 public FirebirdTranslator(TranslateContext transContext)
     : base(transContext)
 {
 }
Пример #25
0
 public static Expression Include(TranslateContext transContext, Expression expression)
 {
     return(new RelationshipIncluder {
         _transContext = transContext, _policy = transContext.QueryPolicy
     }.Visit(expression));
 }
Пример #26
0
 public override TranslatorBase CreateTranslator(TranslateContext transContext)
 {
     return(new OracleTranslator(transContext));
 }
Пример #27
0
 public MySqlTranslator(TranslateContext transContext)
     : base(transContext)
 {
 }
 /// <summary>
 /// 对 ELinq 表达式进行翻译,并返回翻译的结果。
 /// </summary>
 /// <param name="transContext"></param>
 /// <param name="expression">一个 ELinq 表达式。</param>
 /// <returns></returns>
 public virtual Expression Translate(TranslateContext transContext, Expression expression)
 {
     expression = PartialEvaluator.Eval(expression, CanBeEvaluatedLocally);
     return(TranslateInternal(transContext, expression));
 }
Пример #29
0
 public OracleTranslator(TranslateContext transContext)
     : base(transContext)
 {
 }
 /// <summary>
 /// 获取一个 ELinq 翻译器。
 /// </summary>
 /// <param name="transContext"></param>
 /// <returns></returns>
 public abstract TranslatorBase CreateTranslator(TranslateContext transContext);