PetaPoco.SqlKata 1.1.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 and Polyglot Notebooks. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
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 48,465 6/18/2021
2.0.2 1,380 6/1/2020
1.6.5 31,316 11/5/2019
1.6.4 650 11/1/2019
1.6.1 23,890 5/1/2019
1.5.5 1,625 4/19/2019
1.4.1 587 4/18/2019
1.3.2 2,318 1/4/2019
1.2.2 839 10/19/2018
1.1.5 751 10/18/2018