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.
|
|
|
|
|
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2019-06-23 11:22:43 -04:00
|
|
|
"sort"
|
2017-01-03 03:20:28 -05:00
|
|
|
"strings"
|
2021-07-04 09:10:46 -04:00
|
|
|
"time"
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2021-08-12 19:11:42 -04:00
|
|
|
"xorm.io/xorm/convert"
|
|
|
|
"xorm.io/xorm/dialects"
|
2020-03-22 11:12:55 -04:00
|
|
|
"xorm.io/xorm/internal/utils"
|
|
|
|
"xorm.io/xorm/schemas"
|
2017-01-03 03:20:28 -05:00
|
|
|
)
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
// ErrNoElementsOnSlice represents an error there is no element when insert
|
2021-08-12 19:11:42 -04:00
|
|
|
var ErrNoElementsOnSlice = errors.New("no element on slice when insert")
|
2020-03-22 11:12:55 -04:00
|
|
|
|
2017-01-03 03:20:28 -05:00
|
|
|
// Insert insert one or more beans
|
|
|
|
func (session *Session) Insert(beans ...interface{}) (int64, error) {
|
|
|
|
var affected int64
|
|
|
|
var err error
|
|
|
|
|
2017-08-22 07:39:52 -04:00
|
|
|
if session.isAutoClose {
|
2017-01-03 03:20:28 -05:00
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
2019-09-25 15:02:54 -04:00
|
|
|
session.autoResetStatement = false
|
|
|
|
defer func() {
|
|
|
|
session.autoResetStatement = true
|
|
|
|
session.resetStatement()
|
|
|
|
}()
|
|
|
|
|
2017-01-03 03:20:28 -05:00
|
|
|
for _, bean := range beans {
|
2021-08-12 19:11:42 -04:00
|
|
|
var cnt int64
|
|
|
|
var err error
|
|
|
|
switch v := bean.(type) {
|
2019-06-23 11:22:43 -04:00
|
|
|
case map[string]interface{}:
|
2021-08-12 19:11:42 -04:00
|
|
|
cnt, err = session.insertMapInterface(v)
|
2019-06-23 11:22:43 -04:00
|
|
|
case []map[string]interface{}:
|
2021-08-12 19:11:42 -04:00
|
|
|
cnt, err = session.insertMultipleMapInterface(v)
|
2019-06-23 11:22:43 -04:00
|
|
|
case map[string]string:
|
2021-08-12 19:11:42 -04:00
|
|
|
cnt, err = session.insertMapString(v)
|
2019-06-23 11:22:43 -04:00
|
|
|
case []map[string]string:
|
2021-08-12 19:11:42 -04:00
|
|
|
cnt, err = session.insertMultipleMapString(v)
|
2019-06-23 11:22:43 -04:00
|
|
|
default:
|
|
|
|
sliceValue := reflect.Indirect(reflect.ValueOf(bean))
|
|
|
|
if sliceValue.Kind() == reflect.Slice {
|
2021-08-12 19:11:42 -04:00
|
|
|
cnt, err = session.insertMultipleStruct(bean)
|
2019-06-23 11:22:43 -04:00
|
|
|
} else {
|
2021-08-12 19:11:42 -04:00
|
|
|
cnt, err = session.insertStruct(bean)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
}
|
2021-08-12 19:11:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return affected, err
|
|
|
|
}
|
|
|
|
affected += cnt
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return affected, err
|
|
|
|
}
|
|
|
|
|
2021-08-12 19:11:42 -04:00
|
|
|
func (session *Session) insertMultipleStruct(rowsSlicePtr interface{}) (int64, error) {
|
2017-01-03 03:20:28 -05:00
|
|
|
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 {
|
2021-08-12 19:11:42 -04:00
|
|
|
return 0, ErrNoElementsOnSlice
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
if err := session.statement.SetRefBean(sliceValue.Index(0).Interface()); err != nil {
|
2017-05-01 20:50:33 -04:00
|
|
|
return 0, err
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2018-07-19 22:10:17 -04:00
|
|
|
tableName := session.statement.TableName()
|
|
|
|
if len(tableName) <= 0 {
|
2017-01-03 03:20:28 -05:00
|
|
|
return 0, ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
2020-06-15 16:46:01 -04:00
|
|
|
var (
|
|
|
|
table = session.statement.RefTable
|
|
|
|
size = sliceValue.Len()
|
|
|
|
colNames []string
|
|
|
|
colMultiPlaces []string
|
|
|
|
args []interface{}
|
|
|
|
cols []*schemas.Column
|
|
|
|
)
|
2017-01-03 03:20:28 -05:00
|
|
|
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
v := sliceValue.Index(i)
|
2020-03-22 11:12:55 -04:00
|
|
|
var vv reflect.Value
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Interface:
|
|
|
|
vv = reflect.Indirect(v.Elem())
|
|
|
|
default:
|
|
|
|
vv = reflect.Indirect(v)
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
// --
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
for _, col := range table.Columns() {
|
|
|
|
ptrFieldValue, err := col.ValueOfV(&vv)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
fieldValue := *ptrFieldValue
|
|
|
|
if col.IsAutoIncrement && utils.IsZero(fieldValue.Interface()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if col.MapType == schemas.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
|
|
|
|
}
|
|
|
|
if (col.IsCreated || col.IsUpdated) && session.statement.UseAutoTime {
|
2021-08-12 19:11:42 -04:00
|
|
|
val, t, err := session.engine.nowTime(col)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-03-22 11:12:55 -04:00
|
|
|
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)
|
|
|
|
var colName = col.Name
|
|
|
|
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
|
|
|
col := table.GetColumn(colName)
|
|
|
|
setColumnInt(bean, col, 1)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
arg, err := session.statement.Value2Interface(col, fieldValue)
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-03-22 11:12:55 -04:00
|
|
|
args = append(args, arg)
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
if i == 0 {
|
2017-01-03 03:20:28 -05:00
|
|
|
colNames = append(colNames, col.Name)
|
|
|
|
cols = append(cols, col)
|
|
|
|
}
|
2020-03-22 11:12:55 -04:00
|
|
|
colPlaces = append(colPlaces, "?")
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
2020-03-22 11:12:55 -04:00
|
|
|
|
2017-01-03 03:20:28 -05:00
|
|
|
colMultiPlaces = append(colMultiPlaces, strings.Join(colPlaces, ", "))
|
|
|
|
}
|
|
|
|
cleanupProcessorsClosures(&session.beforeClosures)
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
quoter := session.engine.dialect.Quoter()
|
2018-07-19 22:10:17 -04:00
|
|
|
var sql string
|
2020-03-22 11:12:55 -04:00
|
|
|
colStr := quoter.Join(colNames, ",")
|
|
|
|
if session.engine.dialect.URI().DBType == schemas.ORACLE {
|
2019-08-26 22:17:23 -04:00
|
|
|
temp := fmt.Sprintf(") INTO %s (%v) VALUES (",
|
2020-03-22 11:12:55 -04:00
|
|
|
quoter.Quote(tableName),
|
|
|
|
colStr)
|
2019-08-26 22:17:23 -04:00
|
|
|
sql = fmt.Sprintf("INSERT ALL INTO %s (%v) VALUES (%v) SELECT 1 FROM DUAL",
|
2020-03-22 11:12:55 -04:00
|
|
|
quoter.Quote(tableName),
|
|
|
|
colStr,
|
2017-04-06 21:47:25 -04:00
|
|
|
strings.Join(colMultiPlaces, temp))
|
|
|
|
} else {
|
2019-08-26 22:17:23 -04:00
|
|
|
sql = fmt.Sprintf("INSERT INTO %s (%v) VALUES (%v)",
|
2020-03-22 11:12:55 -04:00
|
|
|
quoter.Quote(tableName),
|
|
|
|
colStr,
|
2017-04-06 21:47:25 -04:00
|
|
|
strings.Join(colMultiPlaces, "),("))
|
|
|
|
}
|
2018-07-19 22:10:17 -04:00
|
|
|
res, err := session.exec(sql, args...)
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2018-07-19 22:10:17 -04:00
|
|
|
session.cacheInsert(tableName)
|
2017-01-03 03:20:28 -05:00
|
|
|
|
|
|
|
lenAfterClosures := len(session.afterClosures)
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
elemValue := reflect.Indirect(sliceValue.Index(i)).Addr().Interface()
|
|
|
|
|
|
|
|
// handle AfterInsertProcessor
|
2017-08-22 07:39:52 -04:00
|
|
|
if session.isAutoCommit {
|
2017-01-03 03:20:28 -05:00
|
|
|
// !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)
|
|
|
|
}
|
2020-06-15 16:46:01 -04:00
|
|
|
if processor, ok := elemValue.(AfterInsertProcessor); ok {
|
2017-01-03 03:20:28 -05:00
|
|
|
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 {
|
2020-06-15 16:46:01 -04:00
|
|
|
if _, ok := elemValue.(AfterInsertProcessor); ok {
|
2017-01-03 03:20:28 -05:00
|
|
|
session.afterInsertBeans[elemValue] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanupProcessorsClosures(&session.afterClosures)
|
|
|
|
return res.RowsAffected()
|
|
|
|
}
|
|
|
|
|
|
|
|
// InsertMulti insert multiple records
|
|
|
|
func (session *Session) InsertMulti(rowsSlicePtr interface{}) (int64, error) {
|
2017-08-22 07:39:52 -04:00
|
|
|
if session.isAutoClose {
|
2017-01-03 03:20:28 -05:00
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
|
|
|
|
if sliceValue.Kind() != reflect.Slice {
|
2020-06-15 16:46:01 -04:00
|
|
|
return 0, ErrPtrSliceType
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
2021-08-12 19:11:42 -04:00
|
|
|
return session.insertMultipleStruct(rowsSlicePtr)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
2021-08-12 19:11:42 -04:00
|
|
|
func (session *Session) insertStruct(bean interface{}) (int64, error) {
|
2020-03-22 11:12:55 -04:00
|
|
|
if err := session.statement.SetRefBean(bean); err != nil {
|
2017-05-01 20:50:33 -04:00
|
|
|
return 0, err
|
|
|
|
}
|
2017-08-22 07:39:52 -04:00
|
|
|
if len(session.statement.TableName()) <= 0 {
|
2017-01-03 03:20:28 -05:00
|
|
|
return 0, ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle BeforeInsertProcessor
|
|
|
|
for _, closure := range session.beforeClosures {
|
|
|
|
closure(bean)
|
|
|
|
}
|
|
|
|
cleanupProcessorsClosures(&session.beforeClosures) // cleanup after used
|
|
|
|
|
|
|
|
if processor, ok := interface{}(bean).(BeforeInsertProcessor); ok {
|
|
|
|
processor.BeforeInsert()
|
|
|
|
}
|
2018-07-19 22:10:17 -04:00
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
var tableName = session.statement.TableName()
|
|
|
|
table := session.statement.RefTable
|
|
|
|
|
2018-07-19 22:10:17 -04:00
|
|
|
colNames, args, err := session.genInsertColumns(bean)
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
sqlStr, args, err := session.statement.GenInsertSQL(colNames, args)
|
|
|
|
if err != nil {
|
2019-09-24 09:22:39 -04:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2017-01-03 03:20:28 -05:00
|
|
|
handleAfterInsertProcessorFunc := func(bean interface{}) {
|
2017-08-22 07:39:52 -04:00
|
|
|
if session.isAutoCommit {
|
2017-01-03 03:20:28 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-12 19:11:42 -04:00
|
|
|
// if there is auto increment column and driver don't support return it
|
|
|
|
if len(table.AutoIncrement) > 0 && !session.engine.driver.Features().SupportReturnInsertedID {
|
|
|
|
var sql = sqlStr
|
|
|
|
if session.engine.dialect.URI().DBType == schemas.ORACLE {
|
|
|
|
sql = "select seq_atable.currval from dual"
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
2021-08-12 19:11:42 -04:00
|
|
|
rows, err := session.queryRows(sql, args...)
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-08-12 19:11:42 -04:00
|
|
|
defer rows.Close()
|
|
|
|
|
2018-01-27 10:20:59 -05:00
|
|
|
defer handleAfterInsertProcessorFunc(bean)
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2018-07-19 22:10:17 -04:00
|
|
|
session.cacheInsert(tableName)
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
if table.Version != "" && session.statement.CheckVersion {
|
2017-01-03 03:20:28 -05:00
|
|
|
verValue, err := table.VersionColumn().ValueOf(bean)
|
|
|
|
if err != nil {
|
2020-03-22 11:12:55 -04:00
|
|
|
session.engine.logger.Errorf("%v", err)
|
2017-01-03 03:20:28 -05:00
|
|
|
} else if verValue.IsValid() && verValue.CanSet() {
|
2018-12-11 20:01:41 -05:00
|
|
|
session.incrVersionFieldValue(verValue)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 19:11:42 -04:00
|
|
|
var id int64
|
|
|
|
if !rows.Next() {
|
|
|
|
if rows.Err() != nil {
|
|
|
|
return 0, rows.Err()
|
|
|
|
}
|
2019-06-23 11:22:43 -04:00
|
|
|
return 0, errors.New("insert successfully but not returned id")
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
2021-08-12 19:11:42 -04:00
|
|
|
if err := rows.Scan(&id); err != nil {
|
2017-01-03 03:20:28 -05:00
|
|
|
return 1, err
|
|
|
|
}
|
|
|
|
aiValue, err := table.AutoIncrColumn().ValueOf(bean)
|
|
|
|
if err != nil {
|
2020-03-22 11:12:55 -04:00
|
|
|
session.engine.logger.Errorf("%v", err)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if aiValue == nil || !aiValue.IsValid() || !aiValue.CanSet() {
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 19:11:42 -04:00
|
|
|
return 1, convert.AssignValue(*aiValue, id)
|
2020-03-22 11:12:55 -04:00
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
res, err := session.exec(sqlStr, args...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
defer handleAfterInsertProcessorFunc(bean)
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
session.cacheInsert(tableName)
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
if table.Version != "" && session.statement.CheckVersion {
|
|
|
|
verValue, err := table.VersionColumn().ValueOf(bean)
|
2017-01-03 03:20:28 -05:00
|
|
|
if err != nil {
|
2020-03-22 11:12:55 -04:00
|
|
|
session.engine.logger.Errorf("%v", err)
|
|
|
|
} else if verValue.IsValid() && verValue.CanSet() {
|
|
|
|
session.incrVersionFieldValue(verValue)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
2020-03-22 11:12:55 -04:00
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
if table.AutoIncrement == "" {
|
|
|
|
return res.RowsAffected()
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
var id int64
|
|
|
|
id, err = res.LastInsertId()
|
|
|
|
if err != nil || id <= 0 {
|
|
|
|
return res.RowsAffected()
|
|
|
|
}
|
2017-01-03 03:20:28 -05:00
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
aiValue, err := table.AutoIncrColumn().ValueOf(bean)
|
|
|
|
if err != nil {
|
|
|
|
session.engine.logger.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if aiValue == nil || !aiValue.IsValid() || !aiValue.CanSet() {
|
2017-01-03 03:20:28 -05:00
|
|
|
return res.RowsAffected()
|
|
|
|
}
|
2020-03-22 11:12:55 -04:00
|
|
|
|
2021-08-12 19:11:42 -04:00
|
|
|
if err := convert.AssignValue(*aiValue, id); err != nil {
|
2021-07-04 09:10:46 -04:00
|
|
|
return 0, err
|
|
|
|
}
|
2020-03-22 11:12:55 -04:00
|
|
|
|
|
|
|
return res.RowsAffected()
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2017-08-22 07:39:52 -04:00
|
|
|
if session.isAutoClose {
|
2017-01-03 03:20:28 -05:00
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
2021-08-12 19:11:42 -04:00
|
|
|
return session.insertStruct(bean)
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
|
|
|
|
2018-07-19 22:10:17 -04:00
|
|
|
func (session *Session) cacheInsert(table string) error {
|
|
|
|
if !session.statement.UseCache {
|
|
|
|
return nil
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
2020-03-22 11:12:55 -04:00
|
|
|
cacher := session.engine.cacherMgr.GetCacher(table)
|
2018-07-19 22:10:17 -04:00
|
|
|
if cacher == nil {
|
|
|
|
return nil
|
2017-01-03 03:20:28 -05:00
|
|
|
}
|
2020-06-15 16:46:01 -04:00
|
|
|
session.engine.logger.Debugf("[cache] clear SQL: %v", table)
|
2018-07-19 22:10:17 -04:00
|
|
|
cacher.ClearIds(table)
|
2017-01-03 03:20:28 -05:00
|
|
|
return nil
|
|
|
|
}
|
2018-07-19 22:10:17 -04:00
|
|
|
|
|
|
|
// 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() {
|
2020-03-22 11:12:55 -04:00
|
|
|
if col.MapType == schemas.ONLYFROMDB {
|
2018-07-19 22:10:17 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-03-22 11:12:55 -04:00
|
|
|
if session.statement.OmitColumnMap.Contain(col.Name) {
|
2018-07-19 22:10:17 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-03-22 11:12:55 -04:00
|
|
|
if len(session.statement.ColumnMap) > 0 && !session.statement.ColumnMap.Contain(col.Name) {
|
2018-07-19 22:10:17 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-03-22 11:12:55 -04:00
|
|
|
if session.statement.IncrColumns.IsColExist(col.Name) {
|
2018-07-19 22:10:17 -04:00
|
|
|
continue
|
2020-03-22 11:12:55 -04:00
|
|
|
} else if session.statement.DecrColumns.IsColExist(col.Name) {
|
2019-09-24 09:22:39 -04:00
|
|
|
continue
|
2020-03-22 11:12:55 -04:00
|
|
|
} else if session.statement.ExprColumns.IsColExist(col.Name) {
|
2018-07-19 22:10:17 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-12 19:11:42 -04:00
|
|
|
if col.IsDeleted {
|
|
|
|
arg, err := dialects.FormatColumnTime(session.engine.dialect, session.engine.DatabaseTZ, col, time.Time{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
args = append(args, arg)
|
|
|
|
colNames = append(colNames, col.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-07-19 22:10:17 -04:00
|
|
|
fieldValuePtr, err := col.ValueOf(bean)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
fieldValue := *fieldValuePtr
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
if col.IsAutoIncrement && utils.IsValueZero(fieldValue) {
|
|
|
|
continue
|
2018-07-19 22:10:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// !evalphobia! set fieldValue as nil when column is nullable and zero-value
|
2020-03-22 11:12:55 -04:00
|
|
|
if _, ok := getFlagForColumn(session.statement.NullableMap, col); ok {
|
|
|
|
if col.Nullable && utils.IsValueZero(fieldValue) {
|
2018-07-19 22:10:17 -04:00
|
|
|
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
|
2021-08-12 19:11:42 -04:00
|
|
|
val, t, err := session.engine.nowTime(col)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-07-19 22:10:17 -04:00
|
|
|
args = append(args, val)
|
|
|
|
|
|
|
|
var colName = col.Name
|
|
|
|
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
|
|
|
col := table.GetColumn(colName)
|
|
|
|
setColumnTime(bean, col, t)
|
|
|
|
})
|
2020-03-22 11:12:55 -04:00
|
|
|
} else if col.IsVersion && session.statement.CheckVersion {
|
2018-07-19 22:10:17 -04:00
|
|
|
args = append(args, 1)
|
|
|
|
} else {
|
2020-03-22 11:12:55 -04:00
|
|
|
arg, err := session.statement.Value2Interface(col, fieldValue)
|
2018-07-19 22:10:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return colNames, args, err
|
|
|
|
}
|
|
|
|
args = append(args, arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
colNames = append(colNames, col.Name)
|
|
|
|
}
|
|
|
|
return colNames, args, nil
|
|
|
|
}
|
2019-06-23 11:22:43 -04:00
|
|
|
|
|
|
|
func (session *Session) insertMapInterface(m map[string]interface{}) (int64, error) {
|
|
|
|
if len(m) == 0 {
|
|
|
|
return 0, ErrParamsType
|
|
|
|
}
|
|
|
|
|
2019-08-26 22:17:23 -04:00
|
|
|
tableName := session.statement.TableName()
|
|
|
|
if len(tableName) <= 0 {
|
|
|
|
return 0, ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
2019-06-23 11:22:43 -04:00
|
|
|
var columns = make([]string, 0, len(m))
|
2020-03-22 11:12:55 -04:00
|
|
|
exprs := session.statement.ExprColumns
|
2019-06-23 11:22:43 -04:00
|
|
|
for k := range m {
|
2020-03-22 11:12:55 -04:00
|
|
|
if !exprs.IsColExist(k) {
|
2019-09-24 09:22:39 -04:00
|
|
|
columns = append(columns, k)
|
|
|
|
}
|
2019-06-23 11:22:43 -04:00
|
|
|
}
|
|
|
|
sort.Strings(columns)
|
|
|
|
|
|
|
|
var args = make([]interface{}, 0, len(m))
|
|
|
|
for _, colName := range columns {
|
|
|
|
args = append(args, m[colName])
|
|
|
|
}
|
|
|
|
|
2020-01-20 10:45:14 -05:00
|
|
|
return session.insertMap(columns, args)
|
2019-06-23 11:22:43 -04:00
|
|
|
}
|
|
|
|
|
2021-08-12 19:11:42 -04:00
|
|
|
func (session *Session) insertMultipleMapInterface(maps []map[string]interface{}) (int64, error) {
|
|
|
|
if len(maps) <= 0 {
|
|
|
|
return 0, ErrNoElementsOnSlice
|
|
|
|
}
|
|
|
|
|
|
|
|
tableName := session.statement.TableName()
|
|
|
|
if len(tableName) <= 0 {
|
|
|
|
return 0, ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
var columns = make([]string, 0, len(maps[0]))
|
|
|
|
exprs := session.statement.ExprColumns
|
|
|
|
for k := range maps[0] {
|
|
|
|
if !exprs.IsColExist(k) {
|
|
|
|
columns = append(columns, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(columns)
|
|
|
|
|
|
|
|
var argss = make([][]interface{}, 0, len(maps))
|
|
|
|
for _, m := range maps {
|
|
|
|
var args = make([]interface{}, 0, len(m))
|
|
|
|
for _, colName := range columns {
|
|
|
|
args = append(args, m[colName])
|
|
|
|
}
|
|
|
|
argss = append(argss, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.insertMultipleMap(columns, argss)
|
|
|
|
}
|
|
|
|
|
2019-06-23 11:22:43 -04:00
|
|
|
func (session *Session) insertMapString(m map[string]string) (int64, error) {
|
|
|
|
if len(m) == 0 {
|
|
|
|
return 0, ErrParamsType
|
|
|
|
}
|
|
|
|
|
2019-08-26 22:17:23 -04:00
|
|
|
tableName := session.statement.TableName()
|
|
|
|
if len(tableName) <= 0 {
|
|
|
|
return 0, ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
2019-06-23 11:22:43 -04:00
|
|
|
var columns = make([]string, 0, len(m))
|
2020-03-22 11:12:55 -04:00
|
|
|
exprs := session.statement.ExprColumns
|
2019-06-23 11:22:43 -04:00
|
|
|
for k := range m {
|
2020-03-22 11:12:55 -04:00
|
|
|
if !exprs.IsColExist(k) {
|
2019-09-24 09:22:39 -04:00
|
|
|
columns = append(columns, k)
|
|
|
|
}
|
2019-06-23 11:22:43 -04:00
|
|
|
}
|
2020-01-20 10:45:14 -05:00
|
|
|
|
2019-06-23 11:22:43 -04:00
|
|
|
sort.Strings(columns)
|
|
|
|
|
2019-08-26 22:17:23 -04:00
|
|
|
var args = make([]interface{}, 0, len(m))
|
|
|
|
for _, colName := range columns {
|
|
|
|
args = append(args, m[colName])
|
|
|
|
}
|
|
|
|
|
2020-01-20 10:45:14 -05:00
|
|
|
return session.insertMap(columns, args)
|
|
|
|
}
|
|
|
|
|
2021-08-12 19:11:42 -04:00
|
|
|
func (session *Session) insertMultipleMapString(maps []map[string]string) (int64, error) {
|
|
|
|
if len(maps) <= 0 {
|
|
|
|
return 0, ErrNoElementsOnSlice
|
|
|
|
}
|
|
|
|
|
|
|
|
tableName := session.statement.TableName()
|
|
|
|
if len(tableName) <= 0 {
|
|
|
|
return 0, ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
var columns = make([]string, 0, len(maps[0]))
|
|
|
|
exprs := session.statement.ExprColumns
|
|
|
|
for k := range maps[0] {
|
|
|
|
if !exprs.IsColExist(k) {
|
|
|
|
columns = append(columns, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(columns)
|
|
|
|
|
|
|
|
var argss = make([][]interface{}, 0, len(maps))
|
|
|
|
for _, m := range maps {
|
|
|
|
var args = make([]interface{}, 0, len(m))
|
|
|
|
for _, colName := range columns {
|
|
|
|
args = append(args, m[colName])
|
|
|
|
}
|
|
|
|
argss = append(argss, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.insertMultipleMap(columns, argss)
|
|
|
|
}
|
|
|
|
|
2020-01-20 10:45:14 -05:00
|
|
|
func (session *Session) insertMap(columns []string, args []interface{}) (int64, error) {
|
|
|
|
tableName := session.statement.TableName()
|
|
|
|
if len(tableName) <= 0 {
|
|
|
|
return 0, ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
2020-06-15 16:46:01 -04:00
|
|
|
sql, args, err := session.statement.GenInsertMapSQL(columns, args)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2019-06-23 11:22:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-08-12 19:11:42 -04:00
|
|
|
|
|
|
|
func (session *Session) insertMultipleMap(columns []string, argss [][]interface{}) (int64, error) {
|
|
|
|
tableName := session.statement.TableName()
|
|
|
|
if len(tableName) <= 0 {
|
|
|
|
return 0, ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
sql, args, err := session.statement.GenInsertMultipleMapSQL(columns, argss)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|