12
12
* by PostgreSQL
13
13
*
14
14
* IDENTIFICATION
15
- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.326 2003/04/04 20:42:12 momjian Exp $
15
+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.327 2003/04/25 02:28:22 tgl Exp $
16
16
*
17
17
*-------------------------------------------------------------------------
18
18
*/
@@ -838,9 +838,6 @@ dumpClasses_nodumpData(Archive *fout, char *oid, void *dctxv)
838
838
* possibility of retrieving data in the wrong column order. (The
839
839
* default column ordering of COPY will not be what we want in certain
840
840
* corner cases involving ADD COLUMN and inheritance.)
841
- *
842
- * NB: caller should have already determined that there are dumpable
843
- * columns, so that fmtCopyColumnList will return something.
844
841
*/
845
842
if (g_fout -> remoteVersion >= 70300 )
846
843
column_list = fmtCopyColumnList (tbinfo );
@@ -975,6 +972,7 @@ dumpClasses_dumpData(Archive *fout, char *oid, void *dctxv)
975
972
PQExpBuffer q = createPQExpBuffer ();
976
973
PGresult * res ;
977
974
int tuple ;
975
+ int nfields ;
978
976
int field ;
979
977
980
978
/*
@@ -1024,14 +1022,21 @@ dumpClasses_dumpData(Archive *fout, char *oid, void *dctxv)
1024
1022
exit_nicely ();
1025
1023
}
1026
1024
1025
+ nfields = PQnfields (res );
1027
1026
for (tuple = 0 ; tuple < PQntuples (res ); tuple ++ )
1028
1027
{
1029
1028
archprintf (fout , "INSERT INTO %s " , fmtId (classname ));
1029
+ if (nfields == 0 )
1030
+ {
1031
+ /* corner case for zero-column table */
1032
+ archprintf (fout , "DEFAULT VALUES;\n" );
1033
+ continue ;
1034
+ }
1030
1035
if (attrNames == true)
1031
1036
{
1032
1037
resetPQExpBuffer (q );
1033
1038
appendPQExpBuffer (q , "(" );
1034
- for (field = 0 ; field < PQnfields ( res ) ; field ++ )
1039
+ for (field = 0 ; field < nfields ; field ++ )
1035
1040
{
1036
1041
if (field > 0 )
1037
1042
appendPQExpBuffer (q , ", " );
@@ -1041,7 +1046,7 @@ dumpClasses_dumpData(Archive *fout, char *oid, void *dctxv)
1041
1046
archprintf (fout , "%s" , q -> data );
1042
1047
}
1043
1048
archprintf (fout , "VALUES (" );
1044
- for (field = 0 ; field < PQnfields ( res ) ; field ++ )
1049
+ for (field = 0 ; field < nfields ; field ++ )
1045
1050
{
1046
1051
if (field > 0 )
1047
1052
archprintf (fout , ", " );
@@ -1154,45 +1159,38 @@ dumpClasses(const TableInfo *tblinfo, const int numTables, Archive *fout,
1154
1159
1155
1160
if (tblinfo [i ].dump )
1156
1161
{
1157
- const char * column_list ;
1158
-
1159
1162
if (g_verbose )
1160
1163
write_msg (NULL , "preparing to dump the contents of table %s\n" ,
1161
1164
classname );
1162
1165
1163
- /* Get column list first to check for zero-column table */
1164
- column_list = fmtCopyColumnList (& (tblinfo [i ]));
1165
- if (column_list )
1166
- {
1167
- dumpCtx = (DumpContext * ) malloc (sizeof (DumpContext ));
1168
- dumpCtx -> tblinfo = (TableInfo * ) tblinfo ;
1169
- dumpCtx -> tblidx = i ;
1170
- dumpCtx -> oids = oids ;
1166
+ dumpCtx = (DumpContext * ) malloc (sizeof (DumpContext ));
1167
+ dumpCtx -> tblinfo = (TableInfo * ) tblinfo ;
1168
+ dumpCtx -> tblidx = i ;
1169
+ dumpCtx -> oids = oids ;
1171
1170
1172
- if (!dumpData )
1173
- {
1174
- /* Dump/restore using COPY */
1175
- dumpFn = dumpClasses_nodumpData ;
1176
- resetPQExpBuffer (copyBuf );
1177
- appendPQExpBuffer (copyBuf , "COPY %s %s %sFROM stdin;\n" ,
1178
- fmtId (tblinfo [i ].relname ),
1179
- column_list ,
1171
+ if (!dumpData )
1172
+ {
1173
+ /* Dump/restore using COPY */
1174
+ dumpFn = dumpClasses_nodumpData ;
1175
+ resetPQExpBuffer (copyBuf );
1176
+ appendPQExpBuffer (copyBuf , "COPY %s %s %sFROM stdin;\n" ,
1177
+ fmtId (tblinfo [i ].relname ),
1178
+ fmtCopyColumnList ( & ( tblinfo [ i ])) ,
1180
1179
(oids && tblinfo [i ].hasoids ) ? "WITH OIDS " : "" );
1181
- copyStmt = copyBuf -> data ;
1182
- }
1183
- else
1184
- {
1185
- /* Restore using INSERT */
1186
- dumpFn = dumpClasses_dumpData ;
1187
- copyStmt = NULL ;
1188
- }
1189
-
1190
- ArchiveEntry (fout , tblinfo [i ].oid , tblinfo [i ].relname ,
1191
- tblinfo [i ].relnamespace -> nspname ,
1192
- tblinfo [i ].usename ,
1193
- "TABLE DATA" , NULL , "" , "" , copyStmt ,
1194
- dumpFn , dumpCtx );
1180
+ copyStmt = copyBuf -> data ;
1195
1181
}
1182
+ else
1183
+ {
1184
+ /* Restore using INSERT */
1185
+ dumpFn = dumpClasses_dumpData ;
1186
+ copyStmt = NULL ;
1187
+ }
1188
+
1189
+ ArchiveEntry (fout , tblinfo [i ].oid , tblinfo [i ].relname ,
1190
+ tblinfo [i ].relnamespace -> nspname ,
1191
+ tblinfo [i ].usename ,
1192
+ "TABLE DATA" , NULL , "" , "" , copyStmt ,
1193
+ dumpFn , dumpCtx );
1196
1194
}
1197
1195
}
1198
1196
@@ -6980,7 +6978,7 @@ fmtQualifiedId(const char *schema, const char *id)
6980
6978
* Return a column list clause for the given relation.
6981
6979
*
6982
6980
* Special case: if there are no undropped columns in the relation, return
6983
- * NULL , not an invalid "()" column list.
6981
+ * "" , not an invalid "()" column list.
6984
6982
*/
6985
6983
static const char *
6986
6984
fmtCopyColumnList (const TableInfo * ti )
@@ -7010,7 +7008,7 @@ fmtCopyColumnList(const TableInfo *ti)
7010
7008
}
7011
7009
7012
7010
if (!needComma )
7013
- return NULL ; /* no undropped columns */
7011
+ return "" ; /* no undropped columns */
7014
7012
7015
7013
appendPQExpBuffer (q , ")" );
7016
7014
return q -> data ;
0 commit comments