public void Simple()
 {
   SqlTokenizer tokenizer = new SqlTokenizer("SELECT * FROM Test");
   Assert.AreEqual("SELECT", tokenizer.NextToken());
   Assert.AreEqual("*", tokenizer.NextToken());
   Assert.AreEqual("FROM", tokenizer.NextToken());
   Assert.AreEqual("Test", tokenizer.NextToken());
   Assert.IsNull(tokenizer.NextToken());
 }
 public void AnsiQuotes()
 {
     string sql = "SELECT 'a', \"a\", `a`";
       SqlTokenizer tokenizer = new SqlTokenizer(sql);
       tokenizer.AnsiQuotes = false;
       Assert.AreEqual("SELECT", tokenizer.NextToken());
       Assert.AreEqual("'a'", tokenizer.NextToken());
       Assert.IsTrue(tokenizer.Quoted);
       Assert.AreEqual(",", tokenizer.NextToken());
       Assert.AreEqual("\"a\"", tokenizer.NextToken());
       Assert.IsTrue(tokenizer.Quoted);
       Assert.AreEqual(",", tokenizer.NextToken());
       Assert.AreEqual("`a`", tokenizer.NextToken());
       Assert.IsTrue(tokenizer.Quoted);
       Assert.IsNull(tokenizer.NextToken());
 }
