Ejemplo n.º 1
0
        //Match function built for repository that doesn't have the power of search by default. Meaning search will be done after results are received. Example of this storage is Azure TableStorage
        bool Match(T data, SearchExpression expression)
        {
            if (expression == null)
            {
                return(true);
            }
            if (expression is PropertySearchExpression)
            {
                int      tempInt    = 0;
                double   tempDouble = 0;
                DateTime tempDatetime;
                PropertySearchExpression propertySearchExpression = expression as PropertySearchExpression;
                PropertyInfo             pInfo = typeof(T).GetProperty(propertySearchExpression.Property);
                var value = pInfo.GetValue(data);
                switch (propertySearchExpression.Operator)
                {
                case "=": return((value ?? "").ToString().Trim().ToLower() == propertySearchExpression.Value.Trim().ToLower());

                case "!=": return((value ?? "").ToString().Trim().ToLower() != propertySearchExpression.Value.Trim().ToLower());

                case ">":
                    if (pInfo.GetType() == typeof(int) && int.TryParse(propertySearchExpression.Value, out tempInt))
                    {
                        return((int)value > tempInt);
                    }
                    if (pInfo.GetType() == typeof(double) && double.TryParse(propertySearchExpression.Value, out tempDouble))
                    {
                        return((double)value > tempDouble);
                    }
                    if (pInfo.GetType() == typeof(DateTime) && DateTime.TryParse(propertySearchExpression.Value, out tempDatetime))
                    {
                        return((DateTime)value > tempDatetime);
                    }

                    else
                    {
                        return(false);
                    }

                case "<":
                    if (pInfo.GetType() == typeof(int) && int.TryParse(propertySearchExpression.Value, out tempInt))
                    {
                        return((int)value < tempInt);
                    }
                    if (pInfo.GetType() == typeof(double) && double.TryParse(propertySearchExpression.Value, out tempDouble))
                    {
                        return((double)value < tempDouble);
                    }
                    if (pInfo.GetType() == typeof(DateTime) && DateTime.TryParse(propertySearchExpression.Value, out tempDatetime))
                    {
                        return((DateTime)value < tempDatetime);
                    }
                    else
                    {
                        return(false);
                    }

                case "%": return((value ?? "").ToString().Trim().ToLower().Contains(propertySearchExpression.Value.Trim().ToLower()));

                default: return(false);
                }
            }

            else
            {
                BinarySearchExpression <T> binarySearchExpression = expression as BinarySearchExpression <T>;
                switch (binarySearchExpression.BinaryOperator)
                {
                case "||": return(Match(data, binarySearchExpression.LeftSearch) || Match(data, binarySearchExpression.RightSearch));

                case "&&": return(Match(data, binarySearchExpression.LeftSearch) && Match(data, binarySearchExpression.RightSearch));

                default: return(false);
                }
            }
        }
Ejemplo n.º 2
0
        static SearchExpression GetSearchExpression(Stack <Token> tokenList)
        {
            Stack <Token> output = new Stack <Token>();
            bool          error  = false;

            while ((tokenList.Count > 1 || tokenList.Count == 1 && tokenList.Peek().TokenType != TokenType.Expresssion))
            {
                Token token = tokenList.Pop();
                if (token.TokenType == TokenType.BinaryOperator || token.TokenType == TokenType.AssignmentOperator)
                {
                    Token          rightHand      = output.Pop();
                    Token          leftHand       = output.Pop();
                    ExpressionType expressionType = GetExpressionType(leftHand, rightHand, token);
                    if (expressionType == ExpressionType.member)
                    {
                        Token propertyExpressionToken = new Token();
                        PropertySearchExpression propertySearchExpression = new PropertySearchExpression();
                        propertySearchExpression.Property = leftHand.value as string;
                        propertySearchExpression.Operator = (string)token.value;
                        propertySearchExpression.Value    = (string)rightHand.value;
                        propertyExpressionToken.value     = propertySearchExpression;
                        propertyExpressionToken.TokenType = TokenType.Expresssion;
                        tokenList.Push(propertyExpressionToken);
                        while (output.Count > 0)
                        {
                            tokenList.Push(output.Pop());
                        }
                    }

                    else if (expressionType == ExpressionType.binary && leftHand.TokenType == TokenType.Expresssion && rightHand.TokenType == TokenType.Expresssion)
                    {
                        Token binaryExpressionToken = new Token();
                        BinarySearchExpression <T> binarySearchExpression = new BinarySearchExpression <T>();
                        binarySearchExpression.BinaryOperator = token.value as string;
                        binarySearchExpression.LeftSearch     = leftHand.value as SearchExpression;
                        binarySearchExpression.RightSearch    = rightHand.value as SearchExpression;
                        binaryExpressionToken.TokenType       = TokenType.Expresssion;
                        binaryExpressionToken.value           = binarySearchExpression;
                        tokenList.Push(binaryExpressionToken);
                        while (output.Count > 0)
                        {
                            tokenList.Push(output.Pop());
                        }
                    }

                    else
                    {
                        error = true;
                        break;
                    }
                }
                else
                {
                    output.Push(token);
                }
            }


            if (error || tokenList.Count > 1)
            {
                return(null);
            }
            return(tokenList.Pop().value as SearchExpression);
        }