1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-18 09:40:56 +00:00
Beego/orm/orm_object.go
2013-08-01 15:51:53 +08:00

54 lines
1016 B
Go

package orm
import (
"database/sql"
"fmt"
"reflect"
)
type insertSet struct {
mi *modelInfo
orm *orm
stmt *sql.Stmt
closed bool
}
func (o *insertSet) Insert(md Modeler) (int64, error) {
if o.closed {
return 0, ErrStmtClosed
}
md.Init(md, true)
val := reflect.ValueOf(md)
ind := reflect.Indirect(val)
if val.Type() != o.mi.addrField.Type() {
panic(fmt.Sprintf("<Inserter.Insert> need type `%s` but found `%s`", o.mi.addrField.Type(), val.Type()))
}
id, err := o.orm.alias.DbBaser.InsertStmt(o.stmt, o.mi, ind)
if err != nil {
return id, err
}
if id > 0 {
if o.mi.fields.auto != nil {
ind.Field(o.mi.fields.auto.fieldIndex).SetInt(id)
}
}
return id, nil
}
func (o *insertSet) Close() error {
o.closed = true
return o.stmt.Close()
}
func newInsertSet(orm *orm, mi *modelInfo) (Inserter, error) {
bi := new(insertSet)
bi.orm = orm
bi.mi = mi
st, err := orm.alias.DbBaser.PrepareInsert(orm.db, mi)
if err != nil {
return nil, err
}
bi.stmt = st
return bi, nil
}