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.