An SQL Parser/Lexer for C#
This C# library provides a SQL parser and lexer implementation using ANTLR. It allows you to parse SQL queries into an abstract syntax tree (AST) and perform various operations on the parsed queries.
You can install the library via NuGet:
dotnet add package SQLParser
To use this library in your C# project, follow these steps:
Add a reference to the SQLParser
package in your project.
Import the SQLParser
namespace in your code:
using SQLParser.Parsers.TSql;
using SQLParser;
Create a parser listener class:
/// <summary>
/// This is an example of a printer that can be used to parse a statement.
/// </summary>
/// <seealso cref="TSqlParserBaseListener" />
internal class Printer : TSqlParserBaseListener
{
/// <summary>
/// Gets or sets a value indicating whether [statement found].
/// </summary>
/// <value>
/// <c>true</c> if [statement found]; otherwise, <c>false</c>.
/// </value>
public bool StatementFound { get; set; }
/// <summary>
/// Enter a parse tree produced by <see cref="M:SQLParser.Parsers.TSql.TSqlParser.dml_clause" />.
/// <para>The default implementation does nothing.</para>
/// </summary>
/// <param name="context">The parse tree.</param>
public override void EnterDml_clause([NotNull] TSqlParser.Dml_clauseContext context)
{
// This is a select statement if the select_statement_standalone is not null
StatementFound |= context.select_statement_standalone() != null;
base.EnterDml_clause(context);
}
}
Parse the query:
Parser.Parse("SELECT * FROM Somewhere", ExamplePrinter, Enums.SQLType.TSql);
Contributions are welcome! If you encounter any bugs, issues, or have feature requests, please create an issue on this repository.
If you want to contribute to the codebase, follow these steps:
Please ensure that your code follows the existing code style and passes the tests before submitting a pull request.
This library is released under the Apache 2 License.