public void CaseExpression_Regression_Duplicated_Case()
        {
            List <TSQLStatement> statements = TSQLStatementReader.ParseStatements(
                @"select case when abi = '03654' then 0 else 1 end as abi;",
                includeWhitespace: false);

            Assert.AreEqual(1, statements.Count);

            List <TSQLToken> tokens = statements[0].Tokens;

            TokenComparisons.CompareTokenLists(
                new List <TSQLToken>()
            {
                new TSQLKeyword(0, "select"),
                new TSQLKeyword(7, "case"),
                new TSQLKeyword(12, "when"),
                new TSQLIdentifier(17, "abi"),
                new TSQLOperator(21, "="),
                new TSQLStringLiteral(23, "'03654'"),
                new TSQLKeyword(31, "then"),
                new TSQLNumericLiteral(36, "0"),
                new TSQLKeyword(38, "else"),
                new TSQLNumericLiteral(43, "1"),
                new TSQLKeyword(45, "end"),
                new TSQLKeyword(49, "as"),
                new TSQLIdentifier(52, "abi")
            },
                tokens);
        }
        public void CaseExpression_Dont_Overrun()
        {
            List <TSQLStatement> statements = TSQLStatementReader.ParseStatements(
                @"
					BEGIN
						SELECT CASE WHEN 1 = 2 THEN 0 ELSE 1 END
					END"
                // normalizing line endings to unix format to ensure passing
                // tests in various environments
                .Replace("\r", ""),
                includeWhitespace: false);

            Assert.AreEqual(3, statements.Count);
            Assert.AreEqual(1, statements[0].Tokens.Count);

            TokenComparisons.CompareTokenLists(
                new List <TSQLToken>()
            {
                new TSQLKeyword(18, "SELECT"),
                new TSQLKeyword(25, "CASE"),
                new TSQLKeyword(30, "WHEN"),
                new TSQLNumericLiteral(35, "1"),
                new TSQLOperator(37, "="),
                new TSQLNumericLiteral(39, "2"),
                new TSQLKeyword(41, "THEN"),
                new TSQLNumericLiteral(46, "0"),
                new TSQLKeyword(48, "ELSE"),
                new TSQLNumericLiteral(53, "1"),
                new TSQLKeyword(55, "END")
            },
                statements[1].Tokens);

            Assert.AreEqual(1, statements[2].Tokens.Count);
            Assert.IsTrue(statements[2].Tokens[0].IsKeyword(TSQLKeywords.END));
        }
Example #3
0
        public void Parse_GO()
        {
            List <TSQLToken> tokens = TSQLTokenizer.ParseTokens("GO");

            TokenComparisons.CompareTokenLists(
                new List <TSQLToken>()
            {
                new TSQLKeyword(0, "GO")
            },
                tokens);
        }
Example #4
0
 public void Parse_uspSearchCandidateResumes_NoWhitespace()
 {
     using (StringReader reader = new StringReader(Resources.AdventureWorks2014_dbo_uspSearchCandidateResumes))
         using (TSQLTokenizer lexer = new TSQLTokenizer(reader))
         {
             TokenComparisons.CompareStreamStartToList(
                 GetuspSearchCandidateResumesTokens()
                 .Where(t => !(t is TSQLWhitespace)).ToList(),
                 lexer);
         }
 }
Example #5
0
 public void Parse_GOFromStream()
 {
     using (TextReader reader = new StringReader("GO"))
         using (TSQLTokenizer lexer = new TSQLTokenizer(reader))
         {
             TokenComparisons.CompareStreamToList(
                 new List <TSQLToken>()
             {
                 new TSQLKeyword(0, "GO")
             },
                 lexer);
         }
 }
Example #6
0
 public void Parse_uspSearchCandidateResumes()
 {
     using (StringReader reader = new StringReader(Resources.AdventureWorks2014_dbo_uspSearchCandidateResumes))
         using (TSQLTokenizer lexer = new TSQLTokenizer(reader)
         {
             IncludeWhitespace = true
         })
         {
             TokenComparisons.CompareStreamStartToList(
                 GetuspSearchCandidateResumesTokens(),
                 lexer);
         }
 }
Example #7
0
        public void Parse_uspSearchCandidateResumes_NoWhitespace()
        {
            using (StringReader reader = new StringReader(Resources.AdventureWorks2014_dbo_uspSearchCandidateResumes))
                using (TSQLTokenizer lexer = new TSQLTokenizer(reader))
                {
                    Assert.IsFalse(lexer.IncludeWhitespace);
                    Assert.IsFalse(lexer.UseQuotedIdentifiers);

                    TokenComparisons.CompareStreamStartToList(
                        GetuspSearchCandidateResumesTokens()
                        .Where(t => !(t is TSQLWhitespace)).ToList(),
                        lexer);
                }
        }
Example #8
0
        public void Parse_EmptyMoney()
        {
            List <TSQLToken> tokens = TSQLTokenizer.ParseTokens(
                "$,",
                includeWhitespace: false);

            TokenComparisons.CompareTokenLists(
                new List <TSQLToken>()
            {
                new TSQLMoneyLiteral(0, "$"),
                new TSQLCharacter(1, ",")
            },
                tokens);
        }
Example #9
0
        public void Parse_OldStyleOuterJoins()
        {
            // .. WHERE tablea.id *= tableb.id
            List <TSQLToken> tokens = TSQLTokenizer.ParseTokens(
                "*= =*",
                includeWhitespace: false);

            TokenComparisons.CompareTokenLists(
                new List <TSQLToken>()
            {
                new TSQLOperator(0, "*="),
                new TSQLOperator(3, "=*")
            },
                tokens);
        }
