pgsql

package
v1.0.47 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package pgsql provides PostgreSQL-specific utilities and helpers.

Overview

The pgsql package contains PostgreSQL-specific functionality including:

  • SQL reserved keyword validation
  • Data type mappings and conversions
  • PostgreSQL-specific schema introspection helpers

Components

keywords.go - SQL reserved keywords validation

Provides functions to check if identifiers conflict with SQL reserved words
and need quoting for safe usage in PostgreSQL queries.

datatypes.go - PostgreSQL data type utilities

Contains mappings between PostgreSQL data types and their equivalents in other
systems, as well as type conversion and normalization functions.

Usage

// Check if identifier needs quoting
if pgsql.IsReservedKeyword("user") {
    // Quote the identifier
}

// Normalize data type
normalizedType := pgsql.NormalizeDataType("varchar(255)")

Purpose

This package supports the PostgreSQL reader and writer implementations by providing shared utilities for handling PostgreSQL-specific schema elements and constraints.

Index

Constants

This section is empty.

Variables

View Source
var GoToPGSQLTypes = map[string]string{
	"bool":            "boolean",
	"int64":           "bigint",
	"int":             "integer",
	"int8":            "smallint",
	"int16":           "smallint",
	"int32":           "integer",
	"uint":            "integer",
	"uint8":           "smallint",
	"uint16":          "smallint",
	"uint32":          "integer",
	"uint64":          "bigint",
	"uintptr":         "bigint",
	"znullint64":      "bigint",
	"znullint32":      "integer",
	"znullbyte":       "integer",
	"float64":         "double precision",
	"float32":         "real",
	"complex64":       "double precision",
	"complex128":      "double precision",
	"customfloat64":   "double precision",
	"string":          "text",
	"Pointer":         "bigint",
	"[]byte":          "bytea",
	"customdate":      "date",
	"customtime":      "time",
	"customtimestamp": "timestamp",
	"sqlfloat64":      "double precision",
	"sqlfloat16":      "double precision",
	"sqluuid":         "uuid",
	"sqljsonb":        "jsonb",
	"sqljson":         "json",
	"sqlint64":        "bigint",
	"sqlint32":        "integer",
	"sqlint16":        "smallint",
	"sqlbool":         "boolean",
	"sqlstring":       "text",
	"nullablejsonb":   "jsonb",
	"nullablejson":    "json",
	"nullableuuid":    "uuid",
	"sqldate":         "date",
	"sqltime":         "time",
	"sqltimestamp":    "timestamp",
	"citext":          "citext",
}
View Source
var GoToStdTypes = map[string]string{
	"bool":            "boolean",
	"int64":           "bigint",
	"int":             "integer",
	"int8":            "smallint",
	"int16":           "smallint",
	"int32":           "integer",
	"uint":            "integer",
	"uint8":           "smallint",
	"uint16":          "smallint",
	"uint32":          "integer",
	"uint64":          "bigint",
	"uintptr":         "bigint",
	"znullint64":      "bigint",
	"znullint32":      "integer",
	"znullbyte":       "smallint",
	"float64":         "double",
	"float32":         "double",
	"complex64":       "double",
	"complex128":      "double",
	"customfloat64":   "double",
	"string":          "text",
	"Pointer":         "bigint",
	"[]byte":          "blob",
	"customdate":      "date",
	"customtime":      "time",
	"customtimestamp": "timestamp",
	"sqlfloat64":      "double",
	"sqlfloat16":      "double",
	"sqluuid":         "uuid",
	"sqljsonb":        "jsonb",
	"sqljson":         "json",
	"sqlint64":        "bigint",
	"sqlint32":        "integer",
	"sqlint16":        "smallint",
	"sqlbool":         "boolean",
	"sqlstring":       "text",
	"nullablejsonb":   "jsonb",
	"nullablejson":    "json",
	"nullableuuid":    "uuid",
	"sqldate":         "date",
	"sqltime":         "time",
	"sqltimestamp":    "timestamp",
}

Functions

func BuildApplicationName added in v1.0.47

func BuildApplicationName(component string) string

BuildApplicationName returns a PostgreSQL application_name in the form: relspecgo/<version>[:<component>]

func CanonicalizeBaseType added in v1.0.47

func CanonicalizeBaseType(baseType string) string

CanonicalizeBaseType resolves aliases to canonical PostgreSQL type names.

func Connect added in v1.0.47

func Connect(ctx context.Context, connString, component string) (*pgx.Conn, error)

Connect establishes a PostgreSQL connection with a default relspec application_name when the caller does not provide one in the DSN.

func ConvertSQLType

func ConvertSQLType(anytype string) string

func ElementType added in v1.0.47

func ElementType(sqlType string) string

ElementType returns the underlying element type for array types. For non-array types, it returns the input unchanged.

func ExtractBaseType added in v1.0.47

func ExtractBaseType(sqlType string) string

ExtractBaseType returns the type without outer array suffixes and modifiers. Examples: - varchar(255) -> varchar - text[] -> text - numeric(10,2)[] -> numeric

func ExtractBaseTypeLower added in v1.0.47

func ExtractBaseTypeLower(sqlType string) string

ExtractBaseTypeLower is ExtractBaseType with lowercase normalization.

func GetPostgresBaseTypes added in v1.0.47

func GetPostgresBaseTypes() []string

GetPostgresBaseTypes returns a sorted-ish stable list of registered base type names.

func GetPostgresKeywords

func GetPostgresKeywords() []string

func GetPostgresTypes added in v1.0.47

func GetPostgresTypes(includeArrays bool) []string

GetPostgresTypes returns the registered PostgreSQL types. When includeArrays is true, each base type also includes an array variant ("type[]").

func GetSQLType

func GetSQLType(anytype string) string

func GetStdTypeFromGo

func GetStdTypeFromGo(pTypeName string) string

func HasExplicitTypeModifier added in v1.0.47

func HasExplicitTypeModifier(sqlType string) bool

HasExplicitTypeModifier reports if the type already includes "(...)".

func IsArrayType added in v1.0.47

func IsArrayType(sqlType string) bool

IsArrayType reports whether the SQL type has one or more [] suffixes.

func IsGoType

func IsGoType(pTypeName string) bool

func IsKnownPostgresType added in v1.0.47

func IsKnownPostgresType(sqlType string) bool

IsKnownPostgresType reports whether a type (including array forms) exists in the registry.

func ParseConfigWithApplicationName added in v1.0.47

func ParseConfigWithApplicationName(connString, component string) (*pgx.ConnConfig, error)

ParseConfigWithApplicationName parses a connection string and applies a default application_name when one is not explicitly provided by the caller.

func SupportsLength added in v1.0.47

func SupportsLength(sqlType string) bool

SupportsLength reports if this SQL type accepts a single length/dimension modifier.

func SupportsPrecision added in v1.0.47

func SupportsPrecision(sqlType string) bool

SupportsPrecision reports if this SQL type accepts precision (and possibly scale).

func ValidSQLType

func ValidSQLType(sqltype string) bool

Types

type TypeSpec added in v1.0.47

type TypeSpec struct {
	SupportsLength    bool
	SupportsPrecision bool
}

TypeSpec describes PostgreSQL type capabilities used by parsers/writers.

Jump to

Keyboard shortcuts

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