Generate sqlite insert SQL
This commit is contained in:
parent
0a35abb59f
commit
adb0e6293e
@ -2,9 +2,10 @@ package sqlite
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/jinzhu/gorm/dialects/common/utils"
|
||||
"github.com/jinzhu/gorm/model"
|
||||
|
||||
// import sqlite3 driver
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
@ -14,27 +15,70 @@ import (
|
||||
type Dialect struct {
|
||||
}
|
||||
|
||||
// Quote quote for value
|
||||
func (dialect Dialect) Quote(name string) string {
|
||||
return fmt.Sprintf(`"%s"`, name)
|
||||
}
|
||||
|
||||
// Insert insert
|
||||
func (*Dialect) Insert(tx *gorm.DB) error {
|
||||
func (dialect *Dialect) Insert(tx *gorm.DB) (err error) {
|
||||
var (
|
||||
args []interface{}
|
||||
defaultValueColumns []string
|
||||
assignmentsChan = utils.GetCreatingAssignments(tx)
|
||||
tableNameChan = utils.GetTable(tx)
|
||||
args []interface{}
|
||||
assignmentsChan = model.GetAssignments(tx)
|
||||
tableNameChan = model.GetTable(tx)
|
||||
)
|
||||
|
||||
s := bytes.NewBufferString("INSERT INTO ")
|
||||
s.WriteString(<-tableNameChan)
|
||||
tableName := <-tableNameChan
|
||||
s.WriteString(tableName)
|
||||
|
||||
if assignments := <-assignmentsChan; len(assignments) > 0 {
|
||||
for column, value := range assignments {
|
||||
args = append(args, value...)
|
||||
columns := []string{}
|
||||
|
||||
// Write columns (table.column1, table.column2, table.column3)
|
||||
s.WriteString(" (")
|
||||
|
||||
// Write values (v1, v2, v3), (v2-1, v2-2, v2-3)
|
||||
valueBuffer := bytes.NewBufferString("VALUES ")
|
||||
|
||||
for idx, fields := range assignments {
|
||||
if idx != 0 {
|
||||
s.WriteString(", ")
|
||||
}
|
||||
valueBuffer = bytes.NewBufferString(" (")
|
||||
|
||||
for j, field := range fields {
|
||||
if idx == 0 {
|
||||
columns = append(columns, field.Field.DBName)
|
||||
if j != 0 {
|
||||
s.WriteString(", ")
|
||||
}
|
||||
s.WriteString(dialect.Quote(tableName))
|
||||
s.WriteString(".")
|
||||
s.WriteString(dialect.Quote(field.Field.DBName))
|
||||
}
|
||||
|
||||
if j != 0 {
|
||||
valueBuffer.WriteString(", ")
|
||||
}
|
||||
valueBuffer.WriteString("?")
|
||||
|
||||
if field.IsBlank {
|
||||
args = append(args, field.Field.DefaultValue)
|
||||
} else {
|
||||
args = append(args, field.Value.Interface())
|
||||
}
|
||||
}
|
||||
valueBuffer = bytes.NewBufferString(") ")
|
||||
}
|
||||
s.WriteString(") ")
|
||||
|
||||
_, err = valueBuffer.WriteTo(s)
|
||||
} else {
|
||||
// assign default value
|
||||
s.WriteString(" DEFAULT VALUES")
|
||||
}
|
||||
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Query query
|
||||
|
Loading…
x
Reference in New Issue
Block a user