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