Index: src/bin/pg_dump/pg_dump.c =================================================================== RCS file: /var/cvsup/pgsql/src/bin/pg_dump/pg_dump.c,v retrieving revision 1.237 diff -c -r1.237 pg_dump.c *** src/bin/pg_dump/pg_dump.c 11 Jan 2002 23:21:55 -0000 1.237 --- src/bin/pg_dump/pg_dump.c 16 Jan 2002 01:05:21 -0000 *************** *** 501,506 **** --- 501,556 ---- return 1; } + /* + * returns a newly alloc'd string consisting of + * ( column1, column2, ..., columnN ) + * with the column names quoted if force_quotes true + */ + + static char* + get_column_list(const char* tablename, bool force_quotes); + + static char* + get_column_list(const char* tablename, bool force_quotes) + { + PGresult *res; + PQExpBuffer q = createPQExpBuffer(); + char* attlist = NULL; + int attnum = 0; + const char* col; + + appendPQExpBuffer(q, + "SELECT * FROM %s LIMIT 1",fmtId(tablename,force_quotes)); + + res = PQexec(g_conn, q->data); + if (!res || + PQresultStatus(res) != PGRES_TUPLES_OK) + { + write_msg(NULL, "Query to obtain column names for %s failed\n", + tablename); + write_msg(NULL, "Error message from server: %s", + PQerrorMessage(g_conn)); + exit_nicely(); + } + + attlist = malloc((NAMEDATALEN + 2) * PQnfields(res) + 4); + attlist[0] = '\0'; + + strncat(attlist,"(",1); + for(;attnum 0 ) + strncat(attlist,",",1); + col = fmtId(PQfname(res, attnum), force_quotes); + strncat(attlist,col,strlen(col)); + }; + strncat(attlist,") ",2); + + PQclear(res); + destroyPQExpBuffer(q); + return attlist; + } + /* * Convert a string value to an SQL string literal, * with appropriate escaping of special characters. *************** *** 590,601 **** if (!dumpData) { /* Dump/restore using COPY */ dumpFn = dumpClasses_nodumpData; ! sprintf(copyBuf, "COPY %s %sFROM stdin;\n", fmtId(tblinfo[i].relname, force_quotes), (oids && tblinfo[i].hasoids) ? "WITH OIDS " : ""); copyStmt = copyBuf; } else { --- 640,655 ---- if (!dumpData) { + char* attlist = get_column_list(tblinfo[i].relname, force_quotes); + //char* attlist = ""; /* Dump/restore using COPY */ dumpFn = dumpClasses_nodumpData; ! sprintf(copyBuf, "COPY %s %s%sFROM stdin;\n", fmtId(tblinfo[i].relname, force_quotes), + attlist, (oids && tblinfo[i].hasoids) ? "WITH OIDS " : ""); copyStmt = copyBuf; + free(attlist); } else {