Beispiel #3
0
        public void ParameterWithSpecialCharacters()
        {
            string       sql       = "SELECT * FROM Test WHERE id=@id_$123";
            SqlTokenizer tokenizer = new SqlTokenizer(sql);

            tokenizer.ReturnComments = true;
            Assert.AreEqual("SELECT", tokenizer.NextToken());
            Assert.AreEqual("*", tokenizer.NextToken());
            Assert.AreEqual("FROM", tokenizer.NextToken());
            Assert.AreEqual("Test", tokenizer.NextToken());
            Assert.AreEqual("WHERE", tokenizer.NextToken());
            Assert.AreEqual("id", tokenizer.NextToken());
            Assert.AreEqual("=", tokenizer.NextToken());
            Assert.AreEqual("@id_$123", tokenizer.NextToken());
            Assert.IsNull(tokenizer.NextToken());
        }
        public void AnsiQuotes()
        {
            string       sql       = "SELECT 'a', \"a\", `a`";
            SqlTokenizer tokenizer = new SqlTokenizer(sql);

            tokenizer.AnsiQuotes = false;
            Assert.Equal("SELECT", tokenizer.NextToken());
            Assert.Equal("'a'", tokenizer.NextToken());
            Assert.True(tokenizer.Quoted);
            Assert.Equal(",", tokenizer.NextToken());
            Assert.Equal("\"a\"", tokenizer.NextToken());
            Assert.True(tokenizer.Quoted);
            Assert.Equal(",", tokenizer.NextToken());
            Assert.Equal("`a`", tokenizer.NextToken());
            Assert.True(tokenizer.Quoted);
            Assert.Null(tokenizer.NextToken());
        }
        public void SqlServerMode()
        {
            string       sql       = "SELECT `a`, [id], [name] FROM [Fact]";
            SqlTokenizer tokenizer = new SqlTokenizer(sql);

            tokenizer.SqlServerMode = true;
            tokenizer.NextToken();
            Assert.Equal("`a`", tokenizer.NextToken());
            Assert.True(tokenizer.Quoted);
            tokenizer.NextToken(); // read ,
            Assert.Equal("[id]", tokenizer.NextToken());
            Assert.True(tokenizer.Quoted);
            tokenizer.NextToken(); // read ,
            Assert.Equal("[name]", tokenizer.NextToken());
            Assert.True(tokenizer.Quoted);
            tokenizer.NextToken(); // read FROM
            Assert.Equal("[Fact]", tokenizer.NextToken());
            Assert.True(tokenizer.Quoted);
        }
        public void Parameter()
        {
            string       sql       = "SELECT * FROM Test WHERE id=@id AND id2=?id2";
            SqlTokenizer tokenizer = new SqlTokenizer(sql);

            tokenizer.ReturnComments = true;
            Assert.Equal("SELECT", tokenizer.NextToken());
            Assert.Equal("*", tokenizer.NextToken());
            Assert.Equal("FROM", tokenizer.NextToken());
            Assert.Equal("Test", tokenizer.NextToken());
            Assert.Equal("WHERE", tokenizer.NextToken());
            Assert.Equal("id", tokenizer.NextToken());
            Assert.Equal("=", tokenizer.NextToken());
            Assert.Equal("@id", tokenizer.NextToken());
            Assert.Equal("AND", tokenizer.NextToken());
            Assert.Equal("id2", tokenizer.NextToken());
            Assert.Equal("=", tokenizer.NextToken());
            Assert.Equal("?id2", tokenizer.NextToken());
            Assert.Null(tokenizer.NextToken());
        }
        public void DashSingleLineComment()
        {
            string comment = "-- this is my comment\r\n";
              string sql = String.Format("SELECT {0} * FROM Test", comment);
              SqlTokenizer tokenizer = new SqlTokenizer(sql);
              tokenizer.ReturnComments = true;
              Assert.AreEqual("SELECT", tokenizer.NextToken());
              Assert.AreEqual(comment, tokenizer.NextToken());
              Assert.AreEqual("*", tokenizer.NextToken());
              Assert.AreEqual("FROM", tokenizer.NextToken());
              Assert.AreEqual("Test", tokenizer.NextToken());
              Assert.IsNull(tokenizer.NextToken());

              tokenizer = new SqlTokenizer(sql);
              tokenizer.ReturnComments = false;
              Assert.AreEqual("SELECT", tokenizer.NextToken());
              Assert.AreEqual("*", tokenizer.NextToken());
              Assert.AreEqual("FROM", tokenizer.NextToken());
              Assert.AreEqual("Test", tokenizer.NextToken());
              Assert.IsNull(tokenizer.NextToken());
        }
        public void MultiLineComment()
        {
            string       comment   = "/* this is my comment \r\n lines 2 \r\n line 3*/";
            string       sql       = String.Format("SELECT{0} * FROM Test", comment);
            SqlTokenizer tokenizer = new SqlTokenizer(sql);

            tokenizer.ReturnComments = true;
            Assert.Equal("SELECT", tokenizer.NextToken());
            Assert.Equal(comment.Trim(), tokenizer.NextToken());
            Assert.Equal("*", tokenizer.NextToken());
            Assert.Equal("FROM", tokenizer.NextToken());
            Assert.Equal("Test", tokenizer.NextToken());
            Assert.Null(tokenizer.NextToken());

            tokenizer = new SqlTokenizer(sql);
            tokenizer.ReturnComments = false;
            Assert.Equal("SELECT", tokenizer.NextToken());
            Assert.Equal("*", tokenizer.NextToken());
            Assert.Equal("FROM", tokenizer.NextToken());
            Assert.Equal("Test", tokenizer.NextToken());
            Assert.Null(tokenizer.NextToken());
        }
        public void HashSingleLineComment()
        {
            string       comment   = "#this is my comment\r\n";
            string       sql       = String.Format("SELECT {0} * FROM Test", comment);
            SqlTokenizer tokenizer = new SqlTokenizer(sql);

            tokenizer.ReturnComments = true;
            Assert.Equal("SELECT", tokenizer.NextToken());
            Assert.Equal(comment, tokenizer.NextToken());
            Assert.Equal("*", tokenizer.NextToken());
            Assert.Equal("FROM", tokenizer.NextToken());
            Assert.Equal("Test", tokenizer.NextToken());
            Assert.Null(tokenizer.NextToken());

            tokenizer = new SqlTokenizer(sql);
            tokenizer.ReturnComments = false;
            Assert.Equal("SELECT", tokenizer.NextToken());
            Assert.Equal("*", tokenizer.NextToken());
            Assert.Equal("FROM", tokenizer.NextToken());
            Assert.Equal("Test", tokenizer.NextToken());
            Assert.Null(tokenizer.NextToken());
        }
 public void Slash()
 {
     string sql = "AND // OR";
       SqlTokenizer tokenizer = new SqlTokenizer(sql);
       tokenizer.AnsiQuotes = false;
       Assert.AreEqual("AND", tokenizer.NextToken());
       Assert.AreEqual("/", tokenizer.NextToken());
       Assert.AreEqual("/", tokenizer.NextToken());
       Assert.AreEqual("OR", tokenizer.NextToken());
       Assert.IsNull(tokenizer.NextToken());
 }
 public void Parameter()
 {
     string sql = "SELECT * FROM Test WHERE id=@id AND id2=?id2";
       SqlTokenizer tokenizer = new SqlTokenizer(sql);
       tokenizer.ReturnComments = true;
       Assert.AreEqual("SELECT", tokenizer.NextToken());
       Assert.AreEqual("*", tokenizer.NextToken());
       Assert.AreEqual("FROM", tokenizer.NextToken());
       Assert.AreEqual("Test", tokenizer.NextToken());
       Assert.AreEqual("WHERE", tokenizer.NextToken());
       Assert.AreEqual("id", tokenizer.NextToken());
       Assert.AreEqual("=", tokenizer.NextToken());
       Assert.AreEqual("@id", tokenizer.NextToken());
       Assert.AreEqual("AND", tokenizer.NextToken());
       Assert.AreEqual("id2", tokenizer.NextToken());
       Assert.AreEqual("=", tokenizer.NextToken());
       Assert.AreEqual("?id2", tokenizer.NextToken());
       Assert.IsNull(tokenizer.NextToken());
 }
        public void MultiLineComment()
        {
            string comment = "/* this is my comment \r\n lines 2 \r\n line 3*/";
              string sql = String.Format("SELECT{0} * FROM Test", comment);
              SqlTokenizer tokenizer = new SqlTokenizer(sql);
              tokenizer.ReturnComments = true;
              Assert.AreEqual("SELECT", tokenizer.NextToken());
              Assert.AreEqual(comment.Trim(), tokenizer.NextToken());
              Assert.AreEqual("*", tokenizer.NextToken());
              Assert.AreEqual("FROM", tokenizer.NextToken());
              Assert.AreEqual("Test", tokenizer.NextToken());
              Assert.IsNull(tokenizer.NextToken());

              tokenizer = new SqlTokenizer(sql);
              tokenizer.ReturnComments = false;
              Assert.AreEqual("SELECT", tokenizer.NextToken());
              Assert.AreEqual("*", tokenizer.NextToken());
              Assert.AreEqual("FROM", tokenizer.NextToken());
              Assert.AreEqual("Test", tokenizer.NextToken());
              Assert.IsNull(tokenizer.NextToken());
        }
 public void ParameterWithSpecialCharacters()
 {
     string sql = "SELECT * FROM Test WHERE id=@id_$123";
       SqlTokenizer tokenizer = new SqlTokenizer(sql);
       tokenizer.ReturnComments = true;
       Assert.AreEqual("SELECT", tokenizer.NextToken());
       Assert.AreEqual("*", tokenizer.NextToken());
       Assert.AreEqual("FROM", tokenizer.NextToken());
       Assert.AreEqual("Test", tokenizer.NextToken());
       Assert.AreEqual("WHERE", tokenizer.NextToken());
       Assert.AreEqual("id", tokenizer.NextToken());
       Assert.AreEqual("=", tokenizer.NextToken());
       Assert.AreEqual("@id_$123", tokenizer.NextToken());
       Assert.IsNull(tokenizer.NextToken());
 }
 public void ParseProcBody()
 {
     string sql = "CREATE PROCEDURE spTest(testid INT, testname VARCHAR(20)) BEGIN SELECT 1; END";
       SqlTokenizer tokenizer = new SqlTokenizer(sql);
       tokenizer.AnsiQuotes = false;
       Assert.AreEqual("CREATE", tokenizer.NextToken());
       Assert.AreEqual("PROCEDURE", tokenizer.NextToken());
       Assert.AreEqual("spTest", tokenizer.NextToken());
       Assert.AreEqual("(", tokenizer.NextToken());
       Assert.AreEqual("testid", tokenizer.NextToken());
       Assert.AreEqual("INT", tokenizer.NextToken());
       Assert.AreEqual(",", tokenizer.NextToken());
       Assert.AreEqual("testname", tokenizer.NextToken());
       Assert.AreEqual("VARCHAR", tokenizer.NextToken());
       Assert.AreEqual("(", tokenizer.NextToken());
       Assert.AreEqual("20", tokenizer.NextToken());
       Assert.AreEqual(")", tokenizer.NextToken());
       Assert.AreEqual(")", tokenizer.NextToken());
       Assert.AreEqual("BEGIN", tokenizer.NextToken());
       Assert.AreEqual("SELECT", tokenizer.NextToken());
       Assert.AreEqual("1", tokenizer.NextToken());
       Assert.AreEqual(";", tokenizer.NextToken());
       Assert.AreEqual("END", tokenizer.NextToken());
       Assert.IsNull(tokenizer.NextToken());
 }
 public void StringLiteral()
 {
     string sql = "SELECT 'a', 1, 'b'";
       SqlTokenizer tokenizer = new SqlTokenizer(sql);
       tokenizer.ReturnComments = false;
       Assert.AreEqual("SELECT", tokenizer.NextToken());
       Assert.AreEqual("'a'", tokenizer.NextToken());
       Assert.AreEqual(",", tokenizer.NextToken());
       Assert.AreEqual("1", tokenizer.NextToken());
       Assert.AreEqual(",", tokenizer.NextToken());
       Assert.AreEqual("'b'", tokenizer.NextToken());
       Assert.IsNull(tokenizer.NextToken());
 }
 public void SqlServerMode()
 {
     string sql = "SELECT `a`, [id], [name] FROM [test]";
       SqlTokenizer tokenizer = new SqlTokenizer(sql);
       tokenizer.SqlServerMode = true;
       tokenizer.NextToken();
       Assert.AreEqual("`a`", tokenizer.NextToken());
       Assert.IsTrue(tokenizer.Quoted);
       tokenizer.NextToken();  // read ,
       Assert.AreEqual("[id]", tokenizer.NextToken());
       Assert.IsTrue(tokenizer.Quoted);
       tokenizer.NextToken();  // read ,
       Assert.AreEqual("[name]", tokenizer.NextToken());
       Assert.IsTrue(tokenizer.Quoted);
       tokenizer.NextToken();  // read FROM
       Assert.AreEqual("[test]", tokenizer.NextToken());
       Assert.IsTrue(tokenizer.Quoted);
 }
 public void UserVariable()
 {
     string sql = "SELECT 'a', 1, @@myVar";
       SqlTokenizer tokenizer = new SqlTokenizer(sql);
       tokenizer.ReturnComments = false;
       Assert.AreEqual("SELECT", tokenizer.NextToken());
       Assert.AreEqual("'a'", tokenizer.NextToken());
       Assert.AreEqual(",", tokenizer.NextToken());
       Assert.AreEqual("1", tokenizer.NextToken());
       Assert.AreEqual(",", tokenizer.NextToken());
       Assert.AreEqual("@@myVar", tokenizer.NextToken());
       Assert.IsNull(tokenizer.NextToken());
 }