Esempio n. 1
0
        /// <summary>
        /// 根据指定类型<see cref="DbSetConnectExpression"/>表达式创建数据源语句片段。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <param name="expression">指定表达式。</param>
        /// <returns>创建结果。</returns>
        protected virtual ISourceFragment CreateSourceForSetConnect(GenerateContext context, DbExpression expression)
        {
            var content = (DbSetConnectExpression)expression;
            var source  = CreateSource(context, content.Source);
            var target  = CreateSource(context, content.Target);

            if (source is SetFragment select)
            {
                select.AddSource(target, content.Kind);
            }
            else
            {
                select = new SetFragment(context, source);
                select.AddSource(target, content.Kind);
            }
            context.RegisterSource(content, select);
            if (!select.Members.Any())
            {
                var unittype = content.Source as DbUnitTypeExpression;
                var members  = RetrievalMembers(context, source, unittype.Item, false);
                select.Members.AddRange(members.Select(a => CreateMember(context, select, a.Property, a)));
            }
            if (!target.Members.Any())
            {
                var unittype = content.Target as DbUnitTypeExpression;
                RetrievalMembers(context, target, unittype.Item, false).ToArray();
            }
            context.RegisterSource(content.Item, select);
            if (IsSelectFragment(content))
            {
                return(InitialSelectFragment(new SelectFragment(context, select), content));
            }
            return(select);
        }
Esempio n. 2
0
        /// <summary>
        /// 根据指定类型<see cref="DbDataSetExpression"/>表达式创建数据源语句片段。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <param name="expression">指定表达式。</param>
        /// <returns>创建结果。</returns>
        protected virtual ISourceFragment CreateSourceForDataSet(GenerateContext context, DbExpression expression)
        {
            var            content  = (DbDataSetExpression)expression;
            var            metadata = context.Metadata.Table(content.Item.ClrType);
            SourceFragment table    = null;

            if (metadata.InheritSets.Length > 0)
            {
                if (content.Name != null)
                {
                    throw new NotSupportedException(Res.NotSupportedInheritSetUsingDbName);
                }
                table = new InheritFragment(context, metadata);
            }
            else
            {
                table = new TableFragment(context, content);
            }
            context.RegisterSource(content.Item, table);
            context.RegisterSource(content, table);
            if (IsSelectFragment(content))
            {
                return(InitialSelectFragment(new SelectFragment(context, table), content));
            }
            if (table is InheritFragment inherit)
            {
                inherit.Initialize();
            }
            return(table);
        }
Esempio n. 3
0
 //生成前注册相关表达式。
 private void GenerateForUpdateRegister(GenerateContext context, DbExpression content, ISourceFragment source)
 {
     if (content != null && content is DbSelectExpression select)
     {
         context.RegisterSource(select.Source, source, true);
         context.RegisterSource(select.Source.Item, source, true);
     }
 }
