pg

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2023 License: MIT Imports: 4 Imported by: 0

README

pg

用于golang database/sql 的PostgreSQL驱动

GoDoc license Go Report Card

Go Version Support

Go version FOSSA Status

PostgreSQL Version Support

PostgreSQL version PostgreSQL version PostgreSQL version PostgreSQL version PostgreSQL version PostgreSQL version PostgreSQL version

安装

go get github.com/blusewang/pg/v2

使用

通用查询

sql.Register("pq", pg.Driver{})
db, err := sql.Open("pg", "pg://user:password@dbhost.yourdomain.com/database_name?application_name=app_name&sslmode=verify-full")
if err != nil {
return err
}
defer db.Close()
rows, err := db.Query("select * from bluse where id>$1", 0)
if err != nil {
return err
}
...

异步订阅
    l, err := pg.NewListener("pg://user:password@dbhost.yourdomain.com/database_name?application_name=app_name&sslmode=verify-full")
if err != nil {
return
}

t.Log(l.Listen("channel_name"))
go func() {
time.Sleep(time.Minute)
_ = l.Terminate()
}()
for {
pid, channel, message, err := l.GetNotification()
if err == io.EOF {
t.Fatal(err)
} else if err != nil {
t.Fatal(err)
}
log.Println(pid, channel, message)
}

文档

更多的细节及使用示例,参见: https://pkg.go.dev/github.com/blusewang/pg/v2.

特性

  • Scan() 时允许传入指针,完美对应数据库中的null
  • 所有查询全部自动prepare并缓存。
  • 支持pg://前缀的URI
  • 支持Listen式的异步消息订阅
配置要求
  • 配置上需将sql.SetMaxIdleConns(x)sql.SetMaxOpenConns(x)两处的x设置为相同的值,才能让缓存实现价值。

协议实现

  • 本驱动是为Web服务而设计
状态 功能 备注
  • - [x]
启动 支持:无密码、明文密码、md5、SCRAM-SHA-256
  • - [x]
简单查询 必备
  • - [x]
扩展查询 必备
  • - [x]
取消正在处理的请求 必备
  • - [x]
终止 必备
  • - [x]
SSL会话加密 远程安全
  • - [x]
异步 listen/notify

暂不支持SCRAM-SHA-256-PLUS。当pg_hba.conf使用:

hostssl all             all             0.0.0.0/0               scram-sha-256 clientcert=verify-full

时,Server会发类型为SCRAM-SHA-256-PLUS类型的请求。此时客户端将自动降级至SCRAM-SHA-256

License

FOSSA Status

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Driver

type Driver struct {
	app.Driver
}

type Error

type Error struct {
	frame.PgError
}

type Listener

type Listener interface {
	Listen(channel string) error
	GetNotification() (pid uint32, channel, message string, err error)
	Terminate() error
}

func NewListener

func NewListener(ctx context.Context, dsnString string) (Listener, error)

Directories

Path Synopsis
internal
app
client/scram
Package scram implements a SCRAM-{SHA-1,etc} client per RFC5802.
Package scram implements a SCRAM-{SHA-1,etc} client per RFC5802.

Jump to

Keyboard shortcuts

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