Skip to content
Snippets Groups Projects
Commit 4fff132d authored by Tom Lane's avatar Tom Lane
Browse files

Revert patch that broke \d commands, until it can be fixed.

parent 03829995
Branches
Tags
No related merge requests found
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright 2000 by PostgreSQL Global Development Group * Copyright 2000 by PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.54 2003/02/19 03:54:39 momjian Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.55 2003/02/21 21:34:27 tgl Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "common.h" #include "common.h"
...@@ -42,26 +42,6 @@ ...@@ -42,26 +42,6 @@
#include "print.h" #include "print.h"
#include "mainloop.h" #include "mainloop.h"
/* Workarounds for Windows */
/* Probably to be moved up the source tree in the future, perhaps to be replaced by
* more specific checks like configure-style HAVE_GETTIMEOFDAY macros.
*/
#ifndef WIN32
typedef struct timeval TimevalStruct;
#define GETTIMEOFDAY(T) gettimeofday(T, NULL)
#define DIFF_MSEC(T, U) ((((T)->tv_sec - (U)->tv_sec) * 1000000.0 + (T)->tv_usec - (U)->tv_usec) / 1000.0)
#else
typedef struct _timeb TimevalStruct;
#define GETTIMEOFDAY(T) _ftime(&T)
#define DIFF_MSEC(T, U) ((((T)->time - (U)->time) * 1000.0 + (T)->millitm - (U)->millitm))
#endif
extern bool prompt_state; extern bool prompt_state;
/* /*
...@@ -226,63 +206,6 @@ handle_sigint(SIGNAL_ARGS) ...@@ -226,63 +206,6 @@ handle_sigint(SIGNAL_ARGS)
#endif /* not WIN32 */ #endif /* not WIN32 */
/* ConnectionUp
*
* Returns whether our backend connection is still there.
*/
static bool
ConnectionUp()
{
return PQstatus(pset.db) != CONNECTION_BAD;
}
/* CheckConnection
*
* Verify that we still have a good connection to the backend, and if not,
* see if it can be restored.
*
* Returns true if either the connection was still there, or it could be
* restored successfully; false otherwise. If, however, there was no
* connection and the session is non-interactive, this will exit the program
* with a code of EXIT_BADCONN.
*/
static bool
CheckConnection()
{
bool OK = ConnectionUp();
if (!OK)
{
if (!pset.cur_cmd_interactive)
{
psql_error("connection to server was lost\n");
exit(EXIT_BADCONN);
}
fputs(gettext("The connection to the server was lost. Attempting reset: "), stderr);
PQreset(pset.db);
OK = ConnectionUp();
if (!OK)
{
fputs(gettext("Failed.\n"), stderr);
PQfinish(pset.db);
pset.db = NULL;
SetVariable(pset.vars, "DBNAME", NULL);
SetVariable(pset.vars, "HOST", NULL);
SetVariable(pset.vars, "PORT", NULL);
SetVariable(pset.vars, "USER", NULL);
SetVariable(pset.vars, "ENCODING", NULL);
}
else
fputs(gettext("Succeeded.\n"), stderr);
}
return OK;
}
/* /*
* PSQLexec * PSQLexec
* *
...@@ -327,22 +250,19 @@ PSQLexec(const char *query, bool ignore_command_ok) ...@@ -327,22 +250,19 @@ PSQLexec(const char *query, bool ignore_command_ok)
cancelConn = pset.db; cancelConn = pset.db;
if (PQsendQuery(pset.db, query)) if (PQsendQuery(pset.db, query))
{ {
rstatus = PGRES_EMPTY_QUERY; while ((newres = PQgetResult(pset.db)) != NULL)
while (((newres = PQgetResult(pset.db)) != NULL) &&
(rstatus == PGRES_COPY_IN) &&
(rstatus == PGRES_COPY_OUT))
{ {
rstatus = PQresultStatus(newres); rstatus = PQresultStatus(newres);
if (ignore_command_ok && rstatus == PGRES_COMMAND_OK) if (ignore_command_ok && rstatus == PGRES_COMMAND_OK)
{ {
PQclear(newres); PQclear(newres);
continue;
} }
else
{
PQclear(res); PQclear(res);
res = newres; res = newres;
} if (rstatus == PGRES_COPY_IN ||
rstatus == PGRES_COPY_OUT)
break;
} }
} }
rstatus = PQresultStatus(res); rstatus = PQresultStatus(res);
...@@ -357,44 +277,126 @@ PSQLexec(const char *query, bool ignore_command_ok) ...@@ -357,44 +277,126 @@ PSQLexec(const char *query, bool ignore_command_ok)
rstatus == PGRES_COPY_IN || rstatus == PGRES_COPY_IN ||
rstatus == PGRES_COPY_OUT)) rstatus == PGRES_COPY_OUT))
return res; return res;
else
{
psql_error("%s", PQerrorMessage(pset.db)); psql_error("%s", PQerrorMessage(pset.db));
PQclear(res); PQclear(res);
CheckConnection(); if (PQstatus(pset.db) == CONNECTION_BAD)
{
if (!pset.cur_cmd_interactive)
{
psql_error("connection to server was lost\n");
exit(EXIT_BADCONN);
}
fputs(gettext("The connection to the server was lost. Attempting reset: "), stderr);
PQreset(pset.db);
if (PQstatus(pset.db) == CONNECTION_BAD)
{
fputs(gettext("Failed.\n"), stderr);
PQfinish(pset.db);
pset.db = NULL;
SetVariable(pset.vars, "DBNAME", NULL);
SetVariable(pset.vars, "HOST", NULL);
SetVariable(pset.vars, "PORT", NULL);
SetVariable(pset.vars, "USER", NULL);
SetVariable(pset.vars, "ENCODING", NULL);
}
else
fputs(gettext("Succeeded.\n"), stderr);
}
return NULL; return NULL;
} }
}
/* /*
* PrintNotifications: check for asynchronous notifications, and print them out * SendQuery: send the query string to the backend
* (and print out results)
* *
* Note: This is the "front door" way to send a query. That is, use it to
* send queries actually entered by the user. These queries will be subject to
* single step mode.
* To send "back door" queries (generated by slash commands, etc.) in a
* controlled way, use PSQLexec().
*
* Returns true if the query executed successfully, false otherwise.
*/ */
static void bool
PrintNotifications(void) SendQuery(const char *query)
{ {
bool success = false;
PGresult *results;
PGnotify *notify; PGnotify *notify;
#ifndef WIN32
struct timeval before,
after;
#else
struct _timeb before,
after;
#endif
while ((notify = PQnotifies(pset.db)) != NULL) if (!pset.db)
{ {
fprintf(pset.queryFout, gettext("Asynchronous NOTIFY '%s' from backend with pid %d received.\n"), psql_error("You are currently not connected to a database.\n");
notify->relname, notify->be_pid); return false;
free(notify);
fflush(pset.queryFout);
} }
if (GetVariableBool(pset.vars, "SINGLESTEP"))
{
char buf[3];
printf(gettext("***(Single step mode: Verify query)*********************************************\n"
"%s\n"
"***(press return to proceed or enter x and return to cancel)********************\n"),
query);
fflush(stdout);
if (fgets(buf, sizeof(buf), stdin) != NULL)
if (buf[0] == 'x')
return false;
} }
else
{
const char *var = GetVariable(pset.vars, "ECHO");
if (var && strncmp(var, "queries", strlen(var)) == 0)
puts(query);
}
/* cancelConn = pset.db;
* PrintQueryTuples: assuming query result is OK, print its tuples
* #ifndef WIN32
* Returns true if successful, false otherwise. if (pset.timing)
*/ gettimeofday(&before, NULL);
static bool results = PQexec(pset.db, query);
PrintQueryTuples(const PGresult *results) if (pset.timing)
gettimeofday(&after, NULL);
#else
if (pset.timing)
_ftime(&before);
results = PQexec(pset.db, query);
if (pset.timing)
_ftime(&after);
#endif
if (PQresultStatus(results) == PGRES_COPY_IN)
copy_in_state = true;
/* keep cancel connection for copy out state */
if (PQresultStatus(results) != PGRES_COPY_OUT)
cancelConn = NULL;
if (results == NULL)
{ {
fputs(PQerrorMessage(pset.db), pset.queryFout);
success = false;
}
else
{
switch (PQresultStatus(results))
{
case PGRES_TUPLES_OK:
/* write output to \g argument, if any */ /* write output to \g argument, if any */
if (pset.gfname) if (pset.gfname)
{ {
...@@ -409,7 +411,8 @@ PrintQueryTuples(const PGresult *results) ...@@ -409,7 +411,8 @@ PrintQueryTuples(const PGresult *results)
{ {
pset.queryFout = queryFout_copy; pset.queryFout = queryFout_copy;
pset.queryFoutPipe = queryFoutPipe_copy; pset.queryFoutPipe = queryFoutPipe_copy;
return false; success = false;
break;
} }
printQuery(results, &pset.popt, pset.queryFout); printQuery(results, &pset.popt, pset.queryFout);
...@@ -422,35 +425,14 @@ PrintQueryTuples(const PGresult *results) ...@@ -422,35 +425,14 @@ PrintQueryTuples(const PGresult *results)
free(pset.gfname); free(pset.gfname);
pset.gfname = NULL; pset.gfname = NULL;
success = true;
} }
else else
{ {
printQuery(results, &pset.popt, pset.queryFout); printQuery(results, &pset.popt, pset.queryFout);
success = true;
} }
return true;
}
/*
* PrintQueryResults: analyze query results and print them out
*
* Note: Utility function for use by SendQuery() only.
*
* Returns true if the query executed successfully, false otherwise.
*/
static bool
PrintQueryResults(PGresult *results,
const TimevalStruct *before,
const TimevalStruct *after)
{
bool success = false;
switch (PQresultStatus(results))
{
case PGRES_TUPLES_OK:
success = PrintQueryTuples(results);
break; break;
case PGRES_EMPTY_QUERY: case PGRES_EMPTY_QUERY:
success = true; success = true;
...@@ -489,88 +471,58 @@ PrintQueryResults(PGresult *results, ...@@ -489,88 +471,58 @@ PrintQueryResults(PGresult *results,
fflush(pset.queryFout); fflush(pset.queryFout);
if (!CheckConnection()) return false; if (PQstatus(pset.db) == CONNECTION_BAD)
PrintNotifications();
/* Possible microtiming output */
if (pset.timing && success)
printf(gettext("Time: %.2f ms\n"), DIFF_MSEC(after, before));
return success;
}
/*
* SendQuery: send the query string to the backend
* (and print out results)
*
* Note: This is the "front door" way to send a query. That is, use it to
* send queries actually entered by the user. These queries will be subject to
* single step mode.
* To send "back door" queries (generated by slash commands, etc.) in a
* controlled way, use PSQLexec().
*
* Returns true if the query executed successfully, false otherwise.
*/
bool
SendQuery(const char *query)
{ {
PGresult *results; if (!pset.cur_cmd_interactive)
TimevalStruct before, after;
if (!pset.db)
{ {
psql_error("You are currently not connected to a database.\n"); psql_error("connection to server was lost\n");
return false; exit(EXIT_BADCONN);
} }
fputs(gettext("The connection to the server was lost. Attempting reset: "), stderr);
if (GetVariableBool(pset.vars, "SINGLESTEP")) PQreset(pset.db);
if (PQstatus(pset.db) == CONNECTION_BAD)
{ {
char buf[3]; fputs(gettext("Failed.\n"), stderr);
PQfinish(pset.db);
printf(gettext("***(Single step mode: Verify query)*********************************************\n" PQclear(results);
"%s\n" pset.db = NULL;
"***(press return to proceed or enter x and return to cancel)********************\n"), SetVariable(pset.vars, "DBNAME", NULL);
query); SetVariable(pset.vars, "HOST", NULL);
fflush(stdout); SetVariable(pset.vars, "PORT", NULL);
if (fgets(buf, sizeof(buf), stdin) != NULL) SetVariable(pset.vars, "USER", NULL);
if (buf[0] == 'x') SetVariable(pset.vars, "ENCODING", NULL);
return false; return false;
} }
else else
{ fputs(gettext("Succeeded.\n"), stderr);
const char *var = GetVariable(pset.vars, "ECHO");
if (var && strncmp(var, "queries", strlen(var)) == 0)
puts(query);
} }
cancelConn = pset.db; /* check for asynchronous notification returns */
while ((notify = PQnotifies(pset.db)) != NULL)
if (pset.timing)
GETTIMEOFDAY(&before);
results = PQexec(pset.db, query);
if (pset.timing)
GETTIMEOFDAY(&after);
if (PQresultStatus(results) == PGRES_COPY_IN)
copy_in_state = true;
/* keep cancel connection for copy out state */
if (PQresultStatus(results) != PGRES_COPY_OUT)
cancelConn = NULL;
if (results == NULL)
{ {
fputs(PQerrorMessage(pset.db), pset.queryFout); fprintf(pset.queryFout, gettext("Asynchronous NOTIFY '%s' from backend with pid %d received.\n"),
return false; notify->relname, notify->be_pid);
free(notify);
fflush(pset.queryFout);
} }
return PrintQueryResults(results, &before, &after); if (results)
PQclear(results); PQclear(results);
} }
/* Possible microtiming output */
if (pset.timing && success)
#ifndef WIN32
printf(gettext("Time: %.2f ms\n"),
((after.tv_sec - before.tv_sec) * 1000000.0 + after.tv_usec - before.tv_usec) / 1000.0);
#else
printf(gettext("Time: %.2f ms\n"),
((after.time - before.time) * 1000.0 + after.millitm - before.millitm));
#endif
return success;
}
/* /*
* PageOutput * PageOutput
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment