1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-14 07:10:56 +00:00
Beego/orm/orm_object.go

88 lines
2.1 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-07-30 12:32:38 +00:00
package orm
import (
"fmt"
"reflect"
)
2014-01-17 15:28:54 +00:00
// an insert queryer struct
2013-07-30 12:32:38 +00:00
type insertSet struct {
mi *modelInfo
orm *orm
2013-08-09 05:20:19 +00:00
stmt stmtQuerier
2013-07-30 12:32:38 +00:00
closed bool
}
2013-08-07 11:11:44 +00:00
var _ Inserter = new(insertSet)
2014-01-17 15:28:54 +00:00
// insert model ignore it's registered or not.
func (o *insertSet) Insert(md interface{}) (int64, error) {
2013-07-30 12:32:38 +00:00
if o.closed {
return 0, ErrStmtClosed
}
val := reflect.ValueOf(md)
ind := reflect.Indirect(val)
typ := ind.Type()
name := getFullName(typ)
if val.Kind() != reflect.Ptr {
2013-10-09 03:37:16 +00:00
panic(fmt.Errorf("<Inserter.Insert> cannot use non-ptr model struct `%s`", name))
}
if name != o.mi.fullName {
2013-10-09 03:37:16 +00:00
panic(fmt.Errorf("<Inserter.Insert> need model `%s` but found `%s`", o.mi.fullName, name))
2013-07-30 12:32:38 +00:00
}
id, err := o.orm.alias.DbBaser.InsertStmt(o.stmt, o.mi, ind, o.orm.alias.TZ)
2013-07-30 12:32:38 +00:00
if err != nil {
return id, err
}
if id > 0 {
2013-08-07 11:11:44 +00:00
if o.mi.fields.pk.auto {
2016-03-17 13:34:49 +00:00
if o.mi.fields.pk.fieldType&IsPositiveIntegerField > 0 {
2016-02-24 10:46:14 +00:00
ind.FieldByIndex(o.mi.fields.pk.fieldIndex).SetUint(uint64(id))
2013-08-30 04:32:05 +00:00
} else {
2016-02-24 10:46:14 +00:00
ind.FieldByIndex(o.mi.fields.pk.fieldIndex).SetInt(id)
2013-08-30 04:32:05 +00:00
}
2013-07-30 12:32:38 +00:00
}
}
return id, nil
}
2014-01-17 15:28:54 +00:00
// close insert queryer statement
2013-07-30 12:32:38 +00:00
func (o *insertSet) Close() error {
2013-08-07 11:11:44 +00:00
if o.closed {
return ErrStmtClosed
}
2013-07-30 12:32:38 +00:00
o.closed = true
return o.stmt.Close()
}
2014-01-17 15:28:54 +00:00
// create new insert queryer.
2013-07-30 12:32:38 +00:00
func newInsertSet(orm *orm, mi *modelInfo) (Inserter, error) {
bi := new(insertSet)
bi.orm = orm
bi.mi = mi
2013-08-09 05:20:19 +00:00
st, query, err := orm.alias.DbBaser.PrepareInsert(orm.db, mi)
2013-07-30 12:32:38 +00:00
if err != nil {
return nil, err
}
2013-08-09 05:20:19 +00:00
if Debug {
bi.stmt = newStmtQueryLog(orm.alias, st, query)
} else {
bi.stmt = st
}
2013-07-30 12:32:38 +00:00
return bi, nil
}