Skip to content

raminrahimzada/SQLEngine

Repository files navigation

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

About

C# SQL Engine for generating sql queries

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages