驱动开发

默认情况下,gdb模块已经提供了一些常用的驱动支持,并允许开发者对接自定义的数据库驱动。

适用场景:新增第三方数据库驱动、对已有驱动进行定制化、实现自定义回调处理等。

驱动接口

Driver接口

接口文档:https://godoc.org/github.com/gogf/gf/database/gdb#Driver

开发者自定义的驱动需要实现以下接口:

  1. // Driver is the interface for integrating sql drivers into package gdb.
  2. type Driver interface {
  3. // New creates and returns a database object for specified database server.
  4. New(core *Core, node *ConfigNode) (DB, error)
  5. }

其中的New方法用于根据Core数据库基础对象以及ConfigNode配置对象创建驱动对应的数据库操作对象,需要注意的是,返回的数据库对象需要实现DB接口。而数据库基础对象Core已经实现了DB接口,因此开发者只需要”继承”Core对象,然后根据需要覆盖对应的接口实现方法即可。

DB接口

接口文档:https://godoc.org/github.com/gogf/gf/database/gdb#DB

DB接口是数据库操作的核心接口,这里主要对接口的几个重要方法做说明:

  1. Open方法用于创建特定的数据库连接对象,返回的是标准库的*sql.DB通用数据库对象。
  2. Do*系列方法的第一个参数linkLink接口对象,该对象在master-slave模式下可能是一个主节点对象,也可能是从节点对象,因此如果在继承的驱动对象实现中使用该link参数时,注意当前的运行模式。slave节点在大部分的数据库主从模式中往往是不可写的。
  3. HandleSqlBeforeCommit方法将会在每一条SQL提交给数据库服务端执行时被调用做一些提交前的回调处理。
  4. 其他接口方法详见接口文档或者源码文件。

驱动注册

通过以下方法注册自定义驱动到gdb模块:

  1. // Register registers custom database driver to gdb.
  2. func Register(name string, driver Driver) error

其中的驱动名称name可以是已有的驱动名称,例如mysql, mssql, pgsql等等,当出现同名的驱动注册时,新的驱动将会覆盖老的驱动。

新增第三方驱动

新增一个第三方的驱动到gdb模块中非常简单,可以参考gdb模块源码中已对接的数据库类型代码示例:

  1. https://github.com/gogf/gf/blob/master/database/gdb/gdb_driver_mysql.go
  2. https://github.com/gogf/gf/blob/master/database/gdb/gdb_driver_mssql.go
  3. https://github.com/gogf/gf/blob/master/database/gdb/gdb_driver_pgsql.go
  4. https://github.com/gogf/gf/blob/master/database/gdb/gdb_driver_oracle.go
  5. https://github.com/gogf/gf/blob/master/database/gdb/gdb_driver_sqlite.go
  6. 更多: https://github.com/gogf/gf/blob/master/database/gdb

开发自定义驱动

我们来看一个自定义驱动的示例,我们需要将所有执行的SQL语句记录到monitor表中,以方便于进行SQL审计。

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

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

我们看到,这里在包初始化方法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"