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.
879 lines
22 KiB
879 lines
22 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.
|
||
|
|
||
|
package xorm
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"reflect"
|
||
6 years ago
|
"sort"
|
||
8 years ago
|
"strconv"
|
||
|
"strings"
|
||
|
|
||
5 years ago
|
"xorm.io/builder"
|
||
6 years ago
|
"xorm.io/core"
|
||
8 years ago
|
)
|
||
|
|
||
|
// Insert insert one or more beans
|
||
|
func (session *Session) Insert(beans ...interface{}) (int64, error) {
|
||
|
var affected int64
|
||
|
var err error
|
||
|
|
||
7 years ago
|
if session.isAutoClose {
|
||
8 years ago
|
defer session.Close()
|
||
|
}
|
||
|
|
||
5 years ago
|
session.autoResetStatement = false
|
||
|
defer func() {
|
||
|
session.autoResetStatement = true
|
||
|
session.resetStatement()
|
||
|
}()
|
||
|
|
||
8 years ago
|
for _, bean := range beans {
|
||
6 years ago
|
switch bean.(type) {
|
||
|
case map[string]interface{}:
|
||
|
cnt, err := session.insertMapInterface(bean.(map[string]interface{}))
|
||
|
if err != nil {
|
||
|
return affected, err
|
||
|
}
|
||
|
affected += cnt
|
||
|
case []map[string]interface{}:
|
||
|
s := bean.([]map[string]interface{})
|
||
|
for i := 0; i < len(s); i++ {
|
||
|
cnt, err := session.insertMapInterface(s[i])
|
||
|
if err != nil {
|
||
|
return affected, err
|
||
|
}
|
||
|
affected += cnt
|
||
|
}
|
||
|
case map[string]string:
|
||
|
cnt, err := session.insertMapString(bean.(map[string]string))
|
||
|
if err != nil {
|
||
|
return affected, err
|
||
|
}
|
||
|
affected += cnt
|
||
|
case []map[string]string:
|
||
|
s := bean.([]map[string]string)
|
||
|
for i := 0; i < len(s); i++ {
|
||
|
cnt, err := session.insertMapString(s[i])
|
||
|
if err != nil {
|
||
|
return affected, err
|
||
|
}
|
||
|
affected += cnt
|
||
|
}
|
||
|
default:
|
||
|
sliceValue := reflect.Indirect(reflect.ValueOf(bean))
|
||
|
if sliceValue.Kind() == reflect.Slice {
|
||
|
size := sliceValue.Len()
|
||
|
if size > 0 {
|
||
|
if session.engine.SupportInsertMany() {
|
||
|
cnt, err := session.innerInsertMulti(bean)
|
||
8 years ago
|
if err != nil {
|
||
|
return affected, err
|
||
|
}
|
||
|
affected += cnt
|
||
6 years ago
|
} else {
|
||
|
for i := 0; i < size; i++ {
|
||
|
cnt, err := session.innerInsert(sliceValue.Index(i).Interface())
|
||
|
if err != nil {
|
||
|
return affected, err
|
||
|
}
|
||
|
affected += cnt
|
||
|
}
|
||
8 years ago
|
}
|
||
|
}
|
||
6 years ago
|
} else {
|
||
|
cnt, err := session.innerInsert(bean)
|
||
|
if err != nil {
|
||
|
return affected, err
|
||
|
}
|
||
|
affected += cnt
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return affected, err
|
||
|
}
|
||
|
|
||
|
func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error) {
|
||
|
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
|
||
|
if sliceValue.Kind() != reflect.Slice {
|
||
|
return 0, errors.New("needs a pointer to a slice")
|
||
|
}
|
||
|
|
||
|
if sliceValue.Len() <= 0 {
|
||
|
return 0, errors.New("could not insert a empty slice")
|
||
|
}
|
||
|
|
||
6 years ago
|
if err := session.statement.setRefBean(sliceValue.Index(0).Interface()); err != nil {
|
||
8 years ago
|
return 0, err
|
||
|
}
|
||
8 years ago
|
|
||
6 years ago
|
tableName := session.statement.TableName()
|
||
|
if len(tableName) <= 0 {
|
||
8 years ago
|
return 0, ErrTableNotFound
|
||
|
}
|
||
|
|
||
7 years ago
|
table := session.statement.RefTable
|
||
8 years ago
|
size := sliceValue.Len()
|
||
|
|
||
|
var colNames []string
|
||
|
var colMultiPlaces []string
|
||
|
var args []interface{}
|
||
|
var cols []*core.Column
|
||
|
|
||
|
for i := 0; i < size; i++ {
|
||
|
v := sliceValue.Index(i)
|
||
|
vv := reflect.Indirect(v)
|
||
|
elemValue := v.Interface()
|
||
|
var colPlaces []string
|
||
|
|
||
|
// handle BeforeInsertProcessor
|
||
|
// !nashtsai! does user expect it's same slice to passed closure when using Before()/After() when insert multi??
|
||
|
for _, closure := range session.beforeClosures {
|
||
|
closure(elemValue)
|
||
|
}
|
||
|
|
||
|
if processor, ok := interface{}(elemValue).(BeforeInsertProcessor); ok {
|
||
|
processor.BeforeInsert()
|
||
|
}
|
||
|
// --
|
||
|
|
||
|
if i == 0 {
|
||
|
for _, col := range table.Columns() {
|
||
|
ptrFieldValue, err := col.ValueOfV(&vv)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
fieldValue := *ptrFieldValue
|
||
|
if col.IsAutoIncrement && isZero(fieldValue.Interface()) {
|
||
|
continue
|
||
|
}
|
||
|
if col.MapType == core.ONLYFROMDB {
|
||
|
continue
|
||
|
}
|
||
|
if col.IsDeleted {
|
||
|
continue
|
||
|
}
|
||
6 years ago
|
if session.statement.omitColumnMap.contain(col.Name) {
|
||
|
continue
|
||
8 years ago
|
}
|
||
6 years ago
|
if len(session.statement.columnMap) > 0 && !session.statement.columnMap.contain(col.Name) {
|
||
|
continue
|
||
8 years ago
|
}
|
||
7 years ago
|
if (col.IsCreated || col.IsUpdated) && session.statement.UseAutoTime {
|
||
7 years ago
|
val, t := session.engine.nowTime(col)
|
||
8 years ago
|
args = append(args, val)
|
||
|
|
||
|
var colName = col.Name
|
||
|
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
||
|
col := table.GetColumn(colName)
|
||
|
setColumnTime(bean, col, t)
|
||
|
})
|
||
7 years ago
|
} else if col.IsVersion && session.statement.checkVersion {
|
||
8 years ago
|
args = append(args, 1)
|
||
|
var colName = col.Name
|
||
|
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
||
|
col := table.GetColumn(colName)
|
||
|
setColumnInt(bean, col, 1)
|
||
|
})
|
||
|
} else {
|
||
|
arg, err := session.value2Interface(col, fieldValue)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
args = append(args, arg)
|
||
|
}
|
||
|
|
||
|
colNames = append(colNames, col.Name)
|
||
|
cols = append(cols, col)
|
||
|
colPlaces = append(colPlaces, "?")
|
||
|
}
|
||
|
} else {
|
||
|
for _, col := range cols {
|
||
|
ptrFieldValue, err := col.ValueOfV(&vv)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
fieldValue := *ptrFieldValue
|
||
|
|
||
|
if col.IsAutoIncrement && isZero(fieldValue.Interface()) {
|
||
|
continue
|
||
|
}
|
||
|
if col.MapType == core.ONLYFROMDB {
|
||
|
continue
|
||
|
}
|
||
|
if col.IsDeleted {
|
||
|
continue
|
||
|
}
|
||
6 years ago
|
if session.statement.omitColumnMap.contain(col.Name) {
|
||
|
continue
|
||
8 years ago
|
}
|
||
6 years ago
|
if len(session.statement.columnMap) > 0 && !session.statement.columnMap.contain(col.Name) {
|
||
|
continue
|
||
8 years ago
|
}
|
||
7 years ago
|
if (col.IsCreated || col.IsUpdated) && session.statement.UseAutoTime {
|
||
7 years ago
|
val, t := session.engine.nowTime(col)
|
||
8 years ago
|
args = append(args, val)
|
||
|
|
||
|
var colName = col.Name
|
||
|
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
||
|
col := table.GetColumn(colName)
|
||
|
setColumnTime(bean, col, t)
|
||
|
})
|
||
7 years ago
|
} else if col.IsVersion && session.statement.checkVersion {
|
||
8 years ago
|
args = append(args, 1)
|
||
|
var colName = col.Name
|
||
|
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
||
|
col := table.GetColumn(colName)
|
||
|
setColumnInt(bean, col, 1)
|
||
|
})
|
||
|
} else {
|
||
|
arg, err := session.value2Interface(col, fieldValue)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
args = append(args, arg)
|
||
|
}
|
||
|
|
||
|
colPlaces = append(colPlaces, "?")
|
||
|
}
|
||
|
}
|
||
|
colMultiPlaces = append(colMultiPlaces, strings.Join(colPlaces, ", "))
|
||
|
}
|
||
|
cleanupProcessorsClosures(&session.beforeClosures)
|
||
|
|
||
6 years ago
|
var sql string
|
||
7 years ago
|
if session.engine.dialect.DBType() == core.ORACLE {
|
||
5 years ago
|
temp := fmt.Sprintf(") INTO %s (%v) VALUES (",
|
||
7 years ago
|
session.engine.Quote(tableName),
|
||
5 years ago
|
quoteColumns(colNames, session.engine.Quote, ","))
|
||
|
sql = fmt.Sprintf("INSERT ALL INTO %s (%v) VALUES (%v) SELECT 1 FROM DUAL",
|
||
7 years ago
|
session.engine.Quote(tableName),
|
||
5 years ago
|
quoteColumns(colNames, session.engine.Quote, ","),
|
||
8 years ago
|
strings.Join(colMultiPlaces, temp))
|
||
|
} else {
|
||
5 years ago
|
sql = fmt.Sprintf("INSERT INTO %s (%v) VALUES (%v)",
|
||
7 years ago
|
session.engine.Quote(tableName),
|
||
5 years ago
|
quoteColumns(colNames, session.engine.Quote, ","),
|
||
8 years ago
|
strings.Join(colMultiPlaces, "),("))
|
||
|
}
|
||
6 years ago
|
res, err := session.exec(sql, args...)
|
||
8 years ago
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
6 years ago
|
session.cacheInsert(tableName)
|
||
8 years ago
|
|
||
|
lenAfterClosures := len(session.afterClosures)
|
||
|
for i := 0; i < size; i++ {
|
||
|
elemValue := reflect.Indirect(sliceValue.Index(i)).Addr().Interface()
|
||
|
|
||
|
// handle AfterInsertProcessor
|
||
7 years ago
|
if session.isAutoCommit {
|
||
8 years ago
|
// !nashtsai! does user expect it's same slice to passed closure when using Before()/After() when insert multi??
|
||
|
for _, closure := range session.afterClosures {
|
||
|
closure(elemValue)
|
||
|
}
|
||
|
if processor, ok := interface{}(elemValue).(AfterInsertProcessor); ok {
|
||
|
processor.AfterInsert()
|
||
|
}
|
||
|
} else {
|
||
|
if lenAfterClosures > 0 {
|
||
|
if value, has := session.afterInsertBeans[elemValue]; has && value != nil {
|
||
|
*value = append(*value, session.afterClosures...)
|
||
|
} else {
|
||
|
afterClosures := make([]func(interface{}), lenAfterClosures)
|
||
|
copy(afterClosures, session.afterClosures)
|
||
|
session.afterInsertBeans[elemValue] = &afterClosures
|
||
|
}
|
||
|
} else {
|
||
|
if _, ok := interface{}(elemValue).(AfterInsertProcessor); ok {
|
||
|
session.afterInsertBeans[elemValue] = nil
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
cleanupProcessorsClosures(&session.afterClosures)
|
||
|
return res.RowsAffected()
|
||
|
}
|
||
|
|
||
|
// InsertMulti insert multiple records
|
||
|
func (session *Session) InsertMulti(rowsSlicePtr interface{}) (int64, error) {
|
||
7 years ago
|
if session.isAutoClose {
|
||
8 years ago
|
defer session.Close()
|
||
|
}
|
||
|
|
||
|
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
|
||
|
if sliceValue.Kind() != reflect.Slice {
|
||
|
return 0, ErrParamsType
|
||
|
|
||
|
}
|
||
|
|
||
|
if sliceValue.Len() <= 0 {
|
||
|
return 0, nil
|
||
|
}
|
||
|
|
||
|
return session.innerInsertMulti(rowsSlicePtr)
|
||
|
}
|
||
|
|
||
|
func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
||
6 years ago
|
if err := session.statement.setRefBean(bean); err != nil {
|
||
8 years ago
|
return 0, err
|
||
|
}
|
||
7 years ago
|
if len(session.statement.TableName()) <= 0 {
|
||
8 years ago
|
return 0, ErrTableNotFound
|
||
|
}
|
||
|
|
||
7 years ago
|
table := session.statement.RefTable
|
||
8 years ago
|
|
||
|
// handle BeforeInsertProcessor
|
||
|
for _, closure := range session.beforeClosures {
|
||
|
closure(bean)
|
||
|
}
|
||
|
cleanupProcessorsClosures(&session.beforeClosures) // cleanup after used
|
||
|
|
||
|
if processor, ok := interface{}(bean).(BeforeInsertProcessor); ok {
|
||
|
processor.BeforeInsert()
|
||
|
}
|
||
6 years ago
|
|
||
|
colNames, args, err := session.genInsertColumns(bean)
|
||
8 years ago
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
5 years ago
|
exprs := session.statement.exprColumns
|
||
|
colPlaces := strings.Repeat("?, ", len(colNames))
|
||
|
if exprs.Len() <= 0 && len(colPlaces) > 0 {
|
||
|
colPlaces = colPlaces[0 : len(colPlaces)-2]
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
var tableName = session.statement.TableName()
|
||
6 years ago
|
var output string
|
||
|
if session.engine.dialect.DBType() == core.MSSQL && len(table.AutoIncrement) > 0 {
|
||
|
output = fmt.Sprintf(" OUTPUT Inserted.%s", table.AutoIncrement)
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
var buf = builder.NewWriter()
|
||
|
if _, err := buf.WriteString(fmt.Sprintf("INSERT INTO %s", session.engine.Quote(tableName))); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
if len(colPlaces) <= 0 {
|
||
|
if session.engine.dialect.DBType() == core.MYSQL {
|
||
|
if _, err := buf.WriteString(" VALUES ()"); err != nil {
|
||
5 years ago
|
return 0, err
|
||
|
}
|
||
|
} else {
|
||
5 years ago
|
if _, err := buf.WriteString(fmt.Sprintf("%s DEFAULT VALUES", output)); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
5 years ago
|
}
|
||
7 years ago
|
} else {
|
||
5 years ago
|
if _, err := buf.WriteString(" ("); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
if err := writeStrings(buf, append(colNames, exprs.colNames...), "`", "`"); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
if session.statement.cond.IsValid() {
|
||
|
if _, err := buf.WriteString(fmt.Sprintf(")%s SELECT ", output)); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
if err := session.statement.writeArgs(buf, args); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
if len(exprs.args) > 0 {
|
||
|
if _, err := buf.WriteString(","); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
}
|
||
|
if err := exprs.writeArgs(buf); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
if _, err := buf.WriteString(fmt.Sprintf(" FROM %v WHERE ", session.engine.Quote(tableName))); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
if err := session.statement.cond.WriteTo(buf); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
7 years ago
|
} else {
|
||
5 years ago
|
buf.Append(args...)
|
||
|
|
||
|
if _, err := buf.WriteString(fmt.Sprintf(")%s VALUES (%v",
|
||
|
output,
|
||
|
colPlaces)); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
if err := exprs.writeArgs(buf); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
if _, err := buf.WriteString(")"); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
7 years ago
|
}
|
||
|
}
|
||
8 years ago
|
|
||
6 years ago
|
if len(table.AutoIncrement) > 0 && session.engine.dialect.DBType() == core.POSTGRES {
|
||
5 years ago
|
if _, err := buf.WriteString(" RETURNING " + session.engine.Quote(table.AutoIncrement)); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
sqlStr := buf.String()
|
||
|
args = buf.Args()
|
||
|
|
||
8 years ago
|
handleAfterInsertProcessorFunc := func(bean interface{}) {
|
||
7 years ago
|
if session.isAutoCommit {
|
||
8 years ago
|
for _, closure := range session.afterClosures {
|
||
|
closure(bean)
|
||
|
}
|
||
|
if processor, ok := interface{}(bean).(AfterInsertProcessor); ok {
|
||
|
processor.AfterInsert()
|
||
|
}
|
||
|
} else {
|
||
|
lenAfterClosures := len(session.afterClosures)
|
||
|
if lenAfterClosures > 0 {
|
||
|
if value, has := session.afterInsertBeans[bean]; has && value != nil {
|
||
|
*value = append(*value, session.afterClosures...)
|
||
|
} else {
|
||
|
afterClosures := make([]func(interface{}), lenAfterClosures)
|
||
|
copy(afterClosures, session.afterClosures)
|
||
|
session.afterInsertBeans[bean] = &afterClosures
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
if _, ok := interface{}(bean).(AfterInsertProcessor); ok {
|
||
|
session.afterInsertBeans[bean] = nil
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
cleanupProcessorsClosures(&session.afterClosures) // cleanup after used
|
||
|
}
|
||
|
|
||
|
// for postgres, many of them didn't implement lastInsertId, so we should
|
||
|
// implemented it ourself.
|
||
7 years ago
|
if session.engine.dialect.DBType() == core.ORACLE && len(table.AutoIncrement) > 0 {
|
||
7 years ago
|
res, err := session.queryBytes("select seq_atable.currval from dual", args...)
|
||
8 years ago
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
7 years ago
|
defer handleAfterInsertProcessorFunc(bean)
|
||
8 years ago
|
|
||
6 years ago
|
session.cacheInsert(tableName)
|
||
8 years ago
|
|
||
7 years ago
|
if table.Version != "" && session.statement.checkVersion {
|
||
8 years ago
|
verValue, err := table.VersionColumn().ValueOf(bean)
|
||
|
if err != nil {
|
||
7 years ago
|
session.engine.logger.Error(err)
|
||
8 years ago
|
} else if verValue.IsValid() && verValue.CanSet() {
|
||
6 years ago
|
session.incrVersionFieldValue(verValue)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if len(res) < 1 {
|
||
|
return 0, errors.New("insert no error but not returned id")
|
||
|
}
|
||
|
|
||
|
idByte := res[0][table.AutoIncrement]
|
||
|
id, err := strconv.ParseInt(string(idByte), 10, 64)
|
||
|
if err != nil || id <= 0 {
|
||
|
return 1, err
|
||
|
}
|
||
|
|
||
|
aiValue, err := table.AutoIncrColumn().ValueOf(bean)
|
||
|
if err != nil {
|
||
7 years ago
|
session.engine.logger.Error(err)
|
||
8 years ago
|
}
|
||
|
|
||
|
if aiValue == nil || !aiValue.IsValid() || !aiValue.CanSet() {
|
||
|
return 1, nil
|
||
|
}
|
||
|
|
||
|
aiValue.Set(int64ToIntValue(id, aiValue.Type()))
|
||
|
|
||
|
return 1, nil
|
||
6 years ago
|
} else if len(table.AutoIncrement) > 0 && (session.engine.dialect.DBType() == core.POSTGRES || session.engine.dialect.DBType() == core.MSSQL) {
|
||
7 years ago
|
res, err := session.queryBytes(sqlStr, args...)
|
||
8 years ago
|
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
7 years ago
|
defer handleAfterInsertProcessorFunc(bean)
|
||
8 years ago
|
|
||
6 years ago
|
session.cacheInsert(tableName)
|
||
8 years ago
|
|
||
7 years ago
|
if table.Version != "" && session.statement.checkVersion {
|
||
8 years ago
|
verValue, err := table.VersionColumn().ValueOf(bean)
|
||
|
if err != nil {
|
||
7 years ago
|
session.engine.logger.Error(err)
|
||
8 years ago
|
} else if verValue.IsValid() && verValue.CanSet() {
|
||
6 years ago
|
session.incrVersionFieldValue(verValue)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if len(res) < 1 {
|
||
6 years ago
|
return 0, errors.New("insert successfully but not returned id")
|
||
8 years ago
|
}
|
||
|
|
||
|
idByte := res[0][table.AutoIncrement]
|
||
|
id, err := strconv.ParseInt(string(idByte), 10, 64)
|
||
|
if err != nil || id <= 0 {
|
||
|
return 1, err
|
||
|
}
|
||
|
|
||
|
aiValue, err := table.AutoIncrColumn().ValueOf(bean)
|
||
|
if err != nil {
|
||
7 years ago
|
session.engine.logger.Error(err)
|
||
8 years ago
|
}
|
||
|
|
||
|
if aiValue == nil || !aiValue.IsValid() || !aiValue.CanSet() {
|
||
|
return 1, nil
|
||
|
}
|
||
|
|
||
|
aiValue.Set(int64ToIntValue(id, aiValue.Type()))
|
||
|
|
||
|
return 1, nil
|
||
|
} else {
|
||
|
res, err := session.exec(sqlStr, args...)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
defer handleAfterInsertProcessorFunc(bean)
|
||
|
|
||
6 years ago
|
session.cacheInsert(tableName)
|
||
8 years ago
|
|
||
7 years ago
|
if table.Version != "" && session.statement.checkVersion {
|
||
8 years ago
|
verValue, err := table.VersionColumn().ValueOf(bean)
|
||
|
if err != nil {
|
||
7 years ago
|
session.engine.logger.Error(err)
|
||
8 years ago
|
} else if verValue.IsValid() && verValue.CanSet() {
|
||
6 years ago
|
session.incrVersionFieldValue(verValue)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if table.AutoIncrement == "" {
|
||
|
return res.RowsAffected()
|
||
|
}
|
||
|
|
||
|
var id int64
|
||
|
id, err = res.LastInsertId()
|
||
|
if err != nil || id <= 0 {
|
||
|
return res.RowsAffected()
|
||
|
}
|
||
|
|
||
|
aiValue, err := table.AutoIncrColumn().ValueOf(bean)
|
||
|
if err != nil {
|
||
7 years ago
|
session.engine.logger.Error(err)
|
||
8 years ago
|
}
|
||
|
|
||
|
if aiValue == nil || !aiValue.IsValid() || !aiValue.CanSet() {
|
||
|
return res.RowsAffected()
|
||
|
}
|
||
|
|
||
|
aiValue.Set(int64ToIntValue(id, aiValue.Type()))
|
||
|
|
||
|
return res.RowsAffected()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// InsertOne insert only one struct into database as a record.
|
||
|
// The in parameter bean must a struct or a point to struct. The return
|
||
|
// parameter is inserted and error
|
||
|
func (session *Session) InsertOne(bean interface{}) (int64, error) {
|
||
7 years ago
|
if session.isAutoClose {
|
||
8 years ago
|
defer session.Close()
|
||
|
}
|
||
|
|
||
|
return session.innerInsert(bean)
|
||
|
}
|
||
|
|
||
6 years ago
|
func (session *Session) cacheInsert(table string) error {
|
||
|
if !session.statement.UseCache {
|
||
|
return nil
|
||
8 years ago
|
}
|
||
6 years ago
|
cacher := session.engine.getCacher(table)
|
||
|
if cacher == nil {
|
||
|
return nil
|
||
8 years ago
|
}
|
||
6 years ago
|
session.engine.logger.Debug("[cache] clear sql:", table)
|
||
|
cacher.ClearIds(table)
|
||
8 years ago
|
return nil
|
||
|
}
|
||
6 years ago
|
|
||
|
// genInsertColumns generates insert needed columns
|
||
|
func (session *Session) genInsertColumns(bean interface{}) ([]string, []interface{}, error) {
|
||
|
table := session.statement.RefTable
|
||
|
colNames := make([]string, 0, len(table.ColumnsSeq()))
|
||
|
args := make([]interface{}, 0, len(table.ColumnsSeq()))
|
||
|
|
||
|
for _, col := range table.Columns() {
|
||
|
if col.MapType == core.ONLYFROMDB {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if col.IsDeleted {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if session.statement.omitColumnMap.contain(col.Name) {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if len(session.statement.columnMap) > 0 && !session.statement.columnMap.contain(col.Name) {
|
||
|
continue
|
||
|
}
|
||
|
|
||
5 years ago
|
if session.statement.incrColumns.isColExist(col.Name) {
|
||
6 years ago
|
continue
|
||
5 years ago
|
} else if session.statement.decrColumns.isColExist(col.Name) {
|
||
|
continue
|
||
|
} else if session.statement.exprColumns.isColExist(col.Name) {
|
||
6 years ago
|
continue
|
||
|
}
|
||
|
|
||
|
fieldValuePtr, err := col.ValueOf(bean)
|
||
|
if err != nil {
|
||
|
return nil, nil, err
|
||
|
}
|
||
|
fieldValue := *fieldValuePtr
|
||
|
|
||
|
if col.IsAutoIncrement {
|
||
|
switch fieldValue.Type().Kind() {
|
||
|
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
|
||
|
if fieldValue.Int() == 0 {
|
||
|
continue
|
||
|
}
|
||
|
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
|
||
|
if fieldValue.Uint() == 0 {
|
||
|
continue
|
||
|
}
|
||
|
case reflect.String:
|
||
|
if len(fieldValue.String()) == 0 {
|
||
|
continue
|
||
|
}
|
||
|
case reflect.Ptr:
|
||
|
if fieldValue.Pointer() == 0 {
|
||
|
continue
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// !evalphobia! set fieldValue as nil when column is nullable and zero-value
|
||
|
if _, ok := getFlagForColumn(session.statement.nullableMap, col); ok {
|
||
|
if col.Nullable && isZero(fieldValue.Interface()) {
|
||
|
var nilValue *int
|
||
|
fieldValue = reflect.ValueOf(nilValue)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (col.IsCreated || col.IsUpdated) && session.statement.UseAutoTime /*&& isZero(fieldValue.Interface())*/ {
|
||
|
// if time is non-empty, then set to auto time
|
||
|
val, t := session.engine.nowTime(col)
|
||
|
args = append(args, val)
|
||
|
|
||
|
var colName = col.Name
|
||
|
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
||
|
col := table.GetColumn(colName)
|
||
|
setColumnTime(bean, col, t)
|
||
|
})
|
||
|
} else if col.IsVersion && session.statement.checkVersion {
|
||
|
args = append(args, 1)
|
||
|
} else {
|
||
|
arg, err := session.value2Interface(col, fieldValue)
|
||
|
if err != nil {
|
||
|
return colNames, args, err
|
||
|
}
|
||
|
args = append(args, arg)
|
||
|
}
|
||
|
|
||
|
colNames = append(colNames, col.Name)
|
||
|
}
|
||
|
return colNames, args, nil
|
||
|
}
|
||
6 years ago
|
|
||
|
func (session *Session) insertMapInterface(m map[string]interface{}) (int64, error) {
|
||
|
if len(m) == 0 {
|
||
|
return 0, ErrParamsType
|
||
|
}
|
||
|
|
||
5 years ago
|
tableName := session.statement.TableName()
|
||
|
if len(tableName) <= 0 {
|
||
|
return 0, ErrTableNotFound
|
||
|
}
|
||
|
|
||
6 years ago
|
var columns = make([]string, 0, len(m))
|
||
5 years ago
|
exprs := session.statement.exprColumns
|
||
6 years ago
|
for k := range m {
|
||
5 years ago
|
if !exprs.isColExist(k) {
|
||
|
columns = append(columns, k)
|
||
|
}
|
||
6 years ago
|
}
|
||
|
sort.Strings(columns)
|
||
|
|
||
|
var args = make([]interface{}, 0, len(m))
|
||
|
for _, colName := range columns {
|
||
|
args = append(args, m[colName])
|
||
|
}
|
||
|
|
||
5 years ago
|
w := builder.NewWriter()
|
||
|
if session.statement.cond.IsValid() {
|
||
|
if _, err := w.WriteString(fmt.Sprintf("INSERT INTO %s (", session.engine.Quote(tableName))); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
if err := writeStrings(w, append(columns, exprs.colNames...), "`", "`"); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
if _, err := w.WriteString(") SELECT "); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
if err := session.statement.writeArgs(w, args); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
if len(exprs.args) > 0 {
|
||
|
if _, err := w.WriteString(","); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
if err := exprs.writeArgs(w); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if _, err := w.WriteString(fmt.Sprintf(" FROM %s WHERE ", session.engine.Quote(tableName))); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
if err := session.statement.cond.WriteTo(w); err != nil {
|
||
5 years ago
|
return 0, err
|
||
|
}
|
||
|
} else {
|
||
5 years ago
|
qm := strings.Repeat("?,", len(columns))
|
||
|
qm = qm[:len(qm)-1]
|
||
|
|
||
|
if _, err := w.WriteString(fmt.Sprintf("INSERT INTO %s (`%s`) VALUES (%s)", session.engine.Quote(tableName), strings.Join(columns, "`,`"), qm)); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
w.Append(args...)
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
sql := w.String()
|
||
|
args = w.Args()
|
||
|
|
||
6 years ago
|
if err := session.cacheInsert(tableName); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
res, err := session.exec(sql, args...)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
affected, err := res.RowsAffected()
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
return affected, nil
|
||
|
}
|
||
|
|
||
|
func (session *Session) insertMapString(m map[string]string) (int64, error) {
|
||
|
if len(m) == 0 {
|
||
|
return 0, ErrParamsType
|
||
|
}
|
||
|
|
||
5 years ago
|
tableName := session.statement.TableName()
|
||
|
if len(tableName) <= 0 {
|
||
|
return 0, ErrTableNotFound
|
||
|
}
|
||
|
|
||
6 years ago
|
var columns = make([]string, 0, len(m))
|
||
5 years ago
|
exprs := session.statement.exprColumns
|
||
6 years ago
|
for k := range m {
|
||
5 years ago
|
if !exprs.isColExist(k) {
|
||
|
columns = append(columns, k)
|
||
|
}
|
||
6 years ago
|
}
|
||
|
sort.Strings(columns)
|
||
|
|
||
5 years ago
|
var args = make([]interface{}, 0, len(m))
|
||
|
for _, colName := range columns {
|
||
|
args = append(args, m[colName])
|
||
|
}
|
||
|
|
||
5 years ago
|
w := builder.NewWriter()
|
||
|
if session.statement.cond.IsValid() {
|
||
|
if _, err := w.WriteString(fmt.Sprintf("INSERT INTO %s (", session.engine.Quote(tableName))); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
6 years ago
|
|
||
5 years ago
|
if err := writeStrings(w, append(columns, exprs.colNames...), "`", "`"); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
6 years ago
|
|
||
5 years ago
|
if _, err := w.WriteString(") SELECT "); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
if err := session.statement.writeArgs(w, args); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
if len(exprs.args) > 0 {
|
||
|
if _, err := w.WriteString(","); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
if err := exprs.writeArgs(w); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if _, err := w.WriteString(fmt.Sprintf(" FROM %s WHERE ", session.engine.Quote(tableName))); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
if err := session.statement.cond.WriteTo(w); err != nil {
|
||
5 years ago
|
return 0, err
|
||
|
}
|
||
|
} else {
|
||
5 years ago
|
qm := strings.Repeat("?,", len(columns))
|
||
|
qm = qm[:len(qm)-1]
|
||
|
|
||
|
if _, err := w.WriteString(fmt.Sprintf("INSERT INTO %s (`%s`) VALUES (%s)", session.engine.Quote(tableName), strings.Join(columns, "`,`"), qm)); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
w.Append(args...)
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
sql := w.String()
|
||
|
args = w.Args()
|
||
|
|
||
6 years ago
|
if err := session.cacheInsert(tableName); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
res, err := session.exec(sql, args...)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
affected, err := res.RowsAffected()
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
return affected, nil
|
||
|
}
|