postgres

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: Apache-2.0 Imports: 7 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

View Source
const (
	// QueryExecModeExec uses the extended protocol without a prepared-statement cache.
	QueryExecModeExec = "exec"
	// QueryExecModeCacheStatement enables pgx's prepared-statement cache.
	QueryExecModeCacheStatement = "cache_statement"
	// QueryExecModeCacheDescribe caches parameter and result type descriptions.
	QueryExecModeCacheDescribe = "cache_describe"
	// QueryExecModeDescribeExec describes a statement before every execution.
	QueryExecModeDescribeExec = "describe_exec"
	// QueryExecModeSimpleProtocol uses pgx's simple query protocol.
	QueryExecModeSimpleProtocol = "simple_protocol"
)

Variables

This section is empty.

Functions

func DSN

func DSN(conf *Config) (string, error)

DSN builds a libpq-style connection string from conf.

Example

ExampleDSN shows how to build a libpq-style connection string, including an optional search_path.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlx/postgres"
)

func main() {
	dsn, err := postgres.DSN(&postgres.Config{
		Host:       "localhost",
		Port:       "5432",
		Database:   "mydb",
		Username:   "user",
		Password:   "pass",
		SearchPath: "myschema",
	})
	fmt.Println(err == nil)
	fmt.Println(dsn)
}
Output:
true
host=localhost port=5432 user=user password=pass dbname=mydb sslmode=disable TimeZone=UTC search_path=myschema
Example (QueryExecMode)

ExampleDSN_queryExecMode shows how to select a non-default pgx execution mode. See the package documentation before using a mode other than exec.

package main

import (
	"fmt"
	"strings"

	"github.com/phcp-tech/common-library-golang/dbsqlx/postgres"
)

func main() {
	dsn, err := postgres.DSN(&postgres.Config{
		Host:          "localhost",
		Port:          "5432",
		Database:      "mydb",
		Username:      "user",
		Password:      "pass",
		QueryExecMode: postgres.QueryExecModeCacheStatement,
	})
	fmt.Println(err)
	fmt.Println(strings.Contains(dsn, "default_query_exec_mode=cache_statement"))
}
Output:
<nil>
true

func InitDefault

func InitDefault(conf *Config) error

InitDefault opens PostgreSQL and stores it as the dbsqlx default database.

Example

ExampleInitDefault shows the default-instance pattern. Call InitDefault once at application startup; dbsqlx.Default() returns the shared *sqlx.DB for the process.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlx/postgres"
)

func main() {
	err := postgres.InitDefault(&postgres.Config{
		Host:     "127.0.0.1",
		Port:     "1",
		Database: "mydb",
		Username: "user",
		Password: "pass",
	})
	fmt.Println(err != nil) // true — server unreachable
}

func NewPostgres

func NewPostgres(conf *Config) (*sqlx.DB, error)

NewPostgres opens a PostgreSQL-backed *sqlx.DB via the pgx stdlib driver. Open verifies connectivity with an eager ping — a non-nil error is returned immediately when the database is unreachable.

Example

ExampleNewPostgres shows how to open a PostgreSQL-backed *sqlx.DB. Open verifies connectivity with an eager ping and runs SHOW search_path — a non-nil error is returned immediately when the server is unreachable.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlx/postgres"
)

func main() {
	_, err := postgres.NewPostgres(&postgres.Config{
		Host:     "127.0.0.1",
		Port:     "1",
		Database: "mydb",
		Username: "user",
		Password: "pass",
	})
	fmt.Println(err != nil) // true — server unreachable, Open pings eagerly
}
Output:
true

func ZhSortSql added in v0.1.37

func ZhSortSql(para *dto.PageParameter) string

ZhSortSql builds an ORDER BY clause that approximates pinyin ordering for a Chinese-text column, using PostgreSQL's convert_to to re-encode the column before comparing byte order — GBK/GB18030 codepoints are assigned roughly in pinyin order for common Han characters, so sorting the converted bytes approximates a pinyin sort. para.Charset selects the target encoding (e.g. "GBK", "GB18030"); it defaults to "GBK" when empty.

This is PostgreSQL-specific: convert_to does not exist on MySQL, SQLite, or ClickHouse. dbsqlx.SortSql deliberately does not offer this — see its doc comment for why.

para.Sort and the resolved charset are validated with the same identifier-safety rules dbsqlx.SortSql itself uses; if either is unsafe, ZhSortSql falls back to dbsqlx.SortSql's plain "ORDER BY <column> <direction>" behaviour instead (which also handles defaulting Sort to "id" and Direction to "ASC").

Example

ExampleZhSortSql shows how to build an ORDER BY clause that approximates pinyin ordering for a Chinese-text column, then append it directly to a raw SELECT statement alongside dbsqlx.PageSql for pagination. This is PostgreSQL-specific — see ZhSortSql's doc comment for why.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlx/postgres"
	"github.com/phcp-tech/common-library-golang/dto"
)

func main() {
	para := dto.PageParameter{Sort: "name", Direction: "ASC"}
	query := "SELECT * FROM products" + postgres.ZhSortSql(&para)
	fmt.Println(query)
}
Output:
SELECT * FROM products ORDER BY convert_to(name,'GBK') ASC

Types

type Config

type Config struct {
	// Required connection settings.
	Host       string
	Port       string
	Database   string
	Username   string
	Password   string
	SearchPath string
	// QueryExecMode selects a pgx query execution mode. The zero value preserves
	// pgx's default; see the QueryExecMode constants for mode-specific constraints.
	QueryExecMode string

	// Pool tuning; zero values fall back to dbsqlx package defaults.
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime int
	ConnMaxIdletime int
}

Config contains PostgreSQL connection settings.

Directories

Path Synopsis
Package component provides sqlx PostgreSQL lifecycle integration for bootstrap.
Package component provides sqlx PostgreSQL lifecycle integration for bootstrap.

Jump to

Keyboard shortcuts

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