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 GetDBAdminDBURI() string GetDBOpts() map[string]string GetDBOptsString() string GetDBHost() string SetDBHost(string) GetDBPort() int SetDBPort(int) GetDBUser() string SetDBUser(string) GetDBSecret() string SetDBSecret(string) IsDBNoSSL() bool SetDBNoSSL(bool) GetDBName() string SetDBName(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) GetDBAdminDBURI() string { return fmt.Sprintf("%s%s?%s", b.GetDBBaseURI(), b.GetDBDriver(), b.GetDBOptsString()) } 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) SetDBHost(val string) { b.DBHost = val } func (b *DBConfig) GetDBPort() int { return b.DBPort } func (b *DBConfig) SetDBPort(val int) { b.DBPort = val } func (b *DBConfig) GetDBUser() string { return b.DBUser } func (b *DBConfig) SetDBUser(val string) { b.DBUser = val } func (b *DBConfig) GetDBSecret() string { return b.DBSecret } func (b *DBConfig) SetDBSecret(val string) { b.DBSecret = val } func (b *DBConfig) IsDBNoSSL() bool { return b.DBNoSSL } func (b *DBConfig) SetDBNoSSL(val bool) { b.DBNoSSL = val } func (b *DBConfig) GetDBName() string { return b.DBName } func (b *DBConfig) SetDBName(val string) { b.DBName = val }