Exemple #1
0
        public SqlFromQuery ToOutQuery(params SqlSelectItem[] selectItem)
        {
            if (selectItem == null || selectItem.Length <= 0)
            {
                throw new ArgumentException("must select some one");
            }

            foreach (var item in selectItem)
            {
                if (item.Target.Expression.Type == ExpressionType.SelectItemExpression)
                {
                    SqlSelectItemExpression si = (SqlSelectItemExpression)item.Target.Expression;
                    if (si.Owner == this)
                    {
                        SqlSelectItemExpression tar = new SqlSelectItemExpression(si.Expression, item.Target.AliasName);
                        tar.Owner = this;
                        Selects.Add(item.Target.AliasName, tar);
                    }
                    else
                    {
                        Selects.Add(item.Target.AliasName, item.Target);
                    }
                }
                else
                {
                    Selects.Add(item.Target.AliasName, item.Target);
                }
            }

            return(new SqlFromQuery(this));
        }
Exemple #2
0
        private void BuildSelectItem(SqlSelectItemExpression item, BuildQueryContext ctx)
        {
            //判断item.Expression是否是子Select项,是则表示外部查询(FromQuery)引用的Select项
            if (item.Expression.Type == ExpressionType.SelectItemExpression)
            {
                SqlSelectItemExpression si = (SqlSelectItemExpression)item.Expression;
                //判断当前查询是否等于Select项的所有者,否则表示Select项的所有者的外部查询引用该Select项
                if (ReferenceEquals(ctx.CurrentQuery, item.Owner))
                {
                    ctx.AppendFormat("{0}.\"{1}\"", ctx.GetQueryAliasName(si.Owner), si.AliasName);
                }
                else
                {
                    ctx.AppendFormat("{0}.\"{1}\"", ctx.GetQueryAliasName(item.Owner), si.AliasName);
                }

                //处理选择项别名
                if (ctx.CurrentQueryInfo.BuildStep == BuildQueryStep.BuildSelect)//&& !ctx.IsBuildCTESelectItem)
                {
                    if (item.AliasName != si.AliasName)
                    {
                        ctx.AppendFormat(" \"{0}\"", item.AliasName);
                    }
                }
            }
            else //----上面为FromQuery的Select项,下面为Query或SubQuery的Select项----
            {
                //判断当前查询是否等于Select项的所有者,否则表示Select项的所有者的外部查询引用该Select项
                if (ReferenceEquals(ctx.CurrentQuery, item.Owner))
                {
                    BuildExpression(item.Expression, ctx);
                }
                else
                {
                    ctx.AppendFormat("{0}.\"{1}\"", ctx.GetQueryAliasName(item.Owner), item.AliasName);
                }

                //处理选择项别名
                if (ctx.CurrentQueryInfo.BuildStep == BuildQueryStep.BuildSelect)//&& !ctx.IsBuildCTESelectItem)
                {
                    MemberExpression memberExp = item.Expression as MemberExpression;
                    if (Expression.IsNull(memberExp)
                        /*|| memberExp.Type == ExpressionType.AggregationRefFieldExpression*/ //注意:聚合引用字段必须用别名
                        || memberExp.Name != item.AliasName)
                    {
                        ctx.AppendFormat(" \"{0}\"", item.AliasName);
                    }
                }
            }
        }
 public SqlSelectItem(Expression val, string aliasName)
 {
     Target = new SqlSelectItemExpression(val, aliasName);
 }
 public SqlSelectItem(Expression val)
 {
     //Todo: 是否判断val是否已是QuerySelect类型
     Target = new SqlSelectItemExpression(val);
 }
Exemple #5
0
 public void AddSelectItem(SqlSelectItemExpression item)
 {
     item.Owner = this;
     Selects.Add(item.AliasName, item);
 }