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 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 | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. 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. |
-
.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.