PetaPoco.SqlKata 1.1.5

.NET Standard 2.0 .NET Framework 4.5
There is a newer version of this package available.
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
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="PetaPoco.SqlKata" Version="1.1.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PetaPoco.SqlKata --version 1.1.5
#r "nuget: PetaPoco.SqlKata, 1.1.5"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// 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

Icon

PetaPoco.SqlKata NuGet

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
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
2.1.11 14,546 6/18/2021
2.0.2 1,142 6/1/2020
1.6.5 3,880 11/5/2019
1.6.4 528 11/1/2019
1.6.1 12,344 5/1/2019
1.5.5 1,504 4/19/2019
1.4.1 453 4/18/2019
1.3.2 2,130 1/4/2019
1.2.2 709 10/19/2018
1.1.5 593 10/18/2018