OracleSqlParser.Fluent 0.1.0-preview.1

This is a prerelease version of OracleSqlParser.Fluent.
dotnet add package OracleSqlParser.Fluent --version 0.1.0-preview.1
                    
NuGet\Install-Package OracleSqlParser.Fluent -Version 0.1.0-preview.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OracleSqlParser.Fluent" Version="0.1.0-preview.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OracleSqlParser.Fluent" Version="0.1.0-preview.1" />
                    
Directory.Packages.props
<PackageReference Include="OracleSqlParser.Fluent" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OracleSqlParser.Fluent --version 0.1.0-preview.1
                    
#r "nuget: OracleSqlParser.Fluent, 0.1.0-preview.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OracleSqlParser.Fluent@0.1.0-preview.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OracleSqlParser.Fluent&version=0.1.0-preview.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=OracleSqlParser.Fluent&version=0.1.0-preview.1&prerelease
                    
Install as a Cake Tool

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 string parameter is a name, an alias, a bind name or a literal value. It is never SQL text. Only Sql.Expr parses SQL text. Sql.Col("count(*)") and Select("a; drop table t") throw ArgumentException.
  • Names follow the parser's rules. Unquoted parts are upper-cased ("e.sal" is E.SAL). Quoted parts keep their case: Sql.Col("\"Order\".\"Id\""). Invalid names ("a b", "", "1abc") throw ArgumentException.
  • Sql.Lit("O'Brien") stores the text unescaped, and the generator quotes it. Sql.Lit((string)null!) throws; use Sql.Null.
  • Sql.Lit(decimal) keeps its scale: Sql.Lit(1.10m) prints 1.10. Numbers are formatted with the invariant culture.
  • Negative numbers are a unary minus over a literal, as the parser reads -1.
  • Sql.Func refuses the names the parser reads as special forms (CAST, TRIM, EXTRACT, ...); use Sql.Expr for their syntax. It also refuses the unquoted pseudo-columns (SYSDATE, ROWNUM, USER, ...) for any argument count, since SYSDATE() is not valid Oracle. Use Sql.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 SourceEditor on an edited tree. Print the edited tree with ToSql (or SqlScriptGenerator), 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 SELECT act on its query block. On a UNION, a VALUES list or a parenthesized query, Where, From, AddColumn and the other query-block methods throw InvalidOperationException that names the body type. OrderBy, Offset, FetchFirst and With work on any body except a VALUES list. ForUpdate needs a query block.
  • Where replaces, AndWhere and OrWhere combine. An existing OR condition is grouped: a = 1 OR b = 2 with AndWhere(c = 3) becomes (a = 1 OR b = 2) AND c = 3.
  • A column is set once. Set on a column the UPDATE already assigns replaces that value. Columns and WhenMatchedUpdate throw when a column is listed twice. Names compare by their folded last part, so e.sal and sal are one column. A column inside a row-value assignment (a, b) = (...), or any SET ROW, makes Set throw.
  • FOR UPDATE and row limits. ForUpdate throws when the statement has OFFSET or FETCH, and Offset or FetchFirst throws when it has FOR UPDATE. In both cases the statement is left unchanged.

Printing and the target Oracle version

  • node.ToSql(options, target) checks the node, prints it with SqlScriptGenerator, and checks the printed text. Validate does the same checks and returns the node.
  • The check reparses the printed SQL with ParseOptions { OracleVersion = target }. Any diagnostic, including a version gate, is an InvalidOperationException that lists the messages. The reparsed tree must equal the node, or the call throws. The default target is Oracle 23. Pass OracleVersion.Oracle19 for 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-row VALUES needs 23ai, and Sql.Lit(true) throws on 19c.
  • Validate and ToSql check completeness first: an UPDATE with no Set, a MERGE with no source, ON or clause, an INSERT with neither VALUES nor a query, an empty select list, and an INSERT column list whose count differs from its query's select list. A MERGE built with Sql.MergeInto("t") but never given a source throws.
  • Validate and ToSql on a SqlScript, Statement or Expression also check the reparse. For other nodes (a WHERE or FROM clause, a table reference) Validate checks completeness only. ToSql passes them to SqlScriptGenerator, which throws NotSupportedException for a node it cannot print on its own. That exception is not caught.
  • SqlScriptGenerator.Generate does not run these checks. On a half-built tree it prints SET or ON with nothing after them. Call ToSql for 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 ANSI LEFT JOIN ... ON is printed, and ToSql accepts 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.Table throws.
  • 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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