* Connect to the server. Returns a valid PGconn pointer if connected, * or NULL on non-permanent error. On permanent error, the function will * call exit(1) directly. */
| 59 | * call exit(1) directly. |
| 60 | */ |
| 61 | PGconn * |
| 62 | GetConnection(void) |
| 63 | { |
| 64 | PGconn *tmpconn; |
| 65 | int argcount = 7; /* dbname, replication, fallback_app_name, |
| 66 | * host, user, port, password */ |
| 67 | int i; |
| 68 | const char **keywords; |
| 69 | const char **values; |
| 70 | const char *tmpparam; |
| 71 | bool need_password; |
| 72 | PQconninfoOption *conn_opts = NULL; |
| 73 | PQconninfoOption *conn_opt; |
| 74 | char *err_msg = NULL; |
| 75 | |
| 76 | /* pg_recvlogical uses dbname only; others use connection_string only. */ |
| 77 | Assert(dbname == NULL || connection_string == NULL); |
| 78 | |
| 79 | /* |
| 80 | * Merge the connection info inputs given in form of connection string, |
| 81 | * options and default values (dbname=replication, replication=true, etc.) |
| 82 | * Explicitly discard any dbname value in the connection string; |
| 83 | * otherwise, PQconnectdbParams() would interpret that value as being |
| 84 | * itself a connection string. |
| 85 | */ |
| 86 | i = 0; |
| 87 | if (connection_string) |
| 88 | { |
| 89 | conn_opts = PQconninfoParse(connection_string, &err_msg); |
| 90 | if (conn_opts == NULL) |
| 91 | { |
| 92 | pg_log_error("%s", err_msg); |
| 93 | exit(1); |
| 94 | } |
| 95 | |
| 96 | for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++) |
| 97 | { |
| 98 | if (conn_opt->val != NULL && conn_opt->val[0] != '\0' && |
| 99 | strcmp(conn_opt->keyword, "dbname") != 0) |
| 100 | argcount++; |
| 101 | } |
| 102 | |
| 103 | keywords = pg_malloc0((argcount + 1) * sizeof(*keywords)); |
| 104 | values = pg_malloc0((argcount + 1) * sizeof(*values)); |
| 105 | |
| 106 | for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++) |
| 107 | { |
| 108 | if (conn_opt->val != NULL && conn_opt->val[0] != '\0' && |
| 109 | strcmp(conn_opt->keyword, "dbname") != 0) |
| 110 | { |
| 111 | keywords[i] = conn_opt->keyword; |
| 112 | values[i] = conn_opt->val; |
| 113 | i++; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | else |
| 118 | { |
no test coverage detected