Esempio n. 4
0
        /// <summary>
        /// 生成用于插入数据项的语句片段。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <param name="content">插入表达式。</param>
        /// <returns>生成结果。</returns>
        protected virtual SqlFragment GenerateForInsert(GenerateContext context, DbExpression content = null)
        {
            var data = (GenerateDataForInsert)context.Data;

            if (content != null && content is DbSelectExpression select)
            {
                context.RegisterSource(select.Source, data.CommitObject);
                context.RegisterSource(select.Source.Item, data.CommitObject);
            }
            try
            {
                if (data.MainUnit is CommitKeyUnit keyunit)
                {     //没有标识列
                    if (data.Items.Count > 1)
                    { //多个
                        if (data.HasExpressionKey || data.ReturnMembers.Any())
                        {
                            return(GenerateForInsertTempTable(context));
                        }
                        else
                        {
                            return(GenerateForInsertSimple(context));
                        }
                    }
                    else
                    {//单个
                        if (data.HasExpressionKey)
                        {
                            return(GenerateForInsertTempTable(context));
                        }
                        else
                        {
                            return(GenerateForInsertSimple(context));
                        }
                    }
                }
                else
                {     //标识列
                    if (data.Items.Count > 1 || !context.Feature.HasCapable(EDbCapable.ExternalLocalVariable))
                    { //多个
                        return(GenerateForInsertIdentityTempTable(context));
                    }
                    else
                    {//单个
                        return(GenerateForInsertIdentitySingle(context));
                    }
                }
            }
            finally
            {
                data.GenerateOutput();
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 根据指定类型<see cref="DbGroupByExpression"/>表达式创建数据源语句片段。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <param name="expression">指定表达式。</param>
        /// <returns>创建结果。</returns>
        protected virtual ISourceFragment CreateSourceForGroupBy(GenerateContext context, DbExpression expression)
        {
            var content   = (DbGroupByExpression)expression;
            var groupitem = (DbGroupItemExpression)content.Item;
            var source    = CreateSource(context, content.Source);
            var body      = new SelectFragment(context, source);

            body.RetrievalMembers(content.Key, false);
            foreach (var member in body.Members.OfType <ReferenceMemberFragment>())
            {
                body.GroupBys.Add(member.Reference);
            }
            var container     = new SelectFragment(context, body);
            var virtualSource = new VirtualSourceFragment(context, expression, container, source, body);

            context.RegisterSource(content, virtualSource);
            context.RegisterSource(content.Item, virtualSource);
            return(container);
        }
Esempio n. 6
0
        /// <summary>
        /// 根据指定类型<see cref="DbCollectionMemberExpression"/>表达式创建数据源语句片段。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <param name="expression">指定表达式。</param>
        /// <returns>创建结果。</returns>
        protected virtual ISourceFragment CreateSourceForCollectionMember(GenerateContext context, DbExpression expression)
        {
            var content       = (DbCollectionMemberExpression)expression;
            var source        = GetSource(context, content.Expression);
            var target        = CreateSource(context, content.TargetSet);
            var container     = (SelectFragment)source.Parent;
            var virtualSource = new VirtualSourceFragment(context, expression, container, source);

            context.RegisterSource(content, virtualSource);
            return(virtualSource);
        }
Esempio n. 7
0
        /// <summary>
        /// 根据指定类型<see cref="DbObjectMemberExpression"/>表达式创建数据源语句片段。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <param name="expression">指定表达式。</param>
        /// <returns>创建结果。</returns>
        protected virtual ISourceFragment CreateSourceForObjectMember(GenerateContext context, DbExpression expression)
        {
            var content = (DbObjectMemberExpression)expression;
            var source  = GetSource(context, content.Expression);

            if (source is VirtualSourceFragment virtualSource)
            {
                return(virtualSource.GetBody());
            }
            else
            {
                var select = (SelectFragment)source.Parent;
                if (select.IsRecommandLock)
                {
                    select = select.Parent as SelectFragment;
                }
                var pairs = content.Pairs;
                if (content.CompositePairs != null)
                {
                    var relation = CreateSource(context, content.RelationSet);
                    select.AddSource(relation);
                    context.RegisterSource(content.RelationSet, relation);
                    relation.Join      = EJoinType.LeftJoin;
                    relation.Condition = pairs.Select(a => select.CreateExpression(a)).Merge();
                    pairs = content.CompositePairs;
                }
                var target = CreateSource(context, content.TargetSet);
                select.AddSource(target);
                context.RegisterSource(content.TargetSet, target);
                if (content.CompositePairs == null)
                {
                    target.Join = content.Nullable ? EJoinType.LeftJoin : EJoinType.InnerJoin;
                }
                else
                {
                    target.Join = EJoinType.InnerJoin;
                }
                target.Condition = pairs.Select(a => select.CreateExpression(a)).Merge();
                return(target);
            }
        }
Esempio n. 8
0
        /// <summary>
        /// 根据指定类型<see cref="DbGroupJoinExpression"/>表达式创建数据源语句片段。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <param name="expression">指定表达式。</param>
        /// <returns>创建结果。</returns>
        protected virtual ISourceFragment CreateSourceForGroupJoin(GenerateContext context, DbExpression expression)
        {
            var content       = (DbGroupJoinExpression)expression;
            var source        = CreateSource(context, content.Source);
            var taget         = CreateSource(context, content.Target);
            var container     = new SelectFragment(context, source);
            var virtualSource = new VirtualSourceFragment(context, expression, container, source);

            context.RegisterSource(content.Item, virtualSource);
            if (IsSelectFragment(content))
            {
                return(InitialSelectFragment(container, content));
            }
            return(container);
        }
Esempio n. 9
0
        /// <summary>
        /// 根据指定类型<see cref="DbGroupJoinExpression"/>表达式创建虚拟数据源连接语句片段。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <param name="expression">表达式。</param>
        /// <param name="source">虚拟数据源</param>
        /// <returns>查询语句片段。</returns>
        public virtual ISourceFragment CreateVirtualJoinForGroupJoin(GenerateContext context, DbCrossJoinExpression expression, VirtualSourceFragment source)
        {
            var content  = (DbGroupJoinExpression)source.Expression;
            var groupset = (DbGroupSetExpression)expression.Target;

            var target = GetSource(context, content.Target.Item);

            context.RegisterSource(expression.Target.Item, target);

            var joinBody  = target;
            var container = source.Container;

            container.AddSource(target);
            joinBody.Join      = groupset.Default == null ? EJoinType.InnerJoin : EJoinType.LeftJoin;
            joinBody.Condition = content.KeyPairs.Select(a => container.CreateExpression(a)).Merge();
            return(joinBody);
        }
Esempio n. 10
0
        /// <summary>
        /// 根据指定类型<see cref="DbCollectionMemberExpression"/>表达式创建虚拟数据源连接语句片段。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <param name="expression">表达式。</param>
        /// <param name="source">虚拟数据源</param>
        /// <returns>查询语句片段。</returns>
        public virtual ISourceFragment CreateVirtualJoinForCollectionMember(GenerateContext context, DbCrossJoinExpression expression, VirtualSourceFragment source)
        {
            var content   = (DbCollectionMemberExpression)source.Expression;
            var target    = GetSource(context, content.TargetSet.Item);
            var container = source.Container;

            if (content.Metadata.IsComposite)
            {
                var relationSet = GenerateVirtualCompositeJoinForCollectionMember(context, content, container, false);
                relationSet.Join = content.Default == null ? EJoinType.InnerJoin : EJoinType.LeftJoin;
            }
            container.AddSource(target);
            GenerateVirtualJoinForCollectionMember(context, source, target, true);
            if (content.Metadata.IsComposite)
            {
                target.Join = EJoinType.InnerJoin;
            }
            else
            {
                target.Join = content.Default == null ? EJoinType.InnerJoin : EJoinType.LeftJoin;
            }
            context.RegisterSource(content.Item, target);
            return(target);
        }