GithubHelp home page GithubHelp logo

soundarkarunagaran / sqlmodeller Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jmenziessmith/sqlmodeller

0.0 1.0 0.0 1.33 MB

Sql Statement Builder

License: MIT License

Batchfile 0.10% C# 99.90%

sqlmodeller's Introduction

SqlModeller

Sql Query Builder

NuGet

PM> Install-Package SqlModeller

Example

var countryTable = new Table("c", "Country");
var teamTable = new Table("t", "Team");
var playerTable = new Table("p", "Player");

var cteQuery = new SelectQuery()

// SELECT
    .SelectRowNumber("_ROW")
        .SelectCount("_COUNT")
        .SelectGroupKey("_GROUP_KEY")
        .Select("1 as ONE")
        .SelectAll()
        .Select(countryTable, "ID", "C_ID", Aggregate.Min) // should not aggregate, its the group by
        .Select(teamTable, "ID", "T_ID", Aggregate.Min)
        .Select(teamTable, "Name", "T_NAME", Aggregate.Avg)
        .Select(playerTable, "Name", "P_NAME", Aggregate.Sum)
// FROM
    .From(countryTable)
        .LeftJoin(teamTable, "CountryID", countryTable, "ID")
        .Join(playerTable, "TeamID", teamTable, "ID", JoinType.InnerJoin, "AND 1 = 1")
// WHERE
    .Where(Combine.And)
        .Where("p.Name IS NOT NULL")
        .WhereColumnLike(teamTable, "Name", "F.C.", LikeMode.WildcardLeft)
        .WhereColumnLike(teamTable, "Description", "\"premier league\" -winners", LikeMode.FreeText)
        .WhereColumnColumn(teamTable, "ID", Compare.NotEqual, countryTable, "ID")
        .WhereColumnValue(playerTable, "FirstName", Compare.NotEqual, "Peter")
        .WhereColumnValue(playerTable, "StartDate", Compare.NotEqual, DateTime.Now)
        .WhereCollection(Combine.Or, new WhereFilterCollection()
            .WhereColumnColumn(teamTable, "Value1", Compare.GreaterThan, countryTable, "Value2")
            .WhereColumnValue(teamTable, "Value3", Compare.LessThan, 1)
        )
// GROUP BY
    .GroupBy(countryTable, "ID")
// Having
    .Having(Combine.And)
        .Having("SUM(t.Points) > 4")
        .HavingColumnValue(Aggregate.Sum, playerTable, "Goals", Compare.GreaterThan, 10)
        .HavingCollection(Combine.Or,new HavingFilterCollection()
            .HavingColumnValue(Aggregate.Min, playerTable, "RedCards", Compare.GreaterThan, 1)
            .HavingColumnValue(Aggregate.Max, playerTable, "RedCards", Compare.LessThan, 5)
        )
// ORDER BY
    .OrderBy(countryTable, "ID", OrderDir.Asc)
           .OrderByDesc(playerTable, "ID");

var cte = new CommonTableExpression()
          {
              Alias = "cte1",
              Query = cteQuery
          };


var query = new Query();
query.CommonTableExpressions.Add(cte);
query.SelectQuery = new SelectQuery()
    .SelectRowNumber("_ROW")
    .SelectTotal("_TOTAL_ROWS", 0)
    .SelectAll()
    .From(cte.Alias, cte.Alias)
    .OrderBy(cte.Alias, "_ROW")
    .Offset(10)
    .Fetch(5);

var compiled = query.Compile();

Console.WriteLine(compiled.ParameterSql);
Console.WriteLine(compiled.Sql);

sqlmodeller's People

Contributors

advertisingstudio avatar jmenziessmith avatar

Watchers

Soundararajan Karunagaran avatar

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.