Пример #1
0
 public ExtensionAttribute(string configuration, string expression) : base(configuration, expression)
 {
     SqlFlags         = SqlFlags.None;
     ExpectExpression = true;
     ServerSideOnly   = true;
     PreferServerSide = true;
 }
Пример #2
0
        public SqlExpression(Type?systemType, string expr, int precedence, SqlFlags flags, params ISqlExpression[] parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            foreach (var value in parameters)
            {
                if (value == null)
                {
                    throw new ArgumentNullException(nameof(parameters));
                }
            }

            SystemType = systemType;
            Expr       = expr;
            Precedence = precedence;
            Parameters = parameters;
            Flags      = flags;
        }
Пример #3
0
            public static SqlExpression BuildSqlExpression(SqlExtension root, Type systemType, int precedence, SqlFlags sqlFlags)
            {
                var sb             = new StringBuilder();
                var resolvedParams = new Dictionary <SqlExtensionParam, string>();
                var resolving      = new HashSet <SqlExtensionParam>();
                var newParams      = new List <ISqlExpression>();

                Func <string, string, string> valueProvider = null;
                Stack <SqlExtension>          current       = new Stack <SqlExtension>();

                valueProvider = (name, delimiter) =>
                {
                    var found = root.GetParametersByName(name);
                    if (current.Count != 0)
                    {
                        found = current.Peek().GetParametersByName(name).Concat(found);
                    }
                    string result = null;
                    foreach (var p in found)
                    {
                        string paramValue;
                        if (resolvedParams.TryGetValue(p, out paramValue))
                        {
                            result = paramValue;
                        }
                        else
                        {
                            if (resolving.Contains(p))
                            {
                                throw new InvalidOperationException("Circular reference");
                            }

                            resolving.Add(p);
                            var ext = p.Extension;
                            if (ext != null)
                            {
                                current.Push(ext);
                                paramValue = ResolveExpressionValues(ext.Expr, valueProvider);
                                current.Pop();
                            }
                            else
                            {
                                sb.Length = 0;
                                if (p.Expression != null)
                                {
                                    paramValue = string.Format("{{{0}}}", newParams.Count);
                                    newParams.Add(p.Expression);
                                }
                            }

                            resolvedParams.Add(p, paramValue);

                            if (string.IsNullOrEmpty(paramValue))
                            {
                                continue;
                            }

                            if (!string.IsNullOrEmpty(result))
                            {
                                result = result + delimiter;
                            }
                            result = result + paramValue;
                        }

                        if (delimiter == null && !string.IsNullOrEmpty(result))
                        {
                            break;
                        }
                    }

                    return(result);
                };

                var expr          = ResolveExpressionValues(root.Expr, valueProvider);
                var sqlExpression = new SqlExpression(systemType, expr, precedence, sqlFlags, newParams.ToArray());

                return(sqlExpression);
            }