2022-04-20 16:12:55 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DriverType to explicitly type the kind of sql driver we are using
|
|
|
|
type DriverType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
PGX DriverType = "PGX"
|
|
|
|
SQLX DriverType = "SQLX"
|
|
|
|
Unknown DriverType = "Unknown"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultConfig are default parameters for connecting to a Postgres sql
|
|
|
|
var DefaultConfig = Config{
|
|
|
|
Hostname: "localhost",
|
|
|
|
Port: 8077,
|
|
|
|
DatabaseName: "vulcanize_testing",
|
|
|
|
Username: "vdbm",
|
|
|
|
Password: "password",
|
2022-04-20 22:12:44 +00:00
|
|
|
Driver: "PGX",
|
2022-04-20 16:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveDriverType resolves a DriverType from a provided string
|
|
|
|
func ResolveDriverType(str string) (DriverType, error) {
|
|
|
|
switch strings.ToLower(str) {
|
|
|
|
case "pgx", "pgxpool":
|
|
|
|
return PGX, nil
|
|
|
|
case "sqlx":
|
|
|
|
return SQLX, nil
|
|
|
|
default:
|
|
|
|
return Unknown, fmt.Errorf("unrecognized driver type string: %s", str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config holds params for a Postgres db
|
|
|
|
type Config struct {
|
|
|
|
// conn string params
|
|
|
|
Hostname string
|
|
|
|
Port int
|
|
|
|
DatabaseName string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
|
|
|
|
// conn settings
|
|
|
|
MaxConns int
|
|
|
|
MaxIdle int
|
|
|
|
MinConns int
|
|
|
|
MaxConnIdleTime time.Duration
|
|
|
|
MaxConnLifetime time.Duration
|
|
|
|
ConnTimeout time.Duration
|
|
|
|
|
|
|
|
// driver type
|
|
|
|
Driver DriverType
|
|
|
|
}
|
|
|
|
|
|
|
|
// DbConnectionString constructs and returns the connection string from the config
|
|
|
|
func (c Config) DbConnectionString() string {
|
|
|
|
if len(c.Username) > 0 && len(c.Password) > 0 {
|
|
|
|
return fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?sslmode=disable",
|
|
|
|
c.Username, c.Password, c.Hostname, c.Port, c.DatabaseName)
|
|
|
|
}
|
|
|
|
if len(c.Username) > 0 && len(c.Password) == 0 {
|
|
|
|
return fmt.Sprintf("postgresql://%s@%s:%d/%s?sslmode=disable",
|
|
|
|
c.Username, c.Hostname, c.Port, c.DatabaseName)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("postgresql://%s:%d/%s?sslmode=disable", c.Hostname, c.Port, c.DatabaseName)
|
|
|
|
}
|