编写驱动

原文:https://gorm.io/docs/write_driver.html

编写新驱动 Write new driver

GORM provides official support for sqlite, mysql, postgres, sqlserver.

​ GORM 为 sqlitemysqlpostgressqlserver 提供了官方支持。

Some databases may be compatible with the mysql or postgres dialect, in which case you could just use the dialect for those databases.

​ 一些数据库可能与 mysqlpostgres 方言兼容,在这种情况下,您可以使用这些数据库的方言。

For others, you can create a new driver, it needs to implement the dialect interface.

​ 对于其他数据库,您可以创建一个新驱动程序,它需要实现 方言接口

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
type Dialector interface {
  Name() string
  Initialize(*DB) error
  Migrator(db *DB) Migrator
  DataTypeOf(*schema.Field) string
  DefaultValueOf(*schema.Field) clause.Expression
  BindVarTo(writer clause.Writer, stmt *Statement, v interface{})
  QuoteTo(clause.Writer, string)
  Explain(sql string, vars ...interface{}) string
}

Checkout the MySQL Driver as example

​ 以 MySQL 驱动程序为例

最后修改 October 10, 2024: 更新 (a4b8f85)