*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.6 1996/08/10 00:22:44 scrappy Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.7 1996/08/19 13:25:40 scrappy Exp $
*
*-------------------------------------------------------------------------
*/
PGconn*
PQsetdb(const char *pghost, const char* pgport, const char* pgoptions, const char* pgtty, const char* dbName)
{
- PGconn *conn;
- const char *tmp;
+ PGconn *conn;
+ const char *tmp;
- conn = (PGconn*)malloc(sizeof(PGconn));
-
- if (!conn) {
- fprintf(stderr,"FATAL: PQsetdb() -- unable to allocate memory for a PGconn");
- return (PGconn*)NULL;
- }
+ conn = (PGconn*)malloc(sizeof(PGconn));
+ if (conn == NULL)
+ fprintf(stderr,
+ "FATAL: PQsetdb() -- unable to allocate memory for a PGconn");
+ else {
conn->Pfout = NULL;
conn->Pfin = NULL;
conn->Pfdebug = NULL;
conn->port = NULL;
conn->notifyList = DLNewList();
-
+
if (!pghost || pghost[0] == '\0') {
- if (!(tmp = getenv("PGHOST"))) {
- tmp = DefaultHost;
- }
- conn->pghost = strdup(tmp);
+ if (!(tmp = getenv("PGHOST"))) {
+ tmp = DefaultHost;
+ }
+ conn->pghost = strdup(tmp);
} else
- conn->pghost = strdup(pghost);
-
+ conn->pghost = strdup(pghost);
+
if (!pgport || pgport[0] == '\0') {
- if (!(tmp = getenv("PGPORT"))) {
- tmp = POSTPORT;
- }
- conn->pgport = strdup(tmp);
+ if (!(tmp = getenv("PGPORT"))) {
+ tmp = POSTPORT;
+ }
+ conn->pgport = strdup(tmp);
} else
- conn->pgport = strdup(pgport);
-
+ conn->pgport = strdup(pgport);
+
if (!pgtty || pgtty[0] == '\0') {
- if (!(tmp = getenv("PGTTY"))) {
- tmp = DefaultTty;
- }
- conn->pgtty = strdup(tmp);
+ if (!(tmp = getenv("PGTTY"))) {
+ tmp = DefaultTty;
+ }
+ conn->pgtty = strdup(tmp);
} else
- conn->pgtty = strdup(pgtty);
-
+ conn->pgtty = strdup(pgtty);
+
if (!pgoptions || pgoptions[0] == '\0') {
- if (!(tmp = getenv("PGOPTIONS"))) {
- tmp = DefaultOption;
- }
- conn->pgoptions = strdup(tmp);
- } else
- conn->pgoptions = strdup(pgoptions);
-#if 0
- if (!dbName || dbName[0] == '\0') {
- char errorMessage[ERROR_MSG_LENGTH];
- if (!(tmp = getenv("PGDATABASE")) &&
- !(tmp = fe_getauthname(errorMessage))) {
- sprintf(conn->errorMessage,
- "FATAL: PQsetdb: Unable to determine a database name!\n");
-/* pqdebug("%s", conn->errorMessage); */
- conn->dbName = NULL;
- return conn;
- }
- conn->dbName = strdup(tmp);
+ if (!(tmp = getenv("PGOPTIONS"))) {
+ tmp = DefaultOption;
+ }
+ conn->pgoptions = strdup(tmp);
} else
- conn->dbName = strdup(dbName);
-#endif
+ conn->pgoptions = strdup(pgoptions);
if (((tmp = dbName) && (dbName[0] != '\0')) ||
- ((tmp = getenv("PGDATABASE")))) {
+ ((tmp = getenv("PGDATABASE")))) {
conn->dbName = strdup(tmp);
} else {
char errorMessage[ERROR_MSG_LENGTH];
if ((tmp = fe_getauthname(errorMessage)) != 0) {
conn->dbName = strdup(tmp);
- free(tmp);
+ free((char*)tmp);
} else {
sprintf(conn->errorMessage,
- "FATAL: PQsetdb: Unable to determine a database name!\n");
-/* pqdebug("%s", conn->errorMessage); */
+ "FATAL: PQsetdb: Unable to determine a database name!\n");
conn->dbName = NULL;
return conn;
}
}
conn->status = connectDB(conn);
- return conn;
+ if (conn->status == CONNECTION_OK) {
+ PGresult *res;
+ /* Send a blank query to make sure everything works; in particular, that
+ the database exists.
+ */
+ res = PQexec(conn," ");
+ if (res == NULL || res->resultStatus != PGRES_EMPTY_QUERY) {
+ /* PQexec has put error message in conn->errorMessage */
+ closePGconn(conn);
+ }
+ PQclear(res);
+ }
+ }
+ return conn;
}
+
/*
* connectDB -
- * make a connection to the database, returns 1 if successful or 0 if not
+ * make a connection to the backend so it is ready to receive queries.
+ * return CONNECTION_OK if successful, CONNECTION_BAD if not.
*
*/
static ConnStatusType
int laddrlen = sizeof(struct sockaddr);
Port *port = conn->port;
int portno;
- PGresult *res;
char *user;
/*
conn->port = port;
- /* we have a connection now,
- send a blank query down to make sure the database exists*/
- res = PQexec(conn," ");
- if (res == NULL || res->resultStatus != PGRES_EMPTY_QUERY) {
- /* error will already be in conn->errorMessage */
- goto connect_errReturn;
- }
- free(res);
return CONNECTION_OK;
connect_errReturn:
if (conn->Pfout) fclose(conn->Pfout);
if (conn->Pfin) fclose(conn->Pfin);
if (conn->Pfdebug) fclose(conn->Pfdebug);
+ conn->status = CONNECTION_BAD; /* Well, not really _bad_ - just absent */
}
/*