Table of Contents

Class SqlCmdParser

Namespace
ItTiger.TigerQuery
Assembly
ItTiger.TigerQuery.dll
Incrementally tokenizes and parses SQL scripts with optional sqlcmd directives.
public sealed class SqlCmdParser
Inheritance
SqlCmdParser
Inherited Members

Remarks

ReadBatchesAsync(CancellationToken) recognizes GO separators in every SqlCmdMode. In sqlcmd modes it also expands $(name), processes :setvar, and applies :ON ERROR EXIT or :ON ERROR IGNORE to the shared execution context.

In sqlcmd modes the parser also recognizes and validates the :Out and :Error output directives, which previously failed as unknown commands. They are represented internally as ordered execution steps that only TigerQueryEngine consumes; ReadBatchesAsync(CancellationToken) projects them away and still returns batches only.

Parsing validates TigerQuery/sqlcmd structure only. It does not validate T-SQL. The parser consumes its TextReader and is not thread-safe.

Constructors

SqlCmdParser(TextReader, TigerQueryEngineOptions, QueryExecutionContext)

Initializes a parser over a text reader and execution context.
public SqlCmdParser(TextReader reader, TigerQueryEngineOptions options, QueryExecutionContext context)

Parameters

reader TextReader
The script source. The parser does not dispose it.
options TigerQueryEngineOptions
The parsing and variable options.
context QueryExecutionContext
The mutable context that receives variable and :ON ERROR state.

Exceptions

ArgumentNullException
reader, options, or context is null.

Methods

ReadBatchesAsync(CancellationToken)

Asynchronously yields logical SQL batches in source order.
public IAsyncEnumerable<SqlBatch> ReadBatchesAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A token observed throughout parser enumeration.

Returns

IAsyncEnumerable<SqlBatch>
An asynchronous stream of parser-produced batches.

Remarks

Variables are expanded as text is parsed. Undefined ordinary variables remain as their literal $(name) references. A variable used as a GO count must expand to one valid int value.

A directive after buffered SQL and before its terminating GO updates the context before that batch is yielded. Consumers that defer execution must therefore snapshot relevant context state for each yielded batch.

Recognized :Out and :Error directives are validated but not returned by this method. Consumers that need output routing must execute through TigerQueryEngine.

Exceptions

OperationCanceledException
cancellationToken is cancelled during enumeration.
TigerQueryException
A sqlcmd directive, GO count, quoted section, or comment is malformed.

ReadElement()

Reads the next lexical element from the script.
public SqlElement? ReadElement()

Returns

SqlElement
The next element, or null at end of input.

Exceptions

TigerQueryException
A quoted section or multiline comment is unterminated, or parser state is invalid.