Skip to content

Fluent UPDATE builder with validation, error tagging and dialect-safe formatting.

๐Ÿ› ๏ธ UpdateBuilder Developer Guide

UpdateBuilder constructs SQL UPDATE statements using a fluent API that ensures clarity, correctness, and dialect-aware formatting.


๐Ÿ“† Since

Available since: v1.2.0


๐Ÿ”ง Method Reference

Method Description
WithDialect Sets SQL dialect for escaping
Table Sets the table to update
Set Assigns values to columns using Field.WithValue()
Where Sets the WHERE clause
Returning Adds a RETURNING clause
Build() Compiles the full SQL statement

โœ๏ธ Example Usage

โœ… Supported Features

  • Target table declaration via .Table(...)
  • Column assignment with .Set(...) (no aliasing allowed)
  • Condition building using .Where(...), .AndWhere(...), .OrWhere(...)
  • Dialect support for identifier quoting
  • Deprecation-safe: .WithDialect(...) still works and is covered

๐Ÿงฑ Fluent API Example

sql, args, err := builder.NewUpdate().
    Table("users").
    Set("status", "active").
    Where("id = ?", 42).
    UseDialect("postgres").
    Build()

Produces:

UPDATE "users" SET "status" = ? WHERE id = ?

Args:

[]any{"active", 42}

๐Ÿ” Validation

  • Requires .Table(...) and at least one .Set(...) call
  • Rejects aliases: calling .Set("email AS contact", ...) returns an error
  • Safe fallback: .WithDialect(...) is marked deprecated but still functional

๐Ÿงช Test Coverage

โœ… 100% tested, grouped by method and edge cases.

Area Covered
.Table(...) โœ…
.Set(...) โœ…
.Where/And/Or โœ…
.Build() โœ…
.UseDialect() โœ…
.WithDialect() โœ…
Validation โœ…
Aliased field โœ… error triggered

๐Ÿงช Even if necessary, tests will be tested. Because coverage isn't just a number โ€” it's a philosophy.


๐Ÿ’ก Best Practices

  • โœ… Use UseDialect(...) for proper identifier quoting
  • ๐Ÿšซ Do not alias fields in .Set(...)
  • ๐Ÿ“ Chain conditions instead of nesting or formatting manually