* do_connect -- handler for \connect * * Connects to a database with given parameters. If we are told to re-use * parameters, parameters from the previous connection are used where the * command's own options do not supply a value. Otherwise, libpq defaults * are used. * * In interactive mode, if connection fails with the given parameters, * the old connection will be kept. */
| 3090 | * the old connection will be kept. |
| 3091 | */ |
| 3092 | static bool |
| 3093 | do_connect(enum trivalue reuse_previous_specification, |
| 3094 | char *dbname, char *user, char *host, char *port) |
| 3095 | { |
| 3096 | PGconn *o_conn = pset.db, |
| 3097 | *n_conn = NULL; |
| 3098 | PQconninfoOption *cinfo; |
| 3099 | int nconnopts = 0; |
| 3100 | bool same_host = false; |
| 3101 | char *password = NULL; |
| 3102 | char *client_encoding; |
| 3103 | bool success = true; |
| 3104 | bool keep_password = true; |
| 3105 | bool has_connection_string; |
| 3106 | bool reuse_previous; |
| 3107 | |
| 3108 | has_connection_string = dbname ? |
| 3109 | recognized_connection_string(dbname) : false; |
| 3110 | |
| 3111 | /* Complain if we have additional arguments after a connection string. */ |
| 3112 | if (has_connection_string && (user || host || port)) |
| 3113 | { |
| 3114 | pg_log_error("Do not give user, host, or port separately when using a connection string"); |
| 3115 | return false; |
| 3116 | } |
| 3117 | |
| 3118 | switch (reuse_previous_specification) |
| 3119 | { |
| 3120 | case TRI_YES: |
| 3121 | reuse_previous = true; |
| 3122 | break; |
| 3123 | case TRI_NO: |
| 3124 | reuse_previous = false; |
| 3125 | break; |
| 3126 | default: |
| 3127 | reuse_previous = !has_connection_string; |
| 3128 | break; |
| 3129 | } |
| 3130 | |
| 3131 | /* |
| 3132 | * If we intend to re-use connection parameters, collect them out of the |
| 3133 | * old connection, then replace individual values as necessary. (We may |
| 3134 | * need to resort to looking at pset.dead_conn, if the connection died |
| 3135 | * previously.) Otherwise, obtain a PQconninfoOption array containing |
| 3136 | * libpq's defaults, and modify that. Note this function assumes that |
| 3137 | * PQconninfo, PQconndefaults, and PQconninfoParse will all produce arrays |
| 3138 | * containing the same options in the same order. |
| 3139 | */ |
| 3140 | if (reuse_previous) |
| 3141 | { |
| 3142 | if (o_conn) |
| 3143 | cinfo = PQconninfo(o_conn); |
| 3144 | else if (pset.dead_conn) |
| 3145 | cinfo = PQconninfo(pset.dead_conn); |
| 3146 | else |
| 3147 | { |
| 3148 | /* This is reachable after a non-interactive \connect failure */ |
| 3149 | pg_log_error("No database connection exists to re-use parameters from"); |
no test coverage detected