Skip to content

A Roslyn source generator that reads SQL files at compile time and generates a C# class with const string values for each SQL file content.

Notifications You must be signed in to change notification settings

CoreyKaylor/ConstantSQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ConstantSQL

A Roslyn source generator that reads SQL files at compile time and generates a C# class with const string values for each SQL file content. This gives you the best of both worlds, direct C# constants but a SQL file with SQL syntax support with formatting and suggestions applicable to SQL without requiring special IDE integration.

Installation

dotnet add package ConstantSQL

Usage

ConstantSQL supports two ways to organize your SQL files:

Single-Statement Files

One SQL query per file - the constant is named after the file:

GetUsers.sql:

SELECT * FROM Users WHERE Active = 1

Generated code:

public const string GetUsers = @"SELECT * FROM Users WHERE Active = 1";

Multi-Statement Files

Multiple named SQL queries in a single file using -- @name: markers:

UserQueries.sql:

-- @name: GetActiveUsers
SELECT Id, Name, Email 
FROM Users 
WHERE Active = 1;

-- @name: GetUserById
SELECT Id, Name, Email, CreatedDate
FROM Users 
WHERE Id = @UserId;

-- @name: UpdateUserEmail
UPDATE Users 
SET Email = @Email, ModifiedDate = GETUTCDATE()
WHERE Id = @UserId;

Generated code:

public const string GetActiveUsers = @"SELECT Id, Name, Email 
FROM Users 
WHERE Active = 1;";

public const string GetUserById = @"SELECT Id, Name, Email, CreatedDate
FROM Users 
WHERE Id = @UserId;";

public const string UpdateUserEmail = @"UPDATE Users 
SET Email = @Email, ModifiedDate = GETUTCDATE()
WHERE Id = @UserId;";

Using the Generated Constants

using ConstantSQL.SqlFiles;

// From single-statement file
var query1 = Queries.GetUsers;

// From multi-statement file
var query2 = Queries.GetActiveUsers;
var query3 = Queries.UpdateUserEmail;

Configuration

By default, the source generator looks for all *.sql files in your project:

<ItemGroup>
  <!-- Default: includes all SQL files -->
  <SqlFiles Include="**\*.sql" />
</ItemGroup>

Features

  • ✅ Compile-time SQL file processing
  • ✅ Strongly-typed access to SQL queries
  • ✅ IntelliSense support
  • ✅ No runtime file I/O
  • ✅ Automatic handling of special characters and escaping
  • ✅ Support for files with duplicate names (with automatic suffixing)
  • ✅ Single-statement files (one query per file)
  • ✅ Multi-statement files (multiple named queries per file using -- @name: markers)

Example

Given a SQL file GetActiveUsers.sql:

SELECT Id, Name, Email 
FROM Users 
WHERE Active = 1
ORDER BY Name

The generator creates:

namespace ConstantSQL.SqlFiles
{
    public static class Queries
    {
        public const string GetActiveUsers = @"SELECT Id, Name, Email 
FROM Users 
WHERE Active = 1
ORDER BY Name";
    }
}

License

Apache 2.0

About

A Roslyn source generator that reads SQL files at compile time and generates a C# class with const string values for each SQL file content.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages