--- 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 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 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,