/Prequel

Minimal SQLCMD-Compatible Preprocessor

Primary LanguageC#ISC LicenseISC

Prequel

Prequel is a minimal SQLCMD-compatible preprocessor.

  • Adds a 'SQLCMD mode' to your project with a couple lines of code.
  • Supports GO, $(var), :setvar, and :r.
  • Does not require the SQLCMD utility to be present.

Status

Build NuGet NuGet

  • Stable: in private use for years with very few reported defects.
  • Tested: 100% coverage by automated tests.
  • Documented: IntelliSense on everything.

Installation

Install this NuGet Package in your project.

Usage

SQL in, preprocessed batches out — it's as simple as that.

// Import the namespace
using Prequel;

// Create a preprocessor
var preprocessor = new SqlCmdPreprocessor();

// Optional: set some preprocessor variables
preprocessor.Variables["Foo"] = "Bar";

// Preprocess!
var batches = preprocessor.Process(sql);

// Do something with the batches
foreach (var batch in batches)
{
    // ...
}

SQLCMD Feature Support

Prequel supports a limited subset of SQLCMD preprocessing features.

GO — batch separator

SELECT * FROM Foo; -- first batch
GO
SELECT * FROM Bar; -- second batch

$(var) — preprocessor variable expansion

SELECT $(Columns) FROM Foo;

:setvar — set a preprocessor variable

:setvar Columns Name        -- this works
:setvar Columns "Id, Name"  -- also works; required if value contains space

:r — include a file

:r OtherFile.sql     -- this works
:r "Other File.sql"  -- also works; required if path contains space

Notes

Preprocessor directives are case-insensitive.

A GO batch separator must appear the the beginning of a line. No other content may appear on that line.

A :setvar or :r directive must appear at the beginning of a line. An optional line comment may follow the directive.

$(…) may appear anywhere, including inside other preprocessor directives.