Sequel 1.1.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Sequel --version 1.1.3                
NuGet\Install-Package Sequel -Version 1.1.3                
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="Sequel" Version="1.1.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sequel --version 1.1.3                
#r "nuget: Sequel, 1.1.3"                
#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.
// Install Sequel as a Cake Addin
#addin nuget:?package=Sequel&version=1.1.3

// Install Sequel as a Cake Tool
#tool nuget:?package=Sequel&version=1.1.3                

SeQueL

NuGet Version Build Status

SeQueL is a blazing fast SQL builder with an interface that emulates writing actual SQL queries.

It comes bundled with an ISqlMapper<TEntity> which can be used to generate metadata for domain classes. Yielding:

  • Table name
  • Key column
  • Fields
  • Fields excluding key
  • Fields qualified by table name
  • Non-key fields qualified by table name
  • SqlBuilder representing CREATE / UPDATE / READ / DELETE for domain class for straight-forward CRUD application

Getting Started

SELECT

var sqlBuilder = new SqlBuilder()
  .Select("Id", "Salary")
  .From("dbo.Test");

var sql = sqlBuilder.ToSql(); // .ToString() also works

/*
SELECT Id, Salary FROM dbo.Test
*/

INSERT

var sqlBuilder = new SqlBuilder()
  .Insert("dbo.Test")
  .Columns("Name", "Salary")
  .Values("'John'", "50").Values("'Jane'", "100");

var sql = sqlBuilder.ToSql(); // .ToString() also works

/*
INSERT INTO dbo.Test (Name, Salary) VALUES ('John', 50), ('Jane', 100)
*/

UPDATE

var sqlBuilder = new SqlBuilder()
  .Update("dbo.Test")
  .Set("Salary = 100", "ManagerId = 2")
  .Where("EmployeeId = 1");

var sql = sqlBuilder.ToSql(); // .ToString() also works

/*
UPDATE dbo.Test SET Salary = 100, ManagerId = 2 WHERE EmployeeId = 1
*/

DELETE

var sqlBuilder = new SqlBuilder()
  .Delete()
  .From("dbo.Test")
  .Where("EmployeeId = 1");

var sql = sqlBuilder.ToSql(); // .ToString() also works

/*
DELETE FROM dbo.Test WHERE EmployeeId = 1
*/

Using ISqlMapper<TEntity>

The default implementation SqlMapper<TEntity> will extract it's metadata using System.Type and fast-member. The table name is derived from the System.Type.Name field, Id is assumed as the key (don't worry this can easily be overriden), and field data is drawn from a fast-member MemberSet which includes all public, writeable and primitive types.

The fields array always returns the SqlMapper.Key as the first item, to make integrations with things like dapper's multi mapping feature easier.

The example below is taking directly from SqlMapperTests.cs. For more in-depth information, review that file.

public class MockEntity
{
  public int Id { get; set; }
  public string Name { get; set; }
  public int Cents { get; set; }

  public double Dollars => Cents * 100;

  public IEnumerable<MockChildEntity> Children { get; set; }
}

public class MockChildEntity
{
  public int Id { get; set; }
  public int ParentId { get; set; }
  public string Name { get; set; }
}

var sqlMapper = new SqlMapper<MockEntity>();

sqlMapper.Table; // "MockEntity"
sqlMapper.Key; // "Id"
sqlMapper.Fields // "Id", "Name", "Cents" (Key field is always first)
sqlMapper.NonKeyFields // "Name", "Cents"
sqlMapper.CreateSql.ToSql(); // INSERT INTO MockEntity (Cents, Name) VALUES (@Cents, @Name)
sqlMapper.ReadSql.ToSql(); // SELECT MockEntity.Id, MockEntity.Cents, MockEntity.Name FROM MockEntity
sqlMapper.UpdateSql.ToSql(); // UPDATE MockEntity SET Cents = @Cents, Name = @Name WHERE Id = @Id
sqlMapper.DeleteSql.ToSql(); // DELETE FROM MockEntity WHERE Id = @Id

.NET Core Integration

Using the SQL Mapper directly out of the box with the .NET Core IoC is dead simple. Simply register it within the ConfiureServices() method of your Startup.cs.

services.AddSingleton(typeof(ISqlMapper<>), typeof(SqlMapper<>));

Despite fast-member's far surperior performance to reflection, it is still recommended to impose the singleton pattern given that there is overhead to generating the required metadata.

Runtime customization

SqlMapper<TEntity> can be customized at run. Configurable properties at runtime include:

  • string Table
  • string Key
  • string[] Fields (NonKeyFields will be derived from this)
public class MockEntity
{
  public int UID { get; set; }
  public string Name { get; set; }
  public int Cents { get; set; }
}

var fields = new string[] { "UID", "Name", "Cents" };
var sqlMapper = new SqlMapper<MockEntity>("MockEntityTbl", "UID", fields);

Complete Customization

For complete control you have two options:

  1. Inherit from SqlMapper<TEntity> and override any of the available virtual fields.
  2. Implement your own ISqlMapper<TEntity> from scratch.

Custom Templating

sequel has built-in templates to support the actions listed above, but also supports custom templating for edge cases. To formulate the SQL string, sequel uses || delimiters surrounding the following keywords:

  • ||select||
  • ||from||
  • ||join||
  • ||where||
  • ||groupby||
  • ||having||
  • ||orderby|
  • ||insert||
  • ||columns||
  • ||values|
  • ||update||
  • ||set||
  • ||delete||

To use SqlBuilder with a custom template, provide the template when constructing new SqlBuilder("YOUR CUSTOM TEMPLATE").

The default templates are as follows:

Select

||select|| ||from|| ||join|| ||where|| ||groupby|| ||having|| ||orderby||

Insert

||insert|| ||columns|| ||values||

Update

||update|| ||set|| ||where||

Delete

||delete|| ||from|| ||join|| ||where||

Usage with dapper.net

using(var conn = new SqlConnection("your connection string")
{
  var sqlBuilder = new SqlBuilder()
    .Select("Id", "Salary")
    .From("dbo.Test")
    .Where("Id", "@Id");

  var sql = sqlBuilder.ToSql(); // .ToString() also works 
  /*
  SELECT Id, Salary FROM dbo.Test WHERE Id = @Id
  */ 
    
  var result = conn.Query(sql, new { Id = 1 });
}

Find a bug?

There's an issue for that.

License

Built with ♥ by Pim Brouwers in Toronto, ON. Licensed under GNU.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Sequel:

Package Downloads
LunchPail.Repository

A .NET Standard compliant abstract repository for use with LunchPail.

NBean

Hybrid-ORM for .Net

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.4 1,691 9/16/2022
3.1.3 419 9/16/2022
3.1.2 396 9/16/2022
3.1.1 468 7/31/2021
3.1.0 493 12/4/2020
3.0.2 421 11/30/2020
3.0.1 475 11/15/2020
3.0.0 367 11/14/2020
2.0.0 1,361 10/28/2019
1.1.5 789 3/20/2019
1.1.4 619 3/5/2019
1.1.3 1,097 8/31/2018
1.1.2 786 8/24/2018
1.1.1 773 8/24/2018
1.1.0 762 8/23/2018
1.0.6 776 8/23/2018
1.0.5 2,303 8/21/2018
1.0.4 1,048 7/26/2018
1.0.3 800 7/25/2018
1.0.2 894 5/5/2018
1.0.1 904 5/5/2018
1.0.0 821 5/2/2018

Patch to remove value mutation on SelectWithAlias & OrderByDesc iterations