SqlField RegisterCteField(ISqlExpression baseExpression, [NotNull] ISqlExpression expression, int index, string alias)
            {
                if (expression == null)
                {
                    throw new ArgumentNullException(nameof(expression));
                }

                var cteField = _cte.RegisterFieldMapping(baseExpression, expression, index, () =>
                {
                    var f = QueryHelper.GetUnderlyingField(baseExpression ?? expression);

                    var newField = f == null
                                                                ? new SqlField {
                        SystemType = expression.SystemType, CanBeNull = expression.CanBeNull, Name = alias
                    }
                                                                : new SqlField(f);

                    newField.PhysicalName = newField.Name;
                    return(newField);
                });

                if (!SqlTable.Fields.TryGetValue(cteField.Name, out var field))
                {
                    field = new SqlField(cteField);
                    SqlTable.Add(field);
                }

                return(field);
            }
            public override SqlInfo[] ConvertToSql(Expression expression, int level, ConvertFlags flags)
            {
                var info = base.ConvertToSql(expression, level, flags);

                if (!_calculatingIndex)
                {
                    _calculatingIndex = true;

                    var queryContext = GetQueryContext();
                    // If Field is needed we have to populate it in CTE
                    if (queryContext != null)
                    {
                        var subInfo = queryContext.ConvertToSql(expression, level, flags);
                        if (subInfo.Any(si => si.Index < 0))
                        {
                            subInfo = queryContext.ConvertToIndex(expression, level, flags);
                        }

                        for (int i = 0; i < info.Length; i++)
                        {
                            _cte.RegisterFieldMapping((SqlField)info[i].Sql, subInfo[i].Index);
                        }
                    }

                    _calculatingIndex = false;
                }

                return(info);
            }
            public override SqlInfo[] ConvertToSql(Expression expression, int level, ConvertFlags flags)
            {
                var queryContext = GetQueryContext();

                if (queryContext == null)
                {
                    return(base.ConvertToSql(expression, level, flags));
                }

                SqlInfo[] ConvertConvertToSqlAndRegister(Expression exp, int lvl, ConvertFlags fl)
                {
                    var baseInfos = base.ConvertToSql(exp, lvl, fl);
                    var subInfos  = queryContext.ConvertToIndex(exp, lvl, fl);

                    var pairs = from bi in baseInfos
                                from si in subInfos.Where(si =>
                                                          si.MemberChain.Count == bi.MemberChain.Count && si.MemberChain.Count == 1 && si.MemberChain[0] == bi.MemberChain[0])
                                select new { bi, si };

                    foreach (var pair in pairs)
                    {
                        _cte.RegisterFieldMapping((SqlField)pair.bi.Sql);
                    }

                    return(baseInfos);
                }

                var result = ConvertConvertToSqlAndRegister(expression, level, flags);

                return(result);
            }
예제 #4
0
            SqlField RegisterCteField(ISqlExpression?baseExpression, ISqlExpression expression, int index, string?alias)
            {
                if (expression == null)
                {
                    throw new ArgumentNullException(nameof(expression));
                }

                var cteField = _cte.RegisterFieldMapping(index, () =>
                {
                    var f = QueryHelper.GetUnderlyingField(baseExpression ?? expression);

                    var newField = f == null
                                                ? new SqlField(expression.SystemType !, alias, expression.CanBeNull)
                                                : new SqlField(f);

                    if (alias != null)
                    {
                        newField.Name = alias;
                    }

                    newField.PhysicalName = newField.Name;
                    return(newField);
                });