diff --git a/src/bin/psql/psql.c b/src/bin/psql/psql.c index 8cb1ba68df8351e0ae6f8e818f8a1420ee081055..e5b37a16b20fb56ac304b28f8c8bbc991b781d3d 100644 --- a/src/bin/psql/psql.c +++ b/src/bin/psql/psql.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.17 1996/08/10 05:02:53 scrappy Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.18 1996/08/14 04:56:48 scrappy Exp $ * *------------------------------------------------------------------------- */ @@ -988,6 +988,10 @@ MainLoop(PsqlSettings *settings, FILE *source) bool querySent = 0; bool interactive; READ_ROUTINE GetNextLine; + bool connected = 1; + /* We are connected to the backend (last time we looked) */ + bool eof = 0; + /* We've reached the end of our command input. */ interactive = ((source == stdin) && !settings->notty); #define PROMPT "=> " @@ -1012,9 +1016,13 @@ MainLoop(PsqlSettings *settings, FILE *source) query[0] = '\0'; /* main loop for getting queries and executing them */ - while ((line = GetNextLine(settings->prompt, source)) != NULL) - { - exitStatus = 0; + while (connected && !eof) { + line = GetNextLine(settings->prompt, source); + if (line == NULL) { /* No more input. Time to quit */ + printf("EOF\n"); /* Goes on prompt line */ + eof = 1; + } else { + exitStatus = 0; line = rightTrim(line); /* remove whitespaces on the right, incl. \n's */ if (line[0] == '\0') { @@ -1099,11 +1107,16 @@ MainLoop(PsqlSettings *settings, FILE *source) exitStatus = SendQuery(settings, query); querySent = 1; + if (PQstatus(settings->db) == CONNECTION_BAD) { + connected = 0; + fprintf(stderr, "We have lost the connection to the backend, so " + "further processing is impossible. Terminating.\n"); + } } - - free(line); /* free storage malloc'd by GetNextLine */ - } /* while */ - return exitStatus; + free(line); /* free storage malloc'd by GetNextLine */ + } + } /* while */ + return exitStatus; } int diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index b81ae8535b18939b96b428372731c856f86b55b0..a1586bb64c3b83775e56b34fc530870bf7775349 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.15 1996/08/13 01:34:27 scrappy Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.16 1996/08/14 04:56:55 scrappy Exp $ * *------------------------------------------------------------------------- */ @@ -387,17 +387,26 @@ PQexec(PGconn* conn, const char* query) /* check to see if the query string is too long */ if (strlen(query) > MAX_MESSAGE_LEN) { - sprintf(conn->errorMessage, "PQexec() -- query is too long. Maximum length is %d\n", MAX_MESSAGE_LEN -2 ); + sprintf(conn->errorMessage, "PQexec() -- query is too long. " + "Maximum length is %d\n", MAX_MESSAGE_LEN -2 ); return NULL; } + /* Don't try to send if we know there's no live connection. */ + if (conn->status != CONNECTION_OK) { + sprintf(conn->errorMessage, "PQexec() -- There is no connection " + "to the backend.\n"); + return NULL; + } + /* the frontend-backend protocol uses 'Q' to designate queries */ sprintf(buffer,"Q%s",query); /* send the query to the backend; */ if (pqPuts(buffer,pfout, pfdebug) == 1) { (void) sprintf(conn->errorMessage, - "PQexec() -- while sending query: %s\n-- fprintf to Pfout failed: errno=%d\n%s\n", + "PQexec() -- while sending query: %s\n" + "-- fprintf to Pfout failed: errno=%d\n%s\n", query, errno,strerror(errno)); return NULL; } @@ -414,7 +423,12 @@ PQexec(PGconn* conn, const char* query) if (id == EOF) { /* hmm, no response from the backend-end, that's bad */ (void) sprintf(conn->errorMessage, - "PQexec() -- No response from backend\n"); + "PQexec() -- Request was sent to backend, but backend " + "closed the channel before " + "responding. This probably means the backend " + "terminated abnormally before or while processing " + "the request.\n"); + conn->status = CONNECTION_BAD; /* No more connection to backend */ return (PGresult*)NULL; }