postgres

package
v0.1.26 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChineseSortSql added in v0.1.26

func ChineseSortSql(para *dto.PageParameter) string

ChineseSortSql 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. dbgorm.SortSql deliberately does not offer this — see its doc comment for why. For MySQL use dbgorm/mysql.ChineseSortSql instead.

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

Example

ExampleChineseSortSql 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 dbgorm.PageSql for pagination. This is PostgreSQL-specific — see ChineseSortSql's doc comment for why.

package main

import (
	"fmt"

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

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

func Dialector

func Dialector(conf *Config) (gorm.Dialector, error)

Dialector returns a PostgreSQL GORM dialector from conf.

func InitDefault

func InitDefault(conf *Config) error

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

Example

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

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbgorm/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) (*gorm.DB, error)

NewPostgres opens a PostgreSQL-backed GORM database.

Example

ExampleNewPostgres shows how to open a PostgreSQL GORM database. GORM's PostgreSQL driver pings the server and runs SHOW search_path on Open — a non-nil error is returned when the server is unreachable.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbgorm/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, GORM pings on Open
}
Output:
true

Types

type Config

type Config struct {
	// Required connection settings.
	Host       string
	Port       string
	Database   string
	Username   string
	Password   string
	SearchPath string

	// gorm.Config fields for connection pool settings.
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime int
	ConnMaxIdletime int
	Logger          *slog.Logger
}

Config contains PostgreSQL connection settings.

Directories

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

Jump to

Keyboard shortcuts

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