PetaPoco.SqlKata
1.1.5
See the version list below for details.
dotnet add package PetaPoco.SqlKata --version 1.1.5
NuGet\Install-Package PetaPoco.SqlKata -Version 1.1.5
<PackageReference Include="PetaPoco.SqlKata" Version="1.1.5" />
paket add PetaPoco.SqlKata --version 1.1.5
#r "nuget: PetaPoco.SqlKata, 1.1.5"
// Install PetaPoco.SqlKata as a Cake Addin
#addin nuget:?package=PetaPoco.SqlKata&version=1.1.5
// Install PetaPoco.SqlKata as a Cake Tool
#tool nuget:?package=PetaPoco.SqlKata&version=1.1.5
PetaPoco.SqlKata 
PetaPoco is a handy micro-ORM, but the SQL builder that comes with it is extremely limited. This library lets you use SqlKata as a replacement query builder.
Usage
Basic
using (var db = new PetaPoco.Database(...))
{
// Build any SqlKata query
var query = new Query("MyTable")
.Where("Foo", "bar");
// Use the query in place of PetaPoco.Sql
var records = db.Fetch<MyClass>(query.ToSql());
}
Note that while PetaPoco has an EnableAutoSelect
feature that lets you omit the SELECT
part of a query if your classes are set up correctly, SqlKata requires a table name in order to generate a query. If you try to use a Query
without a table name, SqlKata will throw an InvalidOperationException
when you call ToSql()
.
Generate from POCO
Since part of the benefit of PetaPoco is that it understands information embedded in a POCO, this library also has two extension methods to help do the same thing, letting you avoid retyping table and column names.
public class MyClass
{
property int ID { get; set; }
[Column("NAME_FIELD")]
property string Name { get; set; }
}
// This is equivalent to new Query("MyClass")
// If the class has a TableName property, that will be used instead.
var query = new Query().ForType<MyClass>();
// SELECT [ID], [NAME_FIELD] FROM [MyClass]
var query = new Query().GenerateSelect<MyClass>();
Both of these methods also have overloads that can take an IMapper
instance, if you don't want to use a default ConventionMapper
.
Compilers
Transforming a SqlKata Query
into a SQL string requires a compiler. SqlKata comes with compilers for SQL Server, Postgres, MySql, and Firebird. For many simple queries, the generated SQL looks the same regardless of which compiler you use, but for certain queries the compiler will produce SQL tailored for that specific database. The compilers also know which characters to use to escape identifiers.
By default, this library uses the SQL Server compiler. If you want to use a different compiler, there are a couple of different ways you can do so.
// Specify the compiler for one SQL statement
var sql = query.ToSql(CompilerType.MySql);
// Change the default compiler for all SQL statements
SqlKataExtensions.DefaultCompiler = CompilerType.Postgres;
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETFramework 4.5
- PetaPoco.Compiled (>= 6.0.316 && < 7.0.0)
- SqlKata (>= 1.0.4 && < 2.0.0)
-
.NETStandard 2.0
- PetaPoco.Compiled (>= 6.0.316 && < 7.0.0)
- SqlKata (>= 1.0.4 && < 2.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on PetaPoco.SqlKata:
Package | Downloads |
---|---|
Pantry.PetaPoco
Repositories for Root Aggregates. |
GitHub repositories
This package is not used by any popular GitHub repositories.