Skip to content

Commit

Permalink
Merge pull request #8 from nicholasxuu/hotfix-update-where-clone
Browse files Browse the repository at this point in the history
hotfix remove clone caused problem
  • Loading branch information
HeathLee authored Sep 18, 2020
2 parents 44c4485 + 6023e18 commit 9ed2dad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 34 deletions.
32 changes: 0 additions & 32 deletions dialect/sql/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,38 +761,6 @@ 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
Expand Down
13 changes: 11 additions & 2 deletions dialect/sql/sqlgraph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,8 @@ func (u *updater) nodes(ctx context.Context, tx dialect.ExecQuerier) (int, error
if pred := u.Predicate; pred != nil {
pred(selector)
}

// note: selector.P() predicate is contaminated by this function.
query, args := selector.Query()
rows := &sql.Rows{}
if err := u.tx.Query(ctx, query, args, rows); err != nil {
Expand All @@ -672,13 +674,20 @@ 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.Clone().P())

// remake predicate by creating a new selector.
selector2 := u.builder.Select(u.Node.ID.Column).
From(u.builder.Table(u.Node.Table))
if pred := u.Predicate; pred != nil {
pred(selector2)
}
update := u.builder.Update(u.Node.Table).Where(selector2.P())
if err := u.setTableColumns(update, addEdges, clearEdges); err != nil {
return 0, err
}
if !update.Empty() {
var res sql.Result
query, args := update.QueryWithSelectPredicate()
query, args := update.Query()
if err := tx.Exec(ctx, query, args, &res); err != nil {
return 0, err
}
Expand Down

0 comments on commit 9ed2dad

Please sign in to comment.