mydb/adapter/postgresql/database_pgx.go
2023-09-18 15:15:42 +08:00

27 lines
532 B
Go

//go:build !pq
// +build !pq
package postgresql
import (
"context"
"database/sql"
"time"
"git.hexq.cn/tiglog/mydb/internal/sqladapter"
_ "github.com/jackc/pgx/v4/stdlib"
)
func (*database) OpenDSN(sess sqladapter.Session, dsn string) (*sql.DB, error) {
connURL, err := ParseURL(dsn)
if err != nil {
return nil, err
}
if tz := connURL.Options["timezone"]; tz != "" {
loc, _ := time.LoadLocation(tz)
ctx := context.WithValue(sess.Context(), "timezone", loc)
sess.SetContext(ctx)
}
return sql.Open("pgx", dsn)
}