Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle https://xorm.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

145 lines
3.7 KiB

  1. // Copyright 2015 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. // BeforeInsertProcessor executed before an object is initially persisted to the database
  6. type BeforeInsertProcessor interface {
  7. BeforeInsert()
  8. }
  9. // BeforeUpdateProcessor executed before an object is updated
  10. type BeforeUpdateProcessor interface {
  11. BeforeUpdate()
  12. }
  13. // BeforeDeleteProcessor executed before an object is deleted
  14. type BeforeDeleteProcessor interface {
  15. BeforeDelete()
  16. }
  17. // BeforeSetProcessor executed before data set to the struct fields
  18. type BeforeSetProcessor interface {
  19. BeforeSet(string, Cell)
  20. }
  21. // AfterSetProcessor executed after data set to the struct fields
  22. type AfterSetProcessor interface {
  23. AfterSet(string, Cell)
  24. }
  25. // AfterInsertProcessor executed after an object is persisted to the database
  26. type AfterInsertProcessor interface {
  27. AfterInsert()
  28. }
  29. // AfterUpdateProcessor executed after an object has been updated
  30. type AfterUpdateProcessor interface {
  31. AfterUpdate()
  32. }
  33. // AfterDeleteProcessor executed after an object has been deleted
  34. type AfterDeleteProcessor interface {
  35. AfterDelete()
  36. }
  37. // AfterLoadProcessor executed after an ojbect has been loaded from database
  38. type AfterLoadProcessor interface {
  39. AfterLoad()
  40. }
  41. // AfterLoadSessionProcessor executed after an ojbect has been loaded from database with session parameter
  42. type AfterLoadSessionProcessor interface {
  43. AfterLoad(*Session)
  44. }
  45. type executedProcessorFunc func(*Session, interface{}) error
  46. type executedProcessor struct {
  47. fun executedProcessorFunc
  48. session *Session
  49. bean interface{}
  50. }
  51. func (executor *executedProcessor) execute() error {
  52. return executor.fun(executor.session, executor.bean)
  53. }
  54. func (session *Session) executeProcessors() error {
  55. processors := session.afterProcessors
  56. session.afterProcessors = make([]executedProcessor, 0)
  57. for _, processor := range processors {
  58. if err := processor.execute(); err != nil {
  59. return err
  60. }
  61. }
  62. return nil
  63. }
  64. func cleanupProcessorsClosures(slices *[]func(interface{})) {
  65. if len(*slices) > 0 {
  66. *slices = make([]func(interface{}), 0)
  67. }
  68. }
  69. func executeBeforeClosures(session *Session, bean interface{}) {
  70. // handle before delete processors
  71. for _, closure := range session.beforeClosures {
  72. closure(bean)
  73. }
  74. cleanupProcessorsClosures(&session.beforeClosures)
  75. }
  76. func executeBeforeSet(bean interface{}, fields []string, scanResults []interface{}) {
  77. if b, hasBeforeSet := bean.(BeforeSetProcessor); hasBeforeSet {
  78. for ii, key := range fields {
  79. b.BeforeSet(key, Cell(scanResults[ii].(*interface{})))
  80. }
  81. }
  82. }
  83. func executeAfterSet(bean interface{}, fields []string, scanResults []interface{}) {
  84. if b, hasAfterSet := bean.(AfterSetProcessor); hasAfterSet {
  85. for ii, key := range fields {
  86. b.AfterSet(key, Cell(scanResults[ii].(*interface{})))
  87. }
  88. }
  89. }
  90. func buildAfterProcessors(session *Session, bean interface{}) {
  91. // handle afterClosures
  92. for _, closure := range session.afterClosures {
  93. session.afterProcessors = append(session.afterProcessors, executedProcessor{
  94. fun: func(sess *Session, bean interface{}) error {
  95. closure(bean)
  96. return nil
  97. },
  98. session: session,
  99. bean: bean,
  100. })
  101. }
  102. if a, has := bean.(AfterLoadProcessor); has {
  103. session.afterProcessors = append(session.afterProcessors, executedProcessor{
  104. fun: func(sess *Session, bean interface{}) error {
  105. a.AfterLoad()
  106. return nil
  107. },
  108. session: session,
  109. bean: bean,
  110. })
  111. }
  112. if a, has := bean.(AfterLoadSessionProcessor); has {
  113. session.afterProcessors = append(session.afterProcessors, executedProcessor{
  114. fun: func(sess *Session, bean interface{}) error {
  115. a.AfterLoad(sess)
  116. return nil
  117. },
  118. session: session,
  119. bean: bean,
  120. })
  121. }
  122. }