| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package drivers
-
- import (
- "fmt"
-
- "github.com/jmoiron/sqlx"
- _ "github.com/lib/pq"
- )
-
- type PostgresDriver struct{}
-
- func (d *PostgresDriver) Name() string {
- return "postgresql"
- }
-
- func (d *PostgresDriver) BuildDSN(config DBConfig) string {
- return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
- config.Host,
- config.Port,
- config.Username,
- config.Password,
- config.Database)
- }
-
- func (d *PostgresDriver) Open(config DBConfig) (*sqlx.DB, error) {
- dsn := d.BuildDSN(config)
- db, err := sqlx.Open("postgres", dsn)
- if err != nil {
- return nil, err
- }
-
- // 使用公共的连接池配置函数
- configureConnectionPool(db, config)
-
- // 测试连接
- if err = db.Ping(); err != nil {
- return nil, err
- }
-
- return db, nil
- }
-
- func init() {
- Register(&PostgresDriver{})
- }
|