Skip to content

Commit

Permalink
Merge pull request #7 from nicholasxuu/hotfix-update-where
Browse files Browse the repository at this point in the history
fix update predicate problem
  • Loading branch information
HeathLee authored Sep 18, 2020
2 parents cc2389d + e422535 commit 44c4485
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
32 changes: 32 additions & 0 deletions dialect/sql/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions dialect/sql/sqlgraph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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.Clone().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
}
Expand Down

0 comments on commit 44c4485

Please sign in to comment.