Dialector; plugin authors implement Plugin; custom types implement Valuer.
Dialector
Initialize once during Open to set up the connection pool, then uses the remaining methods during query building and migration.
| Method | When called |
|---|---|
Name() | Plugin registration, error messages |
Initialize(*DB) | Once during gorm.Open |
Migrator(*DB) | When db.Migrator() is called |
DataTypeOf(*schema.Field) | AutoMigrate column type resolution |
DefaultValueOf(*schema.Field) | AutoMigrate default value clauses |
BindVarTo(...) | Bind variable rendering (e.g., $1 for Postgres, ? for MySQL) |
QuoteTo(...) | Identifier quoting (e.g., backticks vs double quotes) |
Explain(...) | SQL string interpolation for logging and ToSQL |
Plugin
db.Use(plugin). GORM calls Initialize once and stores the plugin by its Name.
ParamsFilter
ConnPool. When present, GORM passes the SQL and its bind variables through ParamsFilter before execution, allowing the pool to redact sensitive values from logs.
ConnPool
*sql.DB and *sql.Tx both implement this interface. You can provide a custom implementation (e.g., a read replica router) via Config.ConnPool.
SavePointerDialectorInterface
db.SavePoint(name) and db.RollbackTo(name) work. If not implemented, those methods return ErrUnsupportedDriver.
TxBeginner
*sql.DB. GORM calls BeginTx to start a transaction when the current connection pool is a plain *sql.DB.
ConnPoolBeginner
TxBeginner, but BeginTx returns a ConnPool instead of *sql.Tx. Implement this on a custom pool to return a custom transactional pool (for example, a read-replica router that switches to the primary during a transaction).
TxCommitter
*sql.Tx. GORM checks whether the current ConnPool satisfies this interface to determine if a transaction is active. db.Commit() and db.Rollback() delegate to this interface.
Tx
ConnPool (for query execution), TxCommitter (for commit/rollback), and adds StmtContext for creating transaction-bound prepared statements. *sql.Tx satisfies this interface.
Valuer
clause.Expr to provide the SQL fragment and bind variables.
GetDBConnector
ConnPool to expose the underlying *sql.DB. db.DB() checks for this interface before falling back to a direct type assertion on the pool.
Rows
*sql.Rows satisfies this interface. GORM uses Rows internally when scanning query results, and you can implement it for testing with mock rows.
ErrorTranslator
Dialector. When Config.TranslateError is true and the dialector implements this interface, GORM passes every database error through Translate before storing it on db.Error. Dialectors use this to map driver-specific error codes to standard GORM errors such as ErrDuplicatedKey and ErrForeignKeyViolated.