Exemple #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));
        }
 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);
 }
        /// <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();
            }
        }
        /// <summary>
        /// Loads all the values for the current line and returns them in a list
        /// </summary>
        /// <returns>Returns a list of values</returns>
        public List<string> GetValues()
        {
            HabaneroStringBuilder stringBuilder = new HabaneroStringBuilder(_currentLine.Replace(",\"\",", ",,"));
            stringBuilder.SetQuotes(new string[] { "\"" });
            stringBuilder.RemoveQuotedSections();
            if (stringBuilder.IndexOf("\"") > -1)
            {
                string nextLine = _reader.ReadLine();
                if (nextLine == null)
                {
                    nextLine = "";
                    throw new UserException("Unclosed quote in CSV file, line " + _lineNo);
                }
                _currentLine = _currentLine + nextLine;

                _lineNo++;
                return GetValues();
            }
            List<string> values = new List<string>();
            
            int commaPos = 0;
            int pos = 0;
            int endPos = 0;
            do
            {
                commaPos = stringBuilder.IndexOf(",", pos);
                if (commaPos == -1)
                {
                    endPos = stringBuilder.ToString().Length;
                }
                else
                {
                    endPos = commaPos;
                }
                string value = stringBuilder.Substring(pos, endPos - pos).PutBackQuotedSections().ToString().Trim();
                if ((value.StartsWith("\"") && value.EndsWith("\"")) || (value.StartsWith("'") && value.EndsWith("'")))
                {
                    value = value.Substring(1, value.Length - 2);
                }
                values.Add(value);
                pos = commaPos + 1;
            } while (commaPos != -1);
            return values;
        }