CSharpDB.Sql
3.0.0
Prefix Reserved
dotnet add package CSharpDB.Sql --version 3.0.0
NuGet\Install-Package CSharpDB.Sql -Version 3.0.0
<PackageReference Include="CSharpDB.Sql" Version="3.0.0" />
<PackageVersion Include="CSharpDB.Sql" Version="3.0.0" />
<PackageReference Include="CSharpDB.Sql" />
paket add CSharpDB.Sql --version 3.0.0
#r "nuget: CSharpDB.Sql, 3.0.0"
#:package CSharpDB.Sql@3.0.0
#addin nuget:?package=CSharpDB.Sql&version=3.0.0
#tool nuget:?package=CSharpDB.Sql&version=3.0.0
CSharpDB.Sql
SQL tokenizer, recursive-descent parser, and abstract syntax tree (AST) for the CSharpDB embedded database engine.
Overview
CSharpDB.Sql provides a complete SQL front-end: a single-pass tokenizer, a recursive-descent parser, and a rich AST hierarchy. It transforms SQL text into strongly-typed statement and expression objects that downstream components (query planner, execution engine) consume. Zero external dependencies.
Features
- 100+ token types covering SQL keywords, literals, identifiers,
@parameters, operators, and punctuation - Full DDL/DML parsing: CREATE/DROP/ALTER TABLE, CREATE/DROP INDEX, CREATE/DROP VIEW, CREATE/DROP TRIGGER,
ANALYZE - Identity syntax support:
IDENTITYandAUTOINCREMENTmodifiers onINTEGER PRIMARY KEYcolumns - Rich query support: SELECT with JOINs (INNER, LEFT, RIGHT, CROSS), WHERE, GROUP BY, HAVING, ORDER BY, LIMIT/OFFSET, CTEs (
WITH), set operations (UNION,INTERSECT,EXCEPT), and subquery syntax - Expression tree: binary/unary operators, LIKE, IN, BETWEEN, IS NULL, aggregate functions (COUNT, SUM, AVG, MIN, MAX with DISTINCT), scalar subqueries,
IN (SELECT ...), andEXISTS (...) - Fast-path optimizers:
TryParseSimpleSelect,TryParseSimplePrimaryKeyLookup, andTryParseSimpleInsertdetect common patterns and skip full AST construction
Usage
using CSharpDB.Sql;
// Parse a SQL statement
var statement = Parser.Parse("SELECT id, name FROM users WHERE age > 21");
// The result is a strongly-typed AST node
if (statement is SelectStatement select)
{
Console.WriteLine($"Table: {select.From}");
Console.WriteLine($"Columns: {select.Columns.Count}");
Console.WriteLine($"Has WHERE: {select.Where != null}");
}
// Parse DDL
var ddl = Parser.Parse("""
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
user_id INTEGER,
total REAL
)
""");
// Fast-path detection for simple queries
string sql = "SELECT name FROM users WHERE id = 1";
if (Parser.TryParseSimplePrimaryKeyLookup(sql, out var lookup))
{
// Skip full parsing for SELECT ... WHERE pk = value
}
JOIN Detection and Multi-Table Queries
The parser builds a recursive TableRef tree from the FROM clause. Each JOIN keyword produces a JoinTableRef node whose Left and Right children are themselves TableRef nodes, so chained joins form a left-deep binary tree:
SELECT o.id, c.name, p.title
FROM orders o
INNER JOIN customers c ON c.id = o.customer_id
LEFT JOIN products p ON p.id = o.product_id
Parses into:
JoinTableRef (LEFT JOIN, ON p.id = o.product_id)
├── Left: JoinTableRef (INNER JOIN, ON c.id = o.customer_id)
│ ├── Left: SimpleTableRef("orders", alias: "o")
│ └── Right: SimpleTableRef("customers", alias: "c")
└── Right: SimpleTableRef("products", alias: "p")
Walking the tree to collect every table involved is a simple recursive visit:
using CSharpDB.Sql;
var stmt = Parser.Parse("""
SELECT o.id, c.name, p.title
FROM orders o
INNER JOIN customers c ON c.id = o.customer_id
LEFT JOIN products p ON p.id = o.product_id
""") as SelectStatement;
// Recursively collect all tables from the FROM tree
static IEnumerable<SimpleTableRef> CollectTables(TableRef tableRef) => tableRef switch
{
SimpleTableRef simple => [simple],
JoinTableRef join => CollectTables(join.Left).Concat(CollectTables(join.Right)),
_ => [],
};
foreach (var table in CollectTables(stmt!.From))
Console.WriteLine($"Table: {table.TableName}, Alias: {table.Alias}");
// Output:
// Table: orders, Alias: o
// Table: customers, Alias: c
// Table: products, Alias: p
The execution layer uses the same recursive pattern — BuildFromOperator(TableRef) walks the tree, creates a scan operator for each SimpleTableRef, and combines them with join operators (hash join, index nested-loop join, or nested-loop fallback) at each JoinTableRef node. Column references like o.id are resolved through QualifiedMappings on the composite schema.
AST Hierarchy
Statements: CreateTableStatement, DropTableStatement, InsertStatement, SelectStatement, CompoundSelectStatement, UpdateStatement, DeleteStatement, AlterTableStatement, CreateIndexStatement, DropIndexStatement, CreateViewStatement, DropViewStatement, CreateTriggerStatement, DropTriggerStatement, AnalyzeStatement, WithStatement
Expressions: LiteralExpression, ParameterExpression, ColumnRefExpression, BinaryExpression, UnaryExpression, LikeExpression, InExpression, InSubqueryExpression, ScalarSubqueryExpression, ExistsExpression, BetweenExpression, IsNullExpression, FunctionCallExpression
Table References: SimpleTableRef, JoinTableRef (Inner, LeftOuter, RightOuter, Cross)
Installation
dotnet add package CSharpDB.Sql
For the recommended all-in-one package:
dotnet add package CSharpDB
Dependencies
CSharpDB.Primitives- shared type system and schema definitions
Related Packages
| Package | Description |
|---|---|
| CSharpDB.Execution | Query planner and operator tree that consumes this AST |
| CSharpDB.Engine | Embedded database engine |
License
MIT - see LICENSE for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- CSharpDB.Primitives (>= 3.0.0)
NuGet packages (5)
Showing the top 5 NuGet packages that depend on CSharpDB.Sql:
| Package | Downloads |
|---|---|
|
CSharpDB.Execution
Query planner, operator tree, and expression evaluator for the CSharpDB embedded database. |
|
|
CSharpDB.Engine
Lightweight embedded SQL database engine for .NET. Single-file storage, WAL durability, concurrent readers, and a typed Collection<T> NoSQL API. |
|
|
CSharpDB
All-in-one package for CSharpDB application development. Includes the unified client, engine, ADO.NET provider, and diagnostics. |
|
|
CSharpDB.Client
Unified CSharpDB client SDK with pluggable transports (Direct, HTTP, gRPC, TCP, Named Pipes). |
|
|
CSharpDB.Service
Deprecated compatibility facade over CSharpDB.Client for existing hosts. Planned for removal in v2.0.0. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.0.0 | 117 | 4/8/2026 |
| 2.9.1 | 122 | 4/7/2026 |
| 2.8.1 | 119 | 4/6/2026 |
| 2.8.0 | 116 | 4/4/2026 |
| 2.7.0 | 113 | 3/31/2026 |
| 2.6.0 | 118 | 3/29/2026 |
| 2.5.0 | 209 | 3/28/2026 |
| 2.4.0 | 114 | 3/24/2026 |
| 2.3.0 | 113 | 3/22/2026 |
| 2.2.0 | 107 | 3/21/2026 |
| 2.0.1 | 124 | 3/14/2026 |
| 2.0.0 | 112 | 3/13/2026 |
| 1.9.0 | 131 | 3/12/2026 |
| 1.8.0 | 132 | 3/11/2026 |
| 1.7.0 | 129 | 3/8/2026 |
| 1.6.0 | 123 | 3/8/2026 |
| 1.5.0 | 123 | 3/7/2026 |
| 1.4.0 | 126 | 3/7/2026 |
| 1.3.0 | 125 | 3/6/2026 |
| 1.2.0 | 121 | 3/5/2026 |