pgsql

package module
v2.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 17 Imported by: 14

Documentation

Overview

Package pgsql implements gdb.Driver, which supports operations for database PostgreSQL.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() gdb.Driver

New create and returns a driver that implements gdb.Driver, which supports operations for PostgreSql.

Types

type Driver

type Driver struct {
	*gdb.Core
}

Driver is the driver for postgresql database.

func (*Driver) CheckLocalTypeForField added in v2.2.1

func (d *Driver) CheckLocalTypeForField(ctx context.Context, fieldType string, fieldValue any) (gdb.LocalType, error)

CheckLocalTypeForField checks and returns corresponding local golang type for given db type. The parameter `fieldType` is in lower case, like: `int2`, `int4`, `int8`, `_int2`, `_int4`, `_int8`, `_float4`, `_float8`, etc.

PostgreSQL type mapping:

| PostgreSQL Type              | Local Go Type |
|------------------------------|---------------|
| int2, int4                   | int           |
| int8                         | int64         |
| uuid                         | uuid.UUID     |
| _int2, _int4                 | []int32       | // Note: pq package does not provide Int16Array; int32 is used for compatibility
| _int8                        | []int64       |
| _float4                      | []float32     |
| _float8                      | []float64     |
| _bool                        | []bool        |
| _varchar, _text              | []string      |
| _char, _bpchar               | []string      |
| _numeric, _decimal, _money   | []float64     |
| _bytea                       | [][]byte      |
| _uuid                        | []uuid.UUID   |

func (*Driver) ConvertValueForField added in v2.7.2

func (d *Driver) ConvertValueForField(ctx context.Context, fieldType string, fieldValue any) (any, error)

ConvertValueForField converts value to database acceptable value.

func (*Driver) ConvertValueForLocal added in v2.1.2

func (d *Driver) ConvertValueForLocal(ctx context.Context, fieldType string, fieldValue any) (any, error)

ConvertValueForLocal converts value to local Golang type of value according field type name from database. The parameter `fieldType` is in lower case, like: `int2`, `int4`, `int8`, `_int2`, `_int4`, `_int8`, `uuid`, `_uuid`, etc.

See: https://www.postgresql.org/docs/current/datatype.html

PostgreSQL type mapping:

| PostgreSQL Type | SQL Type                       | pq Type         | Go Type     |
|-----------------|--------------------------------|-----------------|-------------|
| int2            | int2, smallint                 | -               | int         |
| int4            | int4, integer                  | -               | int         |
| int8            | int8, bigint, bigserial        | -               | int64       |
| uuid            | uuid                           | -               | uuid.UUID   |
| _int2           | int2[], smallint[]             | pq.Int32Array   | []int32     |
| _int4           | int4[], integer[]              | pq.Int32Array   | []int32     |
| _int8           | int8[], bigint[]               | pq.Int64Array   | []int64     |
| _float4         | float4[], real[]               | pq.Float32Array | []float32   |
| _float8         | float8[], double precision[]   | pq.Float64Array | []float64   |
| _bool           | boolean[], bool[]              | pq.BoolArray    | []bool      |
| _varchar        | varchar[], character varying[] | pq.StringArray  | []string    |
| _text           | text[]                         | pq.StringArray  | []string    |
| _char, _bpchar  | char[], character[]            | pq.StringArray  | []string    |
| _numeric        | numeric[]                      | pq.Float64Array | []float64   |
| _decimal        | decimal[]                      | pq.Float64Array | []float64   |
| _money          | money[]                        | pq.Float64Array | []float64   |
| _bytea          | bytea[]                        | pq.ByteaArray   | [][]byte    |
| _uuid           | uuid[]                         | pq.StringArray  | []uuid.UUID |

Note: PostgreSQL also supports these array types but they are not yet mapped:

  • _date (date[]), _timestamp (timestamp[]), _timestamptz (timestamptz[])
  • _jsonb (jsonb[]), _json (json[])

func (*Driver) DoExec added in v2.1.3

func (d *Driver) DoExec(ctx context.Context, link gdb.Link, sql string, args ...any) (result sql.Result, err error)

DoExec commits the sql string and its arguments to underlying driver through given link object and returns the execution result.

func (*Driver) DoFilter

func (d *Driver) DoFilter(
	ctx context.Context, link gdb.Link, sql string, args []any,
) (newSql string, newArgs []any, err error)

DoFilter deals with the sql string before commits it to underlying sql driver.

func (*Driver) DoInsert

func (d *Driver) DoInsert(
	ctx context.Context,
	link gdb.Link, table string, list gdb.List, option gdb.DoInsertOption,
) (result sql.Result, err error)

DoInsert inserts or updates data for given table. The list parameter must contain at least one record, which was previously validated.

func (*Driver) FormatUpsert added in v2.6.4

func (d *Driver) FormatUpsert(columns []string, list gdb.List, option gdb.DoInsertOption) (string, error)

FormatUpsert returns SQL clause of type upsert for PgSQL. For example: ON CONFLICT (id) DO UPDATE SET ...

func (*Driver) GetChars

func (d *Driver) GetChars() (charLeft string, charRight string)

GetChars returns the security char for this type of database.

func (*Driver) New

func (d *Driver) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error)

New creates and returns a database object for postgresql. It implements the interface of gdb.Driver for extra database driver installation.

func (*Driver) Open

func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error)

Open creates and returns an underlying sql.DB object for pgsql. https://pkg.go.dev/github.com/lib/pq

func (*Driver) OrderRandomFunction added in v2.7.4

func (d *Driver) OrderRandomFunction() string

OrderRandomFunction returns the SQL function for random ordering.

func (*Driver) TableFields

func (d *Driver) TableFields(ctx context.Context, table string, schema ...string) (fields map[string]*gdb.TableField, err error)

TableFields retrieves and returns the fields' information of specified table of current schema.

func (*Driver) Tables

func (d *Driver) Tables(ctx context.Context, schema ...string) (tables []string, err error)

Tables retrieves and returns the tables of current schema. It's mainly used in cli tool chain for automatically generating the models.

type Result added in v2.1.3

type Result struct {
	sql.Result
	// contains filtered or unexported fields
}

func (Result) LastInsertId added in v2.1.3

func (pgr Result) LastInsertId() (int64, error)

func (Result) RowsAffected added in v2.1.3

func (pgr Result) RowsAffected() (int64, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL