2017-01-03 03:20:28 -05: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 18:16:01 -04:00
|
|
|
package builder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2019-09-24 09:22:39 -04:00
|
|
|
"sort"
|
2016-11-03 18:16:01 -04:00
|
|
|
)
|
|
|
|
|
2018-12-18 11:26:26 -05:00
|
|
|
// Insert creates an insert Builder
|
|
|
|
func Insert(eq ...interface{}) *Builder {
|
|
|
|
builder := &Builder{cond: NewCond()}
|
|
|
|
return builder.Insert(eq...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) insertSelectWriteTo(w Writer) error {
|
|
|
|
if _, err := fmt.Fprintf(w, "INSERT INTO %s ", b.into); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b.insertCols) > 0 {
|
|
|
|
fmt.Fprintf(w, "(")
|
|
|
|
for _, col := range b.insertCols {
|
|
|
|
fmt.Fprintf(w, col)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, ") ")
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.selectWriteTo(w)
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:16:01 -04:00
|
|
|
func (b *Builder) insertWriteTo(w Writer) error {
|
2018-12-18 11:26:26 -05:00
|
|
|
if len(b.into) <= 0 {
|
|
|
|
return ErrNoTableName
|
|
|
|
}
|
|
|
|
if len(b.insertCols) <= 0 && b.from == "" {
|
|
|
|
return ErrNoColumnToInsert
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
2018-12-18 11:26:26 -05:00
|
|
|
|
|
|
|
if b.into != "" && b.from != "" {
|
|
|
|
return b.insertSelectWriteTo(w)
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
|
2018-12-18 11:26:26 -05:00
|
|
|
if _, err := fmt.Fprintf(w, "INSERT INTO %s (", b.into); err != nil {
|
2016-11-03 18:16:01 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var args = make([]interface{}, 0)
|
|
|
|
var bs []byte
|
|
|
|
var valBuffer = bytes.NewBuffer(bs)
|
2018-07-19 22:10:17 -04:00
|
|
|
|
2018-12-18 11:26:26 -05:00
|
|
|
for i, col := range b.insertCols {
|
|
|
|
value := b.insertVals[i]
|
2016-11-03 18:16:01 -04:00
|
|
|
fmt.Fprint(w, col)
|
|
|
|
if e, ok := value.(expr); ok {
|
2018-12-18 11:26:26 -05:00
|
|
|
fmt.Fprintf(valBuffer, "(%s)", e.sql)
|
2016-11-03 18:16:01 -04:00
|
|
|
args = append(args, e.args...)
|
2020-03-22 11:12:55 -04:00
|
|
|
} else if value == nil {
|
|
|
|
fmt.Fprintf(valBuffer, `null`)
|
2016-11-03 18:16:01 -04:00
|
|
|
} else {
|
|
|
|
fmt.Fprint(valBuffer, "?")
|
|
|
|
args = append(args, value)
|
|
|
|
}
|
|
|
|
|
2018-12-18 11:26:26 -05:00
|
|
|
if i != len(b.insertCols)-1 {
|
2016-11-03 18:16:01 -04:00
|
|
|
if _, err := fmt.Fprint(w, ","); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := fmt.Fprint(valBuffer, ","); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-09-24 09:22:39 -04:00
|
|
|
|
|
|
|
type insertColsSorter struct {
|
|
|
|
cols []string
|
|
|
|
vals []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s insertColsSorter) Len() int {
|
|
|
|
return len(s.cols)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s insertColsSorter) Swap(i, j int) {
|
|
|
|
s.cols[i], s.cols[j] = s.cols[j], s.cols[i]
|
|
|
|
s.vals[i], s.vals[j] = s.vals[j], s.vals[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s insertColsSorter) Less(i, j int) bool {
|
|
|
|
return s.cols[i] < s.cols[j]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert sets insert SQL
|
|
|
|
func (b *Builder) Insert(eq ...interface{}) *Builder {
|
|
|
|
if len(eq) > 0 {
|
|
|
|
var paramType = -1
|
|
|
|
for _, e := range eq {
|
|
|
|
switch t := e.(type) {
|
|
|
|
case Eq:
|
|
|
|
if paramType == -1 {
|
|
|
|
paramType = 0
|
|
|
|
}
|
|
|
|
if paramType != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for k, v := range t {
|
|
|
|
b.insertCols = append(b.insertCols, k)
|
|
|
|
b.insertVals = append(b.insertVals, v)
|
|
|
|
}
|
|
|
|
case string:
|
|
|
|
if paramType == -1 {
|
|
|
|
paramType = 1
|
|
|
|
}
|
|
|
|
if paramType != 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
b.insertCols = append(b.insertCols, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b.insertCols) == len(b.insertVals) {
|
|
|
|
sort.Sort(insertColsSorter{
|
|
|
|
cols: b.insertCols,
|
|
|
|
vals: b.insertVals,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
b.optype = insertType
|
|
|
|
return b
|
|
|
|
}
|