Example #10
0
        public void Parse_SystemIdentifiers()
        {
            // $IDENTITY, $ROWGUID, $PARTITION
            // https://msdn.microsoft.com/en-us/library/ms176104.aspx
            // https://msdn.microsoft.com/en-us/library/ms188071.aspx
            List <TSQLToken> tokens = TSQLTokenizer.ParseTokens(
                "$IDENTITY $ROWGUID $PARTITION",
                includeWhitespace: false);

            TokenComparisons.CompareTokenLists(
                new List <TSQLToken>()
            {
                new TSQLIdentifier(0, "$IDENTITY"),
                new TSQLIdentifier(10, "$ROWGUID"),
                new TSQLIdentifier(19, "$PARTITION")
            },
                tokens);
        }
Example #11
0
        public void ArgumentList_Simple()
        {
            TSQLTokenizer tokenizer = new TSQLTokenizer(
                "dostuff( 1 , 2 )")
            {
                IncludeWhitespace = true
            };

            Assert.IsTrue(tokenizer.MoveNext());
            Assert.AreEqual("dostuff", tokenizer.Current.Text);
            Assert.IsTrue(tokenizer.MoveNext());
            Assert.IsTrue(tokenizer.Current.IsCharacter(TSQLCharacters.OpenParentheses));
            Assert.IsTrue(tokenizer.MoveNext());

            TSQLArgumentList arguments = new TSQLArgumentListParser().Parse(tokenizer);

            Assert.IsTrue(tokenizer.Current.IsCharacter(TSQLCharacters.CloseParentheses));
            Assert.AreEqual(2, arguments.Count);

            Assert.AreEqual(2, arguments[0].Tokens.Count);
            Assert.AreEqual(TSQLExpressionType.Constant, arguments[0].Type);
            Assert.AreEqual(TSQLTokenType.NumericLiteral, arguments[0].AsConstant.Literal.Type);
            Assert.AreEqual(1, arguments[0].AsConstant.Literal.AsNumericLiteral.Value);

            Assert.AreEqual(2, arguments[1].Tokens.Count);
            Assert.AreEqual(TSQLExpressionType.Constant, arguments[1].Type);
            Assert.AreEqual(TSQLTokenType.NumericLiteral, arguments[1].AsConstant.Literal.Type);
            Assert.AreEqual(2, arguments[1].AsConstant.Literal.AsNumericLiteral.Value);

            TokenComparisons.CompareTokenLists(
                new List <TSQLToken>()
            {
                new TSQLWhitespace(8, " "),
                new TSQLNumericLiteral(9, "1"),
                new TSQLWhitespace(10, " "),
                new TSQLCharacter(11, ","),
                new TSQLWhitespace(12, " "),
                new TSQLNumericLiteral(13, "2"),
                new TSQLWhitespace(14, " ")
            },
                arguments.Tokens);
        }
        public void OperatorExpression_Simple()
        {
            TSQLTokenizer tokenizer = new TSQLTokenizer(
                "+ 2 - 3")
            {
                IncludeWhitespace = true
            };

            TSQLConstantExpression leftSide = new TSQLConstantExpression()
            {
                Literal = new TSQLNumericLiteral(
                    0,
                    "1")
            };

            leftSide.Tokens.Add(leftSide.Literal);

            Assert.IsTrue(tokenizer.MoveNext());

            TSQLOperatorExpression op = new TSQLOperatorExpressionParser().Parse(
                tokenizer,
                leftSide);

            Assert.AreSame(leftSide, op.LeftSide);
            TokenComparisons.CompareTokenLists(
                new List <TSQLToken>()
            {
                // bogus begin position because we made it up
                new TSQLNumericLiteral(0, "1"),

                new TSQLOperator(0, "+"),
                new TSQLWhitespace(1, " "),
                new TSQLNumericLiteral(2, "2"),
                new TSQLWhitespace(3, " "),
                new TSQLOperator(4, "-"),
                new TSQLWhitespace(5, " "),
                new TSQLNumericLiteral(6, "3")
            },
                op.Tokens);

            Assert.AreEqual("+", op.Operator.Text);

            Assert.AreEqual(TSQLExpressionType.Operator, op.RightSide.Type);
            TSQLOperatorExpression rightSide = op.RightSide.AsOperator;

            TokenComparisons.CompareTokenLists(
                new List <TSQLToken>()
            {
                new TSQLNumericLiteral(2, "2"),
                new TSQLWhitespace(3, " "),
                new TSQLOperator(4, "-"),
                new TSQLWhitespace(5, " "),
                new TSQLNumericLiteral(6, "3")
            },
                rightSide.Tokens);

            Assert.AreEqual("-", rightSide.Operator.Text);

            Assert.AreEqual(TSQLExpressionType.Constant, rightSide.LeftSide.Type);
            TSQLConstantExpression leftNumber = rightSide.LeftSide.AsConstant;

            TokenComparisons.CompareTokenLists(
                new List <TSQLToken>()
            {
                new TSQLNumericLiteral(2, "2"),
                new TSQLWhitespace(3, " ")
            },
                leftNumber.Tokens);
            Assert.AreEqual(TSQLTokenType.NumericLiteral, leftNumber.Literal.Type);
            Assert.AreEqual(2, leftNumber.Literal.AsNumericLiteral.Value);

            Assert.AreEqual(TSQLExpressionType.Constant, rightSide.RightSide.Type);
            TSQLConstantExpression rightNumber = rightSide.RightSide.AsConstant;

            TokenComparisons.CompareTokenLists(
                new List <TSQLToken>()
            {
                new TSQLNumericLiteral(6, "3")
            },
                rightNumber.Tokens);
            Assert.AreEqual(TSQLTokenType.NumericLiteral, rightNumber.Literal.Type);
            Assert.AreEqual(3, rightNumber.Literal.AsNumericLiteral.Value);
        }