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.
90 lines
1.8 KiB
90 lines
1.8 KiB
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 (
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
6 years ago
|
// 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)
|
||
|
}
|
||
|
|
||
8 years ago
|
func (b *Builder) insertWriteTo(w Writer) error {
|
||
6 years ago
|
if len(b.into) <= 0 {
|
||
|
return ErrNoTableName
|
||
|
}
|
||
|
if len(b.insertCols) <= 0 && b.from == "" {
|
||
|
return ErrNoColumnToInsert
|
||
8 years ago
|
}
|
||
6 years ago
|
|
||
|
if b.into != "" && b.from != "" {
|
||
|
return b.insertSelectWriteTo(w)
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
if _, err := fmt.Fprintf(w, "INSERT INTO %s (", b.into); err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
|
||
|
var args = make([]interface{}, 0)
|
||
|
var bs []byte
|
||
|
var valBuffer = bytes.NewBuffer(bs)
|
||
6 years ago
|
|
||
6 years ago
|
for i, col := range b.insertCols {
|
||
|
value := b.insertVals[i]
|
||
8 years ago
|
fmt.Fprint(w, col)
|
||
|
if e, ok := value.(expr); ok {
|
||
6 years ago
|
fmt.Fprintf(valBuffer, "(%s)", e.sql)
|
||
8 years ago
|
args = append(args, e.args...)
|
||
|
} else {
|
||
|
fmt.Fprint(valBuffer, "?")
|
||
|
args = append(args, value)
|
||
|
}
|
||
|
|
||
6 years ago
|
if i != len(b.insertCols)-1 {
|
||
8 years ago
|
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
|
||
|
}
|