@@ -3176,33 +3176,89 @@ get_relation_column_alias_ids(Var *node, RelOptInfo *foreignrel,
31763176}
31773177
31783178/*
3179- * Deparse COPY FROM
3179+ * Deparse COPY FROM into given buf.
31803180 */
31813181void
31823182deparseCopyFromSql (StringInfo buf , Relation rel , CopyState cstate ,
31833183 const char * dest_relname )
31843184{
3185+ ListCell * cur ;
3186+
31853187 appendStringInfoString (buf , "COPY " );
31863188 if (dest_relname == NULL )
31873189 deparseRelation (buf , rel );
31883190 else
31893191 appendStringInfoString (buf , dest_relname );
3190- appendStringInfoString (buf , " FROM STDIN WITH (" );
31913192
3192- /* TODO: deparse column names */
31933193 if (cstate -> binary )
31943194 {
3195- ereport (ERROR ,
3196- (errcode (ERRCODE_WRONG_OBJECT_TYPE ),
3197- errmsg ("cannot copy to postgres_fdw table \"%s\" in binary format " ,
3198- RelationGetRelationName (rel ))));
3195+ ereport (ERROR ,
3196+ (errcode (ERRCODE_WRONG_OBJECT_TYPE ),
3197+ errmsg ("cannot copy to postgres_fdw table \"%s\" in binary format " ,
3198+ RelationGetRelationName (rel ))));
3199+ }
3200+
3201+ /* deparse column names */
3202+ if (cstate -> attnumlist != NIL )
3203+ {
3204+ bool first = true;
3205+
3206+ appendStringInfoString (buf , " (" );
3207+ foreach (cur , cstate -> attnumlist )
3208+ {
3209+ int attnum = lfirst_int (cur );
3210+ char * attname ;
3211+
3212+ if (!first )
3213+ appendStringInfoString (buf , ", " );
3214+ first = false;
3215+
3216+ attname = get_relid_attribute_name (rel -> rd_id , attnum );
3217+ appendStringInfoString (buf , quote_identifier (attname ));
3218+ }
3219+ appendStringInfoString (buf , " )" );
31993220 }
3221+
3222+ appendStringInfoString (buf , " FROM STDIN WITH (" );
32003223 if (cstate -> csv_mode )
32013224 {
32023225 appendStringInfoString (buf , " FORMAT csv " );
32033226 appendStringInfo (buf , ", QUOTE '%c'" , * (cstate -> quote ));
32043227 appendStringInfo (buf , ", ESCAPE '%c'" , * (cstate -> escape ));
3205- /* TODO: force quote, force not null, force null */
3228+ if (cstate -> force_notnull != NIL )
3229+ {
3230+ bool first = true;
3231+
3232+ appendStringInfoString (buf , " FORCE NOT NULL (" );
3233+ foreach (cur , cstate -> force_notnull )
3234+ {
3235+ char * attname = strVal (lfirst (cur ));
3236+
3237+ if (!first )
3238+ appendStringInfoString (buf , ", " );
3239+ first = false;
3240+
3241+ appendStringInfoString (buf , quote_identifier (attname ));
3242+ }
3243+ appendStringInfoString (buf , " )" );
3244+ }
3245+ if (cstate -> force_null != NIL )
3246+ {
3247+ bool first = true;
3248+
3249+ appendStringInfoString (buf , " FORCE NULL (" );
3250+ foreach (cur , cstate -> force_null )
3251+ {
3252+ char * attname = strVal (lfirst (cur ));
3253+
3254+ if (!first )
3255+ appendStringInfoString (buf , ", " );
3256+ first = false;
3257+
3258+ appendStringInfoString (buf , quote_identifier (attname ));
3259+ }
3260+ appendStringInfoString (buf , " )" );
3261+ }
32063262 }
32073263 else
32083264 {
0 commit comments