GORM 提供了少量接口,使用户能够为 GORM 定义支持的数据类型,这里以 json 为例

实现自定义数据类型

Scanner / Valuer

自定义的数据类型必须实现 Scanner 和 Valuer 接口,以便让 GORM 知道如何将该类型接收、保存到数据库

例如:

1. type JSON json.RawMessage

3. // 实现 sql.Scanner 接口,Scan 将 value 扫描至 Jsonb
4. func (j *JSON) Scan(value interface{}) error {
5. bytes, ok := value.([]byte)
6. if !ok {
7. return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
8. }

10. result := json.RawMessage{}
11. err := json.Unmarshal(bytes, &result)
12. *j = JSON(result)
13. return err
14. }

16. // 实现 driver.Valuer 接口,Value 返回 json value
17. func (j JSON) Value() (driver.Value, error) {
18. if len(j) == 0 {
19. return nil, nil
20. }
21. return json.RawMessage(j).MarshalJSON()
22. }

有许多第三方包实现了 Scanner/Valuer 接口,可与 GORM 一起使用,例如:

1. import (
2. "github.com/google/uuid"
3. "github.com/lib/pq"
4. )

6. type Post struct {
7. ID     uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4()"`
8. Title  string
9. Tags   pq.StringArray `gorm:"type:text[]"`
10. }

GormDataTypeInterface

GORM 会从 type 标签 中读取字段的数据库类型,如果找不到,则会检查该结构体是否实现了 GormDBDataTypeInterface 或 GormDataTypeInterface 接口,然后使用接口返回值作为数据类型

1. type GormDataTypeInterface interface {
2. GormDataType() string
3. }

5. type GormDBDataTypeInterface interface {
6. GormDBDataType(*gorm.DB, *schema.Field) string
7. }

GormDataType 的结果用于生成通用数据类型,也可以通过 schema.Field 的 DataType 字段得到。这在 编写插件 或者 hook 时可能会有用,例如:

1. func (JSON) GormDataType() string {
2. return "json"
3. }

5. type User struct {
6. Attrs JSON
7. }

9. func (user User) BeforeCreate(tx *gorm.DB) {
10. field := tx.Statement.Schema.LookUpField("Attrs")
11. if field.DataType == "json" {
12. // 做点什么...
13. }
14. }

在迁移时,GormDBDataType 通常会为当前驱动返回恰当的数据类型,例如:

1. func (JSON) GormDBDataType(db *gorm.DB, field *schema.Field) string {
2. // 使用 field.Tag、field.TagSettings 获取字段的 tag
3. // 查看 https://github.com/go-gorm/gorm/blob/master/schema/field.go 获取全部的选项

5. // 根据不同的数据库驱动返回不同的数据类型
6. switch db.Dialector.Name() {
7. case "mysql", "sqlite":
8. return "JSON"
9. case "postgres":
10. return "JSONB"
11. }
12. return ""
13. }

如果 struct 没有实现 GormDBDataTypeInterface 或 GormDataTypeInterface 接口,GORM 会根据 struct 第一个字段推测其数据类型,例如:会为 NullString 使用 string

1. type NullString struct {
2. String string // 使用第一个字段的数据类型
3. Valid  bool
4. }

6. type User struct {
7. Name NullString // 数据类型会是 string
8. }

GormValuerInterface

GORM 提供了 GormValuerInterface 接口,支持使用 SQL 表达式或基于 context 的值进行 create/update,例如:

1. // GORM Valuer interface
2. type GormValuerInterface interface {
3. GormValue(ctx context.Context, db *gorm.DB) clause.Expr
4. }

使用 SQL 表达式进行 Create/Update

1. type Location struct {
2. X, Y int
3. }

5. func (loc Location) GormDataType() string {
6. return "geometry"
7. }

9. func (loc Location) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
10. return clause.Expr{
11. SQL:  "ST_PointFromText(?)",
12. Vars: []interface{}{fmt.Sprintf("POINT(%d %d)", loc.X, loc.Y)},
13. }
14. }

16. // Scan 方法实现了 sql.Scanner 接口
17. func (loc *Location) Scan(v interface{}) error {
18. // Scan a value into struct from database driver
19. }

21. type User struct {
22. ID       int
23. Name     string
24. Location Location
25. }

27. db.Create(&User{
28. Name:     "jinzhu",
29. Location: Location{X: 100, Y: 100},
30. })
31. // INSERT INTO `users` (`name`,`point`) VALUES ("jinzhu",ST_PointFromText("POINT(100 100)"))

33. db.Model(&User{ID: 1}).Updates(User{
34. Name:  "jinzhu",
35. Location: Location{X: 100, Y: 100},
36. })
37. // UPDATE `user_with_points` SET `name`="jinzhu",`location`=ST_PointFromText("POINT(100 100)") WHERE `id` = 1

你也可以根据 SQL 表达式进行 create/update,查看 Create From SQL Expr 和 Update with SQL Expression 获取详情

基于 Context 的值

如果你想创建或更新一个依赖于当前 context 的值,你也可以实现 GormValuerInterface 接口,例如:

1. type EncryptedString struct {
2. Value string
3. }

5. func (es EncryptedString) GormValue(ctx context.Context, db *gorm.DB) (expr clause.Expr) {
6. if encryptionKey, ok := ctx.Value("TenantEncryptionKey").(string); ok {
7. return clause.Expr{SQL: "?", Vars: []interface{}{Encrypt(es.Value, encryptionKey)}}
8. } else {
9. db.AddError(errors.New("invalid encryption key"))
10. }

12. return
13. }

条件表达式

如果你想构建一些查询 helper,你可以让 struct 实现 clause.Expression 接口:

1. type Expression interface {
2. Build(builder Builder)
3. }

查看 JSON 和 SQL Builder 获取详情,下面是一个示例:

1. // 根据 Clause Expression 生成 SQL
2. db.Find(&user, datatypes.JSONQuery("attributes").HasKey("role"))
3. db.Find(&user, datatypes.JSONQuery("attributes").HasKey("orgs", "orga"))

5. // MySQL
6. // SELECT * FROM `users` WHERE JSON_EXTRACT(`attributes`, '$.role') IS NOT NULL
7. // SELECT * FROM `users` WHERE JSON_EXTRACT(`attributes`, '$.orgs.orga') IS NOT NULL

9. // PostgreSQL
10. // SELECT * FROM "user" WHERE "attributes"::jsonb ? 'role'
11. // SELECT * FROM "user" WHERE "attributes"::jsonb -> 'orgs' ? 'orga'

13. db.Find(&user, datatypes.JSONQuery("attributes").Equals("jinzhu", "name"))
14. // MySQL
15. // SELECT * FROM `user` WHERE JSON_EXTRACT(`attributes`, '$.name') = "jinzhu"

17. // PostgreSQL
18. // SELECT * FROM "user" WHERE json_extract_path_text("attributes"::json,'name') = 'jinzhu'

自定义数据类型集合

我们创建了一个 Github 仓库,用于收集各种自定义数据类型<https://github.com/go-gorm/datatypes>,非常欢迎同学们的 pull request ;)