diff options
author | 2023-01-22 12:26:47 +0100 | |
---|---|---|
committer | 2023-01-22 12:26:47 +0100 | |
commit | 0ceacd7b1d0b03e3da1d552aa0bbbe7037e57e90 (patch) | |
tree | 106bd6f80fe5466522b37b5778ebc7c4fd6ab728 /vendor/github.com/uptrace/bun/query_update.go | |
parent | [chore] Add name to instance field for autosuggestion (#1359) (diff) | |
download | gotosocial-0ceacd7b1d0b03e3da1d552aa0bbbe7037e57e90.tar.xz |
[chore] bump db dependencies (#1366)
Diffstat (limited to 'vendor/github.com/uptrace/bun/query_update.go')
-rw-r--r-- | vendor/github.com/uptrace/bun/query_update.go | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/vendor/github.com/uptrace/bun/query_update.go b/vendor/github.com/uptrace/bun/query_update.go index 1662245a8..708bcfbce 100644 --- a/vendor/github.com/uptrace/bun/query_update.go +++ b/vendor/github.com/uptrace/bun/query_update.go @@ -47,9 +47,17 @@ func (q *UpdateQuery) Model(model interface{}) *UpdateQuery { return q } +func (q *UpdateQuery) Err(err error) *UpdateQuery { + q.setErr(err) + return q +} + // Apply calls the fn passing the SelectQuery as an argument. func (q *UpdateQuery) Apply(fn func(*UpdateQuery) *UpdateQuery) *UpdateQuery { - return fn(q) + if fn != nil { + return fn(q) + } + return q } func (q *UpdateQuery) With(name string, query schema.QueryAppender) *UpdateQuery { @@ -174,13 +182,6 @@ func (q *UpdateQuery) Returning(query string, args ...interface{}) *UpdateQuery return q } -func (q *UpdateQuery) hasReturning() bool { - if !q.db.features.Has(feature.Returning) { - return false - } - return q.returningQuery.hasReturning() -} - //------------------------------------------------------------------------------ func (q *UpdateQuery) Operation() string { @@ -229,6 +230,14 @@ func (q *UpdateQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, e } } + if q.hasFeature(feature.Output) && q.hasReturning() { + b = append(b, " OUTPUT "...) + b, err = q.appendOutput(fmter, b) + if err != nil { + return nil, err + } + } + b, err = q.mustAppendWhere(fmter, b, q.hasTableAlias(fmter)) if err != nil { return nil, err @@ -426,7 +435,18 @@ func (q *UpdateQuery) updateSliceWhere(fmter schema.Formatter, model *sliceTable //------------------------------------------------------------------------------ +func (q *UpdateQuery) Scan(ctx context.Context, dest ...interface{}) error { + _, err := q.scanOrExec(ctx, dest, true) + return err +} + func (q *UpdateQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) { + return q.scanOrExec(ctx, dest, len(dest) > 0) +} + +func (q *UpdateQuery) scanOrExec( + ctx context.Context, dest []interface{}, hasDest bool, +) (sql.Result, error) { if q.err != nil { return nil, q.err } @@ -437,25 +457,33 @@ func (q *UpdateQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result } } + // Run append model hooks before generating the query. if err := q.beforeAppendModel(ctx, q); err != nil { return nil, err } + // Generate the query before checking hasReturning. queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes()) if err != nil { return nil, err } - query := internal.String(queryBytes) - - var res sql.Result + useScan := hasDest || (q.hasReturning() && q.hasFeature(feature.Returning|feature.Output)) + var model Model - if hasDest := len(dest) > 0; hasDest || q.hasReturning() { - model, err := q.getModel(dest) + if useScan { + var err error + model, err = q.getModel(dest) if err != nil { return nil, err } + } + + query := internal.String(queryBytes) + + var res sql.Result + if useScan { res, err = q.scan(ctx, q, query, model, hasDest) if err != nil { return nil, err @@ -498,7 +526,7 @@ func (q *UpdateQuery) afterUpdateHook(ctx context.Context) error { // table_alias.column_alias. func (q *UpdateQuery) FQN(column string) Ident { if q.table == nil { - panic("UpdateQuery.SetName requires a model") + panic("UpdateQuery.FQN requires a model") } if q.hasTableAlias(q.db.fmter) { return Ident(q.table.Alias + "." + column) |