SQLC覆盖布尔类型PostgreSQL

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

SQLC Override Bool Type PostgreSQL

问题

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

gen:
  go:
    emit_json_tags: true
    package: "hmdb"
    out: "hmdb"
    overrides:
    - db_type: "hstore"
      nullable: true
      go_type: "github.com/jackc/pgtype.Hstore"
    - db_type: "text"
      nullable: true
      go_type:
        import: "gopkg.in/guregu/null.v4"
        package: "null"
        type: "String"
    - db_type: "timestamptz"
      nullable: true
      go_type:
        import: "gopkg.in/guregu/null.v4"
        package: "null"
        type: "Time"

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

    - db_type: "bool"
      nullable: true
      go_type:
        import: "gopkg.in/guregu/null.v4"
        package: "null"
        type: "Bool"

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

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

-- name: SetUser :one
UPDATE users SET 
    username = coalesce(sqlc.narg(username), username),
    email = coalesce(sqlc.narg('email'), email),
    phone = coalesce(sqlc.narg('phone'), phone),
    password = coalesce(sqlc.narg('password'), password),
    mfatoken = coalesce(sqlc.narg('mfatoken'), mfatoken),
    active = coalesce(sqlc.narg('active'), active)
    WHERE id = $1 RETURNING *;

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

type SetUserParams struct {
    ID       uuid.UUID    `json:"id"`
    Username null.String  `json:"username"`
    Email    null.String  `json:"email"`
    Phone    null.String  `json:"phone"`
    Password null.String  `json:"password"`
    MFAToken null.String  `json:"mfatoken"`
    Active   sql.NullBool `json:"active"`
}

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

英文:

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

gen:
  go:
    emit_json_tags: true
    package: "hmdb"
    out: "hmdb"
    overrides:
    - db_type: "hstore"
      nullable: true
      go_type: "github.com/jackc/pgtype.Hstore"
    - db_type: "text"
      nullable: true
      go_type:
        import: "gopkg.in/guregu/null.v4"
        package: "null"
        type: "String"
    - db_type: "timestamptz"
      nullable: true
      go_type:
        import: "gopkg.in/guregu/null.v4"
        package: "null"
        type: "Time"

Everything here works, but if I add:

    - db_type: "bool"
      nullable: true
      go_type:
        import: "gopkg.in/guregu/null.v4"
        package: "null"
        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:

-- name: SetUser :one
UPDATE users SET 
    username = coalesce(sqlc.narg(username), username),
    email = coalesce(sqlc.narg('email'), email),
    phone = coalesce(sqlc.narg('phone'), phone),
    password = coalesce(sqlc.narg('password'), password),
    mfatoken = coalesce(sqlc.narg('mfatoken'), mfatoken),
    active = coalesce(sqlc.narg('active'), active)
    WHERE id = $1 RETURNING *;

But the generated struct looks like:

type SetUserParams struct {
	ID       uuid.UUID    `json:"id"`
	Username null.String  `json:"username"`
	Email    null.String  `json:"email"`
	Phone    null.String  `json:"phone"`
	Password null.String  `json:"password"`
	MFAToken null.String  `json:"mfatoken"`
    Active   sql.NullBool `json:"active"`
}

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

答案1

得分: 1

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

英文:

Create your schema.yaml like this:

CREATE TABLE users (
    ...
    active pg_catalog.bool
)

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

   - db_type: "pg_catalog.bool"
     nullable: true
     go_type:
       import: "gopkg.in/guregu/null.v4"
       package: "null"
       type: "Bool"

Then after sqlc generate it would look like this:

type SetUserParams struct {
    ...
	Active   null.Bool   `json:"active"`
}

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:

确定