*/
#include "pg_backup_db.h"
-#include "dumpmem.h"
#include "dumputils.h"
#include <unistd.h>
#define DB_MAX_ERR_STMT 128
+/* translator: this is a module name */
static const char *modulename = gettext_noop("archiver (db)");
static void _check_database_version(ArchiveHandle *AH);
static void notice_processor(void *arg, const char *message);
static int
-_parse_version(ArchiveHandle *AH, const char *versionString)
+_parse_version(const char *versionString)
{
int v;
v = parse_version(versionString);
if (v < 0)
- die_horribly(AH, modulename, "could not parse version string \"%s\"\n", versionString);
+ exit_horribly(modulename, "could not parse version string \"%s\"\n", versionString);
return v;
}
const char *remoteversion_str;
int remoteversion;
- myversion = _parse_version(AH, PG_VERSION);
+ myversion = _parse_version(PG_VERSION);
remoteversion_str = PQparameterStatus(AH->connection, "server_version");
if (!remoteversion_str)
- die_horribly(AH, modulename, "could not get server_version from libpq\n");
+ exit_horribly(modulename, "could not get server_version from libpq\n");
- remoteversion = _parse_version(AH, remoteversion_str);
+ remoteversion = _parse_version(remoteversion_str);
AH->public.remoteVersionStr = pg_strdup(remoteversion_str);
AH->public.remoteVersion = remoteversion;
{
write_msg(NULL, "server version: %s; %s version: %s\n",
remoteversion_str, progname, PG_VERSION);
- die_horribly(AH, NULL, "aborting because of server version mismatch\n");
+ exit_horribly(NULL, "aborting because of server version mismatch\n");
}
}
{
password = simple_prompt("Password: ", 100, false);
if (password == NULL)
- die_horribly(AH, modulename, "out of memory\n");
+ exit_horribly(modulename, "out of memory\n");
}
do
free(values);
if (!newConn)
- die_horribly(AH, modulename, "failed to reconnect to database\n");
+ exit_horribly(modulename, "failed to reconnect to database\n");
if (PQstatus(newConn) == CONNECTION_BAD)
{
if (!PQconnectionNeedsPassword(newConn))
- die_horribly(AH, modulename, "could not reconnect to database: %s",
- PQerrorMessage(newConn));
+ exit_horribly(modulename, "could not reconnect to database: %s",
+ PQerrorMessage(newConn));
PQfinish(newConn);
if (password)
if (AH->promptPassword != TRI_NO)
password = simple_prompt("Password: ", 100, false);
else
- die_horribly(AH, modulename, "connection needs password\n");
+ exit_horribly(modulename, "connection needs password\n");
if (password == NULL)
- die_horribly(AH, modulename, "out of memory\n");
+ exit_horribly(modulename, "out of memory\n");
new_pass = true;
}
} while (new_pass);
bool new_pass;
if (AH->connection)
- die_horribly(AH, modulename, "already connected to a database\n");
+ exit_horribly(modulename, "already connected to a database\n");
if (prompt_password == TRI_YES && password == NULL)
{
password = simple_prompt("Password: ", 100, false);
if (password == NULL)
- die_horribly(AH, modulename, "out of memory\n");
+ exit_horribly(modulename, "out of memory\n");
}
AH->promptPassword = prompt_password;
free(values);
if (!AH->connection)
- die_horribly(AH, modulename, "failed to connect to database\n");
+ exit_horribly(modulename, "failed to connect to database\n");
if (PQstatus(AH->connection) == CONNECTION_BAD &&
PQconnectionNeedsPassword(AH->connection) &&
PQfinish(AH->connection);
password = simple_prompt("Password: ", 100, false);
if (password == NULL)
- die_horribly(AH, modulename, "out of memory\n");
+ exit_horribly(modulename, "out of memory\n");
new_pass = true;
}
} while (new_pass);
/* check to see that the backend connection was successfully made */
if (PQstatus(AH->connection) == CONNECTION_BAD)
- die_horribly(AH, modulename, "connection to database \"%s\" failed: %s",
- PQdb(AH->connection), PQerrorMessage(AH->connection));
+ exit_horribly(modulename, "connection to database \"%s\" failed: %s",
+ PQdb(AH->connection), PQerrorMessage(AH->connection));
/* check for version mismatch */
_check_database_version(AH);
{
ArchiveHandle *AH = (ArchiveHandle *) AHX;
- PQfinish(AH->connection); /* noop if AH->connection is NULL */
+ PQfinish(AH->connection); /* noop if AH->connection is NULL */
AH->connection = NULL;
}
write_msg(NULL, "%s", message);
}
+/* Like exit_horribly(), but with a complaint about a particular query. */
+static void
+die_on_query_failure(ArchiveHandle *AH, const char *modulename, const char *query)
+{
+ write_msg(modulename, "query failed: %s",
+ PQerrorMessage(AH->connection));
+ exit_horribly(modulename, "query was: %s\n", query);
+}
void
ExecuteSqlStatement(Archive *AHX, const char *query)
{
- ArchiveHandle *AH = (ArchiveHandle *) AHX;
+ ArchiveHandle *AH = (ArchiveHandle *) AHX;
PGresult *res;
res = PQexec(AH->connection, query);
PGresult *
ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
{
- ArchiveHandle *AH = (ArchiveHandle *) AHX;
+ ArchiveHandle *AH = (ArchiveHandle *) AHX;
PGresult *res;
res = PQexec(AH->connection, query);
errStmt[DB_MAX_ERR_STMT - 2] = '.';
errStmt[DB_MAX_ERR_STMT - 1] = '\0';
}
- warn_or_die_horribly(AH, modulename, "%s: %s Command was: %s\n",
- desc, PQerrorMessage(conn), errStmt);
+ warn_or_exit_horribly(AH, modulename, "%s: %s Command was: %s\n",
+ desc, PQerrorMessage(conn), errStmt);
break;
}
for (; qry < eos; qry++)
{
- char ch = *qry;
+ char ch = *qry;
/* For neatness, we skip any newlines between commands */
if (!(ch == '\n' && AH->sqlparse.curCmd->len == 0))
*/
if (AH->pgCopyIn &&
PQputCopyData(AH->connection, buf, bufLen) <= 0)
- die_horribly(AH, modulename, "error returned by PQputCopyData: %s",
- PQerrorMessage(AH->connection));
+ exit_horribly(modulename, "error returned by PQputCopyData: %s",
+ PQerrorMessage(AH->connection));
}
else if (AH->outputKind == OUTPUT_OTHERDATA)
{
ExecuteSqlCommand(AH, buf, "could not execute query");
else
{
- char *str = (char *) pg_malloc(bufLen + 1);
+ char *str = (char *) pg_malloc(bufLen + 1);
memcpy(str, buf, bufLen);
str[bufLen] = '\0';
PGresult *res;
if (PQputCopyEnd(AH->connection, NULL) <= 0)
- die_horribly(AH, modulename, "error returned by PQputCopyEnd: %s",
- PQerrorMessage(AH->connection));
+ exit_horribly(modulename, "error returned by PQputCopyEnd: %s",
+ PQerrorMessage(AH->connection));
/* Check command status and return to normal libpq state */
res = PQgetResult(AH->connection);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
- warn_or_die_horribly(AH, modulename, "COPY failed for table \"%s\": %s",
- te->tag, PQerrorMessage(AH->connection));
+ warn_or_exit_horribly(AH, modulename, "COPY failed for table \"%s\": %s",
+ te->tag, PQerrorMessage(AH->connection));
PQclear(res);
AH->pgCopyIn = false;