You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
893 B
47 lines
893 B
8 years ago
|
// Copyright 2016 The Xorm Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
8 years ago
|
package builder
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
6 years ago
|
// Update creates an update Builder
|
||
|
func Update(updates ...Eq) *Builder {
|
||
|
builder := &Builder{cond: NewCond()}
|
||
|
return builder.Update(updates...)
|
||
|
}
|
||
|
|
||
8 years ago
|
func (b *Builder) updateWriteTo(w Writer) error {
|
||
6 years ago
|
if len(b.from) <= 0 {
|
||
|
return ErrNoTableName
|
||
8 years ago
|
}
|
||
|
if len(b.updates) <= 0 {
|
||
6 years ago
|
return ErrNoColumnToUpdate
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
if _, err := fmt.Fprintf(w, "UPDATE %s SET ", b.from); err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
|
||
|
for i, s := range b.updates {
|
||
|
if err := s.opWriteTo(",", w); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if i != len(b.updates)-1 {
|
||
|
if _, err := fmt.Fprint(w, ","); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if _, err := fmt.Fprint(w, " WHERE "); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return b.cond.WriteTo(w)
|
||
|
}
|