Skip to content

emir57/.Net-Query-Generator

Repository files navigation

WriteParameter

Query Generator Tool

Supported Databases:

  • MsSql
  • PostgreSql

Features
  • Finding Automatic Id Column

Attributes

[TableName("")]
For Table Name Column
  [TableName("Countries")]
   public sealed class Country
   {
       .
       .
       .
   }
[IdColumn]
For Select Id Column
  [IdColumn]
  public int CountryId { get; set; }
[IgnoreColumn]
For Ignore Column
  [IgnoreColumn]
  public string Currency { get; set; }
[ColumnName("")]
For Different Column Name
  [ColumnName("Name")]
  public string CountryName { get; set; }

Using

Example Table Model
[TableName("Countries")]
public sealed class Country
{
   [IdColumn]
   public int CountryId { get; set; }
   [ColumnName("Name")]
   public string CountryName { get; set; }
   public string Continent { get; set; }
   public string Currency { get; set; }
   [IgnoreColumn]
   public string CountryNameAndContinent
       => String.Format("{0} - {1}", CountryName, Continent);
}

Insert

Insert Query For MsSql
string query = new MsSqlQueryGenerate<Country>()
        .GenerateInsertQuery();
+Output: "insert into dbo.Country (CountryName,Continent) values (@CountryName,@Continent)"

Insert Query For PostgreSql

string query = new NpgQueryGenerate<Country>()
        .GenerateInsertQuery();
+Output: "insert into dbo.Country (\"CountryName\",\"Continent\") values (@CountryName,@Continent)"

Using With Dapper

Country country = new() { CountryName = "TURKEY", Continent = "ASIA", Currency = "TRY" };
int row = await conn.ExecuteAsync(
    query,
    country);

Update

Update Query For MsSql
string query = new MsSqlQueryGenerate<Country>()
        .GenerateUpdateQuery();
+Output: "update dbo.Country set CountryName=@CountryName,Continent=@Continent where CountryId=@CountryId"

Update Query For PostgreSql

string query = new NpgQueryGenerate<Country>()
        .GenerateUpdateQuery();
+Output: "update dbo.Country set \"CountryName\"=@CountryName,\"Continent\"=@Continent where \"CountryId\"=@CountryId"

Using With Dapper

Country country = new() { CountryId = 1, CountryName = "TURKEY", Continent = "ASIA", Currency = "TRY" };
int row = await conn.ExecuteAsync(
    query,
    country);

Delete

Delete Query For MsSql
string query = new MsSqlQueryGenerate<Country>()
        .GenerateDeleteQuery();
+Output: "delete from dbo.Country where CountryId=@CountryId"

Delete Query For PostgreSql

string query = new NpgQueryGenerate<Country>()
        .GenerateDeleteQuery();
+Output: "delete from dbo.Country where \"CountryId\"=@CountryId"

Using With Dapper

Country country = new() { CountryId = 1 };
int row = await conn.ExecuteAsync(
    query,
    country);

Get All

Select Query For MsSql
string query = new MsSqlQueryGenerate<Country>()
        .GenerateGetAllQuery();
+Output: "select CountryId as CountryId,CountryName as CountryName,Continent as Continent,Currency as Currency from dbo.Country order by CountryId"

Select Query For PostgreSql

string query = new NpgQueryGenerate<Country>()
        .GenerateGetAllQuery();
+Output: "select \"CountryId\" as CountryId,\"CountryName\" as CountryName,\"Continent\" as Continent,\"Currency\" as Currency from dbo.BaseModel order by \"CountryId\""

Using With Dapper

var result = await conn.QueryAsync<Country>(query);

Get By Id

Select Where Query For MsSql
string query = new MsSqlQueryGenerate<Country>()
        .GenerateGetByIdQuery(1);
+Output: "select CountryId as CountryId,CountryName as CountryName,Continent as Continent,Currency as Currency from dbo.Country where CountryId=1"

Select Where Query For PostgreSql

string query = new NpgQueryGenerate<Country>()
        .GenerateGetByIdQuery(1);
+Output: "select \"CountryId\" as CountryId,\"CountryName\" as CountryName,\"Continent\" as Continent,\"Currency\" as Currency from dbo.Country where \"CountryId\"=1"

Using With Dapper

var result = await conn.QuerySingleOrDefaultAsync<Country>(query);

About

Query Generator Tool For .Net

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages