QueryLens 1.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package QueryLens --version 1.1.1
                    
NuGet\Install-Package QueryLens -Version 1.1.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="QueryLens" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="QueryLens" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="QueryLens" />
                    
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 QueryLens --version 1.1.1
                    
#r "nuget: QueryLens, 1.1.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 QueryLens@1.1.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=QueryLens&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=QueryLens&version=1.1.1
                    
Install as a Cake Tool

QueryLens

Validate raw SQL at test time, not runtime.

QueryLens gives hand-written SQL a lightweight validation surface for Dapper, ADO.NET, and query-registry style codebases:

  • wrap each statement in SqlQuery, SqlInsert, SqlUpdate, SqlDelete, SqlMerge, or SqlStoredProc
  • optionally set QueryLens.Dialect
  • optionally call QueryLens.LoadDbSchemaFrom(...)
  • assert IsValid, inspect Errors, Warnings, or ErrorSummary

.NET 9 License: MIT

Quick Start

dotnet add package QueryLens

Level 1: Generic syntax with no setup

var query = new SqlQuery("SELECT name FROM customers LIMIT 10");

Assert.True(query.IsValid);

Level 1 is permissive on purpose. If you do not set a dialect, QueryLens accepts common syntax forms across supported engines and only rejects SQL that is structurally broken.

Level 2: Dialect and version checks

QueryLens.Dialect = DialectProfile.SqlServer2017;

var query = new SqlQuery("SELECT STRING_AGG(name, ',') FROM departments");

Assert.True(query.IsValid);

Once a dialect is set, QueryLens adds stricter checks such as:

  • built-in function validation
  • version-gated function availability
  • reserved-word warnings
  • dialect-specific syntax behavior

Level 3: Schema-backed validation

QueryLens.Dialect = DialectProfile.SqlServer2017;
QueryLens.LoadDbSchemaFrom(connectionString);

var query = new SqlQuery("SELECT id, missing_column FROM customers");

Assert.False(query.IsValid);
Assert.Contains(query.Errors, issue => issue.Code == "QL0011");

Schema loading snapshots the database once, then validates against that immutable in-memory snapshot. QueryLens does not hit the database on each validation call.

Core API

QueryLens.Dialect = DialectProfile.SqlServer2019;
QueryLens.LoadDbSchemaFrom(connectionString);

var query = new SqlQuery("SELECT * FROM dbo.orders WHERE order_id = @Id");

var isValid = query.IsValid;
var errors = query.Errors;
var warnings = query.Warnings;
var summary = query.ErrorSummary;

Available facade members:

  • QueryLens.Dialect
  • QueryLens.HasLoadedSchema
  • QueryLens.IsFrozen
  • QueryLens.LoadDbSchemaFrom(...)
  • QueryLens.LoadDbSchemaFromAsync(...)
  • QueryLens.RefreshDbSchema()
  • QueryLens.ClearDbSchema()
  • QueryLens.Reset()
  • QueryLens.ValidateAssembly(assembly)

Freeze Model

QueryLens uses global immutable runtime snapshots.

  • configuration is mutable until first validation
  • after first validation, QueryLens freezes
  • changing Dialect, loading schema, refreshing schema, or clearing schema throws until QueryLens.Reset()

That design keeps cached validation results deterministic and safe under parallel test execution.

Assembly Validation

Mark your SQL constants with the appropriate attribute, then validate the whole assembly in a single assertion.

public static class OrderQueries
{
    [SqlQuery]
    public static readonly string GetById = "SELECT id, total FROM orders WHERE id = @Id";

    [SqlInsert]
    public static readonly string Create = "INSERT INTO orders (id, total) VALUES (@Id, @Total)";

    [SqlUpdate]
    public static readonly string UpdateTotal = "UPDATE orders SET total = @Total WHERE id = @Id";

    [SqlDelete]
    public static readonly string Remove = "DELETE FROM orders WHERE id = @Id";
}

Then in your test project:

QueryLens.Dialect = DialectProfile.SqlServer2017;

var result = QueryLens.ValidateAssembly(typeof(OrderQueries).Assembly);

Assert.True(result.AllValid);

ValidateAssembly discovers every field, property, and method decorated with a [SqlQuery], [SqlInsert], [SqlUpdate], [SqlDelete], [SqlMerge], or [SqlStoredProc] attribute and validates each one against the active QueryLens level.

Statement Types

All statement wrappers implicitly convert to string, so they still drop into Dapper naturally.

await connection.QueryAsync<Customer>(
    new SqlQuery("SELECT * FROM customers WHERE status = @Status"),
    new { Status = "active" });

Supported wrappers:

  • SqlQuery
  • SqlInsert
  • SqlUpdate
  • SqlDelete
  • SqlMerge
  • SqlStoredProc

Validation Levels

Level Trigger What it validates
1 Default Generic SQL structure using a permissive cross-dialect superset
2 QueryLens.Dialect = ... Level 1 plus dialect/version checks
3 QueryLens.LoadDbSchemaFrom(...) Level 2 plus schema object existence and column/proc checks

Performance Model

QueryLens is designed for test-time validation:

  • statement construction is cheap
  • parsing is lazy
  • parsing happens only when validation is requested
  • parsed output is cached
  • validation results are cached
  • runtime code that never touches validation pays effectively no validation cost

Documentation

License

MIT

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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 was computed.  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
1.1.3 742 4/12/2026
1.1.3-preview.40 50 5/3/2026
1.1.2 98 4/12/2026
1.1.1 99 4/12/2026
1.1.0 103 4/12/2026
1.0.0 127 4/11/2026
1.0.0-preview.10 67 4/7/2026
1.0.0-preview.9 56 4/5/2026
1.0.0-preview.8 67 4/5/2026
1.0.0-preview.7 51 4/5/2026
1.0.0-preview.6 64 4/5/2026
1.0.0-preview.4 58 4/4/2026
1.0.0-preview.3 74 4/4/2026
1.0.0-preview.1 61 4/2/2026

v1.1.1: README fixes — added [SqlQuery]/[SqlInsert] attribute usage examples for assembly validation, fixed broken documentation links, removed confusing internal version references.