mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
- Moved from PQsetdbLogin to PQconnectDB.
- Correctly parse connect options. - Changed regression tests accordingly.
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.49 2008/03/20 16:29:44 meskes Exp $ */
|
||||
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.50 2008/03/27 07:56:00 meskes Exp $ */
|
||||
|
||||
#define POSTGRES_ECPG_INTERNAL
|
||||
#include "postgres_fe.h"
|
||||
@ -255,6 +255,13 @@ ECPGnoticeReceiver(void *arg, const PGresult *result)
|
||||
ecpg_log("raising sqlcode %d\n", sqlcode);
|
||||
}
|
||||
|
||||
static int
|
||||
strlen_or_null(const char *string)
|
||||
{
|
||||
if (!string)
|
||||
return 0;
|
||||
return (strlen(string));
|
||||
}
|
||||
|
||||
/* this contains some quick hacks, needs to be cleaned up, but it works */
|
||||
bool
|
||||
@ -263,12 +270,14 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
|
||||
struct sqlca_t *sqlca = ECPGget_sqlca();
|
||||
enum COMPAT_MODE compat = c;
|
||||
struct connection *this;
|
||||
int i;
|
||||
char *dbname = name ? ecpg_strdup(name, lineno) : NULL,
|
||||
*host = NULL,
|
||||
*tmp,
|
||||
*port = NULL,
|
||||
*realname = NULL,
|
||||
*options = NULL;
|
||||
*options = NULL,
|
||||
*connect_string = NULL;
|
||||
|
||||
ecpg_init_sqlca(sqlca);
|
||||
|
||||
@ -351,7 +360,8 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
|
||||
tmp = last_dir_separator(dbname + offset);
|
||||
if (tmp != NULL) /* database name given */
|
||||
{
|
||||
realname = ecpg_strdup(tmp + 1, lineno);
|
||||
if (tmp[1] != '\0') /* non-empty database name */
|
||||
realname = ecpg_strdup(tmp + 1, lineno);
|
||||
*tmp = '\0';
|
||||
}
|
||||
|
||||
@ -460,27 +470,54 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
|
||||
#endif
|
||||
actual_connection = all_connections;
|
||||
|
||||
ecpg_log("ECPGconnect: opening database %s on %s port %s %s%s%s%s\n",
|
||||
ecpg_log("ECPGconnect: opening database %s on %s port %s %s%s %s%s\n",
|
||||
realname ? realname : "<DEFAULT>",
|
||||
host ? host : "<DEFAULT>",
|
||||
port ? (ecpg_internal_regression_mode ? "<REGRESSION_PORT>" : port) : "<DEFAULT>",
|
||||
options ? "with options " : "", options ? options : "",
|
||||
user ? "for user " : "", user ? user : "");
|
||||
|
||||
this->connection = PQsetdbLogin(host, port, options, NULL, realname, user, passwd);
|
||||
connect_string = ecpg_alloc( strlen_or_null(host)
|
||||
+ strlen_or_null(port)
|
||||
+ strlen_or_null(options)
|
||||
+ strlen_or_null(realname)
|
||||
+ strlen_or_null(user)
|
||||
+ strlen_or_null(passwd)
|
||||
+ sizeof(" host = port = dbname = user = password ="), lineno);
|
||||
|
||||
if (options) /* replace '&' if tehre are any */
|
||||
for (i = 0; options[i]; i++)
|
||||
if (options[i] == '&')
|
||||
options[i] = ' ';
|
||||
|
||||
sprintf(connect_string,"%s%s %s%s %s%s %s%s %s%s %s",
|
||||
realname ? "dbname=" : "", realname ? realname : "",
|
||||
host ? "host=" : "", host ? host : "",
|
||||
port ? "port=" : "", port ? port : "",
|
||||
user ? "user=" : "", user ? user : "",
|
||||
passwd ? "password=" : "", passwd ? passwd : "",
|
||||
options ? options : "");
|
||||
|
||||
/* this is deprecated
|
||||
* this->connection = PQsetdbLogin(host, port, options, NULL, realname, user, passwd);*/
|
||||
this->connection = PQconnectdb(connect_string);
|
||||
|
||||
ecpg_free(connect_string);
|
||||
if (host)
|
||||
ecpg_free(host);
|
||||
if (port)
|
||||
ecpg_free(port);
|
||||
if (options)
|
||||
ecpg_free(options);
|
||||
if (dbname)
|
||||
ecpg_free(dbname);
|
||||
|
||||
if (PQstatus(this->connection) == CONNECTION_BAD)
|
||||
{
|
||||
const char *errmsg = PQerrorMessage(this->connection);
|
||||
const char *db = realname ? realname : "<DEFAULT>";
|
||||
|
||||
ecpg_log("ECPGconnect: could not open database %s on %s port %s %s%s%s%s in line %d\n\t%s\n",
|
||||
db,
|
||||
host ? host : "<DEFAULT>",
|
||||
port ? (ecpg_internal_regression_mode ? "<REGRESSION_PORT>" : port) : "<DEFAULT>",
|
||||
options ? "with options " : "", options ? options : "",
|
||||
user ? "for user " : "", user ? user : "",
|
||||
lineno, errmsg);
|
||||
ecpg_log("ECPGconnect: could not open database: %s\n", errmsg);
|
||||
|
||||
ecpg_finish(this);
|
||||
#ifdef ENABLE_THREAD_SAFETY
|
||||
@ -488,33 +525,19 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
|
||||
#endif
|
||||
|
||||
ecpg_raise(lineno, ECPG_CONNECT, ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION, db);
|
||||
if (host)
|
||||
ecpg_free(host);
|
||||
if (port)
|
||||
ecpg_free(port);
|
||||
if (options)
|
||||
ecpg_free(options);
|
||||
if (realname)
|
||||
ecpg_free(realname);
|
||||
if (dbname)
|
||||
ecpg_free(dbname);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (realname)
|
||||
ecpg_free(realname);
|
||||
|
||||
#ifdef ENABLE_THREAD_SAFETY
|
||||
pthread_mutex_unlock(&connections_mutex);
|
||||
#endif
|
||||
|
||||
if (host)
|
||||
ecpg_free(host);
|
||||
if (port)
|
||||
ecpg_free(port);
|
||||
if (options)
|
||||
ecpg_free(options);
|
||||
if (realname)
|
||||
ecpg_free(realname);
|
||||
if (dbname)
|
||||
ecpg_free(dbname);
|
||||
|
||||
this->committed = true;
|
||||
this->autocommit = autocommit;
|
||||
|
||||
|
Reference in New Issue
Block a user