CSharpDB.Sql
4.6.5
Prefix Reserved
See the version list below for details.
dotnet add package CSharpDB.Sql --version 4.6.5
NuGet\Install-Package CSharpDB.Sql -Version 4.6.5
<PackageReference Include="CSharpDB.Sql" Version="4.6.5" />
<PackageVersion Include="CSharpDB.Sql" Version="4.6.5" />
<PackageReference Include="CSharpDB.Sql" />
paket add CSharpDB.Sql --version 4.6.5
#r "nuget: CSharpDB.Sql, 4.6.5"
#:package CSharpDB.Sql@4.6.5
#addin nuget:?package=CSharpDB.Sql&version=4.6.5
#tool nuget:?package=CSharpDB.Sql&version=4.6.5
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. It has no third-party 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 - Safe query correlation: tokenizer-backed normalization replaces literals and parameters before producing a versioned deterministic fingerprint
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
}
// Correlate equivalent query shapes without retaining literals or parameters.
var fingerprint = SqlQueryNormalizer.CreateFingerprint(
"SELECT name FROM users WHERE id = 42");
NormalizeAndFingerprint also returns normalized SQL. Normalized text can
retain identifiers, so applications should treat it as an explicit diagnostics
capture mode rather than safe default log content. Parameter and literal values
are replaced before normalized text or hash input is constructed.
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, ExplainEstimateStatement, 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 definitionsCSharpDB.Observability- versioned query-fingerprint contract
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.Observability (= 4.6.5)
- CSharpDB.Primitives (= 4.6.5)
NuGet packages (6)
Showing the top 5 NuGet packages that depend on CSharpDB.Sql:
| Package | Downloads |
|---|---|
|
CSharpDB
All-in-one package for CSharpDB application development. Includes the unified client, engine, ADO.NET provider, and diagnostics. |
|
|
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.Client
Unified CSharpDB client SDK with Direct, HTTP and gRPC transports. |
|
|
CSharpDB.Migration.DualRun
Bounded, deterministic source-to-CSharpDB query result validation for migration cutovers. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 5.0.0 | 157 | 11/19/2026 |
| 4.7.1 | 221 | 11/8/2026 |
| 4.7.0 | 271 | 10/28/2026 |
| 4.6.6 | 248 | 10/12/2026 |
| 4.6.5 | 242 | 9/28/2026 |
| 4.6.4 | 306 | 9/12/2026 |
| 4.6.3 | 222 | 9/5/2026 |
| 4.6.2 | 247 | 8/14/2026 |
| 4.5.1 | 207 | 8/10/2026 |
| 4.4.0 | 210 | 8/5/2026 |
| 4.3.0 | 215 | 7/27/2026 |
| 4.2.0 | 220 | 7/21/2026 |
| 4.1.0 | 204 | 7/18/2026 |
| 4.0.4 | 205 | 7/17/2026 |
| 4.0.3 | 198 | 7/15/2026 |
| 4.0.2 | 206 | 7/9/2026 |
| 4.0.1 | 201 | 7/5/2026 |
| 4.0.0 | 209 | 6/25/2026 |
| 3.9.1 | 235 | 6/11/2026 |
| 3.9.0 | 197 | 5/31/2026 |