Exemple #1
0
        JoinQueryResult Visit(QueryExpression exp)
        {
            IQueryState     state = QueryExpressionResolver.Resolve(exp, this._scopeParameters, this._queryModel.ScopeTables);
            JoinQueryResult ret   = state.ToJoinQueryResult(this._joinType, this._conditionExpression, this._scopeParameters, this._queryModel.ScopeTables, this._queryModel.GenerateUniqueTableAlias());

            return(ret);
        }
        DbSqlQueryExpression ConvertToDbSqlQueryExpression(Expression exp, Type resultType)
        {
            if (!IsIQueryType(exp.Type))
            {
                throw new NotSupportedException(exp.ToString());
            }

            QueryBase   query       = ExpressionEvaluator.Evaluate(exp) as QueryBase;
            IQueryState qs          = QueryExpressionResolver.Resolve(query.QueryExpression, this._scopeParameters, this._scopeTables);
            MappingData mappingData = qs.GenerateMappingData();

            DbSqlQueryExpression sqlQueryExpression = mappingData.SqlQuery.Update(resultType);

            return(sqlQueryExpression);
        }
        public static IQueryState Resolve(QueryExpression queryExpression, ScopeParameterDictionary scopeParameters, KeyDictionary <string> scopeTables)
        {
            QueryExpressionResolver resolver = new QueryExpressionResolver(scopeParameters, scopeTables);

            return(queryExpression.Accept(resolver));
        }
        public override IQueryState Visit(JoinQueryExpression exp)
        {
            IQueryState qs = QueryExpressionResolver.Resolve(exp.PrevExpression, this._scopeParameters, this._scopeTables);

            ResultElement resultElement = qs.ToFromQueryResult();

            List <IMappingObjectExpression> moeList = new List <IMappingObjectExpression>();

            moeList.Add(resultElement.MappingObjectExpression);

            foreach (JoiningQueryInfo joiningQueryInfo in exp.JoinedQueries)
            {
                ScopeParameterDictionary scopeParameters = resultElement.ScopeParameters.Clone(resultElement.ScopeParameters.Count + moeList.Count);
                for (int i = 0; i < moeList.Count; i++)
                {
                    ParameterExpression p = joiningQueryInfo.Condition.Parameters[i];
                    scopeParameters[p] = moeList[i];
                }

                JoinQueryResult joinQueryResult = JoinQueryExpressionResolver.Resolve(joiningQueryInfo.Query.QueryExpression, resultElement, joiningQueryInfo.JoinType, joiningQueryInfo.Condition, scopeParameters);

                var nullChecking = DbExpression.CaseWhen(new DbCaseWhenExpression.WhenThenExpressionPair(joinQueryResult.JoinTable.Condition, DbConstantExpression.One), DbConstantExpression.Null, DbConstantExpression.One.Type);

                if (joiningQueryInfo.JoinType == JoinType.LeftJoin)
                {
                    joinQueryResult.MappingObjectExpression.SetNullChecking(nullChecking);
                }
                else if (joiningQueryInfo.JoinType == JoinType.RightJoin)
                {
                    foreach (IMappingObjectExpression item in moeList)
                    {
                        item.SetNullChecking(nullChecking);
                    }
                }
                else if (joiningQueryInfo.JoinType == JoinType.FullJoin)
                {
                    joinQueryResult.MappingObjectExpression.SetNullChecking(nullChecking);
                    foreach (IMappingObjectExpression item in moeList)
                    {
                        item.SetNullChecking(nullChecking);
                    }
                }

                resultElement.FromTable.JoinTables.Add(joinQueryResult.JoinTable);
                moeList.Add(joinQueryResult.MappingObjectExpression);
            }

            ScopeParameterDictionary scopeParameters1 = resultElement.ScopeParameters.Clone(resultElement.ScopeParameters.Count + moeList.Count);

            for (int i = 0; i < moeList.Count; i++)
            {
                ParameterExpression p = exp.Selector.Parameters[i];
                scopeParameters1[p] = moeList[i];
            }
            IMappingObjectExpression moe = SelectorResolver.Resolve(exp.Selector, scopeParameters1, resultElement.ScopeTables);

            resultElement.MappingObjectExpression = moe;

            GeneralQueryState queryState = new GeneralQueryState(resultElement);

            return(queryState);
        }
        public override IQueryState Visit(JoinQueryExpression exp)
        {
            IQueryState qs = QueryExpressionResolver.Resolve(exp.PrevExpression, this._scopeParameters, this._scopeTables);

            QueryModel queryModel = qs.ToFromQueryModel();

            List <IObjectModel> modelList = new List <IObjectModel>();

            modelList.Add(queryModel.ResultModel);

            foreach (JoinQueryInfo joinQueryInfo in exp.JoinedQueries)
            {
                ScopeParameterDictionary scopeParameters = queryModel.ScopeParameters.Clone(queryModel.ScopeParameters.Count + modelList.Count);
                for (int i = 0; i < modelList.Count; i++)
                {
                    ParameterExpression p = joinQueryInfo.Condition.Parameters[i];
                    scopeParameters[p] = modelList[i];
                }

                JoinQueryResult joinQueryResult = JoinQueryExpressionResolver.Resolve(joinQueryInfo, queryModel, scopeParameters);

                var nullChecking = DbExpression.CaseWhen(new DbCaseWhenExpression.WhenThenExpressionPair(joinQueryResult.JoinTable.Condition, DbConstantExpression.One), DbConstantExpression.Null, DbConstantExpression.One.Type);

                if (joinQueryInfo.JoinType == JoinType.LeftJoin)
                {
                    joinQueryResult.ResultModel.SetNullChecking(nullChecking);
                }
                else if (joinQueryInfo.JoinType == JoinType.RightJoin)
                {
                    foreach (IObjectModel item in modelList)
                    {
                        item.SetNullChecking(nullChecking);
                    }
                }
                else if (joinQueryInfo.JoinType == JoinType.FullJoin)
                {
                    joinQueryResult.ResultModel.SetNullChecking(nullChecking);
                    foreach (IObjectModel item in modelList)
                    {
                        item.SetNullChecking(nullChecking);
                    }
                }

                queryModel.FromTable.JoinTables.Add(joinQueryResult.JoinTable);
                modelList.Add(joinQueryResult.ResultModel);
            }

            ScopeParameterDictionary scopeParameters1 = queryModel.ScopeParameters.Clone(queryModel.ScopeParameters.Count + modelList.Count);

            for (int i = 0; i < modelList.Count; i++)
            {
                ParameterExpression p = exp.Selector.Parameters[i];
                scopeParameters1[p] = modelList[i];
            }
            IObjectModel model = SelectorResolver.Resolve(exp.Selector, scopeParameters1, queryModel.ScopeTables);

            queryModel.ResultModel = model;

            GeneralQueryState queryState = new GeneralQueryState(queryModel);

            return(queryState);
        }