a6991079de
Add postgres to Devbox * addcmd
105 lines
2.2 KiB
Go
105 lines
2.2 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type DBConfig struct {
|
|
DBUser string `env:"PGUSER,required,notEmpty"`
|
|
DBSecret string `env:"PGPASSWORD,required,notEmpty"`
|
|
DBHost string `env:"PGHOST,required,notEmpty"`
|
|
DBPort int `env:"PGPORT,required,notEmpty"`
|
|
DBName string `env:"PGDATABASE,required,notEmpty"`
|
|
DBNoSSL bool `env:"DB_NOSSL" envDefault:"false"`
|
|
DBPool Pool
|
|
DBQueries *repository.Queries
|
|
DBPoolConfig *pgxpool.Config
|
|
DBEnumTypes []string
|
|
}
|
|
|
|
type ConfigProvider interface {
|
|
GetDBBaseURI() string
|
|
GetDBURI() string
|
|
GetDBOpts() map[string]string
|
|
GetDBOptsString() string
|
|
GetDBHost() string
|
|
GetDBPort() int
|
|
GetDBUser() string
|
|
GetDBSecret() string
|
|
IsDBNoSSL() bool
|
|
GetDBName() string
|
|
GetDBDriver() string
|
|
SetDBPoolConfig() error
|
|
SetDBPool(ctx context.Context) error
|
|
GetDBQueries() *repository.Queries
|
|
GetDBPool() Pool
|
|
DBPing(context.Context) error
|
|
ExecuteDBTransaction(context.Context, func(context.Context, *repository.Queries) error) error
|
|
}
|
|
|
|
func (b *DBConfig) GetDBOpts() map[string]string {
|
|
opts := make(map[string]string)
|
|
|
|
if b.DBNoSSL {
|
|
opts["sslmode"] = "disable"
|
|
}
|
|
|
|
return opts
|
|
}
|
|
|
|
func (b *DBConfig) GetDBOptsString() string {
|
|
str := ""
|
|
opts := b.GetDBOpts()
|
|
isFirst := true
|
|
for key, value := range opts {
|
|
if !isFirst {
|
|
str += "&"
|
|
} else {
|
|
isFirst = false
|
|
}
|
|
str += fmt.Sprintf("%s=%s", key, value)
|
|
}
|
|
|
|
return str
|
|
}
|
|
|
|
func (b *DBConfig) GetDBDriver() string {
|
|
return "postgres"
|
|
}
|
|
|
|
func (b *DBConfig) GetDBBaseURI() string {
|
|
return fmt.Sprintf("%s://%s:%s@%s:%d/", b.GetDBDriver(), b.DBUser, b.DBSecret, b.DBHost, b.DBPort)
|
|
}
|
|
|
|
func (b *DBConfig) GetDBURI() string {
|
|
return fmt.Sprintf("%s%s?%s", b.GetDBBaseURI(), b.DBName, b.GetDBOptsString())
|
|
}
|
|
|
|
func (b *DBConfig) GetDBHost() string {
|
|
return b.DBHost
|
|
}
|
|
|
|
func (b *DBConfig) GetDBPort() int {
|
|
return b.DBPort
|
|
}
|
|
|
|
func (b *DBConfig) GetDBUser() string {
|
|
return b.DBUser
|
|
}
|
|
|
|
func (b *DBConfig) GetDBSecret() string {
|
|
return b.DBSecret
|
|
}
|
|
|
|
func (b *DBConfig) IsDBNoSSL() bool {
|
|
return b.DBNoSSL
|
|
}
|
|
|
|
func (b *DBConfig) GetDBName() string {
|
|
return b.DBName
|
|
}
|