自定义回调处理是最常见的接口开发实现,我们往往只需要对接口中的部分实现进行替换修改,以在原有的实现逻辑中注入自定义的逻辑实现。我们来看一个自定义回调处理的示例,我们需要将所有执行的SQL语句记录到monitor表中,以方便于进行SQL审计。

    为简化示例编写,我们这里实现了一个自定义的MySQL驱动,该驱动继承于gdb模块中已经实现的DriverMysql,并按照需要修改覆盖相应的接口方法。由于所有的SQL语句执行必定会通过DoQuery或者DoExec接口,因此我们在自定义的驱动中实现并覆盖这两个接口方法即可。

    1. package driver
    2. import (
    3. "database/sql"
    4. "github.com/gogf/gf/database/gdb"
    5. "github.com/gogf/gf/os/gtime"
    6. )
    7. // MyDriver is a custom database driver, which is used for testing only.
    8. // For simplifying the unit testing case purpose, MyDriver struct inherits the mysql driver
    9. // gdb.DriverMysql and overwrites its functions DoQuery and DoExec.
    10. // So if there's any sql execution, it goes through MyDriver.DoQuery/MyDriver.DoExec firstly
    11. // and then gdb.DriverMysql.DoQuery/gdb.DriverMysql.DoExec.
    12. // You can call it sql "HOOK" or "HiJack" as your will.
    13. type MyDriver struct {
    14. *gdb.DriverMysql
    15. }
    16. var (
    17. // customDriverName is my driver name, which is used for registering.
    18. customDriverName = "MyDriver"
    19. )
    20. func init() {
    21. // It here registers my custom driver in package initialization function "init".
    22. // You can later use this type in the database configuration.
    23. if err := gdb.Register(customDriverName, &MyDriver{}); err != nil {
    24. panic(err)
    25. }
    26. }
    27. // New creates and returns a database object for mysql.
    28. // It implements the interface of gdb.Driver for extra database driver installation.
    29. func (d *MyDriver) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error) {
    30. return &MyDriver{
    31. &gdb.DriverMysql{
    32. Core: core,
    33. },
    34. }, nil
    35. }
    36. // DoQuery commits the sql string and its arguments to underlying driver
    37. // through given link object and returns the execution result.
    38. func (d *MyDriver) DoQuery(link gdb.Link, sql string, args ...interface{}) (rows *sql.Rows, err error) {
    39. tsMilli := gtime.TimestampMilli()
    40. rows, err = d.DriverMysql.DoQuery(link, sql, args...)
    41. link.Exec(
    42. "INSERT INTO `monitor`(`sql`,`cost`,`time`,`error`) VALUES(?,?,?,?)",
    43. gdb.FormatSqlWithArgs(sql, args),
    44. gtime.TimestampMilli()-tsMilli,
    45. gtime.Now(),
    46. err,
    47. )
    48. return
    49. }
    50. // DoExec commits the query string and its arguments to underlying driver
    51. // through given link object and returns the execution result.
    52. func (d *MyDriver) DoExec(link gdb.Link, sql string, args ...interface{}) (result sql.Result, err error) {
    53. tsMilli := gtime.TimestampMilli()
    54. result, err = d.DriverMysql.DoExec(link, sql, args...)
    55. link.Exec(
    56. "INSERT INTO `monitor`(`sql`,`cost`,`time`,`error`) VALUES(?,?,?,?)",
    57. gdb.FormatSqlWithArgs(sql, args),
    58. gtime.TimestampMilli()-tsMilli,
    59. gtime.Now(),
    60. err,
    61. )
    62. return
    63. }

    我们看到,这里在包初始化方法init中使用了gdb.Register("MyDriver", &MyDriver{})来注册了了一个自定义名称的驱动。我们也可以通过gdb.Register("mysql", &MyDriver{})来覆盖已有的框架mysql驱动为自己的驱动。

    驱动名称mysql为框架默认的DriverMysql驱动的名称。

    由于这里我们使用了一个新的驱动名称MyDriver,因此在gdb配置中的type数据库类型时,需要填写该驱动名称。以下是一个使用配置的示例:

    1. [database]
    2. type = "MyDriver"
    3. link = "root:12345678@tcp(127.0.0.1:3306)/test"