コード例 #1
0
        public virtual GeneralQueryState AsSubQueryState()
        {
            DbSqlQueryExpression sqlQuery = this.CreateSqlQuery();
            DbSubQueryExpression subQuery = new DbSubQueryExpression(sqlQuery);

            QueryModel newQueryModel = new QueryModel(this._queryModel.ScopeParameters, this._queryModel.ScopeTables, this._queryModel.IgnoreFilters);

            DbTableSegment        tableSeg  = new DbTableSegment(subQuery, newQueryModel.GenerateUniqueTableAlias(), LockType.Unspecified);
            DbFromTableExpression fromTable = new DbFromTableExpression(tableSeg);

            newQueryModel.FromTable = fromTable;

            DbTable aliasTable = new DbTable(tableSeg.Alias);

            //TODO 根据旧的生成新 ResultModel
            IObjectModel newResultModel = this.QueryModel.ResultModel.ToNewObjectModel(sqlQuery, aliasTable, fromTable);

            newQueryModel.ResultModel = newResultModel;

            //得将 subQuery.SqlQuery.Orders 告诉 以下创建的 result
            //将 orderPart 传递下去
            for (int i = 0; i < this.QueryModel.Orderings.Count; i++)
            {
                DbOrdering   ordering    = this.QueryModel.Orderings[i];
                DbExpression orderingExp = ordering.Expression;

                string alias = null;

                DbColumnSegment columnExpression = sqlQuery.ColumnSegments.Find(a => DbExpressionEqualityComparer.EqualsCompare(orderingExp, a.Body));

                // 对于重复的则不需要往 sqlQuery.Columns 重复添加了
                if (columnExpression != null)
                {
                    alias = columnExpression.Alias;
                }
                else
                {
                    alias = Utils.GenerateUniqueColumnAlias(sqlQuery);
                    DbColumnSegment columnSeg = new DbColumnSegment(orderingExp, alias);
                    sqlQuery.ColumnSegments.Add(columnSeg);
                }

                DbColumnAccessExpression columnAccessExpression = new DbColumnAccessExpression(aliasTable, DbColumn.MakeColumn(orderingExp, alias));
                newQueryModel.Orderings.Add(new DbOrdering(columnAccessExpression, ordering.OrderType));
            }

            newQueryModel.InheritOrderings = true;

            GeneralQueryState queryState = new GeneralQueryState(newQueryModel);

            return(queryState);
        }
コード例 #2
0
ファイル: QueryStateBase.cs プロジェクト: yj-smart/Chloe
        public virtual GeneralQueryState AsSubQueryState()
        {
            DbSqlQueryExpression sqlQuery = this.CreateSqlQuery();
            DbSubQueryExpression subQuery = new DbSubQueryExpression(sqlQuery);

            ResultElement result = new ResultElement();

            DbTableSegment        tableSeg  = new DbTableSegment(subQuery, result.GenerateUniqueTableAlias());
            DbFromTableExpression fromTable = new DbFromTableExpression(tableSeg);

            result.FromTable = fromTable;

            DbTable table = new DbTable(tableSeg.Alias);

            //TODO 根据旧的生成新 MappingMembers
            IMappingObjectExpression newMoe = this.Result.MappingObjectExpression.ToNewObjectExpression(sqlQuery, table);

            result.MappingObjectExpression = newMoe;

            //得将 subQuery.SqlQuery.Orders 告诉 以下创建的 result
            //将 orderPart 传递下去
            if (this.Result.Orderings.Count > 0)
            {
                for (int i = 0; i < this.Result.Orderings.Count; i++)
                {
                    DbOrdering   ordering    = this.Result.Orderings[i];
                    DbExpression orderingExp = ordering.Expression;

                    string alias = null;

                    DbColumnSegment columnExpression = sqlQuery.ColumnSegments.Where(a => DbExpressionEqualityComparer.EqualsCompare(orderingExp, a.Body)).FirstOrDefault();

                    // 对于重复的则不需要往 sqlQuery.Columns 重复添加了
                    if (columnExpression != null)
                    {
                        alias = columnExpression.Alias;
                    }
                    else
                    {
                        alias = Utils.GenerateUniqueColumnAlias(sqlQuery);
                        DbColumnSegment columnSeg = new DbColumnSegment(orderingExp, alias);
                        sqlQuery.ColumnSegments.Add(columnSeg);
                    }

                    DbColumnAccessExpression columnAccessExpression = new DbColumnAccessExpression(orderingExp.Type, table, alias);
                    result.Orderings.Add(new DbOrdering(columnAccessExpression, ordering.OrderType));
                }
            }

            result.InheritOrderings = true;

            GeneralQueryState queryState = new GeneralQueryState(result);

            return(queryState);
        }