SQLParser
An SQL Parser/Lexer for C#
Install / Use
/learn @JaCraig/SQLParserREADME
SQLParser
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.
Features
- Lexical analysis: Tokenizing SQL queries into individual tokens.
- Syntactic analysis: Parsing SQL queries into an abstract syntax tree.
- Query manipulation: Modifying and transforming the parsed SQL queries.
- Query analysis: Extracting metadata and information from SQL queries.
Installation
You can install the library via NuGet:
dotnet add package SQLParser
Usage
To use this library in your C# project, follow these steps:
-
Add a reference to the
SQLParserpackage in your project. -
Import the
SQLParsernamespace 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);
Contributing
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:
- Fork the repository.
- Create a new branch for your feature/bug fix.
- Make your changes and write tests if applicable.
- Submit a pull request.
Please ensure that your code follows the existing code style and passes the tests before submitting a pull request.
License
This library is released under the Apache 2 License.
Acknowledgments
- This library was built using ANTLR (version 4).
Related Skills
oracle
339.3kBest practices for using the oracle CLI (prompt + file bundling, engines, sessions, and file attachment patterns).
prose
339.3kOpenProse VM skill pack. Activate on any `prose` command, .prose files, or OpenProse mentions; orchestrates multi-agent workflows.
Command Development
83.9kThis skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
Plugin Structure
83.9kThis skill should be used when the user asks to "create a plugin", "scaffold a plugin", "understand plugin structure", "organize plugin components", "set up plugin.json", "use ${CLAUDE_PLUGIN_ROOT}", "add commands/agents/skills/hooks", "configure auto-discovery", or needs guidance on plugin directory layout, manifest configuration, component organization, file naming conventions, or Claude Code plugin architecture best practices.
