Пример #1
0
        private static Criteria GetCriteriaLeaf(CriteriaExpression criteriaExpression)
        {
            string propName       = criteriaExpression.Left.Expression;
            string operatorString = criteriaExpression.Expression;
            object value          = criteriaExpression.Right.Expression;

            Criteria.ComparisonOp comparisonOp = CreateComparisonOperator(operatorString);
            if ((comparisonOp == Criteria.ComparisonOp.In || comparisonOp == Criteria.ComparisonOp.NotIn) && value is string)
            {
                string inValuesString = value.ToString().TrimStart('(').TrimEnd(')');
                HabaneroStringBuilder valueStringBuilder = new HabaneroStringBuilder(inValuesString);
                valueStringBuilder.RemoveQuotedSections();
                List <string> finalStrings = new List <string>();
                int           commaIndex;
                int           lastIndex = 0;
                commaIndex = valueStringBuilder.IndexOf(",");
                while (commaIndex != -1)
                {
                    HabaneroStringBuilder oneValueSubstring = valueStringBuilder.Substring(lastIndex, commaIndex - lastIndex);
                    finalStrings.Add(oneValueSubstring.PutBackQuotedSections().ToString().Trim());
                    lastIndex  = commaIndex + 1;
                    commaIndex = valueStringBuilder.IndexOf(",", lastIndex);
                }
                HabaneroStringBuilder oneValueString = valueStringBuilder.Substring(lastIndex, valueStringBuilder.ToString().Length - lastIndex);
                finalStrings.Add(oneValueString.PutBackQuotedSections().ToString().Trim());
                value = new Criteria.CriteriaValues(finalStrings);
            }
            return(new Criteria(propName, comparisonOp, value));
        }
        public void TestDoubleQuotes()
        {
            HabaneroStringBuilder s = new HabaneroStringBuilder("Hi There Peter, what''s going on");
            Assert.AreEqual("Hi There Peter, what's going on", s.RemoveQuotedSections().ToString());
            Assert.AreEqual("Hi There Peter, what's going on", s.PutBackQuotedSections().ToString());

            s = new HabaneroStringBuilder("Hi There Peter, 'what''s going on'");
            Assert.AreEqual("Hi There Peter, ", s.RemoveQuotedSections().ToString());
            Assert.AreEqual("Hi There Peter, 'what's going on'", s.PutBackQuotedSections().ToString());

            s = new HabaneroStringBuilder("Installation,Pipeclamps,'MP HI 1/4''',4.04");
            Assert.AreEqual("Installation,Pipeclamps,,4.04", s.RemoveQuotedSections().ToString());
            Assert.AreEqual("Installation,Pipeclamps,'MP HI 1/4'',4.04", s.PutBackQuotedSections().ToString());
        }
 public void TestRemoveAndPutBackQuotedSections()
 {
     HabaneroStringBuilder s = new HabaneroStringBuilder("A quoted 'test' is needed to test this functionality");
     s.RemoveQuotedSections();
     Assert.AreEqual("A quoted  is needed to test this functionality", s.ToString());
     s.PutBackQuotedSections();
     Assert.AreEqual("A quoted 'test' is needed to test this functionality", s.ToString());
 }
 public void TestRemoveAndPutBackQuotedSectionsAdvanced()
 {
     HabaneroStringBuilder s = new HabaneroStringBuilder(
         "Peter''s car''s engine said: 'That''s Mark''s Car' and 'That''s Eric''s car'.");
     s.RemoveQuotedSections();
     Assert.AreEqual("Peter's car's engine said:  and .", s.ToString());
     s.PutBackQuotedSections();
     Assert.AreEqual("Peter's car's engine said: 'That's Mark's Car' and 'That's Eric's car'.", s.ToString());
 }
Пример #5
0
        /// <summary>
        /// Parses the expression into a linked list of expression objects
        /// </summary>
        /// <param name="expression">The expression to parse</param>
        private void parseExpression(HabaneroStringBuilder expression)
        {
            HabaneroStringBuilder quotesRemovedExpression = expression;

            //Remove any sections sorounded by opening and closing ' (single quote)
            quotesRemovedExpression.RemoveQuotedSections();

            //Upper case such that the operator or Or and OR etc are identical.
            String expressionWithoutQuotes = quotesRemovedExpression.ToString().ToUpper();

            //Check if the first character is an opening bracket if it is then find the closing bracket
            // and set this as the left expression.
            if (expressionWithoutQuotes.IndexOf("(") == 0)
            {
                int bracketCount     = 1;
                int bracketSearchPos = 0;
                while ((bracketCount > 0) && (bracketSearchPos < expressionWithoutQuotes.Length - 1))
                {
                    bracketSearchPos++;
                    switch (expressionWithoutQuotes[bracketSearchPos])
                    {
                    case '(':
                        bracketCount++;
                        break;

                    case ')':
                        bracketCount--;
                        break;
                    }
                }
                if (bracketSearchPos == expressionWithoutQuotes.Length - 1)
                {
                    parseExpression(
                        quotesRemovedExpression.Substring(1, expressionWithoutQuotes.Length - 2).PutBackQuotedSections());
                    return;
                }
                _left = new CriteriaExpression(quotesRemovedExpression.Substring(1, bracketSearchPos - 1)
                                               .PutBackQuotedSections().ToString().Trim(), _operators);
                int    pos           = -1;
                string foundOperator = "";
                foreach (String op in _operators)
                {
                    int thisPos = expressionWithoutQuotes.IndexOf(op, bracketSearchPos);
                    if ((thisPos == -1 || thisPos >= pos) && pos != -1)
                    {
                        continue;
                    }
                    pos           = thisPos;
                    foundOperator = op;
                }
                if (pos != -1)
                {
                    _right = new CriteriaExpression(quotesRemovedExpression.Substring(pos + foundOperator.Length)
                                                    .PutBackQuotedSections().ToString().Trim(), _operators);
                    _expression = foundOperator;
                }
            }
            else
            {
                foreach (String op in _operators)
                {
                    int pos = expressionWithoutQuotes.IndexOf(op);
                    if (pos == -1 || IsPosInsideBrackets(expressionWithoutQuotes, pos))
                    {
                        continue;
                    }
                    _left = new CriteriaExpression(quotesRemovedExpression.Substring(0, pos)
                                                   .PutBackQuotedSections().ToString().Trim(), _operators);
                    if (op.Trim() == "IN" || op.Trim() == "NOT IN")
                    {
                        var criteriaExpression = new CriteriaExpression("");
                        criteriaExpression._expression = quotesRemovedExpression.Substring(pos + op.Length)
                                                         .PutBackQuotedSections().ToString().Trim();
                        _right = criteriaExpression;
                    }
                    else
                    {
                        _right = new CriteriaExpression(quotesRemovedExpression.Substring(pos + op.Length)
                                                        .PutBackQuotedSections().ToString().Trim(), _operators);
                    }
                    _expression = op;
                    break;
                }
            }
            //If this was a terminal criteria i.e. it has no more children then
            // this is the expression there will be no right and left expression.
            if (string.IsNullOrEmpty(_expression))
            {
                _expression = quotesRemovedExpression.PutBackQuotedSections().DropOuterQuotes().ToString();
            }
        }