Home > Backend Development > Golang > SQLC overrides Bool type PostgreSQL

SQLC overrides Bool type PostgreSQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2024-02-10 22:24:09
forward
1158 people have browsed it

SQLC 覆盖 Bool 类型 PostgreSQL

php小编西瓜为您带来关于SQLC覆盖Bool类型PostgreSQL的介绍。SQLC是一个强大的SQL查询构建器,它可以方便地与各种数据库进行交互。在使用PostgreSQL时,SQLC提供了对Bool类型的覆盖支持,使得我们可以更灵活地处理和查询布尔值。通过SQLC,我们可以轻松地在PostgreSQL中进行布尔值的增删改查操作,提高开发效率并简化代码编写过程。无论是初学者还是有经验的开发者,都可以通过SQLC轻松地操作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"
Copy after login

这里的一切都有效,但如果我添加:

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

我没有得到预期的结果。我也尝试过 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 *;
Copy after login

但生成的结构如下所示:

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"`
}
Copy after login

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

解决方法

像这样创建 schema.yaml

create table users (
    ...
    active pg_catalog.bool
)
Copy after login

sqlc.yaml 中,条目应如下所示:

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

然后在 sqlcgenerate 之后它看起来像这样:

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

因此它使用 null.bool 而不是 sql.nullbool

The above is the detailed content of SQLC overrides Bool type PostgreSQL. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template