mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
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.
60 lines
1.1 KiB
60 lines
1.1 KiB
package builder
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
func (b *Builder) insertWriteTo(w Writer) error {
|
|
if len(b.tableName) <= 0 {
|
|
return errors.New("no table indicated")
|
|
}
|
|
if len(b.inserts) <= 0 {
|
|
return errors.New("no column to be update")
|
|
}
|
|
|
|
if _, err := fmt.Fprintf(w, "INSERT INTO %s (", b.tableName); err != nil {
|
|
return err
|
|
}
|
|
|
|
var args = make([]interface{}, 0)
|
|
var bs []byte
|
|
var valBuffer = bytes.NewBuffer(bs)
|
|
var i = 0
|
|
for col, value := range b.inserts {
|
|
fmt.Fprint(w, col)
|
|
if e, ok := value.(expr); ok {
|
|
fmt.Fprint(valBuffer, e.sql)
|
|
args = append(args, e.args...)
|
|
} else {
|
|
fmt.Fprint(valBuffer, "?")
|
|
args = append(args, value)
|
|
}
|
|
|
|
if i != len(b.inserts)-1 {
|
|
if _, err := fmt.Fprint(w, ","); err != nil {
|
|
return err
|
|
}
|
|
if _, err := fmt.Fprint(valBuffer, ","); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
i = i + 1
|
|
}
|
|
|
|
if _, err := fmt.Fprint(w, ") Values ("); err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := w.Write(valBuffer.Bytes()); err != nil {
|
|
return err
|
|
}
|
|
if _, err := fmt.Fprint(w, ")"); err != nil {
|
|
return err
|
|
}
|
|
|
|
w.Append(args...)
|
|
|
|
return nil
|
|
}
|
|
|