1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 14:50:55 +00:00

now object crud is simple

This commit is contained in:
slene 2013-08-01 15:51:53 +08:00
parent 250cbf593b
commit 51baa35df1
4 changed files with 90 additions and 58 deletions

View File

@ -559,6 +559,42 @@ func (d *dbBase) InsertStmt(stmt *sql.Stmt, mi *modelInfo, ind reflect.Value) (i
}
}
func (d *dbBase) Read(q dbQuerier, mi *modelInfo, ind reflect.Value) error {
pkNames, pkValues, ok := d.existPk(mi, ind)
if ok == false {
return ErrMissPK
}
pkColumns := strings.Join(pkNames, "` = ? AND `")
sels := strings.Join(mi.fields.dbcols, "`, `")
colsNum := len(mi.fields.dbcols)
query := fmt.Sprintf("SELECT `%s` FROM `%s` WHERE `%s` = ?", sels, mi.table, pkColumns)
refs := make([]interface{}, colsNum)
for i, _ := range refs {
var ref interface{}
refs[i] = &ref
}
row := q.QueryRow(query, pkValues...)
if err := row.Scan(refs...); err != nil {
return err
} else {
elm := reflect.New(mi.addrField.Elem().Type())
md := elm.Interface().(Modeler)
md.Init(md)
mind := reflect.Indirect(elm)
d.setColsValues(mi, &mind, mi.fields.dbcols, refs)
ind.Set(mind)
}
return nil
}
func (d *dbBase) Insert(q dbQuerier, mi *modelInfo, ind reflect.Value) (int64, error) {
names, values, err := d.collectValues(mi, ind, true, true)
if err != nil {

View File

@ -4,6 +4,7 @@ import (
"database/sql"
"errors"
"fmt"
"reflect"
"time"
)
@ -26,15 +27,61 @@ type orm struct {
isTx bool
}
func (o *orm) Object(md Modeler) ObjectSeter {
func (o *orm) getMiInd(md Modeler) (mi *modelInfo, ind reflect.Value) {
md.Init(md, true)
name := md.GetTableName()
if mi, ok := modelCache.get(name); ok {
return newObject(o, mi, md)
return mi, reflect.Indirect(reflect.ValueOf(md))
}
panic(fmt.Sprintf("<orm.Object> table name: `%s` not exists", name))
}
func (o *orm) Read(md Modeler) error {
mi, ind := o.getMiInd(md)
err := o.alias.DbBaser.Read(o.db, mi, ind)
if err != nil {
return err
}
return nil
}
func (o *orm) Insert(md Modeler) (int64, error) {
mi, ind := o.getMiInd(md)
id, err := o.alias.DbBaser.Insert(o.db, mi, ind)
if err != nil {
return id, err
}
if id > 0 {
if mi.fields.auto != nil {
ind.Field(mi.fields.auto.fieldIndex).SetInt(id)
}
}
return id, nil
}
func (o *orm) Update(md Modeler) (int64, error) {
mi, ind := o.getMiInd(md)
num, err := o.alias.DbBaser.Update(o.db, mi, ind)
if err != nil {
return num, err
}
return num, nil
}
func (o *orm) Delete(md Modeler) (int64, error) {
mi, ind := o.getMiInd(md)
num, err := o.alias.DbBaser.Delete(o.db, mi, ind)
if err != nil {
return num, err
}
if num > 0 {
if mi.fields.auto != nil {
ind.Field(mi.fields.auto.fieldIndex).SetInt(0)
}
}
return num, nil
}
func (o *orm) QueryTable(ptrStructOrTableName interface{}) QuerySeter {
name := ""
if table, ok := ptrStructOrTableName.(string); ok {

View File

@ -51,52 +51,3 @@ func newInsertSet(orm *orm, mi *modelInfo) (Inserter, error) {
bi.stmt = st
return bi, nil
}
type object struct {
ind reflect.Value
mi *modelInfo
orm *orm
}
func (o *object) Insert() (int64, error) {
id, err := o.orm.alias.DbBaser.Insert(o.orm.db, o.mi, o.ind)
if err != nil {
return id, err
}
if id > 0 {
if o.mi.fields.auto != nil {
o.ind.Field(o.mi.fields.auto.fieldIndex).SetInt(id)
}
}
return id, nil
}
func (o *object) Update() (int64, error) {
num, err := o.orm.alias.DbBaser.Update(o.orm.db, o.mi, o.ind)
if err != nil {
return num, err
}
return num, nil
}
func (o *object) Delete() (int64, error) {
num, err := o.orm.alias.DbBaser.Delete(o.orm.db, o.mi, o.ind)
if err != nil {
return num, err
}
if num > 0 {
if o.mi.fields.auto != nil {
o.ind.Field(o.mi.fields.auto.fieldIndex).SetInt(0)
}
}
return num, nil
}
func newObject(orm *orm, mi *modelInfo, md Modeler) ObjectSeter {
o := new(object)
ind := reflect.Indirect(reflect.ValueOf(md))
o.ind = ind
o.mi = mi
o.orm = orm
return o
}

View File

@ -22,7 +22,10 @@ type Modeler interface {
}
type Ormer interface {
Object(Modeler) ObjectSeter
Read(Modeler) error
Insert(Modeler) (int64, error)
Update(Modeler) (int64, error)
Delete(Modeler) (int64, error)
QueryTable(interface{}) QuerySeter
Using(string) error
Begin() error
@ -31,12 +34,6 @@ type Ormer interface {
Raw(string, ...interface{}) RawSeter
}
type ObjectSeter interface {
Insert() (int64, error)
Update() (int64, error)
Delete() (int64, error)
}
type Inserter interface {
Insert(Modeler) (int64, error)
Close() error
@ -94,6 +91,7 @@ type dbQuerier interface {
}
type dbBaser interface {
Read(dbQuerier, *modelInfo, reflect.Value) error
Insert(dbQuerier, *modelInfo, reflect.Value) (int64, error)
InsertStmt(*sql.Stmt, *modelInfo, reflect.Value) (int64, error)
Update(dbQuerier, *modelInfo, reflect.Value) (int64, error)