Giter Site home page Giter Site logo

todb's Introduction

ToDB, a sql generation utility.

ToDB does not aim to abstract away sql, only to make it much easier to write within C#. ToDB gives you the freedom to create your own high quality queries, without the troublesome syntax errors you miss until runtime as with standard inline sql.

You must still understand sql to use this utility. It is also your responsiblity to gaurd against sql injection as this library does not completly mitigate this risk.

ToDB can be used independently, but is geared torwards use with StackExchange's Dapper.net

Basic select statement, any order is allowed

var cmd = new ToDBCommand();
cmd.From("Order")
    .Select("OrderNumber", "OrderId")
    .Where(
        where => where.AreEqual("OrderType", "@OrderType")
    );
if (getMoreInfo)
{
    cmd.Select("OrderDescription,OrderName");
}
string sql = cmd.ToSql();
select OrderNumber,OrderId,OrderDescription,OrderName from Order where OrderType=@OrderType

Join

cmd.SelectAllFrom("Order")
    .InnerJoin("Customer", on => 
        on.AreEqual("Order.CustomerId", "Customer.CustomerId")
        .And()
        .AreEqual("Order.RegionId", "Customer.RegionId")
    );
select * from Order 
	inner join Customer 
		on Order.CustomerId=Customer.CustomerId 
		and Order.RegionId=Customer.RegionId

How lucky, our model matches our database schema

var parameters = new { CompanyId = 5 };
cmd.SelectFrom<Company>()
    .Where(
        where => where.IsEqual(() => parameters.CompanyId)
    );
select CompanyId,CompanyName from Company where CompanyId=@CompanyId

Complex queries

cmd.From("Product")
    .Select("OrderNumber")
    .Select(subQuery =>
        subQuery.Select("sum(Price)").From("Product")
    , "Total")
    .Where(where =>
        where.IsEqual(() => parameters.CustomerId)
        .And(either =>  
            either.IsGreaterOrEqual(() => parameters.MinimumOder)
            .Or()
            .IsEqual(() => parameters.IsVeryImportant))
    );
select OrderNumber,(select sum(Price) from Product) Total
	from Product
	where CustomerId=@CustomerId 
		and (MinimumOrder>=@MinimumOrder or IsVeryImportant=@IsVeryImportant)

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.