Getting Started with EricksonLopez.SqlBuilder
Install EricksonLopez.SqlBuilder, a dialect compiler package such as EricksonLopez.SqlBuilder.PostgreSql, and EricksonLopez.SqlBuilder.SourceGenerators (2.0.0 on NuGet).
dotnet add package EricksonLopez.SqlBuilder --version 2.0.0<PackageReference Include="EricksonLopez.SqlBuilder" Version="2.0.0" /><PackageVersion Include="EricksonLopez.SqlBuilder" Version="2.0.0" />dotnet add package EricksonLopez.SqlBuilder.PostgreSql --version 2.0.0dotnet add package EricksonLopez.SqlBuilder.Aot --version 2.0.0dotnet add package EricksonLopez.SqlBuilder.SourceGenerators --version 2.0.0dotnet add package EricksonLopez.SqlBuilder.Analyzers --version 2.0.0Quick Start: Compile Immutable SelectQuery<T> to SqlResult
Section titled “Quick Start: Compile Immutable SelectQuery<T> to SqlResult”Annotate a partial entity with [SqlEntity] and [Indexed] (ESQL007 validates indexed sort keys), then compile with PostgreSqlCompiler:
using System;using EricksonLopez.SqlBuilder;using EricksonLopez.SqlBuilder.Abstractions;using EricksonLopez.SqlBuilder.Annotations;using EricksonLopez.SqlBuilder.PostgreSql;
namespace Example.SqlBuilderQuickStart;
[SqlEntity("orders")]public sealed partial class OrderRecord{ [Indexed] public long Id { get; set; } public long CustomerId { get; set; } public string Status { get; set; } = string.Empty; public decimal Total { get; set; }}
public static class SqlBuilderUsage{ public static SqlResult BuildSelect() { var compiler = new PostgreSqlCompiler();
SelectQuery<OrderRecord> activeOrders = Sql.From<OrderRecord>() .Where(o => o.Status == "Confirmed") .OrderByDescending(o => o.Id) .Limit(20);
SqlResult result = compiler.Compile(activeOrders); if (string.IsNullOrWhiteSpace(result.Sql)) { throw new InvalidOperationException("Compiled SQL cannot be empty."); }
return result; }}