2017-01-03 09:20:28 +01:00
|
|
|
// 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.
|
|
|
|
|
2016-11-03 23:16:01 +01:00
|
|
|
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 {
|
2018-07-20 04:10:17 +02:00
|
|
|
return errors.New("no column to be insert")
|
2016-11-03 23:16:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-07-20 04:10:17 +02:00
|
|
|
|
|
|
|
for _, col := range b.inserts.sortedKeys() {
|
|
|
|
value := b.inserts[col]
|
2016-11-03 23:16:01 +01:00
|
|
|
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
|
|
|
|
}
|