QueryBuilder4DelphiGitHub ↗

FLUENT SQL FOR DELPHI 12+

Write queries, not SQL strings.

QueryBuilder4Delphi builds parameterized SQL for six databases from one fluent Delphi API. Values become bindings, never concatenated text. A port of the .NET library SqlKata.

v2.0.0 adds object mapping, nullable DTOs and explicit NULL policies.

Example

Delphi · optional filters and paging
function SearchBooks(const Title, Author: string; PublishedOnly: Boolean;
  Page, PageSize: Integer): IQuery;
begin
  Result := TQuery.Create('Books as b')
    .Join('Authors as a', 'a.Id', 'b.AuthorId')
    .Select(['b.Id', 'b.Title', 'a.Name as AuthorName'])
    .When(Title <> '',
      function(Q: IQuery): IQuery
      begin
        Result := Q.WhereContains('b.Title', Title);
      end)
    .When(Author <> '',
      function(Q: IQuery): IQuery
      begin
        Result := Q.WhereContains('a.Name', Author);
      end)
    .When(PublishedOnly,
      function(Q: IQuery): IQuery
      begin
        Result := Q.WhereTrue('b.IsPublished');
      end)
    .OrderBy('b.Title').OrderBy('b.Id')
    .ForPage(Page, PageSize);
end;
Generated SQL · SQL Server dialect
SELECT [b].[Id], [b].[Title], [a].[Name] AS [AuthorName]
FROM [Books] AS [b]
INNER JOIN [Authors] AS [a] ON [a].[Id] = [b].[AuthorId]
WHERE [b].[Title] like @p0 AND [a].[Name] like @p1
  AND [b].[IsPublished] = cast(1 as bit)
ORDER BY [b].[Title], [b].[Id]
OFFSET @p2 ROWS FETCH NEXT @p3 ROWS ONLY

Parameterized by default

Compile returns SQL with placeholders plus ordered bindings. A diagnostic preview with substituted values is a separate, explicit call.

Six dialects, one API

SQL Server, PostgreSQL, MySQL, SQLite, Oracle and Firebird. Quoting, placeholders and paging follow the selected compiler.

Objects in, objects out

Map rows to classes with ToList, First or Single, write DTOs back, and execute through FireDAC or your own provider.

Up and running in three steps

  1. Add src to the Delphi search path. The core needs only the RTL; FireDAC is optional.
  2. Use two units: QueryBuilder4Delphi.Query.Core and QueryBuilder4Delphi.Query.
  3. Build, compile, execute. Pass the SQL and bindings to your data access layer, or call Execute with a provider.

Read the full guide →

Already have SQL?

Paste a SELECT query and get fluent Delphi code. It runs entirely in your browser.

Open the converter