GithubHelp home page GithubHelp logo

nhlpa / sequel Goto Github PK

View Code? Open in Web Editor NEW
18.0 4.0 3.0 170 KB

.NET SQL builder that emulates writing SQL.

License: MIT License

C# 97.66% PowerShell 2.34%
sql sql-query sql-query-builder sql-builder csharp ado-net sql-queries

sequel's Introduction

Sequel

NuGet Version build

An efficient SQL builder with an interface that emulates writing actual SQL queries.

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
*/

// SELECT with INNER & LEFT JOIN
var sqlBuilder = new SqlBuilder()
  .Select("*")
  .From("dbo.Test t")
  .Join("dbo.Employee e on e.Id = t.EmployeeId")
  .LeftJoin("dbo.Manager m on m.Id = e.ManagerId");

var sql = sqlBuilder.ToSql();

/*
SELECT * FROM dbo.Test t INNER JOIN dbo.Employee e on e.Id = t.EmployeeId LEFT JOIN dbo.Manager m on m.Id = e.ManagerId
*/

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
*/

Injecting custom SQL

You are granted pre- & post-hooks into the final SQL string literaly, for the purpose of injecting custom SQL.

The pre-hook is useful in the case of CTE's or inline declarations.

var sqlBuilder = new SqlBuilder(pre: "WITH cte AS (SELECT 1) ")
  .Select("*")
  .From("cte");

var sql = sqlBuilder.ToSql();

/*
WITH cte AS (SELECT 1) SELECT * FROM cte"
*/

The post-hook is useful for situations like obtaining the last inserted row identifier.

var sqlBuilder = new SqlBuilder(post: "; SELECT last_insert_rowid();")
  .Insert("dbo.Test")
  .Into("Name", "Salary")
  .Value("'Pim'", "50");

var sql = sqlBuilder.ToSql();

/*
INSERT INTO dbo.Test (Name, Salary) VALUES ('Pim', 50); SELECT last_insert_rowid();
*/

An example using Dapper

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 NHLPA Engineering in Toronto, ON. Licensed under MIT.

sequel's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sequel's Issues

Could not install in.net 4.5 winforms project via nuget manager

I get the following error when i try to install winforms project

Could not install package 'Sequel 1.1.5'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

Insert Failed

This insert

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

failed with the following error

BC30456 'Columns' is not a member of 'SqlBuilder'.

Mapper V2 Request

It would be really awesome if the mapper was upgraded to a "Fluent" version. E.G.

var sqlMapper = new SqlMapper();

var sqlString = sqlMapper.Select(rec => rec.Id)
.From("MockEntity")
.Where(rec => rec.Id = "1")
.ToString()

Or somehow merge it into a LINQ type statement to form the SQL string. Or maybe create an SQLBuilder that accepts a type.

var sqlQuery = new SqlBuilder()
.Select(rec => rec.Id)
.From("MockEntity")
.Where(rec => rec.Id = "1");

I really like how LINQ works, it is a shame that we cannot use that same concept here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.