OracleSqlParser.Fluent
0.1.0-preview.1
dotnet add package OracleSqlParser.Fluent --version 0.1.0-preview.1
NuGet\Install-Package OracleSqlParser.Fluent -Version 0.1.0-preview.1
<PackageReference Include="OracleSqlParser.Fluent" Version="0.1.0-preview.1" />
<PackageVersion Include="OracleSqlParser.Fluent" Version="0.1.0-preview.1" />
<PackageReference Include="OracleSqlParser.Fluent" />
paket add OracleSqlParser.Fluent --version 0.1.0-preview.1
#r "nuget: OracleSqlParser.Fluent, 0.1.0-preview.1"
#:package OracleSqlParser.Fluent@0.1.0-preview.1
#addin nuget:?package=OracleSqlParser.Fluent&version=0.1.0-preview.1&prerelease
#tool nuget:?package=OracleSqlParser.Fluent&version=0.1.0-preview.1&prerelease
OracleSqlParser.Fluent
Fluent builders that create and edit OracleSqlParser syntax trees. They are for two jobs: generating and modifying DML in an application, and writing expected trees in test fixtures.
The builders return ordinary parser nodes (SelectStatement, InsertStatement, ColumnReference, ...), so the generator,
SqlNodeComparer, the visitor and the rewriter all work on them unchanged.
The guarantee: every tree the builders produce is the tree the parser returns for the SQL they print. Parentheses that the
printed text needs are inserted as ParenthesizedExpression nodes, and names are read by the parser's own identifier rules.
ToSql checks this at run time, so a tree that would print as different SQL is refused.
Installing
dotnet add package OracleSqlParser.Fluent --prerelease
The package depends on OracleSqlParser. It targets net8.0 and net10.0, is marked IsAotCompatible, and uses no reflection.
Building from source
dotnet build OracleSqlParser.slnx -c Release -warnaserror
dotnet test tests/OracleSqlParser.Fluent.Tests -c Release
Add -p:CI=true to the test run to test net8.0 as well, when the .NET 8 runtime is installed.
Quick start
using OracleSqlParser;
using OracleSqlParser.Fluent;
var q = Sql.Select("e.name", "e.sal")
.From("emp", "e")
.LeftJoin("dept", "d", Sql.Col("e.dept_id").Eq(Sql.Col("d.id")))
.Where(Sql.Col("e.sal").Gt(Sql.Bind("min")))
.OrderByDesc("e.sal")
.FetchFirst(10);
string sql = q.ToSql(target: OracleVersion.Oracle19);
// SELECT e.name, e.sal FROM emp e LEFT OUTER JOIN dept d ON e.dept_id = d.id WHERE e.sal > :min ORDER BY e.sal DESC FETCH FIRST 10 ROWS ONLY
var parsed = (SelectStatement)SqlParser.ParseStatement("SELECT * FROM orders o WHERE o.status = 'OPEN'");
parsed.AndWhere(Sql.Col("o.tenant_id").Eq(Sql.Bind("tenant")));
parsed.ToSql(); // SELECT * FROM orders o WHERE o.status = 'OPEN' AND o.tenant_id = :tenant
var update = Sql.Update("emp").Set("sal", Sql.Col("sal").Times(Sql.Lit(1.1m))).Where(Sql.Col("id").Eq(Sql.Bind("id")));
update.ToSql();
Test fixtures compare a built tree with a parsed one:
Assert.True(SqlNodeComparer.StructurallyEqual(built, SqlParser.ParseStatement(expectedSql)));
Names and literals
- A
stringparameter is a name, an alias, a bind name or a literal value. It is never SQL text. OnlySql.Exprparses SQL text.Sql.Col("count(*)")andSelect("a; drop table t")throwArgumentException. - Names follow the parser's rules. Unquoted parts are upper-cased (
"e.sal"isE.SAL). Quoted parts keep their case:Sql.Col("\"Order\".\"Id\""). Invalid names ("a b","","1abc") throwArgumentException. Sql.Lit("O'Brien")stores the text unescaped, and the generator quotes it.Sql.Lit((string)null!)throws; useSql.Null.Sql.Lit(decimal)keeps its scale:Sql.Lit(1.10m)prints1.10. Numbers are formatted with the invariant culture.- Negative numbers are a unary minus over a literal, as the parser reads
-1. Sql.Funcrefuses the names the parser reads as special forms (CAST,TRIM,EXTRACT, ...); useSql.Exprfor their syntax. It also refuses the unquoted pseudo-columns (SYSDATE,ROWNUM,USER, ...) for any argument count, sinceSYSDATE()is not valid Oracle. UseSql.Col("SYSDATE")for them.
Editing parsed SQL
Edits change the node you call them on, and return it.
- In place. A parsed statement is mutated. Spans, line and column numbers, and token indexes of the edited parts are stale
afterwards. Do not use them with
SourceEditoron an edited tree. Print the edited tree withToSql(orSqlScriptGenerator), which regenerates the SQL and drops comments and original layout. - Keep the original. If you still need the parsed tree, call
DeepClone<T>()first and edit the copy:var copy = parsed.DeepClone<SelectStatement>().AndWhere(...). - Do not reuse a node. The builders insert nodes as they are passed in. Passing one expression to two places makes the
tree share it, and an edit to one place changes both. Build a new node for each place, or clone it with
DeepClone<T>(). - Shape mismatch throws. Clause methods on a
SELECTact on its query block. On aUNION, aVALUESlist or a parenthesized query,Where,From,AddColumnand the other query-block methods throwInvalidOperationExceptionthat names the body type.OrderBy,Offset,FetchFirstandWithwork on any body except aVALUESlist.ForUpdateneeds a query block. Wherereplaces,AndWhereandOrWherecombine. An existingORcondition is grouped:a = 1 OR b = 2withAndWhere(c = 3)becomes(a = 1 OR b = 2) AND c = 3.- A column is set once.
Seton a column theUPDATEalready assigns replaces that value.ColumnsandWhenMatchedUpdatethrow when a column is listed twice. Names compare by their folded last part, soe.salandsalare one column. A column inside a row-value assignment(a, b) = (...), or anySET ROW, makesSetthrow. FOR UPDATEand row limits.ForUpdatethrows when the statement hasOFFSETorFETCH, andOffsetorFetchFirstthrows when it hasFOR UPDATE. In both cases the statement is left unchanged.
Printing and the target Oracle version
node.ToSql(options, target)checks the node, prints it withSqlScriptGenerator, and checks the printed text.Validatedoes the same checks and returns the node.- The check reparses the printed SQL with
ParseOptions { OracleVersion = target }. Any diagnostic, including a version gate, is anInvalidOperationExceptionthat lists the messages. The reparsed tree must equal the node, or the call throws. The default target is Oracle 23. PassOracleVersion.Oracle19for SQL that must run on 19c, for example:Sql.InsertInto("t").Values(Sql.Lit(1)).Values(Sql.Lit(2)).ToSql(target: OracleVersion.Oracle19)throws, because multi-rowVALUESneeds 23ai, andSql.Lit(true)throws on 19c. ValidateandToSqlcheck completeness first: anUPDATEwith noSet, aMERGEwith no source,ONor clause, anINSERTwith neitherVALUESnor a query, an empty select list, and anINSERTcolumn list whose count differs from its query's select list. AMERGEbuilt withSql.MergeInto("t")but never given a source throws.ValidateandToSqlon aSqlScript,StatementorExpressionalso check the reparse. For other nodes (aWHEREorFROMclause, a table reference)Validatechecks completeness only.ToSqlpasses them toSqlScriptGenerator, which throwsNotSupportedExceptionfor a node it cannot print on its own. That exception is not caught.SqlScriptGenerator.Generatedoes not run these checks. On a half-built tree it printsSETorONwith nothing after them. CallToSqlfor built SQL.- The reparse checks what the parser checks for the target version, and nothing more. Fluent does not add its own checks on
the content of expressions you pass in. For example,
Sql.Expr("t.x(+) = u.x")in an ANSILEFT JOIN ... ONis printed, andToSqlaccepts it, although Oracle rejects an outer-join marker in an ANSI join.
Paging on both targets
OFFSET and FETCH work on Oracle 19c and 23ai, with literals or bind variables:
var page = Sql.Select("e.name").From("emp", "e").OrderBy("e.name").Offset(Sql.Bind("off")).FetchFirst(Sql.Bind("n"));
page.ToSql(target: OracleVersion.Oracle19); // SELECT e.name FROM emp e ORDER BY e.name OFFSET :off ROWS FETCH FIRST :n ROWS ONLY
page.ToSql(target: OracleVersion.Oracle23); // the same text
Not covered in v1
- DDL, PL/SQL and SQL*Plus commands.
- Database links in table names (
hr.emp@remote):Sql.Tablethrows. MERGE ... RETURNING: the parser rejects it, so there is no builder.- Clauses the parser models but the builders do not: MODEL, MATCH_RECOGNIZE, PIVOT, flashback, sampling, hierarchical queries,
error logging, hints. Build those nodes with object initializers and mix them with the fluent calls, since each is an ordinary
node. The printed text is still checked by
ToSql. - Comments and layout in edited trees. Edits regenerate through
SqlScriptGenerator, which normalises them.
Versioning
OracleSqlParser.Fluent is versioned on its own, in 0.x. Any release may change its API. It has no [Experimental] attribute.
Its public surface is listed in PublicAPI.Unshipped.txt until it ships.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. 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
- OracleSqlParser (>= 1.0.0)
-
net8.0
- OracleSqlParser (>= 1.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0-preview.1 | 53 | 11/21/2026 |