Home > other >  Gorm registered callback principle
Gorm registered callback principle

Time:11-03

Background: #

When doing projects, meet a monitoring requirements, is each executing SQL statements to report time consuming to Prometheus, the first thought is executing SQL statements every time when writing business logic itself, but it will invade the business code, and some people will forget to report, or report format is not standard, so think of will be reported to logic encapsulated in the framework of gorm,

# specific principles:

Gorm executing SQL statements are implemented by registering callback function, such as performing the following statement:

! [image] (HTTP://https://upload-images.jianshu.io/upload_images/3233651-2d2212c0feff5cbc.png? ImageMogr2/auto - received/strip % 7 cimageview2/2/w/1240)

Will be executed in sequence, queryCallback, queryCallback queryCallback, this is because when gorm initialization register the several functions:

! [image] (HTTP://https://upload-images.jianshu.io/upload_images/3233651-6088e385133631b0.png? ImageMogr2/auto - received/strip % 7 cimageview2/2/w/1240)

If we want to register to write their own function? And want to control the order of the function call, such as want to register a function func2, and after the queryCallback queryCallback calls, before the change? Could be like this:

! [image] (HTTP://https://upload-images.jianshu.io/upload_images/3233651-685d9a359bf0207d.png? ImageMogr2/auto - received/strip % 7 cimageview2/2/w/1240)

Which after function parameter is specified when registered queryCallback name, the first parameter to the Register callbackName is you want to Register the name of the function, callbackName keep uniqueness, in front of or behind the Register will be overwritten,

At this point, the first function, will, in turn, tuning the several functions: queryCallback, func, queryCallback, queryCallback

# source code analysis:

The definition of the callback, above the first statement in the callback to func append to the queries in the field,
` ` `
//Callback is a struct that contains all the CRUD callbacks
//Field ` creates ` contains callbacks will be call the when creating object
//Field ` updates ` contains callbacks will be call the when updating the object
//Field ` deletes ` contains callbacks will be call the when deleting the object
//Field ` queries ` contains callbacks will be call the when querying the object with the query the methods like the Find, First, the Related Association...
//Field ` rowQueries ` contains callbacks will be call the when querying the object with the Row, Rows...
//Field ` processors ` contains all the callback processors, will be 2 generate above callbacks in order
Type the Callback struct {
The logger logger
Creates func [] * * scope (scope)
Updates func [] * * scope (scope)
Deletes func [] * * scope (scope)
The queries [] * func (scope * scope)
Func rowQueries [] * * scope (scope)
Processors [] * CallbackProcessor
}
` ` `

This is the first source code, can, in turn, when the last row adjustable callCallbacks tuning queries func,
` ` `
//First find the First record that match given the conditions of the order by primary key
Func (s * DB) First (out interface {}, where... Interface) * DB {{}
NewScope:=s.N ewScope (out)
NewScope. Search. Limit (1)

Return newScope. Set (" gorm: order_by_primary_key ", "ASC").
InlineCondition (where... ). CallCallbacks (supachai panitchpakdi arent. Callbacks. The queries), db
}
` ` `

` ` `
Func (scope * scope) callCallbacks (funcs func [] * * scope (s)) * scope {
Defer func () {
If err: recover=(); err ! Nil={
If the db, ok:=scope. Db. Db. (sqlTx); Ok {
The db. The Rollback ()
}
Panic (err)
}
} ()
For _, f:=range funcs {
(*) f (scope)
If the scope. SkipLeft {
Break
}
}
Return the scope
}
` ` `

Reference:

[http://gorm.book.jasperxu.com/callbacks.html] (http://gorm.book.jasperxu.com/callbacks.html)

[https://gorm.io/docs/hooks.html] (https://gorm.io/docs/hooks.html)

[Gin practice serial ten custom GORM Callbacks] (https://segmentfault.com/a/1190000014393602)

CodePudding user response:

Since gorm provide SQL log print, estimated to provide what interface, have not studied carefully, just learn

CodePudding user response:

Typically provide some hooks for a custom function registered classes
  • Related