Koshovyi.SqlBuilder
0.2.0
dotnet add package Koshovyi.SqlBuilder --version 0.2.0
NuGet\Install-Package Koshovyi.SqlBuilder -Version 0.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Koshovyi.SqlBuilder" Version="0.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Koshovyi.SqlBuilder --version 0.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Koshovyi.SqlBuilder, 0.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Koshovyi.SqlBuilder as a Cake Addin #addin nuget:?package=Koshovyi.SqlBuilder&version=0.2.0 // Install Koshovyi.SqlBuilder as a Cake Tool #tool nuget:?package=Koshovyi.SqlBuilder&version=0.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SqlBuilder [Beta]
SqlBuilder - simple and tiny SQL builder. Most easy way to create sql queries from code for .NET Core 😃
Install
nuget install Koshovyi.SqlBuilder
Features
- Supports special database attributes and reflection;
- Supports RAW sql string (columns, subqueries, aggregation functions etc.);
- Supports all standard SQL DML queries: SELECT, DELETE, INSERT and UPDATE;
- Supports only paramterized queries for safe value escaping;
- Supports query templates;
- Supports LINQ extensions (
using SqlBuilder.Linq;
); - And many more features;
Usage - Quick Guide
string sql = new Select<Author>(Format.MsSQL)
.Columns(c =>
{
c.Append("s1", "s2", "s3");
c.FuncMin("date");
})
.Where(w =>
{
w.Equal("s1", "s2");
w.IsNotNULL("created_at");
w.IsNULL("activated");
})
.GroupBy(g =>
{
g.Append(false, "country", "city");
g.FuncCount("all", "countOfAll");
})
.OrderBy("age")
.GetSql();
/* Result:
SELECT [s1], [s2], [s3], MIN([date]), COUNT([all]) as 'countOfAll' FROM [tab_authors] WHERE [s1]=@s1 AND [s2]=@s2 AND [created_at] IS NOT NULL AND [activated] IS NULL GROUP BY [country], [city], [all] ORDER BY [age] ASC;
*/
Simple examples (DML)
Select <a id="sql_select"></a>
Insert <a id="sql_insert"></a>
- Insert columns:
string sql = new Insert(Format.MsSQL, "table")
.AppendParameters("a", "b", "c")
.GetSql();
/* Result:
INSERT INTO [table]([a], [b], [c]) VALUES(@a, @b, @c);
*/
- Insert custom columns and custom values:
string sql = new Insert(Format.MsSQL, "table")
.AppendParameters("firstName", "lastName")
.Columns("createdAt")
.Values("'NOW()'")
.GetSql();
/* Result:
INSERT INTO [table]([firstName], [lastName], [createdAt]) VALUES(@firstName, @lastName, 'NOW()');
*/
- Insert new row for <T> + default attributes:
string sql = new Insert<Author>(Format.MsSQL)
.GetSql();
/* Result:
INSERT INTO [author]([firstName], [lastName], [createdAt]) VALUES(@firstName, @lastName, 'NOW()');
*/
Delete <a id="sql_delete"></a>
- Delete all rows:
string sql = new Delete(Format.MsSQL, "table")
.GetSql();
/* Result:
DELETE FROM [table];
*/
- Delete all rows (table with alias):
string sql = new Delete(Format.MsSQL, "table", "t")
.GetSql();
/* Result:
DELETE FROM [table] as [t];
*/
- Delete row where id=@id (Parameter):
string sql = new Delete(Format.MsSQL, "table")
.Where("id")
.GetSql();
/* Result:
DELETE FROM [table] WHERE [id]=@id;
*/
- Delete row where id=123 (Value):
string sql = new Delete(Format.MsSQL, "table")
.Where(w => w.EqualValue("id", "123"))
.GetSql();
/* Result:
DELETE FROM [table] WHERE [id]=123;
*/
- Delete row <T> + where:
string sql = new Delete<Author>(Format.MsSQL, "td")
.Where(w => w.Equal("p1").Less("p2").IsNULL("p3"));
.GetSql();
/* Result:
DELETE FROM [tab_authors] as [td] WHERE [td].[p1]=@p1 AND [td].[p2]<@p2 AND [td].[p3] IS NULL;
*/
Update <a id="sql_update"></a>
- Update all rows:
string sql = new Update<Author>(Format.MsSQL)
.GetSql();
/* Result:
UPDATE [authors] SET [firstname]=@firstname, [lastname]=@lastname;
*/
- Update rows where id=@id (Parameter):
string sql = new Update<Author>(Format.MsSQL)
.Where("id")
.GetSql();
/* Result:
UPDATE [authors] SET [firstname]=@firstname, [lastname]=@lastname WHERE [id]=@id;
*/
- Update rows where id=123 (Value):
string sql = new Update<Author>(Format.MsSQL)
.Where(w => w.EqualValue("id", "123"))
.GetSql();
/* Result:
UPDATE [authors] SET [firstname]=@firstname, [lastname]=@lastname WHERE [id]=123;
*/
- Update rows where id=123 (Value):
string sql = new Update<Author>(Format.MsSQL)
.Where(w => w.EqualValue("id", "123"))
.GetSql();
/* Result:
UPDATE [authors] SET [firstname]=@firstname, [lastname]=@lastname WHERE [id]=123;
*/
Database attributes <a name="db_attributes"></a>
SqlBuilder attributes:
Attribute | Description |
---|---|
TableNameAttribute |
Set custom table name (and optionaly alias) |
ColumnAttribute |
Set custom column name |
PrimaryKeyAttribute |
Attribute for PK |
ForeignKeyAttribute |
Attribute for FK |
IgnoreInsert |
Ignore property from INSERT statement |
IgnoreUpdate |
Ignore property from UPDATE statement |
InsertDefault |
Default value for INSERT statement |
UpdateDefault |
Default value for UPDATE statement |
Reflection <a name="reflection"></a>
SqlBuilder reflection methods:
Method | Description | Attribute |
---|---|---|
GetTableName<T> | Get table name | TableNameAttribute |
GetTableAlias<T> | Get table alias | TableNameAttribute |
GetPrimaryKey<T> | Get PK from table | PrimaryKeyAttribute |
GetForeignKeys<T> | Get FK[] array from table | ForeignKeyAttribute |
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.