const char *objlabel);
static const char *getAttrName(int attrnum, TableInfo *tblInfo);
static const char *fmtCopyColumnList(const TableInfo *ti);
+static PGresult *ExecuteSqlQueryForSingleRow(Archive *fout, char *query);
int
main(int argc, char **argv)
Oid pg_type_oid)
{
PQExpBuffer upgrade_query = createPQExpBuffer();
- int ntups;
PGresult *upgrade_res;
Oid pg_type_array_oid;
"WHERE pg_type.oid = '%u'::pg_catalog.oid;",
pg_type_oid);
- upgrade_res = ExecuteSqlQuery(fout, upgrade_query->data, PGRES_TUPLES_OK);
-
- /* Expecting a single result only */
- ntups = PQntuples(upgrade_res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, upgrade_query->data);
- exit_nicely(1);
- }
+ upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
pg_type_array_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "typarray")));
Oid pg_rel_oid)
{
PQExpBuffer upgrade_query = createPQExpBuffer();
- int ntups;
PGresult *upgrade_res;
Oid pg_type_oid;
bool toast_set = false;
"WHERE c.oid = '%u'::pg_catalog.oid;",
pg_rel_oid);
- upgrade_res = ExecuteSqlQuery(fout, upgrade_query->data, PGRES_TUPLES_OK);
-
- /* Expecting a single result only */
- ntups = PQntuples(upgrade_res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, upgrade_query->data);
- exit_nicely(1);
- }
+ upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
bool is_index)
{
PQExpBuffer upgrade_query = createPQExpBuffer();
- int ntups;
PGresult *upgrade_res;
Oid pg_class_reltoastrelid;
Oid pg_class_reltoastidxid;
"WHERE c.oid = '%u'::pg_catalog.oid;",
pg_class_oid);
- upgrade_res = ExecuteSqlQuery(fout, upgrade_query->data, PGRES_TUPLES_OK);
-
- /* Expecting a single result only */
- ntups = PQntuples(upgrade_res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, upgrade_query->data);
- exit_nicely(1);
- }
+ upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "reltoastrelid")));
pg_class_reltoastidxid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "reltoastidxid")));
PQExpBuffer labelq = createPQExpBuffer();
PQExpBuffer query = createPQExpBuffer();
PGresult *res;
- int ntups;
char *typlen;
char *typinput;
char *typoutput;
tyinfo->dobj.catId.oid);
}
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
-
- /* Expecting a single result only */
- ntups = PQntuples(res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, query->data);
- exit_nicely(1);
- }
+ res = ExecuteSqlQueryForSingleRow(fout, query->data);
typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
PQExpBuffer labelq = createPQExpBuffer();
PQExpBuffer query = createPQExpBuffer();
PGresult *res;
- int ntups;
int i;
char *typnotnull;
char *typdefn;
tyinfo->dobj.catId.oid);
}
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
-
- /* Expecting a single result only */
- ntups = PQntuples(res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, query->data);
- exit_nicely(1);
- }
+ res = ExecuteSqlQueryForSingleRow(fout, query->data);
typnotnull = PQgetvalue(res, 0, PQfnumber(res, "typnotnull"));
typdefn = PQgetvalue(res, 0, PQfnumber(res, "typdefn"));
char *funcsig; /* identity signature */
char *funcfullsig; /* full signature */
char *funcsig_tag;
- int ntups;
char *proretset;
char *prosrc;
char *probin;
finfo->dobj.catId.oid);
}
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
-
- /* Expecting a single result only */
- ntups = PQntuples(res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, query->data);
- exit_nicely(1);
- }
+ res = ExecuteSqlQueryForSingleRow(fout, query->data);
proretset = PQgetvalue(res, 0, PQfnumber(res, "proretset"));
prosrc = PQgetvalue(res, 0, PQfnumber(res, "prosrc"));
PQExpBuffer details;
const char *name;
PGresult *res;
- int ntups;
int i_oprkind;
int i_oprcode;
int i_oprleft;
oprinfo->dobj.catId.oid);
}
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
-
- /* Expecting a single result only */
- ntups = PQntuples(res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, query->data);
- exit_nicely(1);
- }
+ res = ExecuteSqlQueryForSingleRow(fout, query->data);
i_oprkind = PQfnumber(res, "oprkind");
i_oprcode = PQfnumber(res, "oprcode");
char *result;
char query[128];
PGresult *res;
- int ntups;
snprintf(query, sizeof(query),
"SELECT '%u'::pg_catalog.regproc", funcOid);
- res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
-
- ntups = PQntuples(res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, query);
- exit_nicely(1);
- }
+ res = ExecuteSqlQueryForSingleRow(fout, query);
result = pg_strdup(PQgetvalue(res, 0, 0));
opcinfo->dobj.catId.oid);
}
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
-
- /* Expecting a single result only */
- ntups = PQntuples(res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, query->data);
- exit_nicely(1);
- }
+ res = ExecuteSqlQueryForSingleRow(fout, query->data);
i_opcintype = PQfnumber(res, "opcintype");
i_opckeytype = PQfnumber(res, "opckeytype");
"WHERE oid = '%u'::pg_catalog.oid",
opfinfo->dobj.catId.oid);
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
-
- /* Expecting a single result only */
- ntups = PQntuples(res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, query->data);
- exit_nicely(1);
- }
+ res = ExecuteSqlQueryForSingleRow(fout, query->data);
i_amname = PQfnumber(res, "amname");
PQExpBuffer delq;
PQExpBuffer labelq;
PGresult *res;
- int ntups;
int i_collcollate;
int i_collctype;
const char *collcollate;
"WHERE c.oid = '%u'::pg_catalog.oid",
collinfo->dobj.catId.oid);
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
-
- /* Expecting a single result only */
- ntups = PQntuples(res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, query->data);
- exit_nicely(1);
- }
+ res = ExecuteSqlQueryForSingleRow(fout, query->data);
i_collcollate = PQfnumber(res, "collcollate");
i_collctype = PQfnumber(res, "collctype");
PQExpBuffer delq;
PQExpBuffer labelq;
PGresult *res;
- int ntups;
int i_conforencoding;
int i_contoencoding;
int i_conproc;
"WHERE c.oid = '%u'::pg_catalog.oid",
convinfo->dobj.catId.oid);
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
-
- /* Expecting a single result only */
- ntups = PQntuples(res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, query->data);
- exit_nicely(1);
- }
+ res = ExecuteSqlQueryForSingleRow(fout, query->data);
i_conforencoding = PQfnumber(res, "conforencoding");
i_contoencoding = PQfnumber(res, "contoencoding");
char *aggsig;
char *aggsig_tag;
PGresult *res;
- int ntups;
int i_aggtransfn;
int i_aggfinalfn;
int i_aggsortop;
agginfo->aggfn.dobj.catId.oid);
}
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
-
- /* Expecting a single result only */
- ntups = PQntuples(res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, query->data);
- exit_nicely(1);
- }
+ res = ExecuteSqlQueryForSingleRow(fout, query->data);
i_aggtransfn = PQfnumber(res, "aggtransfn");
i_aggfinalfn = PQfnumber(res, "aggfinalfn");
PQExpBuffer labelq;
PQExpBuffer query;
PGresult *res;
- int ntups;
char *nspname;
char *tmplname;
"FROM pg_ts_template p, pg_namespace n "
"WHERE p.oid = '%u' AND n.oid = tmplnamespace",
dictinfo->dicttemplate);
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
- ntups = PQntuples(res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, query->data);
- exit_nicely(1);
- }
+ res = ExecuteSqlQueryForSingleRow(fout, query->data);
nspname = PQgetvalue(res, 0, 0);
tmplname = PQgetvalue(res, 0, 1);
"FROM pg_ts_parser p, pg_namespace n "
"WHERE p.oid = '%u' AND n.oid = prsnamespace",
cfginfo->cfgparser);
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
- ntups = PQntuples(res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, query->data);
- exit_nicely(1);
- }
+ res = ExecuteSqlQueryForSingleRow(fout, query->data);
nspname = PQgetvalue(res, 0, 0);
prsname = PQgetvalue(res, 0, 1);
PQExpBuffer labelq;
PQExpBuffer query;
PGresult *res;
- int ntups;
char *qsrvname;
char *fdwname;
"FROM pg_foreign_data_wrapper w "
"WHERE w.oid = '%u'",
srvinfo->srvfdw);
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
- ntups = PQntuples(res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, query->data);
- exit_nicely(1);
- }
+ res = ExecuteSqlQueryForSingleRow(fout, query->data);
fdwname = PQgetvalue(res, 0, 0);
appendPQExpBuffer(q, "CREATE SERVER %s", qsrvname);
char *result;
PQExpBuffer query;
PGresult *res;
- int ntups;
if (oid == 0)
{
oid);
}
- res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
-
- /* Expecting a single result only */
- ntups = PQntuples(res);
- if (ntups != 1)
- {
- write_msg(NULL, ngettext("query returned %d row instead of one: %s\n",
- "query returned %d rows instead of one: %s\n",
- ntups),
- ntups, query->data);
- exit_nicely(1);
- }
+ res = ExecuteSqlQueryForSingleRow(fout, query->data);
if (fout->remoteVersion >= 70100)
{
appendPQExpBuffer(q, ")");
return q->data;
}
+
+/*
+ * Execute an SQL query and verify that we got exactly one row back.
+ */
+static PGresult *
+ExecuteSqlQueryForSingleRow(Archive *fout, char *query)
+{
+ PGresult *res;
+ int ntups;
+
+ res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
+
+ /* Expecting a single result only */
+ ntups = PQntuples(res);
+ if (ntups != 1)
+ exit_horribly(NULL,
+ ngettext("query returned %d row instead of one: %s\n",
+ "query returned %d rows instead of one: %s\n",
+ ntups),
+ ntups, query);
+
+ return res;
+}