Beispiel #1
0
        //public AppBox.Core.DataTable ToDataTable(params SelectItem[] selectItem)
        //{
        //    return this.ToDataTable(0, -1, selectItem);
        //}

        //public AppBox.Core.DataTable ToDataTable(int topSize, params SelectItem[] selectItem)
        //{
        //    return this.ToDataTable(topSize, -1, selectItem);
        //}

        //public AppBox.Core.DataTable ToDataTable(int pageSize, int pageIndex, params SelectItem[] selectItem)
        //{
        //    if (selectItem == null || selectItem.Length <= 0)
        //        throw new System.ArgumentException("must select some one");

        //    this._topOrPageSize = pageSize;
        //    this._pageIndex = pageIndex;

        //    this._purpose = QueryPurpose.ToDataTable;

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

        //    //递交查询
        //    var db = SqlStore.Get(this.StoreName);
        //    var cmd = db.DbCommandBuilder.CreateQueryCommand(this);
        //    return db.ExecuteToTable(cmd);
        //}

        /// <summary>
        /// 查找FromQuery的目标Query的EntityExpression,用于获取IRuntimeContext执行查询或获取StoreName
        /// </summary>
        /// <returns>The sys context.</returns>
        /// <param name="oq">Oq.</param>
        private static EntityExpression FindTargetEntityExpression(SqlFromQuery oq)
        {
            var query = oq.Target as SqlQuery;

            return(query != null ? query.T : FindTargetEntityExpression((SqlFromQuery)oq.Target));
        }
Beispiel #2
0
        private void BuildNormalQuery(ISqlSelectQuery query, BuildQueryContext ctx)
        {
            //设置上下文
            ctx.BeginBuildQuery(query);

            //构建Select
            ctx.Append("Select ");
            if (query.Purpose == QueryPurpose.ToDataTable && query.Distinct)
            {
                ctx.Append("Distinct ");
            }

            //构建Select Items
            ctx.CurrentQueryInfo.BuildStep = BuildQueryStep.BuildSelect;
            if (query.Purpose == QueryPurpose.Count)
            {
                ctx.Append("Count(*)");
            }
            else
            {
                foreach (var si in query.Selects.Values)
                {
                    BuildSelectItem(si, ctx);
                    ctx.Append(",");
                }
                ctx.RemoveLastChar(); //移除最后多余的,号
            }

            //构建From
            ctx.CurrentQueryInfo.BuildStep = BuildQueryStep.BuildFrom;
            ctx.Append(" From ");
            //判断From源
            if (query is SqlFromQuery)
            {
                SqlFromQuery q = (SqlFromQuery)ctx.CurrentQuery;
                //开始构建From子查询
                ctx.Append("(");
                BuildNormalQuery(q.Target, ctx);
                ctx.Append(")");
                ctx.AppendFormat(" {0}", ((SqlQueryBase)q.Target).AliasName);// ((QueryBase)query).AliasName);
            }
            else
            {
                SqlQuery q     = (SqlQuery)ctx.CurrentQuery;
                var      model = Runtime.RuntimeContext.Current.GetModelAsync <EntityModel>(q.T.ModelID).Result;
                ctx.AppendFormat("\"{0}\" {1}", model.GetSqlTableName(false, null), q.AliasName);
            }

            //构建Where
            ctx.CurrentQueryInfo.BuildStep = BuildQueryStep.BuildWhere;
            if (!Equals(null, ctx.CurrentQuery.Filter))
            {
                ctx.Append(" Where ");
                BuildExpression(ctx.CurrentQuery.Filter, ctx);
            }

            //非分组的情况下构建Order By
            if (query.Purpose != QueryPurpose.Count)
            {
                if (query.GroupByKeys == null && query.HasSortItems)
                {
                    ctx.CurrentQueryInfo.BuildStep = BuildQueryStep.BuildOrderBy;
                    BuildOrderBy(query, ctx);
                }
            }

            //构建Join
            ctx.CurrentQueryInfo.BuildStep = BuildQueryStep.BuildJoin;
            SqlQueryBase q1 = (SqlQueryBase)ctx.CurrentQuery;

            if (q1.HasJoins) //先处理每个手工的联接及每个手工联接相应的自动联接
            {
                BuildJoins(q1.Joins, ctx);
            }
            ctx.BuildQueryAutoJoins(q1); //再处理自动联接

            //处理Skip and Take
            if (query.Purpose != QueryPurpose.Count)
            {
                ctx.CurrentQueryInfo.BuildStep = BuildQueryStep.BuildSkipAndTake;
                if (query.SkipSize > 0)
                {
                    ctx.AppendFormat(" Offset {0}", query.SkipSize);
                }
                if (query.Purpose == QueryPurpose.ToSingleEntity)
                {
                    ctx.Append(" Limit 1 ");
                }
                else if (query.TakeSize > 0)
                {
                    ctx.AppendFormat(" Limit {0} ", query.TakeSize);
                }
            }

            //构建分组、Having及排序
            BuildGroupBy(query, ctx);

            //结束上下文
            ctx.EndBuildQuery(query);
        }