示例#1
0
        public static QueryToken Parse(string tokenString, QueryDescription qd, SubTokensOptions options)
        {
            if (string.IsNullOrEmpty(tokenString))
            {
                throw new ArgumentNullException("tokenString");
            }

            //https://stackoverflow.com/questions/35418597/split-string-on-the-dot-characters-that-are-not-inside-of-brackets
            string[] parts = Regex.Split(tokenString, @"\.(?!([^[]*\]|[^(]*\)))");

            string firstPart = parts.FirstEx();

            QueryToken result = SubToken(null, qd, options, firstPart);

            if (result == null)
            {
                throw new FormatException("Column {0} not found on query {1}".FormatWith(firstPart, QueryUtils.GetKey(qd.QueryName)));
            }

            foreach (var part in parts.Skip(1))
            {
                var newResult = SubToken(result, qd, options, part);
                result = newResult ?? throw new FormatException("Token with key '{0}' not found on {1} of query {2}".FormatWith(part, result.FullKey(), QueryUtils.GetKey(qd.QueryName)));
            }

            return(result);
        }
示例#2
0
        public QueryToken SubTokenInternal(string key, SubTokensOptions options)
        {
            var result = CachedSubTokensOverride(options).TryGetC(key) ?? OnEntityExtension(this).SingleOrDefaultEx(a => a.Key == key);

            if (result == null)
            {
                return(null);
            }

            string allowed = result.IsAllowed();

            if (allowed != null)
            {
                throw new UnauthorizedAccessException($"Access to token '{key}' in '{this.FullKey()}' for query '{QueryUtils.GetKey(this.QueryName)}' is not allowed because: {allowed}");
            }

            return(result);
        }
示例#3
0
        public static QueryToken Parse(string tokenString, QueryDescription qd, SubTokensOptions options)
        {
            if (string.IsNullOrEmpty(tokenString))
            {
                throw new ArgumentNullException("tokenString");
            }

            string[] parts = tokenString.Split('.');

            string firstPart = parts.FirstEx();

            QueryToken result = SubToken(null, qd, options, firstPart);

            if (result == null)
            {
                throw new FormatException("Column {0} not found on query {1}".FormatWith(firstPart, QueryUtils.GetKey(qd.QueryName)));
            }

            foreach (var part in parts.Skip(1))
            {
                var newResult = SubToken(result, qd, options, part);
                result = newResult ?? throw new FormatException("Token with key '{0}' not found on {1} of query {2}".FormatWith(part, result.FullKey(), QueryUtils.GetKey(qd.QueryName)));
            }

            return(result);
        }
示例#4
0
        private Expression GetConditionBasic(BuildExpressionContext context)
        {
            Expression left = Token.BuildExpression(context);

            if (Operation.IsList())
            {
                if (Value == null)
                {
                    return(Expression.Constant(false));
                }

                IList clone = (IList)Activator.CreateInstance(Value.GetType(), Value);

                bool hasNull = false;
                while (clone.Contains(null))
                {
                    clone.Remove(null);
                    hasNull = true;
                }

                if (token.Type == typeof(string))
                {
                    while (clone.Contains(""))
                    {
                        clone.Remove("");
                        hasNull = true;
                    }

                    if (hasNull)
                    {
                        clone.Add("");
                        left = Expression.Coalesce(left, Expression.Constant(""));
                    }
                }


                Expression right    = Expression.Constant(clone, typeof(IEnumerable <>).MakeGenericType(Token.Type.Nullify()));
                var        contains = Expression.Call(miContainsEnumerable.MakeGenericMethod(Token.Type.Nullify()), right, left.Nullify());


                var result = !hasNull || token.Type == typeof(string) ? (Expression)contains :
                             Expression.Or(Expression.Equal(left, Expression.Constant(null, Token.Type.Nullify())), contains);


                if (Operation == FilterOperation.IsIn)
                {
                    return(result);
                }

                if (Operation == FilterOperation.IsNotIn)
                {
                    return(Expression.Not(result));
                }

                throw new InvalidOperationException("Unexpected operation");
            }
            else
            {
                var val = Value;
                if (token.Type == typeof(string) && (val == null || val is string && string.IsNullOrEmpty((string)val)))
                {
                    val  = val ?? "";
                    left = Expression.Coalesce(left, Expression.Constant(""));
                }

                Expression right = Expression.Constant(val, Token.Type);

                return(QueryUtils.GetCompareExpression(Operation, left, right));
            }
        }