From 1080a02d6f9c164459841a28fbb6b5ba3e673205 Mon Sep 17 00:00:00 2001 From: Nicholas Xu Date: Fri, 18 Sep 2020 00:17:36 +0800 Subject: [PATCH 1/2] fix update predicate problem --- dialect/sql/builder.go | 32 ++++++++++++++++++++++++++++++++ dialect/sql/sqlgraph/graph.go | 7 +++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/dialect/sql/builder.go b/dialect/sql/builder.go index 55d96cd76d..7da14c81e4 100644 --- a/dialect/sql/builder.go +++ b/dialect/sql/builder.go @@ -761,6 +761,38 @@ func (u *UpdateBuilder) Query() (string, []interface{}) { return u.String(), u.args } +func (u *UpdateBuilder) QueryWithSelectPredicate() (string, []interface{}) { + u.WriteString("UPDATE ") + u.Ident(u.table).Pad().WriteString("SET ") + for i, c := range u.nulls { + if i > 0 { + u.Comma() + } + u.Ident(c).WriteString(" = NULL") + } + if len(u.nulls) > 0 && len(u.columns) > 0 { + u.Comma() + } + for i, c := range u.columns { + if i > 0 { + u.Comma() + } + u.Ident(c).WriteString(" = ") + switch v := u.values[i].(type) { + case Querier: + u.Join(v) + default: + u.Arg(v) + } + } + if u.where != nil { + u.WriteString(" WHERE ") + u.WriteString(u.where.String()) + u.args = append(u.args, u.where.args...) + } + return u.String(), u.args +} + // DeleteBuilder is a builder for `DELETE` statement. type DeleteBuilder struct { Builder diff --git a/dialect/sql/sqlgraph/graph.go b/dialect/sql/sqlgraph/graph.go index 8f2b1ac565..807735cfc7 100644 --- a/dialect/sql/sqlgraph/graph.go +++ b/dialect/sql/sqlgraph/graph.go @@ -653,8 +653,6 @@ func (u *updater) nodes(ctx context.Context, tx dialect.ExecQuerier) (int, error selector := u.builder.Select(u.Node.ID.Column). From(u.builder.Table(u.Node.Table)) - archivedWherePredicate := selector.Clone().P() - if pred := u.Predicate; pred != nil { pred(selector) } @@ -673,13 +671,14 @@ func (u *updater) nodes(ctx context.Context, tx dialect.ExecQuerier) (int, error if len(ids) == 0 { return 0, nil } - update := u.builder.Update(u.Node.Table).Where(archivedWherePredicate) + // update := u.builder.Update(u.Node.Table).Where(matchID(u.Node.ID.Column, ids)) + update := u.builder.Update(u.Node.Table).Where(selector.P()) if err := u.setTableColumns(update, addEdges, clearEdges); err != nil { return 0, err } if !update.Empty() { var res sql.Result - query, args := update.Query() + query, args := update.QueryWithSelectPredicate() if err := tx.Exec(ctx, query, args, &res); err != nil { return 0, err } From e4225351a0f63be86c55f440ffbe9ce792b6f206 Mon Sep 17 00:00:00 2001 From: Nicholas Xu Date: Fri, 18 Sep 2020 00:21:46 +0800 Subject: [PATCH 2/2] prevent mod problem with clone --- dialect/sql/sqlgraph/graph.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dialect/sql/sqlgraph/graph.go b/dialect/sql/sqlgraph/graph.go index 807735cfc7..8bf79434df 100644 --- a/dialect/sql/sqlgraph/graph.go +++ b/dialect/sql/sqlgraph/graph.go @@ -672,7 +672,7 @@ func (u *updater) nodes(ctx context.Context, tx dialect.ExecQuerier) (int, error return 0, nil } // update := u.builder.Update(u.Node.Table).Where(matchID(u.Node.ID.Column, ids)) - update := u.builder.Update(u.Node.Table).Where(selector.P()) + update := u.builder.Update(u.Node.Table).Where(selector.Clone().P()) if err := u.setTableColumns(update, addEdges, clearEdges); err != nil { return 0, err }