2017-01-03 08:20:28 +00: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"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-xorm/core"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Insert insert one or more beans
|
|
|
|
func (session *Session) Insert(beans ...interface{}) (int64, error) {
|
|
|
|
var affected int64
|
|
|
|
var err error
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
if session.isAutoClose {
|
2017-01-03 08:20:28 +00:00
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
defer session.resetStatement()
|
|
|
|
|
|
|
|
for _, bean := range beans {
|
|
|
|
sliceValue := reflect.Indirect(reflect.ValueOf(bean))
|
|
|
|
if sliceValue.Kind() == reflect.Slice {
|
|
|
|
size := sliceValue.Len()
|
|
|
|
if size > 0 {
|
2017-08-22 11:39:52 +00:00
|
|
|
if session.engine.SupportInsertMany() {
|
2017-01-03 08:20:28 +00:00
|
|
|
cnt, err := session.innerInsertMulti(bean)
|
|
|
|
if err != nil {
|
|
|
|
return affected, err
|
|
|
|
}
|
|
|
|
affected += cnt
|
|
|
|
} else {
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
cnt, err := session.innerInsert(sliceValue.Index(i).Interface())
|
|
|
|
if err != nil {
|
|
|
|
return affected, err
|
|
|
|
}
|
|
|
|
affected += cnt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cnt, err := session.innerInsert(bean)
|
|
|
|
if err != nil {
|
|
|
|
return affected, err
|
|
|
|
}
|
|
|
|
affected += cnt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
if err := session.statement.setRefValue(reflect.ValueOf(sliceValue.Index(0).Interface())); err != nil {
|
2017-05-02 00:50:33 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
2017-01-03 08:20:28 +00:00
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
if len(session.statement.TableName()) <= 0 {
|
2017-01-03 08:20:28 +00:00
|
|
|
return 0, ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
table := session.statement.RefTable
|
2017-01-03 08:20:28 +00:00
|
|
|
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
|
|
|
|
}
|
2017-08-22 11:39:52 +00:00
|
|
|
if session.statement.ColumnStr != "" {
|
|
|
|
if _, ok := getFlagForColumn(session.statement.columnMap, col); !ok {
|
2017-01-03 08:20:28 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2017-08-22 11:39:52 +00:00
|
|
|
if session.statement.OmitStr != "" {
|
|
|
|
if _, ok := getFlagForColumn(session.statement.columnMap, col); ok {
|
2017-01-03 08:20:28 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2017-08-22 11:39:52 +00:00
|
|
|
if (col.IsCreated || col.IsUpdated) && session.statement.UseAutoTime {
|
|
|
|
val, t := session.engine.NowTime2(col.SQLType.Name)
|
2017-01-03 08:20:28 +00: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)
|
|
|
|
})
|
2017-08-22 11:39:52 +00:00
|
|
|
} else if col.IsVersion && session.statement.checkVersion {
|
2017-01-03 08:20:28 +00:00
|
|
|
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
|
|
|
|
}
|
2017-08-22 11:39:52 +00:00
|
|
|
if session.statement.ColumnStr != "" {
|
|
|
|
if _, ok := getFlagForColumn(session.statement.columnMap, col); !ok {
|
2017-01-03 08:20:28 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2017-08-22 11:39:52 +00:00
|
|
|
if session.statement.OmitStr != "" {
|
|
|
|
if _, ok := getFlagForColumn(session.statement.columnMap, col); ok {
|
2017-01-03 08:20:28 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2017-08-22 11:39:52 +00:00
|
|
|
if (col.IsCreated || col.IsUpdated) && session.statement.UseAutoTime {
|
|
|
|
val, t := session.engine.NowTime2(col.SQLType.Name)
|
2017-01-03 08:20:28 +00: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)
|
|
|
|
})
|
2017-08-22 11:39:52 +00:00
|
|
|
} else if col.IsVersion && session.statement.checkVersion {
|
2017-01-03 08:20:28 +00:00
|
|
|
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)
|
|
|
|
|
2017-04-07 01:47:25 +00:00
|
|
|
var sql = "INSERT INTO %s (%v%v%v) VALUES (%v)"
|
|
|
|
var statement string
|
2017-08-22 11:39:52 +00:00
|
|
|
if session.engine.dialect.DBType() == core.ORACLE {
|
2017-04-07 01:47:25 +00:00
|
|
|
sql = "INSERT ALL INTO %s (%v%v%v) VALUES (%v) SELECT 1 FROM DUAL"
|
|
|
|
temp := fmt.Sprintf(") INTO %s (%v%v%v) VALUES (",
|
2017-08-22 11:39:52 +00:00
|
|
|
session.engine.Quote(session.statement.TableName()),
|
|
|
|
session.engine.QuoteStr(),
|
|
|
|
strings.Join(colNames, session.engine.QuoteStr()+", "+session.engine.QuoteStr()),
|
|
|
|
session.engine.QuoteStr())
|
2017-04-07 01:47:25 +00:00
|
|
|
statement = fmt.Sprintf(sql,
|
2017-08-22 11:39:52 +00:00
|
|
|
session.engine.Quote(session.statement.TableName()),
|
|
|
|
session.engine.QuoteStr(),
|
|
|
|
strings.Join(colNames, session.engine.QuoteStr()+", "+session.engine.QuoteStr()),
|
|
|
|
session.engine.QuoteStr(),
|
2017-04-07 01:47:25 +00:00
|
|
|
strings.Join(colMultiPlaces, temp))
|
|
|
|
} else {
|
|
|
|
statement = fmt.Sprintf(sql,
|
2017-08-22 11:39:52 +00:00
|
|
|
session.engine.Quote(session.statement.TableName()),
|
|
|
|
session.engine.QuoteStr(),
|
|
|
|
strings.Join(colNames, session.engine.QuoteStr()+", "+session.engine.QuoteStr()),
|
|
|
|
session.engine.QuoteStr(),
|
2017-04-07 01:47:25 +00:00
|
|
|
strings.Join(colMultiPlaces, "),("))
|
|
|
|
}
|
2017-01-03 08:20:28 +00:00
|
|
|
res, err := session.exec(statement, args...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
if cacher := session.engine.getCacher2(table); cacher != nil && session.statement.UseCache {
|
|
|
|
session.cacheInsert(session.statement.TableName())
|
2017-01-03 08:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lenAfterClosures := len(session.afterClosures)
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
elemValue := reflect.Indirect(sliceValue.Index(i)).Addr().Interface()
|
|
|
|
|
|
|
|
// handle AfterInsertProcessor
|
2017-08-22 11:39:52 +00:00
|
|
|
if session.isAutoCommit {
|
2017-01-03 08:20:28 +00: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)
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
defer session.resetStatement()
|
2017-08-22 11:39:52 +00:00
|
|
|
if session.isAutoClose {
|
2017-01-03 08:20:28 +00:00
|
|
|
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) {
|
2017-08-22 11:39:52 +00:00
|
|
|
if err := session.statement.setRefValue(rValue(bean)); err != nil {
|
2017-05-02 00:50:33 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
2017-08-22 11:39:52 +00:00
|
|
|
if len(session.statement.TableName()) <= 0 {
|
2017-01-03 08:20:28 +00:00
|
|
|
return 0, ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
table := session.statement.RefTable
|
2017-01-03 08:20:28 +00:00
|
|
|
|
|
|
|
// handle BeforeInsertProcessor
|
|
|
|
for _, closure := range session.beforeClosures {
|
|
|
|
closure(bean)
|
|
|
|
}
|
|
|
|
cleanupProcessorsClosures(&session.beforeClosures) // cleanup after used
|
|
|
|
|
|
|
|
if processor, ok := interface{}(bean).(BeforeInsertProcessor); ok {
|
|
|
|
processor.BeforeInsert()
|
|
|
|
}
|
|
|
|
// --
|
2017-08-22 11:39:52 +00:00
|
|
|
colNames, args, err := genCols(session.statement.RefTable, session, bean, false, false)
|
2017-01-03 08:20:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
// insert expr columns, override if exists
|
2017-08-22 11:39:52 +00:00
|
|
|
exprColumns := session.statement.getExpr()
|
2017-01-03 08:20:28 +00:00
|
|
|
exprColVals := make([]string, 0, len(exprColumns))
|
|
|
|
for _, v := range exprColumns {
|
|
|
|
// remove the expr columns
|
|
|
|
for i, colName := range colNames {
|
|
|
|
if colName == v.colName {
|
2017-05-02 00:50:33 +00:00
|
|
|
colNames = append(colNames[:i], colNames[i+1:]...)
|
|
|
|
args = append(args[:i], args[i+1:]...)
|
2017-01-03 08:20:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// append expr column to the end
|
|
|
|
colNames = append(colNames, v.colName)
|
|
|
|
exprColVals = append(exprColVals, v.expr)
|
|
|
|
}
|
|
|
|
|
2017-05-02 00:50:33 +00:00
|
|
|
colPlaces := strings.Repeat("?, ", len(colNames)-len(exprColumns))
|
2017-01-03 08:20:28 +00:00
|
|
|
if len(exprColVals) > 0 {
|
|
|
|
colPlaces = colPlaces + strings.Join(exprColVals, ", ")
|
|
|
|
} else {
|
2017-08-22 11:39:52 +00:00
|
|
|
if len(colPlaces) > 0 {
|
|
|
|
colPlaces = colPlaces[0 : len(colPlaces)-2]
|
|
|
|
}
|
2017-01-03 08:20:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
var sqlStr string
|
|
|
|
if len(colPlaces) > 0 {
|
|
|
|
sqlStr = fmt.Sprintf("INSERT INTO %s (%v%v%v) VALUES (%v)",
|
|
|
|
session.engine.Quote(session.statement.TableName()),
|
|
|
|
session.engine.QuoteStr(),
|
|
|
|
strings.Join(colNames, session.engine.Quote(", ")),
|
|
|
|
session.engine.QuoteStr(),
|
|
|
|
colPlaces)
|
|
|
|
} else {
|
|
|
|
if session.engine.dialect.DBType() == core.MYSQL {
|
|
|
|
sqlStr = fmt.Sprintf("INSERT INTO %s VALUES ()", session.engine.Quote(session.statement.TableName()))
|
|
|
|
} else {
|
|
|
|
sqlStr = fmt.Sprintf("INSERT INTO %s DEFAULT VALUES", session.engine.Quote(session.statement.TableName()))
|
|
|
|
}
|
|
|
|
}
|
2017-01-03 08:20:28 +00:00
|
|
|
|
|
|
|
handleAfterInsertProcessorFunc := func(bean interface{}) {
|
2017-08-22 11:39:52 +00:00
|
|
|
if session.isAutoCommit {
|
2017-01-03 08:20:28 +00: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
|
|
|
|
}
|
|
|
|
|
|
|
|
// for postgres, many of them didn't implement lastInsertId, so we should
|
|
|
|
// implemented it ourself.
|
2017-08-22 11:39:52 +00:00
|
|
|
if session.engine.dialect.DBType() == core.ORACLE && len(table.AutoIncrement) > 0 {
|
2017-01-03 08:20:28 +00:00
|
|
|
res, err := session.query("select seq_atable.currval from dual", args...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
handleAfterInsertProcessorFunc(bean)
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
if cacher := session.engine.getCacher2(table); cacher != nil && session.statement.UseCache {
|
|
|
|
session.cacheInsert(session.statement.TableName())
|
2017-01-03 08:20:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
if table.Version != "" && session.statement.checkVersion {
|
2017-01-03 08:20:28 +00:00
|
|
|
verValue, err := table.VersionColumn().ValueOf(bean)
|
|
|
|
if err != nil {
|
2017-08-22 11:39:52 +00:00
|
|
|
session.engine.logger.Error(err)
|
2017-01-03 08:20:28 +00:00
|
|
|
} else if verValue.IsValid() && verValue.CanSet() {
|
|
|
|
verValue.SetInt(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-08-22 11:39:52 +00:00
|
|
|
session.engine.logger.Error(err)
|
2017-01-03 08:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if aiValue == nil || !aiValue.IsValid() || !aiValue.CanSet() {
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
aiValue.Set(int64ToIntValue(id, aiValue.Type()))
|
|
|
|
|
|
|
|
return 1, nil
|
2017-08-22 11:39:52 +00:00
|
|
|
} else if session.engine.dialect.DBType() == core.POSTGRES && len(table.AutoIncrement) > 0 {
|
2017-01-03 08:20:28 +00:00
|
|
|
//assert table.AutoIncrement != ""
|
2017-08-22 11:39:52 +00:00
|
|
|
sqlStr = sqlStr + " RETURNING " + session.engine.Quote(table.AutoIncrement)
|
2017-01-03 08:20:28 +00:00
|
|
|
res, err := session.query(sqlStr, args...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
handleAfterInsertProcessorFunc(bean)
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
if cacher := session.engine.getCacher2(table); cacher != nil && session.statement.UseCache {
|
|
|
|
session.cacheInsert(session.statement.TableName())
|
2017-01-03 08:20:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
if table.Version != "" && session.statement.checkVersion {
|
2017-01-03 08:20:28 +00:00
|
|
|
verValue, err := table.VersionColumn().ValueOf(bean)
|
|
|
|
if err != nil {
|
2017-08-22 11:39:52 +00:00
|
|
|
session.engine.logger.Error(err)
|
2017-01-03 08:20:28 +00:00
|
|
|
} else if verValue.IsValid() && verValue.CanSet() {
|
|
|
|
verValue.SetInt(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-08-22 11:39:52 +00:00
|
|
|
session.engine.logger.Error(err)
|
2017-01-03 08:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
if cacher := session.engine.getCacher2(table); cacher != nil && session.statement.UseCache {
|
|
|
|
session.cacheInsert(session.statement.TableName())
|
2017-01-03 08:20:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
if table.Version != "" && session.statement.checkVersion {
|
2017-01-03 08:20:28 +00:00
|
|
|
verValue, err := table.VersionColumn().ValueOf(bean)
|
|
|
|
if err != nil {
|
2017-08-22 11:39:52 +00:00
|
|
|
session.engine.logger.Error(err)
|
2017-01-03 08:20:28 +00:00
|
|
|
} else if verValue.IsValid() && verValue.CanSet() {
|
|
|
|
verValue.SetInt(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-08-22 11:39:52 +00:00
|
|
|
session.engine.logger.Error(err)
|
2017-01-03 08:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
defer session.resetStatement()
|
2017-08-22 11:39:52 +00:00
|
|
|
if session.isAutoClose {
|
2017-01-03 08:20:28 +00:00
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.innerInsert(bean)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) cacheInsert(tables ...string) error {
|
2017-08-22 11:39:52 +00:00
|
|
|
if session.statement.RefTable == nil {
|
2017-01-03 08:20:28 +00:00
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
2017-08-22 11:39:52 +00:00
|
|
|
table := session.statement.RefTable
|
|
|
|
cacher := session.engine.getCacher2(table)
|
2017-01-03 08:20:28 +00:00
|
|
|
|
|
|
|
for _, t := range tables {
|
2017-08-22 11:39:52 +00:00
|
|
|
session.engine.logger.Debug("[cache] clear sql:", t)
|
2017-01-03 08:20:28 +00:00
|
|
|
cacher.ClearIds(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|