1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-02 00:21:28 +00:00
Beego/orm/orm.go

222 lines
4.5 KiB
Go
Raw Normal View History

2013-07-30 12:32:38 +00:00
package orm
import (
"database/sql"
"errors"
"fmt"
2013-08-09 05:20:19 +00:00
"os"
2013-08-01 07:51:53 +00:00
"reflect"
2013-07-30 12:32:38 +00:00
"time"
)
2013-08-09 05:20:19 +00:00
const (
Debug_Queries = iota
)
2013-07-30 12:32:38 +00:00
var (
2013-08-09 05:20:19 +00:00
// DebugLevel = Debug_Queries
2013-08-09 06:04:33 +00:00
Debug = false
2013-08-09 05:20:19 +00:00
DebugLog = NewLog(os.Stderr)
2013-07-30 12:32:38 +00:00
DefaultRowsLimit = 1000
DefaultRelsDepth = 5
DefaultTimeLoc = time.Local
2013-08-07 11:11:44 +00:00
ErrTxHasBegan = errors.New("<Ormer.Begin> transaction already begin")
ErrTxDone = errors.New("<Ormer.Commit/Rollback> transaction not begin")
ErrMultiRows = errors.New("<QuerySeter> return multi rows")
2013-08-09 05:20:19 +00:00
ErrNoRows = errors.New("<QuerySeter> no row found")
2013-08-07 11:11:44 +00:00
ErrStmtClosed = errors.New("<QuerySeter> stmt already closed")
ErrNotImplement = errors.New("have not implement")
2013-07-30 12:32:38 +00:00
)
type Params map[string]interface{}
type ParamsList []interface{}
type orm struct {
alias *alias
db dbQuerier
isTx bool
}
2013-08-07 11:11:44 +00:00
var _ Ormer = new(orm)
func (o *orm) getMiInd(md interface{}) (mi *modelInfo, ind reflect.Value) {
val := reflect.ValueOf(md)
ind = reflect.Indirect(val)
typ := ind.Type()
if val.Kind() != reflect.Ptr {
panic(fmt.Sprintf("<Ormer> cannot use non-ptr model struct `%s`", getFullName(typ)))
2013-07-30 12:32:38 +00:00
}
name := getFullName(typ)
if mi, ok := modelCache.getByFN(name); ok {
return mi, ind
}
panic(fmt.Sprintf("<Ormer> table: `%s` not found, maybe not RegisterModel", name))
2013-07-30 12:32:38 +00:00
}
func (o *orm) Read(md interface{}) error {
2013-08-01 07:51:53 +00:00
mi, ind := o.getMiInd(md)
err := o.alias.DbBaser.Read(o.db, mi, ind, o.alias.TZ)
2013-08-01 07:51:53 +00:00
if err != nil {
return err
}
return nil
}
func (o *orm) Insert(md interface{}) (int64, error) {
2013-08-01 07:51:53 +00:00
mi, ind := o.getMiInd(md)
id, err := o.alias.DbBaser.Insert(o.db, mi, ind, o.alias.TZ)
2013-08-01 07:51:53 +00:00
if err != nil {
return id, err
}
if id > 0 {
2013-08-07 11:11:44 +00:00
if mi.fields.pk.auto {
ind.Field(mi.fields.pk.fieldIndex).SetInt(id)
2013-08-01 07:51:53 +00:00
}
}
return id, nil
}
func (o *orm) Update(md interface{}) (int64, error) {
2013-08-01 07:51:53 +00:00
mi, ind := o.getMiInd(md)
num, err := o.alias.DbBaser.Update(o.db, mi, ind, o.alias.TZ)
2013-08-01 07:51:53 +00:00
if err != nil {
return num, err
}
return num, nil
}
func (o *orm) Delete(md interface{}) (int64, error) {
2013-08-01 07:51:53 +00:00
mi, ind := o.getMiInd(md)
num, err := o.alias.DbBaser.Delete(o.db, mi, ind, o.alias.TZ)
2013-08-01 07:51:53 +00:00
if err != nil {
return num, err
}
if num > 0 {
2013-08-07 11:11:44 +00:00
if mi.fields.pk.auto {
ind.Field(mi.fields.pk.fieldIndex).SetInt(0)
2013-08-01 07:51:53 +00:00
}
}
return num, nil
}
func (o *orm) M2mAdd(md interface{}, name string, mds ...interface{}) (int64, error) {
2013-08-07 11:11:44 +00:00
// TODO
panic(ErrNotImplement)
return 0, nil
}
func (o *orm) M2mDel(md interface{}, name string, mds ...interface{}) (int64, error) {
2013-08-07 11:11:44 +00:00
// TODO
panic(ErrNotImplement)
return 0, nil
}
func (o *orm) LoadRel(md interface{}, name string) (int64, error) {
2013-08-07 11:11:44 +00:00
// TODO
panic(ErrNotImplement)
return 0, nil
}
func (o *orm) QueryTable(ptrStructOrTableName interface{}) (qs QuerySeter) {
2013-07-30 12:32:38 +00:00
name := ""
if table, ok := ptrStructOrTableName.(string); ok {
name = snakeString(table)
if mi, ok := modelCache.get(name); ok {
qs = newQuerySet(o, mi)
}
} else {
val := reflect.ValueOf(ptrStructOrTableName)
ind := reflect.Indirect(val)
name = getFullName(ind.Type())
if mi, ok := modelCache.getByFN(name); ok {
qs = newQuerySet(o, mi)
}
2013-07-30 12:32:38 +00:00
}
if qs == nil {
panic(fmt.Sprintf("<Ormer.QueryTable> table name: `%s` not exists", name))
2013-07-30 12:32:38 +00:00
}
return
2013-07-30 12:32:38 +00:00
}
func (o *orm) Using(name string) error {
if o.isTx {
panic("<Ormer.Using> transaction has been start, cannot change db")
2013-07-30 12:32:38 +00:00
}
if al, ok := dataBaseCache.get(name); ok {
o.alias = al
2013-08-09 05:20:19 +00:00
if Debug {
o.db = newDbQueryLog(al, al.DB)
} else {
o.db = al.DB
}
2013-07-30 12:32:38 +00:00
} else {
return errors.New(fmt.Sprintf("<Ormer.Using> unknown db alias name `%s`", name))
2013-07-30 12:32:38 +00:00
}
return nil
}
func (o *orm) Begin() error {
if o.isTx {
2013-08-07 11:11:44 +00:00
return ErrTxHasBegan
2013-07-30 12:32:38 +00:00
}
2013-08-09 05:20:19 +00:00
var tx *sql.Tx
tx, err := o.db.(txer).Begin()
2013-07-30 12:32:38 +00:00
if err != nil {
return err
}
o.isTx = true
2013-08-09 05:20:19 +00:00
if Debug {
o.db.(*dbQueryLog).SetDB(tx)
} else {
o.db = tx
}
2013-07-30 12:32:38 +00:00
return nil
}
func (o *orm) Commit() error {
if o.isTx == false {
2013-08-07 11:11:44 +00:00
return ErrTxDone
2013-07-30 12:32:38 +00:00
}
2013-08-09 05:20:19 +00:00
err := o.db.(txEnder).Commit()
2013-07-30 12:32:38 +00:00
if err == nil {
o.isTx = false
2013-08-09 05:20:19 +00:00
o.Using(o.alias.Name)
2013-08-07 11:11:44 +00:00
} else if err == sql.ErrTxDone {
return ErrTxDone
2013-07-30 12:32:38 +00:00
}
return err
}
func (o *orm) Rollback() error {
if o.isTx == false {
2013-08-07 11:11:44 +00:00
return ErrTxDone
2013-07-30 12:32:38 +00:00
}
2013-08-09 05:20:19 +00:00
err := o.db.(txEnder).Rollback()
2013-07-30 12:32:38 +00:00
if err == nil {
o.isTx = false
2013-08-09 05:20:19 +00:00
o.Using(o.alias.Name)
2013-08-07 11:11:44 +00:00
} else if err == sql.ErrTxDone {
return ErrTxDone
2013-07-30 12:32:38 +00:00
}
return err
}
func (o *orm) Raw(query string, args ...interface{}) RawSeter {
return newRawSet(o, query, args)
}
2013-08-07 11:11:44 +00:00
func (o *orm) Driver() Driver {
return driver(o.alias.Name)
}
2013-07-30 12:32:38 +00:00
func NewOrm() Ormer {
2013-08-07 11:11:44 +00:00
BootStrap() // execute only once
2013-07-30 12:32:38 +00:00
o := new(orm)
err := o.Using("default")
if err != nil {
panic(err)
}
return o
}