| Component | Status |
|---|---|
| AnsiSQL grammar | Production-ready — SQL-89 + FETCH FIRST, 86+ negative tests per dialect |
| MySQL grammar | Production-ready — backticks, LIMIT/OFFSET, INTERVAL, ON DUPLICATE KEY UPDATE |
| PostgreSQL grammar | In development (stub — do not use in production) |
| SQL Server grammar | In development (stub — do not use in production) |
| Query engine | Beta — SELECT with CTEs (recursive + non-recursive), JOINs, window functions, aggregates, UNION/INTERSECT/EXCEPT, ORDER BY, GROUP BY/HAVING |
SqlBuildingBlocks is an extensible open-source library for parsing SQL into manageable, logical classes tailored to different database technologies. It is built upon Irony's SQLGrammar example and leverages the Factory and Strategy patterns for customization of SQL parsing across multiple databases.
SqlBuildingBlocks breaks down SQL into fundamental "building blocks" — NonTerminal classes, each handling a specific part of the SQL language. These NonTerminal classes use a factory pattern to create logical classes that represent SQL elements.
The QueryEngine class provides in-memory SQL execution. It accepts an ITableDataProvider (or IAllTableDataProvider for multi-table queries) and a parsed SqlSelectDefinition, and returns a VirtualDataTable of result rows.
Supported SELECT features:
- Simple projections, column aliases,
SELECT * FROMtable,INNER/LEFT/RIGHT/FULL OUTER JOINWHEREclause with full predicate support (comparisons,IS NULL,LIKE,IN,BETWEEN, logical operators)GROUP BY+HAVING(aggregate predicates)ORDER BYwithASC/DESCandNULLS FIRST/NULLS LASTDISTINCT- Aggregate functions:
COUNT,SUM,AVG,MIN,MAX(includingCOUNT(DISTINCT col)) - Window functions:
ROW_NUMBER,RANK,DENSE_RANK,LAG,LEAD,NTILE,FIRST_VALUE,LAST_VALUE,NTH_VALUE;ROWS BETWEENframes;RANGE BETWEEN INTERVALframes (DateTime ORDER BY) WITH(non-recursive CTE) andWITH RECURSIVECTE; configurable depth limit viaQueryEngineOptions.MaxRecursionDepth(default 100)UNION,UNION ALL,INTERSECT,EXCEPT- Derived tables in
FROMandJOINpositions - SQL Server
TOP N - DBNull / three-valued logic in all predicates
Not yet supported (throws NotSupportedException): correlated subqueries, EXISTS, ROLLUP/CUBE/GROUPING SETS, GROUP BY ROLLUP, DML execution (INSERT/UPDATE/DELETE are parse-only).
// Parse
var grammar = new AnsiSqlGrammar();
var parser = new Parser(grammar);
var parseTree = parser.Parse("SELECT * FROM Orders WHERE Amount > 100");
// Resolve
var resolver = new SelectReferenceResolver(selectDef, connectionProvider, schemaProvider, functionProvider);
resolver.ResolveTablesDatabase();
resolver.ResolveReferences();
// Execute
var engine = new QueryEngine(tableDataProvider, selectDef);
var result = engine.QueryAsDataTable();- Extensibility: Specialized grammars for each database technology, extending a shared AnsiSQL base.
- Usability: Represent complex SQL grammar as manageable, logical classes.
- Testability: Strong unit-testing framework — 1279 passing tests across five NuGet packages.
- PostgreSQL grammar — dialect extensions beyond the current stub
- SQL Server grammar — dialect extensions beyond the current stub
- Correlated subquery execution
GROUP BY ROLLUP/CUBE/GROUPING SETSexecution
We're open to contributions from the community. Please refer to our contributing guidelines for more information.
This project is licensed under the terms of the MIT license. For more information, please see the LICENSE file.
Install via NuGet. All five packages publish at the same version on every merge to main.
| Package Name | Release (NuGet) |
|---|---|
SqlBuildingBlocks.Core |
|
SqlBuildingBlocks.Grammars.AnsiSQL |
|
SqlBuildingBlocks.Grammars.MySQL |
|
SqlBuildingBlocks.Grammars.PostgreSQL |
|
SqlBuildingBlocks.Grammars.SQLServer |
For any inquiries or issues, please open a GitHub issue.