Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="token"></param>
        /// <param name="q"></param>
        public void Token(string token, QueryTranslator q)
        {
            string lcToken = token.ToLowerInvariant();

            //Cope with [,]

            if (token.Equals("[") && !expectingPathContinuation)
            {
                expectingPathContinuation = false;
                if (expectingIndex == 0)
                {
                    throw new QueryException("unexpected [");
                }
                return;
            }
            else if (token.Equals("]"))
            {
                expectingIndex--;
                expectingPathContinuation = true;
                return;
            }

            //Cope with a continued path expression (ie. ].baz)
            if (expectingPathContinuation)
            {
                if (ContinuePathExpression(token, q))
                {
                    return;
                }
            }

            //Cope with a subselect
            if (!inSubselect && (lcToken.Equals("select") || (lcToken.Equals("from") && !isInSpecialFunctionClause)))
            {
                inSubselect = true;
                subselect   = new StringBuilder(20);
            }
            if (inSubselect && token.Equals(StringHelper.ClosedParen))
            {
                bracketsSinceSelect--;

                if (bracketsSinceSelect == -1)
                {
                    QueryTranslator subq = new QueryTranslator(q.Factory, subselect.ToString(), q.EnabledFilters);
                    try
                    {
                        subq.Compile(q);
                    }
                    catch (MappingException me)
                    {
                        throw new QueryException("MappingException occurred compiling subquery", me);
                    }

                    AppendToken(q, subq.SqlString);
                    inSubselect         = false;
                    bracketsSinceSelect = 0;
                }
            }
            if (inSubselect)
            {
                if (token.Equals(StringHelper.OpenParen))
                {
                    bracketsSinceSelect++;
                }
                subselect.Append(token).Append(' ');
                return;
            }

            //Cope with special cases of AND, NOT, ()
            SpecialCasesBefore(lcToken);

            //Close extra brackets we opened
            if (!betweenSpecialCase && expressionTerminators.Contains(lcToken))
            {
                CloseExpression(q, lcToken);
            }

            //take note when this is a boolean expression

            if (booleanOperators.Contains(lcToken))
            {
                booleanTests.RemoveAt(booleanTests.Count - 1);
                booleanTests.Add(true);
            }

            if (lcToken.Equals("not"))
            {
                nots[nots.Count - 1] = !nots[nots.Count - 1];
                negated = !negated;
                return;                 //NOTE: early return
            }

            if (!isInSpecialFunctionClause && specialFunctions.Contains(lcToken))
            {
                isInSpecialFunctionClause = true;
            }
            if (isInSpecialFunctionClause && token.Equals(StringHelper.OpenParen))
            {
                specialFunctionParenCount++;
            }
            if (isInSpecialFunctionClause && token.Equals(StringHelper.ClosedParen))
            {
                specialFunctionParenCount--;
                isInSpecialFunctionClause = specialFunctionParenCount > 0;
            }
            //process a token, mapping OO path expressions to SQL expressions
            DoToken(token, q);

            //Open any extra brackets we might need.

            if (!betweenSpecialCase && expressionOpeners.Contains(lcToken))
            {
                OpenExpression(q, lcToken);
            }

            //Cope with special cases of AND, NOT, )
            SpecialCasesAfter(lcToken);
        }