Append writes to the given SQL builder the SQL command for appending JSON values into the array, optionally defined as a key. Note, the generated SQL will use the Go semantics, the JSON column/key will be set to the given Array in case it is `null` or NULL. For example: Append(u, column, []string{
(u *sql.UpdateBuilder, column string, elems []T, opts ...Option)
| 389 | // WHEN (("c"->'a')::jsonb IS NULL OR ("c"->'a')::jsonb = 'null'::jsonb) |
| 390 | // THEN jsonb_set("c", '{a}', $1, true) ELSE jsonb_set("c", '{a}', "c"->'a' || $2, true) END |
| 391 | func Append[T any](u *sql.UpdateBuilder, column string, elems []T, opts ...Option) { |
| 392 | if len(elems) == 0 { |
| 393 | u.AddError(fmt.Errorf("sqljson: cannot append an empty array to column %q", column)) |
| 394 | return |
| 395 | } |
| 396 | drv, err := newDriver(u.Dialect()) |
| 397 | if err != nil { |
| 398 | u.AddError(err) |
| 399 | return |
| 400 | } |
| 401 | vs := make([]any, len(elems)) |
| 402 | for i, e := range elems { |
| 403 | vs[i] = e |
| 404 | } |
| 405 | drv.Append(u, column, vs, opts...) |
| 406 | } |
| 407 | |
| 408 | // Option allows for calling database JSON paths with functional options. |
| 409 | type Option func(*PathOptions) |
searching dependent graphs…