TylerBrinks / SqlParser-cs

A Friendly SQL Parser for .NET
MIT License
108 stars 19 forks source link

Fails with do-block in postgresql #42

Closed ststeiger closed 1 month ago

ststeiger commented 1 month ago

Hey, nice project ! Unfortunately, it fails if there is a do-block in PostgreSQL:

DO $$
BEGIN
    RAISE NOTICE 'This is an informational message';
END;
$$ LANGUAGE plpgsql;

SELECT * FROM pg;

Otherwise, it looks like it works fine. Just checked, it also fails if I specify the dialect explicitly

SqlParser.Sequence<SqlParser.Ast.Statement> ast = 
    new SqlParser.Parser().ParseSql(real 
    , new SqlParser.Dialects.PostgreSqlDialect()
    ,new SqlParser.ParserOptions() 
);

Here's my test-case:


namespace Testing
{

    internal class TestParser
    {

        public static void Test()
        {

            string real = @"
DO $$
BEGIN
    RAISE NOTICE 'This is an informational message';
END;
$$ LANGUAGE plpgsql;

SELECT * FROM pg;
";

            string sql = @"
-- i am a comment
SELECT '-- I am not a comment' AS abc, * FROM my_table_1;
/* i am also a comment */
SELECT '/* i am also not a comment */' AS abc, * FROM my_table_2;
        ";

            SqlParser.Sequence<SqlParser.Ast.Statement> ast = 
                new SqlParser.Parser().ParseSql(real);

            SqlParser.Ast.Statement[] statements = ast.ToArray();
            foreach (SqlParser.Ast.Statement statement in statements)
            {
                string a = statement.ToString();

                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                SqlParser.SqlTextWriter sw = new SqlParser.SqlTextWriter(sb);
                statement.ToSql(sw);
                string stat = sb.ToString();
                System.Console.WriteLine(stat);
            } // Next statement 

        } // End Sub Test 

    } // End Class TestParser 

} // End Namespace 
TylerBrinks commented 1 month ago

This feature isn't currently part of the Rust project. Feel free to submit an issue there and it will be picked up as part of the maintenance of this codebase. https://github.com/sqlparser-rs/sqlparser-rs