Browse Source

Move processor function into one file (#1637)

Move processor function into one file

Reviewed-on: #1637
pull/1642/head
Lunny Xiao 1 month ago
parent
commit
0a06dc204a
4 changed files with 73 additions and 56 deletions
  1. +66
    -0
      processors.go
  2. +4
    -49
      session.go
  3. +1
    -5
      session_delete.go
  4. +2
    -2
      session_insert.go

+ 66
- 0
processors.go View File

@@ -76,3 +76,69 @@ func (session *Session) executeProcessors() error {
}
return nil
}

func cleanupProcessorsClosures(slices *[]func(interface{})) {
if len(*slices) > 0 {
*slices = make([]func(interface{}), 0)
}
}

func executeBeforeClosures(session *Session, bean interface{}) {
// handle before delete processors
for _, closure := range session.beforeClosures {
closure(bean)
}
cleanupProcessorsClosures(&session.beforeClosures)
}

func executeBeforeSet(bean interface{}, fields []string, scanResults []interface{}) {
if b, hasBeforeSet := bean.(BeforeSetProcessor); hasBeforeSet {
for ii, key := range fields {
b.BeforeSet(key, Cell(scanResults[ii].(*interface{})))
}
}
}

func executeAfterSet(bean interface{}, fields []string, scanResults []interface{}) {
if b, hasAfterSet := bean.(AfterSetProcessor); hasAfterSet {
for ii, key := range fields {
b.AfterSet(key, Cell(scanResults[ii].(*interface{})))
}
}
}

func buildAfterProcessors(session *Session, bean interface{}) {
// handle afterClosures
for _, closure := range session.afterClosures {
session.afterProcessors = append(session.afterProcessors, executedProcessor{
fun: func(sess *Session, bean interface{}) error {
closure(bean)
return nil
},
session: session,
bean: bean,
})
}

if a, has := bean.(AfterLoadProcessor); has {
session.afterProcessors = append(session.afterProcessors, executedProcessor{
fun: func(sess *Session, bean interface{}) error {
a.AfterLoad()
return nil
},
session: session,
bean: bean,
})
}

if a, has := bean.(AfterLoadSessionProcessor); has {
session.afterProcessors = append(session.afterProcessors, executedProcessor{
fun: func(sess *Session, bean interface{}) error {
a.AfterLoad(sess)
return nil
},
session: session,
bean: bean,
})
}
}

+ 4
- 49
session.go View File

@@ -328,12 +328,6 @@ func (session *Session) DB() *core.DB {
return session.db()
}

func cleanupProcessorsClosures(slices *[]func(interface{})) {
if len(*slices) > 0 {
*slices = make([]func(interface{}), 0)
}
}

func (session *Session) canCache() bool {
if session.statement.RefTable == nil ||
session.statement.JoinStr != "" ||
@@ -425,56 +419,17 @@ func (session *Session) row2Slice(rows *core.Rows, fields []string, bean interfa
return nil, err
}

if b, hasBeforeSet := bean.(BeforeSetProcessor); hasBeforeSet {
for ii, key := range fields {
b.BeforeSet(key, Cell(scanResults[ii].(*interface{})))
}
}
executeBeforeSet(bean, fields, scanResults)

return scanResults, nil
}

func (session *Session) slice2Bean(scanResults []interface{}, fields []string, bean interface{}, dataStruct *reflect.Value, table *schemas.Table) (schemas.PK, error) {
defer func() {
if b, hasAfterSet := bean.(AfterSetProcessor); hasAfterSet {
for ii, key := range fields {
b.AfterSet(key, Cell(scanResults[ii].(*interface{})))
}
}
executeAfterSet(bean, fields, scanResults)
}()

// handle afterClosures
for _, closure := range session.afterClosures {
session.afterProcessors = append(session.afterProcessors, executedProcessor{
fun: func(sess *Session, bean interface{}) error {
closure(bean)
return nil
},
session: session,
bean: bean,
})
}

if a, has := bean.(AfterLoadProcessor); has {
session.afterProcessors = append(session.afterProcessors, executedProcessor{
fun: func(sess *Session, bean interface{}) error {
a.AfterLoad()
return nil
},
session: session,
bean: bean,
})
}

if a, has := bean.(AfterLoadSessionProcessor); has {
session.afterProcessors = append(session.afterProcessors, executedProcessor{
fun: func(sess *Session, bean interface{}) error {
a.AfterLoad(sess)
return nil
},
session: session,
bean: bean,
})
}
buildAfterProcessors(session, bean)

var tempMap = make(map[string]int)
var pk schemas.PK


+ 1
- 5
session_delete.go View File

@@ -96,11 +96,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
return 0, err
}

// handle before delete processors
for _, closure := range session.beforeClosures {
closure(bean)
}
cleanupProcessorsClosures(&session.beforeClosures)
executeBeforeClosures(session, bean)

if processor, ok := interface{}(bean).(BeforeDeleteProcessor); ok {
processor.BeforeDelete()


+ 2
- 2
session_insert.go View File

@@ -233,7 +233,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
for _, closure := range session.afterClosures {
closure(elemValue)
}
if processor, ok := interface{}(elemValue).(AfterInsertProcessor); ok {
if processor, ok := elemValue.(AfterInsertProcessor); ok {
processor.AfterInsert()
}
} else {
@@ -246,7 +246,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
session.afterInsertBeans[elemValue] = &afterClosures
}
} else {
if _, ok := interface{}(elemValue).(AfterInsertProcessor); ok {
if _, ok := elemValue.(AfterInsertProcessor); ok {
session.afterInsertBeans[elemValue] = nil
}
}


Loading…
Cancel
Save