Commit 6f11e6df authored by Bruce Momjian's avatar Bruce Momjian

This patch allow pg_dump save name of primary key constraint (if primary

key exist).

awn@bcs.zp.ua
parent 579f8f09
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.181 2000/11/24 22:32:26 petere Exp $ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.182 2000/11/27 20:51:40 momjian Exp $
* *
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
* *
...@@ -1544,6 +1544,8 @@ clearTableInfo(TableInfo *tblinfo, int numTables) ...@@ -1544,6 +1544,8 @@ clearTableInfo(TableInfo *tblinfo, int numTables)
free(tblinfo[i].notnull); free(tblinfo[i].notnull);
if (tblinfo[i].primary_key) if (tblinfo[i].primary_key)
free(tblinfo[i].primary_key); free(tblinfo[i].primary_key);
if (tblinfo[i].primary_key_name)
free(tblinfo[i].primary_key_name);
} }
free(tblinfo); free(tblinfo);
} }
...@@ -2144,6 +2146,49 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs) ...@@ -2144,6 +2146,49 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
else else
tblinfo[i].primary_key = NULL; tblinfo[i].primary_key = NULL;
/* Get primary key name (if primary key exist) */
if (tblinfo[i].primary_key)
{
PGresult *res2;
int n;
resetPQExpBuffer(query);
appendPQExpBuffer(query,
"SELECT c.relname "
"FROM pg_index i, pg_class c "
"WHERE i.indrelid = %s"
"AND i.indisprimary "
"AND c.oid = i.indexrelid ",
tblinfo[i].oid);
res2 = PQexec(g_conn, query->data);
if (!res2 || PQresultStatus(res2) != PGRES_TUPLES_OK)
{
fprintf(stderr, "getTables(): SELECT (for PRIMARY KEY NAME) failed. Explanation from backend: %s",
PQerrorMessage(g_conn));
exit_nicely(g_conn);
}
n = PQntuples(res2);
if (n != 1)
{
fprintf(stderr,
"getTables(): SELECT (for PRIMARY KEY NAME) failed. This is impossible but object with OID == %s have %d primary keys.\n",
tblinfo[i].oid,
n);
exit_nicely(g_conn);
}
tblinfo[i].primary_key_name =
strdup(fmtId(PQgetvalue(res2, 0, 0), force_quotes));
if (tblinfo[i].primary_key_name == NULL)
{
perror("strdup");
exit(1);
}
}
else
tblinfo[i].primary_key_name = NULL;
/* Get Triggers */ /* Get Triggers */
if (tblinfo[i].ntrig > 0) if (tblinfo[i].ntrig > 0)
{ {
...@@ -3558,7 +3603,10 @@ dumpTables(Archive *fout, TableInfo *tblinfo, int numTables, ...@@ -3558,7 +3603,10 @@ dumpTables(Archive *fout, TableInfo *tblinfo, int numTables,
{ {
if (actual_atts + tblinfo[i].ncheck > 0) if (actual_atts + tblinfo[i].ncheck > 0)
appendPQExpBuffer(q, ",\n\t"); appendPQExpBuffer(q, ",\n\t");
appendPQExpBuffer(q, "PRIMARY KEY (%s)", tblinfo[i].primary_key); appendPQExpBuffer(q,
"CONSTRAINT %s PRIMARY KEY (%s)",
tblinfo[i].primary_key_name,
tblinfo[i].primary_key);
} }
appendPQExpBuffer(q, "\n)"); appendPQExpBuffer(q, "\n)");
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: pg_dump.h,v 1.54 2000/11/14 18:37:46 tgl Exp $ * $Id: pg_dump.h,v 1.55 2000/11/27 20:51:40 momjian Exp $
* *
* Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2 * Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2
* *
...@@ -117,6 +117,7 @@ typedef struct _tableInfo ...@@ -117,6 +117,7 @@ typedef struct _tableInfo
int ntrig; /* # of triggers */ int ntrig; /* # of triggers */
TrigInfo *triggers; /* Triggers on the table */ TrigInfo *triggers; /* Triggers on the table */
char *primary_key; /* PRIMARY KEY of the table, if any */ char *primary_key; /* PRIMARY KEY of the table, if any */
char *primary_key_name; /* PRIMARY KEY name, if any */
} TableInfo; } TableInfo;
typedef struct _inhInfo typedef struct _inhInfo
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment