ParseConfigWithOptions builds a *Config from connString and options with similar behavior to the PostgreSQL standard C library libpq. options contains settings that cannot be specified in a connString such as providing a function to get the SSL password.
(connString string, options ParseConfigOptions)
| 321 | // C library libpq. options contains settings that cannot be specified in a connString such as providing a function to |
| 322 | // get the SSL password. |
| 323 | func ParseConfigWithOptions(connString string, options ParseConfigOptions) (*Config, error) { |
| 324 | defaultSettings := defaultSettings() |
| 325 | envSettings := parseEnvSettings() |
| 326 | |
| 327 | connStringSettings := make(map[string]string) |
| 328 | if connString != "" { |
| 329 | var err error |
| 330 | // connString may be a database URL or in PostgreSQL keyword/value format |
| 331 | if strings.HasPrefix(connString, "postgres://") || strings.HasPrefix(connString, "postgresql://") { |
| 332 | connStringSettings, err = parseURLSettings(connString) |
| 333 | if err != nil { |
| 334 | return nil, &ParseConfigError{ConnString: connString, msg: "failed to parse as URL", err: err} |
| 335 | } |
| 336 | } else { |
| 337 | connStringSettings, err = parseKeywordValueSettings(connString) |
| 338 | if err != nil { |
| 339 | return nil, &ParseConfigError{ConnString: connString, msg: "failed to parse as keyword/value", err: err} |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if options.ConnStringAllowedKeys != nil { |
| 345 | allowed := make(map[string]struct{}, len(options.ConnStringAllowedKeys)) |
| 346 | for _, k := range options.ConnStringAllowedKeys { |
| 347 | allowed[canonicalConnStringKey(k)] = struct{}{} |
| 348 | } |
| 349 | for k := range connStringSettings { |
| 350 | if _, ok := allowed[k]; !ok { |
| 351 | return nil, &ParseConfigError{ConnString: connString, msg: fmt.Sprintf("connection string key %q is not in ConnStringAllowedKeys", k)} |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | settings := mergeSettings(defaultSettings, envSettings, connStringSettings) |
| 357 | if service, present := settings["service"]; present { |
| 358 | serviceSettings, err := parseServiceSettings(settings["servicefile"], service) |
| 359 | if err != nil { |
| 360 | return nil, &ParseConfigError{ConnString: connString, msg: "failed to read service", err: err} |
| 361 | } |
| 362 | |
| 363 | settings = mergeSettings(defaultSettings, envSettings, serviceSettings, connStringSettings) |
| 364 | } |
| 365 | |
| 366 | config := &Config{ |
| 367 | createdByParseConfig: true, |
| 368 | Database: settings["database"], |
| 369 | User: settings["user"], |
| 370 | Password: settings["password"], |
| 371 | RuntimeParams: make(map[string]string), |
| 372 | BuildFrontend: pgproto3.NewFrontend, |
| 373 | BuildContextWatcherHandler: func(pgConn *PgConn) ctxwatch.Handler { |
| 374 | return &DeadlineContextWatcherHandler{Conn: pgConn.conn} |
| 375 | }, |
| 376 | OnPgError: func(_ *PgConn, pgErr *PgError) bool { |
| 377 | // we want to automatically close any fatal errors |
| 378 | if strings.EqualFold(pgErr.Severity, "FATAL") { |
| 379 | return false |
| 380 | } |