gorm/callbacks/callbacks.go

84 lines
3.3 KiB
Go
Raw Normal View History

2020-02-02 03:35:01 +03:00
package callbacks
2020-02-04 03:56:15 +03:00
import (
2020-06-02 04:16:07 +03:00
"gorm.io/gorm"
2020-02-04 03:56:15 +03:00
)
2020-02-02 03:35:01 +03:00
2021-04-28 12:19:30 +03:00
var (
createClauses = []string{"INSERT", "VALUES", "ON CONFLICT"}
queryClauses = []string{"SELECT", "FROM", "WHERE", "GROUP BY", "ORDER BY", "LIMIT", "FOR"}
updateClauses = []string{"UPDATE", "SET", "WHERE"}
deleteClauses = []string{"DELETE", "FROM", "WHERE"}
)
2020-03-12 08:05:22 +03:00
type Config struct {
LastInsertIDReversed bool
2021-04-28 12:19:30 +03:00
CreateClauses []string
QueryClauses []string
UpdateClauses []string
DeleteClauses []string
2020-03-12 08:05:22 +03:00
}
func RegisterDefaultCallbacks(db *gorm.DB, config *Config) {
2020-02-02 14:32:27 +03:00
enableTransaction := func(db *gorm.DB) bool {
return !db.SkipDefaultTransaction
}
2021-10-26 17:36:37 +03:00
if len(config.CreateClauses) == 0 {
config.CreateClauses = createClauses
}
if len(config.QueryClauses) == 0 {
config.QueryClauses = queryClauses
}
if len(config.DeleteClauses) == 0 {
config.DeleteClauses = deleteClauses
}
if len(config.UpdateClauses) == 0 {
config.UpdateClauses = updateClauses
}
2020-02-02 14:32:27 +03:00
createCallback := db.Callback().Create()
createCallback.Match(enableTransaction).Register("gorm:begin_transaction", BeginTransaction)
createCallback.Register("gorm:before_create", BeforeCreate)
createCallback.Register("gorm:save_before_associations", SaveBeforeAssociations(true))
2020-03-12 08:05:22 +03:00
createCallback.Register("gorm:create", Create(config))
createCallback.Register("gorm:save_after_associations", SaveAfterAssociations(true))
2020-02-02 14:32:27 +03:00
createCallback.Register("gorm:after_create", AfterCreate)
createCallback.Match(enableTransaction).Register("gorm:commit_or_rollback_transaction", CommitOrRollbackTransaction)
2021-04-28 12:19:30 +03:00
createCallback.Clauses = config.CreateClauses
2020-02-02 14:32:27 +03:00
queryCallback := db.Callback().Query()
2020-02-04 03:56:15 +03:00
queryCallback.Register("gorm:query", Query)
2020-02-02 14:32:27 +03:00
queryCallback.Register("gorm:preload", Preload)
queryCallback.Register("gorm:after_query", AfterQuery)
2021-04-28 12:19:30 +03:00
queryCallback.Clauses = config.QueryClauses
2020-02-02 14:32:27 +03:00
deleteCallback := db.Callback().Delete()
deleteCallback.Match(enableTransaction).Register("gorm:begin_transaction", BeginTransaction)
deleteCallback.Register("gorm:before_delete", BeforeDelete)
deleteCallback.Register("gorm:delete_before_associations", DeleteBeforeAssociations)
2021-10-28 02:24:38 +03:00
deleteCallback.Register("gorm:delete", Delete(config))
2020-02-02 14:32:27 +03:00
deleteCallback.Register("gorm:after_delete", AfterDelete)
deleteCallback.Match(enableTransaction).Register("gorm:commit_or_rollback_transaction", CommitOrRollbackTransaction)
2021-04-28 12:19:30 +03:00
deleteCallback.Clauses = config.DeleteClauses
2020-02-02 14:32:27 +03:00
updateCallback := db.Callback().Update()
updateCallback.Match(enableTransaction).Register("gorm:begin_transaction", BeginTransaction)
2020-06-01 14:41:33 +03:00
updateCallback.Register("gorm:setup_reflect_value", SetupUpdateReflectValue)
2020-02-02 14:32:27 +03:00
updateCallback.Register("gorm:before_update", BeforeUpdate)
updateCallback.Register("gorm:save_before_associations", SaveBeforeAssociations(false))
2021-10-26 17:36:37 +03:00
updateCallback.Register("gorm:update", Update(config))
updateCallback.Register("gorm:save_after_associations", SaveAfterAssociations(false))
2020-02-02 14:32:27 +03:00
updateCallback.Register("gorm:after_update", AfterUpdate)
updateCallback.Match(enableTransaction).Register("gorm:commit_or_rollback_transaction", CommitOrRollbackTransaction)
2021-04-28 12:19:30 +03:00
updateCallback.Clauses = config.UpdateClauses
rowCallback := db.Callback().Row()
rowCallback.Register("gorm:row", RowQuery)
rowCallback.Clauses = config.QueryClauses
2020-02-22 14:41:01 +03:00
2021-04-28 12:19:30 +03:00
rawCallback := db.Callback().Raw()
rawCallback.Register("gorm:raw", RawExec)
rawCallback.Clauses = config.QueryClauses
2020-02-02 03:35:01 +03:00
}