Git Product home page Git Product logo

sqlengine's Introduction

SQL Engine is a query abstraction layer on relational sql databases - MS-SQL, Oracle, MySQL etc..


Library eases of generating cross-dialect sql queries without using any dialect specific syntax
You can generate any complex sql statements using Engine expressions then configure it to build queries in a specific dialect

Build Build status
Quality Quality Gate Status GitHub issues
Nuget Nuget

Database Providers:

SqlEngine.SqlServer Nuget
SqlEngine.Oracle ...
SqlEngine.MySql ...
SqlEngine.PostgreSql ...
SqlEngine.Sqlite ...

Installation

install-package SQLEngine.SqlServer
// Setup builder before using Query.New
// Maybe in static ctor ?
Query.Setup<SqlServerQueryBuilder>();

Capabilities

--wilcard column,literal,variable,functional selection syntax
--standard boolean expressions syntax
--top syntax with column or wilcard
--order by syntax
--variable declaration and set syntax
--variable set selection  syntax
--and/or - ing conditions syntax
--sub query condition syntax
--table aliasing syntax
--grouping syntax with group aggregate functions
--case then syntax

--create table syntax
--create view syntax
--create function syntax
--create procedure syntax
--create index syntax
--create database syntax
--create trigger syntax

--cursor syntax

--normal delete syntax
--delete top N syntax

--drop table,function,view,trigger,procedure syntax
--exec procedure,function syntax
--alter table add/alter/drop statements syntax
--update table statements syntax

--insert with columns and values syntax
--insert with only values syntax
--insert from select  syntax
--insert with no values syntax


Usage :

Demonstration of simple select-where query

using (var q = Query.New)
{
    var id = q.Column("Id");

    q
        .Select
        .Top(1)
        .From("Users")
        .Where(id==11)
        .OrderBy(id);
 
     var query = q.Build();
}


//or using strong typed tables
using (var q = Query.New)
{
    var id = q.Column("Id");

    q
        .Select
        .Top(1)
        .From<UserTable>()
        .Where(id==11)
	.OrderBy(id);

     var query = q.Build();
}

Above 2 queries will be like that:

SELECT TOP(1)  *
    FROM Users
    WHERE Id = 11
    ORDER BY Id

Demonstration of another select-where query

using (var q = Query.New)
{
    var age = q.Column("Age");
    var height = q.Column("Height");

    q
        .Select
        .Top(1)
        .Selector("Name")
        .Selector("Surname")
        .From("Users")
        .Where(age > 18 & height <= 1.7)
        ;
    
    var query = q.Build();
}

Above query will be like that:

SELECT TOP(1)  Name , Surname
    FROM Users
    WHERE (Age > 18) and (Height <= 1.7)

Demostration of left-right-inner joins

var age = t.Column("Age");
    t
	    .Select
            .Top(1)
            .From("Users", "U")
            .Selector("Name")
            .Selector("Surname")
            .InnerJoin("P", "Photos", "UserId")
            .LeftJoin("A", "Attachments", "UserId")
            .RightJoin("S", "Sales", "UserId")
            .Where(age > 18)            
        ;
	
  var query = q.Build();

And the result will be

SELECT TOP(1)  Name , Surname
    FROM Users AS U
	INNER JOIN	Photos AS P ON P.UserId = U.Id
	LEFT JOIN	Attachments AS A ON A.UserId = U.Id
	RIGHT JOIN	Sales AS S ON S.UserId = U.Id
    WHERE Age > 18

Demonstration of create-table query

using (var b = Query.New)
{
    b
        .Create
        .Table("Employees")
        .Columns(c => new[]
        {
            c.Long("ID").Identity(),
            c.Long("UserID")
                .ForeignKey("USERS", "ID").NotNull(),

            c.String("Name").MaxLength(50).Unique(),
            c.Decimal("Weight"),

            //custom column with all props
            c.Column("Age").Type("INT").Check("Age>1 && Age<100"),
            c.Datetime("BirthDate").DefaultValue("GETDATE()"),
            c.Bool("HasDriverLicense"),


            // two column index
            c.Int("Amount1").Unique("IX_Amount1_Amount2",descending:true),
            c.Int("Amount2").Unique("IX_Amount1_Amount2"),

            //calculated column
            c.Column("Sum").CalculatedColumn("Amount1 + Amount2"),
        });
        
        var query = q.Build();	
}

You can see other examples(insert,update,delete,drop,if-else,truncate alter and so on.) in SqlServer.Tests

sqlengine's People

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.