SQLC覆盖布尔类型PostgreSQL

huangapple go评论115阅读模式
英文:

SQLC Override Bool Type PostgreSQL

问题

我正在使用SQLC,我的YAML配置文件包含以下用于PostgreSQL的类型覆盖:

  1. gen:
  2. go:
  3. emit_json_tags: true
  4. package: "hmdb"
  5. out: "hmdb"
  6. overrides:
  7. - db_type: "hstore"
  8. nullable: true
  9. go_type: "github.com/jackc/pgtype.Hstore"
  10. - db_type: "text"
  11. nullable: true
  12. go_type:
  13. import: "gopkg.in/guregu/null.v4"
  14. package: "null"
  15. type: "String"
  16. - db_type: "timestamptz"
  17. nullable: true
  18. go_type:
  19. import: "gopkg.in/guregu/null.v4"
  20. package: "null"
  21. type: "Time"

这里的所有内容都可以正常工作,但是如果我添加以下内容:

  1. - db_type: "bool"
  2. nullable: true
  3. go_type:
  4. import: "gopkg.in/guregu/null.v4"
  5. package: "null"
  6. type: "Bool"

我没有得到预期的结果。我也尝试过使用booleanbit,但都没有成功,无论是否使用nullable

我在这里定义了一个更新查询:

  1. -- name: SetUser :one
  2. UPDATE users SET
  3. username = coalesce(sqlc.narg(username), username),
  4. email = coalesce(sqlc.narg('email'), email),
  5. phone = coalesce(sqlc.narg('phone'), phone),
  6. password = coalesce(sqlc.narg('password'), password),
  7. mfatoken = coalesce(sqlc.narg('mfatoken'), mfatoken),
  8. active = coalesce(sqlc.narg('active'), active)
  9. WHERE id = $1 RETURNING *;

但生成的结构体如下所示:

  1. type SetUserParams struct {
  2. ID uuid.UUID `json:"id"`
  3. Username null.String `json:"username"`
  4. Email null.String `json:"email"`
  5. Phone null.String `json:"phone"`
  6. Password null.String `json:"password"`
  7. MFAToken null.String `json:"mfatoken"`
  8. Active sql.NullBool `json:"active"`
  9. }

我想使用null.Bool而不是sql.NullBool,这可能吗?

英文:

I am using SQLC and my YAML config file contains the following type overrides for postgresql:

  1. gen:
  2. go:
  3. emit_json_tags: true
  4. package: "hmdb"
  5. out: "hmdb"
  6. overrides:
  7. - db_type: "hstore"
  8. nullable: true
  9. go_type: "github.com/jackc/pgtype.Hstore"
  10. - db_type: "text"
  11. nullable: true
  12. go_type:
  13. import: "gopkg.in/guregu/null.v4"
  14. package: "null"
  15. type: "String"
  16. - db_type: "timestamptz"
  17. nullable: true
  18. go_type:
  19. import: "gopkg.in/guregu/null.v4"
  20. package: "null"
  21. type: "Time"

Everything here works, but if I add:

  1. - db_type: "bool"
  2. nullable: true
  3. go_type:
  4. import: "gopkg.in/guregu/null.v4"
  5. package: "null"
  6. type: "Bool"

I do not get the expected result. I have also tried boolean and bit to no avail, both with and without nullable.

I have an update query defined here:

  1. -- name: SetUser :one
  2. UPDATE users SET
  3. username = coalesce(sqlc.narg(username), username),
  4. email = coalesce(sqlc.narg('email'), email),
  5. phone = coalesce(sqlc.narg('phone'), phone),
  6. password = coalesce(sqlc.narg('password'), password),
  7. mfatoken = coalesce(sqlc.narg('mfatoken'), mfatoken),
  8. active = coalesce(sqlc.narg('active'), active)
  9. WHERE id = $1 RETURNING *;

But the generated struct looks like:

  1. type SetUserParams struct {
  2. ID uuid.UUID `json:"id"`
  3. Username null.String `json:"username"`
  4. Email null.String `json:"email"`
  5. Phone null.String `json:"phone"`
  6. Password null.String `json:"password"`
  7. MFAToken null.String `json:"mfatoken"`
  8. Active sql.NullBool `json:"active"`
  9. }

I want to use null.Bool instead of sql.NullBool, is this possible?

答案1

得分: 1

请稍等,我会为您翻译这段内容。

英文:

Create your schema.yaml like this:

  1. CREATE TABLE users (
  2. ...
  3. active pg_catalog.bool
  4. )

In sqlc.yaml the entry should look something like this:

  1. - db_type: "pg_catalog.bool"
  2. nullable: true
  3. go_type:
  4. import: "gopkg.in/guregu/null.v4"
  5. package: "null"
  6. type: "Bool"

Then after sqlc generate it would look like this:

  1. type SetUserParams struct {
  2. ...
  3. Active null.Bool `json:"active"`
  4. }

So it uses null.Bool instead of sql.NullBool.

huangapple
  • 本文由 发表于 2022年12月28日 17:26:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/74938461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定