Home  >  Article  >  Backend Development  >  Go - Correct way to use pgtypes

Go - Correct way to use pgtypes

PHPz
PHPzforward
2024-02-13 22:18:101186browse

Go - 使用 pgtypes 的正确方法

#php editor Xigua today introduces you to a correct method of using pgtypes, which is very important for developers who use Go language for PostgreSQL database programming. pgtypes is a Go language library that provides conversion and processing functions to and from PostgreSQL database types. Proper use of pgtypes can avoid errors and exceptions when dealing with database types, while also improving the readability and maintainability of your code. In this article, we'll detail how to use pgtypes correctly, as well as some common considerations and best practices. If you are a developer who uses Go language for PostgreSQL database programming, then please continue reading, I believe this article will be helpful to you.

Question content

I am developing a simple Go service to connect to the database for basic queries. I'm using sqlc to generate Go functions to interact with the database. When switching the driver from lib/pq to pgx/v5, the types of database fields are now pgtypes instead of Go types . Here is an example:

instead of this:

type ListAccountsParams struct {
        Owner  string `json:"owner"`
        Limit  int32  `json:"limit"`
        Offset int32  `json:"offset"`
    }

I get it now:

type ListAccountsParams struct {
        Owner  pgtype.Text `json:"owner"`
        Limit  pgtype.Int4 `json:"limit"`
        Offset pgtype.Int4 `json:"offset"`
    }

But the only way I found using pgtypes is this:

owner := pgtype.Text{
        String: "Craigs List",
        Valid: true,
    }

Instead of just executing owner := "Craigs List". For numeric types it's even more overkill, all the implementations I found were like this:

pgtype.Numeric{
    Int: big.NewInt(-543), 
    Exp: 3, 
    Status: pgtype.Present
    }

Using the sqlc config file, I can override the types to support the Go standard types, but for me the postgres text type has to be overridden to string There is no point in waiting...

In my opinion this is not the best way to use these types, it's counterintuitive to me. So my question is, am I doing it right? Is there a different approach? Ultimately, is there a way to configure sqlc to use Go types instead of pgtypes while still using the pgx/v5 driver?

Workaround

You can edit the sqlc configuration file to specify custom types for fields that you want to use Go types instead of pgtype types, and Override the type by specifying the required Go type in the sqlc.yaml

file
types:
  - name: ListAccountsParams
    fields:
      - name: Owner
        goType: string
      - name: Limit
        goType: int32
      - name: Offset
        goType: int32

Please note that using Go types directly may lead to potential type mismatches or other problems when interacting with the database, while pgtype types are specifically designed to handle PostgreSQL-specific data types, and Provides a degree of type safety and compatibility!

renew

Based on your comment, when using the pgx/v5 driver with sqlc, the generated code uses the pgtype type to handle PostgreSQL-specific data type, which is sqlc's default behavior when using the pgx/v5 driver, and the way you describe using pgtype types, such as pgtype .Text and pgtype.Int4, is the correct way to handle these types, for example, when handling the pgtype.Text type, you need to String field to the desired value and the Valid field to true.

For numeric types, the pgtype.Numeric type requires the Int, Exp, and Status fields to be set because The pgtype.Numeric type represents an arbitrary-precision integer value (Int) ), an exponent (Exp), and a status (Status) indicating Does the value exist or is empty!

The above is the detailed content of Go - Correct way to use pgtypes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete