Skip to content

Commit

Permalink
Bumped version v0.2
Browse files Browse the repository at this point in the history
Includes destructive changes.

- added session management per request
    - need an identifier for argument to use every session
  • Loading branch information
evalphobia committed Nov 17, 2015
1 parent 15072e4 commit 1d48a17
Show file tree
Hide file tree
Showing 14 changed files with 792 additions and 624 deletions.
26 changes: 13 additions & 13 deletions orm/xorm/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func ExampleInsert() {
orm := New(w)

user := &User{ID: 99, Name: "Adam Smith"}
total, err := orm.Insert(user, func(s Session) (int64, error) {
return s.Insert(user)
total, err := orm.Insert(testID, user, func(s Session) (int64, error) {
return s.Insert(testID, user)
})
if err != nil {
fmt.Printf("error occured, %s", err.Error())
Expand All @@ -93,20 +93,20 @@ func ExampleTransaction() {
user1 := &User{ID: 1, Name: "Adam Smith"}
user2 := &User{ID: 2, Name: "Benjamin Franklin"}

s1, _ := orm.Transaction(user1)
s2, _ := orm.Transaction(user2)
s1, _ := orm.Transaction(testID, user1)
s2, _ := orm.Transaction(testID, user2)

_, err = s1.Insert(user1)
if err != nil {
orm.RollbackAll()
orm.RollbackAll(testID)
return
}
_, err = s2.Insert(user2)
if err != nil {
orm.RollbackAll()
orm.RollbackAll(testID)
return
}
orm.CommitAll()
orm.CommitAll(testID)
}

func ExampleTransactionAuto() {
Expand All @@ -116,20 +116,20 @@ func ExampleTransactionAuto() {
user1 := &User{ID: 1, Name: "Adam Smith"}
user2 := &User{ID: 2, Name: "Benjamin Franklin"}

orm.SetAutoTransaction(true)
_, err = orm.Insert(user1, func(s Session) (int64, error) {
orm.SetAutoTransaction(testID, true)
_, err = orm.Insert(testID, user1, func(s Session) (int64, error) {
return s.Insert(user1)
})
if err != nil {
orm.RollbackAll()
orm.RollbackAll(testID)
return
}
_, err = orm.Insert(user2, func(s Session) (int64, error) {
_, err = orm.Insert(testID, user2, func(s Session) (int64, error) {
return s.Insert(user2)
})
if err != nil {
orm.RollbackAll()
orm.RollbackAll(testID)
return
}
orm.CommitAll()
orm.CommitAll(testID)
}
40 changes: 21 additions & 19 deletions orm/xorm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (

// ORM is wrapper interface for wizard.Xorm
type ORM interface {
ReadOnly(bool)
IsReadOnly() bool
SetAutoTransaction(bool)
IsAutoTransaction() bool
ReadOnly(Identifier, bool)
IsReadOnly(Identifier) bool
SetAutoTransaction(Identifier, bool)
IsAutoTransaction(Identifier) bool

Master(interface{}) Engine
MasterByKey(interface{}, interface{}) Engine
Expand All @@ -26,29 +26,31 @@ type ORM interface {
Get(interface{}, func(Session) (bool, error)) (bool, error)
Find(interface{}, func(Session) error) error
Count(interface{}, func(Session) (int64, error)) (int64, error)
Insert(interface{}, func(Session) (int64, error)) (int64, error)
Update(interface{}, func(Session) (int64, error)) (int64, error)
Insert(Identifier, interface{}, func(Session) (int64, error)) (int64, error)
Update(Identifier, interface{}, func(Session) (int64, error)) (int64, error)
FindParallel(interface{}, interface{}, string, ...interface{}) error
FindParallelByCondition(interface{}, FindCondition) error
CountParallelByCondition(interface{}, FindCondition) ([]int64, error)
UpdateParallelByCondition(interface{}, UpdateCondition) (int64, error)
GetUsingMaster(interface{}, func(Session) (bool, error)) (bool, error)
FindUsingMaster(interface{}, func(Session) error) error
CountUsingMaster(interface{}, func(Session) (int64, error)) (int64, error)
GetUsingMaster(Identifier, interface{}, func(Session) (bool, error)) (bool, error)
FindUsingMaster(Identifier, interface{}, func(Session) error) error
CountUsingMaster(Identifier, interface{}, func(Session) (int64, error)) (int64, error)

NewMasterSession(interface{}) (Session, error)
NewMasterSessionByKey(interface{}, interface{}) (Session, error)
NewSlaveSession(interface{}) (Session, error)
NewSlaveSessionByKey(interface{}, interface{}) (Session, error)
NewAllMasterSessions(interface{}) ([]Session, error)

UseMasterSession(Identifier, interface{}) (Session, error)
UseMasterSessionByKey(Identifier, interface{}, interface{}) (Session, error)
UseSlaveSession(Identifier, interface{}) (Session, error)
UseSlaveSessionByKey(Identifier, interface{}, interface{}) (Session, error)
UseAllMasterSessions(Identifier, interface{}) ([]Session, error)

ForceNewTransaction(interface{}) (Session, error)
Transaction(interface{}) (Session, error)
TransactionByKey(interface{}, interface{}) (Session, error)
AutoTransaction(interface{}, Session) error
CommitAll() error
RollbackAll() error
CloseAll()
Transaction(Identifier, interface{}) (Session, error)
TransactionByKey(Identifier, interface{}, interface{}) (Session, error)
AutoTransaction(Identifier, interface{}, Session) error
CommitAll(Identifier) error
RollbackAll(Identifier) error
CloseList(Identifier)
}

// Session is interface for xorm.Session
Expand Down
37 changes: 5 additions & 32 deletions orm/xorm/xorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import (
type Xorm struct {
*XormWizard
*XormFunction
*XormSession
*XormTransaction
*XormSessionManager
*XormParallel

Wiz *wizard.Wizard
readOnly bool
autoTx bool
Wiz *wizard.Wizard
}

// New creates initialized *Xorm
Expand All @@ -23,34 +20,10 @@ func New(wiz *wizard.Wizard) *Xorm {
orm.Wiz = wiz
orm.XormFunction = &XormFunction{orm: orm}
orm.XormWizard = &XormWizard{wiz}
orm.XormSession = &XormSession{
orm: orm,
sessions: make(map[interface{}]Session),
}
orm.XormTransaction = &XormTransaction{
orm: orm,
transactions: make(map[interface{}]Session),
orm.XormSessionManager = &XormSessionManager{
orm: orm,
list: make(map[Identifier]*SessionList),
}
orm.XormParallel = &XormParallel{orm: orm}
return orm
}

// ReadOnly set write proof flag
func (orm *Xorm) ReadOnly(b bool) {
orm.readOnly = b
}

// IsReadOnly checks in write proof mode or not
func (orm *Xorm) IsReadOnly() bool {
return orm.readOnly
}

// SetAutoTransaction sets auto transaction flag
func (orm *Xorm) SetAutoTransaction(b bool) {
orm.autoTx = b
}

// IsAutoTransaction checks in auto transaction mode or not
func (orm *Xorm) IsAutoTransaction() bool {
return orm.autoTx
}
24 changes: 12 additions & 12 deletions orm/xorm/xorm_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,52 +37,52 @@ func (xfn XormFunction) Count(obj interface{}, fn func(Session) (int64, error))
}

// Insert executes xorm.Sessions.Insert() in master db
func (xfn XormFunction) Insert(obj interface{}, fn func(Session) (int64, error)) (int64, error) {
if xfn.orm.IsReadOnly() {
func (xfn XormFunction) Insert(id Identifier, obj interface{}, fn func(Session) (int64, error)) (int64, error) {
if xfn.orm.IsReadOnly(id) {
return 0, nil
}

s, err := xfn.orm.NewMasterSession(obj)
s, err := xfn.orm.UseMasterSession(id, obj)
if err != nil {
return 0, err
}
return fn(s)
}

// Update executes xorm.Sessions.Update() in master db
func (xfn XormFunction) Update(obj interface{}, fn func(Session) (int64, error)) (int64, error) {
if xfn.orm.IsReadOnly() {
func (xfn XormFunction) Update(id Identifier, obj interface{}, fn func(Session) (int64, error)) (int64, error) {
if xfn.orm.IsReadOnly(id) {
return 0, nil
}

s, err := xfn.orm.NewMasterSession(obj)
s, err := xfn.orm.UseMasterSession(id, obj)
if err != nil {
return 0, err
}
return fn(s)
}

// GetUsingMaster executes xorm.Sessions.Get() in master db
func (xfn XormFunction) GetUsingMaster(obj interface{}, fn func(Session) (bool, error)) (bool, error) {
s, err := xfn.orm.NewMasterSession(obj)
func (xfn XormFunction) GetUsingMaster(id Identifier, obj interface{}, fn func(Session) (bool, error)) (bool, error) {
s, err := xfn.orm.UseMasterSession(id, obj)
if err != nil {
return false, err
}
return fn(s)
}

// FindUsingMaster executes xorm.Sessions.Find() in master db
func (xfn XormFunction) FindUsingMaster(obj interface{}, fn func(Session) error) error {
s, err := xfn.orm.NewMasterSession(obj)
func (xfn XormFunction) FindUsingMaster(id Identifier, obj interface{}, fn func(Session) error) error {
s, err := xfn.orm.UseMasterSession(id, obj)
if err != nil {
return err
}
return fn(s)
}

// CountUsingMaster executes xorm.Sessions.Count() in master db
func (xfn XormFunction) CountUsingMaster(obj interface{}, fn func(Session) (int64, error)) (int64, error) {
s, err := xfn.orm.NewMasterSession(obj)
func (xfn XormFunction) CountUsingMaster(id Identifier, obj interface{}, fn func(Session) (int64, error)) (int64, error) {
s, err := xfn.orm.UseMasterSession(id, obj)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit 1d48a17

Please sign in to comment.