Inline Comments for pg_dump
I've very roughly (first time I've tried anything but hello world c) hacked up inline comments.
pg_dump -I
Exports the comments generated through COMMENT ON in an appropriate manner (line above) the item with a -- in front. More or less a self documenting dump, or atleast an attempt at it.
However, due to my poor programming in this language, I'm not sure of teh best way to handle the issues following:
- Column comments mis-format the next row (Needs a \t or something)
- Database comments non-existent, wasn't sure how or where to pull them out.
- I've only tested TABLE and COLUMN comments. Didn't have a database handy with the rest, and had a limited amount of time to fiddle.
Take a look and see if it's worth anything or if it needs to be fixed up.
--
Rod Taylor
There are always four sides to every story: your side, their side, the truth, and what really happened.
Attachments:
pg_dump.patchapplication/octet-stream; name=pg_dump.patchDownload
--- pg_dump.c.old Thu Dec 21 10:29:39 2000
+++ pg_dump.c Thu Dec 21 12:24:16 2000
@@ -96,6 +96,9 @@
* table with the currently implementation, and (b) it's not clear how to restore
* a partial BLOB backup (given the current OID-based BLOB implementation).
*
+ * Modifications - 21-Dec-2000 - rod.taylor@inquent.com
+ * Added the -I option to insert user comments into the dumped SQL.
+ *
*-------------------------------------------------------------------------
*/
@@ -131,6 +134,7 @@
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
+static char *dumpInlineComment(const char *target, const char *oid);
static void dumpComment(Archive *outfile, const char *target, const char *oid);
static void dumpSequence(Archive *fout, TableInfo tbinfo);
static void dumpACL(Archive *fout, TableInfo tbinfo);
@@ -163,6 +167,7 @@
PGconn *g_conn; /* the database connection */
bool force_quotes; /* User wants to suppress double-quotes */
+bool inline_comments; /* Users wants their comments inline with SQL */
bool dumpData; /* dump data using proper insert strings */
bool attrNames; /* put attr names into insert strings */
bool schemaOnly;
@@ -202,6 +207,7 @@
" -F, --format {c|f|p} output file format (custom, files, plain text)\n"
" -h, --host <hostname> server host name\n"
" -i, --ignore-version proceed when database version != pg_dump version\n"
+ " -I, --inline-comments insert comments inline as well as standard SQL\n"
" -n, --no-quotes suppress most quotes around identifiers\n"
" -N, --quotes enable most quotes around identifiers\n"
" -o, --oids dump object ids (oids)\n"
@@ -228,6 +234,7 @@
" -F {c|f|p} output file format (custom, files, plain text)\n"
" -h <hostname> server host name\n"
" -i proceed when database version != pg_dump version\n"
+ " -I insert comments inline as well as standard SQL\n"
" -n suppress most quotes around identifiers\n"
" -N enable most quotes around identifiers\n"
" -o dump object ids (oids)\n"
@@ -665,6 +672,7 @@
{"attribute-inserts", no_argument, NULL, 'D'},
{"host", required_argument, NULL, 'h'},
{"ignore-version", no_argument, NULL, 'i'},
+ {"inline-comments", no_argument, NULL, 'I'},
{"no-reconnect", no_argument, NULL, 'R'},
{"no-quotes", no_argument, NULL, 'n'},
{"quotes", no_argument, NULL, 'N'},
@@ -707,9 +715,9 @@
}
#ifdef HAVE_GETOPT_LONG
- while ((c = getopt_long(argc, argv, "abcCdDf:F:h:inNoOp:sS:t:uvxzZ:V?", long_options, &optindex)) != -1)
+ while ((c = getopt_long(argc, argv, "abcCdDf:F:h:iInNoOp:sS:t:uvxzZ:V?", long_options, &optindex)) != -1)
#else
- while ((c = getopt(argc, argv, "abcCdDf:F:h:inNoOp:sS:t:uvxzZ:V?-")) != -1)
+ while ((c = getopt(argc, argv, "abcCdDf:F:h:iInNoOp:sS:t:uvxzZ:V?-")) != -1)
#endif
{
@@ -759,6 +767,10 @@
ignore_version = true;
break;
+ case 'I': /* insert comments inline with the SQL for item creation */
+ inline_comments = true;
+ break;
+
case 'n': /* Do not force double-quotes on
* identifiers */
force_quotes = false;
@@ -2711,6 +2723,57 @@
}
/*------------------------------------------------------------------
+ * dumpInlineComments --
+ *
+ * Routine used to setup the -- type comments inline with the SQL
+ * for increased human readability.
+ *-----------------------------------------------------------------
+*/
+
+static char *
+dumpInlineComment(const char *target, const char *oid)
+{
+
+ PGresult *res;
+ PQExpBuffer query;
+ int i_description;
+
+ /*** Build query to find comment ***/
+
+ query = createPQExpBuffer();
+ appendPQExpBuffer(query, "SELECT description FROM pg_description WHERE objoid = ");
+ appendPQExpBuffer(query, oid);
+
+ /*** Execute query ***/
+
+ res = PQexec(g_conn, query->data);
+ if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
+ {
+ fprintf(stderr, "DumpInlineComment: SELECT failed: '%s'.\n",
+ PQerrorMessage(g_conn));
+ exit_nicely(g_conn);
+ }
+
+ /*** If a comment exists, build COMMENT ON statement ***/
+
+ if (PQntuples(res) != 0)
+ {
+ i_description = PQfnumber(res, "description");
+ resetPQExpBuffer(query);
+ appendPQExpBuffer(query, "-- %s\n", PQgetvalue(res, 0, i_description));
+
+ PQclear(res);
+ return (query->data);
+ }
+
+ /*** Clear the statement buffer and return ***/
+
+ PQclear(res);
+ return ("");
+}
+
+
+/*------------------------------------------------------------------
* dumpComments --
*
* This routine is used to dump any comments associated with the
@@ -2862,6 +2925,13 @@
appendPQExpBuffer(delq, "DROP TYPE %s;\n", fmtId(tinfo[i].typname, force_quotes));
resetPQExpBuffer(q);
+
+ /* Dump Inline Comments */
+ if (inline_comments == TRUE)
+ {
+ appendPQExpBuffer(q, "%s", dumpInlineComment(q->data, tinfo[i].oid));
+ };
+
appendPQExpBuffer(q,
"CREATE TYPE %s "
"( internallength = %s, externallength = %s, input = %s, "
@@ -2976,6 +3046,12 @@
appendPQExpBuffer(delqry, "DROP PROCEDURAL LANGUAGE '%s';\n", lanname);
+ /* Dump Inline Comments */
+ if (inline_comments == TRUE)
+ {
+ appendPQExpBuffer(delqry, "%s", dumpInlineComment(delqry->data, finfo[fidx].oid));
+ };
+
appendPQExpBuffer(defqry, "CREATE %sPROCEDURAL LANGUAGE '%s' "
"HANDLER %s LANCOMPILER '%s';\n",
(PQgetvalue(res, i, i_lanpltrusted)[0] == 't') ? "TRUSTED " : "",
@@ -3233,6 +3309,13 @@
findTypeByOid(tinfo, numTypes, oprinfo[i].oprright, zeroAsOpaque) );
resetPQExpBuffer(q);
+
+ /* Dump Inline Comments */
+ if (inline_comments == TRUE)
+ {
+ appendPQExpBuffer(q, "%s", dumpInlineComment(q->data, oprinfo[i].oid));
+ };
+
appendPQExpBuffer(q,
"CREATE OPERATOR %s "
"(PROCEDURE = %s %s%s%s%s%s%s%s%s%s);\n",
@@ -3301,6 +3384,13 @@
appendPQExpBuffer(delq, "DROP AGGREGATE %s;\n", aggSig->data);
resetPQExpBuffer(q);
+
+ /* Dump Inline Comments */
+ if (inline_comments == TRUE)
+ {
+ appendPQExpBuffer(q, "%s", dumpInlineComment(q->data, agginfo[i].oid));
+ };
+
appendPQExpBuffer(q, "CREATE AGGREGATE %s ( %s );\n",
agginfo[i].aggname,
details->data);
@@ -3540,6 +3630,12 @@
resetPQExpBuffer(delq);
resetPQExpBuffer(q);
+ /* Dump Inline Comments */
+ if (inline_comments == TRUE)
+ {
+ appendPQExpBuffer(q, "%s", dumpInlineComment(q->data, tblinfo[i].oid));
+ };
+
/* Use the view definition if there is one */
if (tblinfo[i].viewdef != NULL)
{
@@ -3565,10 +3661,18 @@
/* Is this one of the table's own attrs ? */
if (tblinfo[i].inhAttrs[j] == 0)
{
+
/* Format properly if not first attr */
if (actual_atts > 0)
appendPQExpBuffer(q, ",\n\t");
+ /* Dump Inline Comments */
+ if (inline_comments == TRUE)
+ {
+ appendPQExpBuffer(q, "%s", dumpInlineComment(q->data, tblinfo[i].attoids[j]));
+ };
+
+
/* Attr name & type */
appendPQExpBuffer(q, "%s %s",
fmtId(tblinfo[i].attnames[j], force_quotes),
@@ -3801,6 +3905,13 @@
appendPQExpBuffer(delq, "DROP INDEX %s;\n", id1->data);
resetPQExpBuffer(q);
+
+ /* Dump Inline Comments */
+ if (inline_comments == TRUE)
+ {
+ appendPQExpBuffer(q, "%s", dumpInlineComment(q->data, tblinfo[tableInd].oid));
+ };
+
appendPQExpBuffer(q, "CREATE %s INDEX %s on %s using %s (",
(strcmp(indinfo[i].indisunique, "t") == 0) ? "UNIQUE" : "",
id1->data,
We are already in beta, so I don't think I can apply this. I will keep
it and apply in our 7.2 development tree.
[ Charset ISO-8859-1 unsupported, converting... ]
I've very roughly (first time I've tried anything but hello world c) hacked up inline comments.
pg_dump -I
Exports the comments generated through COMMENT ON in an appropriate manner (line above) the item with a -- in front. More or less a self documenting dump, or atleast an attempt at it.
However, due to my poor programming in this language, I'm not sure of teh best way to handle the issues following:
- Column comments mis-format the next row (Needs a \t or something)
- Database comments non-existent, wasn't sure how or where to pull them out.
- I've only tested TABLE and COLUMN comments. Didn't have a database handy with the rest, and had a limited amount of time to fiddle.Take a look and see if it's worth anything or if it needs to be fixed up.
--
Rod TaylorThere are always four sides to every story: your side, their side, the truth, and what really happened.
[ Attachment, skipping... ]
[ Attachment, skipping... ]
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026
Believe it or not, I was just about to start working on comment support (by
practicing in phpPgAdmin, so I'm happy to look over the code to see if I can
address the issues raised, and maybe to do it for all database objects...?
Chris
Show quoted text
-----Original Message-----
From: pgsql-hackers-owner@postgresql.org
[mailto:pgsql-hackers-owner@postgresql.org]On Behalf Of Bruce Momjian
Sent: Friday, December 22, 2000 3:17 AM
To: Rod Taylor
Cc: pgsql-hackers@postgresql.org
Subject: Re: [HACKERS] Inline Comments for pg_dumpWe are already in beta, so I don't think I can apply this. I will keep
it and apply in our 7.2 development tree.[ Charset ISO-8859-1 unsupported, converting... ]
I've very roughly (first time I've tried anything but hello
world c) hacked up inline comments.
pg_dump -I
Exports the comments generated through COMMENT ON in an
appropriate manner (line above) the item with a -- in front.
More or less a self documenting dump, or atleast an attempt at it.However, due to my poor programming in this language, I'm not
sure of teh best way to handle the issues following:
- Column comments mis-format the next row (Needs a \t or something)
- Database comments non-existent, wasn't sure how or where topull them out.
- I've only tested TABLE and COLUMN comments. Didn't have a
database handy with the rest, and had a limited amount of time to fiddle.
Take a look and see if it's worth anything or if it needs to be
fixed up.
--
Rod TaylorThere are always four sides to every story: your side, their
side, the truth, and what really happened.
[ Attachment, skipping... ]
[ Attachment, skipping... ]
-- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 853-3000 + If your life is a hard drive, | 830 Blythe Avenue + Christ can be your backup. | Drexel Hill, Pennsylvania 19026
Can someone remind me what happened to this issue?
[ Charset ISO-8859-1 unsupported, converting... ]
I've very roughly (first time I've tried anything but hello world c) hacked up inline comments.
pg_dump -I
Exports the comments generated through COMMENT ON in an appropriate manner (line above) the item with a -- in front. More or less a self documenting dump, or atleast an attempt at it.
However, due to my poor programming in this language, I'm not sure of teh best way to handle the issues following:
- Column comments mis-format the next row (Needs a \t or something)
- Database comments non-existent, wasn't sure how or where to pull them out.
- I've only tested TABLE and COLUMN comments. Didn't have a database handy with the rest, and had a limited amount of time to fiddle.Take a look and see if it's worth anything or if it needs to be fixed up.
--
Rod TaylorThere are always four sides to every story: your side, their side, the truth, and what really happened.
[ Attachment, skipping... ]
[ Attachment, skipping... ]
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026
Do we handle COMMENT properly in pg_dump now already?
[ Charset ISO-8859-1 unsupported, converting... ]
I've very roughly (first time I've tried anything but hello world c) hacked up inline comments.
pg_dump -I
Exports the comments generated through COMMENT ON in an appropriate manner (line above) the item with a -- in front. More or less a self documenting dump, or atleast an attempt at it.
However, due to my poor programming in this language, I'm not sure of teh best way to handle the issues following:
- Column comments mis-format the next row (Needs a \t or something)
- Database comments non-existent, wasn't sure how or where to pull them out.
- I've only tested TABLE and COLUMN comments. Didn't have a database handy with the rest, and had a limited amount of time to fiddle.Take a look and see if it's worth anything or if it needs to be fixed up.
--
Rod TaylorThere are always four sides to every story: your side, their side, the truth, and what really happened.
[ Attachment, skipping... ]
[ Attachment, skipping... ]
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026