The gdb
module uses highly flexible and extensible interface design, allowing developers to easily customize implementations and replace any methods defined in the interface.
DB
Interface
Interface documentation: https://pkg.go.dev/github.com/gogf/gf/v2/database/gdb#DB
The DB
interface is the core interface for database operations and the most commonly used interface when using ORM
to operate on databases. Here, we mainly explain several important methods of the interface:
- The
Open
method is used to create a specific database connection object, returning the standard library's*sql.DB
general database object. - The first parameter
link
of theDo*
series methods is aLink
interface object, which may be a master node object or a slave node object in amaster-slave
mode. Therefore, when using thislink
parameter in inherited driver object implementations, be aware of the current running mode. In most database master-slave modes,slave
nodes are often not writable. - The
HandleSqlBeforeCommit
method will be called for some pre-commit callback processing when eachSQL
is submitted to the database server for execution. - For other interface methods, please refer to the interface documentation or source files.
DB
Interface Relationships
GoFrame ORM Relations
Driver
Interface
Interface documentation: https://pkg.go.dev/github.com/gogf/gf/v2/database/gdb#Driver
Custom drivers need to implement the following interface:
// Driver is the interface for integrating sql drivers into package gdb.
type Driver interface {
// New creates and returns a database object for specified database server.
New(core *Core, node *ConfigNode) (DB, error)
}
The New
method is used to create a database operation object corresponding to the driver based on the Core
database base object and the ConfigNode
configuration object. It should be noted that the returned database object needs to implement the DB
interface. The database base object Core
has already implemented the DB
interface, so developers only need to "inherit" the Core
object and then override the corresponding interface implementation methods as needed.
Documents
📄️ ORM Interface - Callback
When developing ORM interfaces using the GoFrame framework, custom callback handling is used to log or authenticate SQL statements. By implementing and overriding interface methods like DoQuery, DoExec, etc., developers can inject custom logic into the default implementation. The example demonstrates how to customize a MySQL driver to log executed SQL statements and configure gdb to use that driver.
📄️ ORM Interface - Driver
ORM interface development in the GoFrame framework, specifically for database driver development and registration. By implementing the interface of the gdb module, you can add third-party database drivers not supported by GoFrame by default or customize existing drivers, ensuring consistency in upper-layer business operations. This document provides detailed steps and sample code to help developers get started quickly.