* Make a database connection with the given parameters. An * interactive password prompt is automatically issued if required. * * If fail_on_error is false, we return NULL without printing any message * on failure, but preserve any prompted password for the next try. * * On success, the global variable 'connstr' is set to a connection string * containing the options used. */
| 2480 | * containing the options used. |
| 2481 | */ |
| 2482 | static PGconn * |
| 2483 | connectDatabase(const char *dbname, const char *connection_string, |
| 2484 | const char *pghost, const char *pgport, const char *pguser, |
| 2485 | trivalue prompt_password, bool fail_on_error) |
| 2486 | { |
| 2487 | PGconn *conn; |
| 2488 | bool new_pass; |
| 2489 | const char *remoteversion_str; |
| 2490 | int my_version; |
| 2491 | const char **keywords = NULL; |
| 2492 | const char **values = NULL; |
| 2493 | PQconninfoOption *conn_opts = NULL; |
| 2494 | static char *password = NULL; |
| 2495 | |
| 2496 | if (prompt_password == TRI_YES && !password) |
| 2497 | password = simple_prompt("Password: ", false); |
| 2498 | |
| 2499 | /* |
| 2500 | * Start the connection. Loop until we have a password if requested by |
| 2501 | * backend. |
| 2502 | */ |
| 2503 | do |
| 2504 | { |
| 2505 | int argcount = 6; |
| 2506 | PQconninfoOption *conn_opt; |
| 2507 | char *err_msg = NULL; |
| 2508 | int i = 0; |
| 2509 | |
| 2510 | if (keywords) |
| 2511 | free(keywords); |
| 2512 | if (values) |
| 2513 | free(values); |
| 2514 | if (conn_opts) |
| 2515 | PQconninfoFree(conn_opts); |
| 2516 | |
| 2517 | /* |
| 2518 | * Merge the connection info inputs given in form of connection string |
| 2519 | * and other options. Explicitly discard any dbname value in the |
| 2520 | * connection string; otherwise, PQconnectdbParams() would interpret |
| 2521 | * that value as being itself a connection string. |
| 2522 | */ |
| 2523 | if (connection_string) |
| 2524 | { |
| 2525 | conn_opts = PQconninfoParse(connection_string, &err_msg); |
| 2526 | if (conn_opts == NULL) |
| 2527 | { |
| 2528 | pg_log_error("%s", err_msg); |
| 2529 | exit_nicely(1); |
| 2530 | } |
| 2531 | |
| 2532 | for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++) |
| 2533 | { |
| 2534 | if (conn_opt->val != NULL && conn_opt->val[0] != '\0' && |
| 2535 | strcmp(conn_opt->keyword, "dbname") != 0) |
| 2536 | argcount++; |
| 2537 | } |
| 2538 | |
| 2539 | keywords = pg_malloc0((argcount + 1) * sizeof(*keywords)); |
no test coverage detected