in ext/pg_query/pg_query_deparse.c [6615:6793]
static void deparseCopyStmt(StringInfo str, CopyStmt *copy_stmt)
{
ListCell *lc = NULL;
ListCell *lc2 = NULL;
appendStringInfoString(str, "COPY ");
if (copy_stmt->relation != NULL)
{
deparseRangeVar(str, copy_stmt->relation, DEPARSE_NODE_CONTEXT_NONE);
if (list_length(copy_stmt->attlist) > 0)
{
appendStringInfoChar(str, '(');
deparseColumnList(str, copy_stmt->attlist);
appendStringInfoChar(str, ')');
}
appendStringInfoChar(str, ' ');
}
if (copy_stmt->query != NULL)
{
appendStringInfoChar(str, '(');
deparsePreparableStmt(str, copy_stmt->query);
appendStringInfoString(str, ") ");
}
if (copy_stmt->is_from)
appendStringInfoString(str, "FROM ");
else
appendStringInfoString(str, "TO ");
if (copy_stmt->is_program)
appendStringInfoString(str, "PROGRAM ");
if (copy_stmt->filename != NULL)
{
deparseStringLiteral(str, copy_stmt->filename);
appendStringInfoChar(str, ' ');
}
else
{
if (copy_stmt->is_from)
appendStringInfoString(str, "STDIN ");
else
appendStringInfoString(str, "STDOUT ");
}
if (list_length(copy_stmt->options) > 0)
{
appendStringInfoString(str, "WITH (");
foreach(lc, copy_stmt->options)
{
DefElem *def_elem = castNode(DefElem, lfirst(lc));
if (strcmp(def_elem->defname, "format") == 0)
{
appendStringInfoString(str, "FORMAT ");
char *format = strVal(def_elem->arg);
if (strcmp(format, "binary") == 0)
appendStringInfoString(str, "BINARY");
else if (strcmp(format, "csv") == 0)
appendStringInfoString(str, "CSV");
else
Assert(false);
}
else if (strcmp(def_elem->defname, "freeze") == 0 && (def_elem->arg == NULL || intVal(def_elem->arg) == 1))
{
appendStringInfoString(str, "FREEZE");
if (def_elem->arg != NULL && intVal(def_elem->arg) == 1)
appendStringInfoString(str, " 1");
}
else if (strcmp(def_elem->defname, "delimiter") == 0)
{
appendStringInfoString(str, "DELIMITER ");
deparseStringLiteral(str, strVal(def_elem->arg));
}
else if (strcmp(def_elem->defname, "null") == 0)
{
appendStringInfoString(str, "NULL ");
deparseStringLiteral(str, strVal(def_elem->arg));
}
else if (strcmp(def_elem->defname, "header") == 0 && (def_elem->arg == NULL || intVal(def_elem->arg) == 1))
{
appendStringInfoString(str, "HEADER");
if (def_elem->arg != NULL && intVal(def_elem->arg) == 1)
appendStringInfoString(str, " 1");
}
else if (strcmp(def_elem->defname, "quote") == 0)
{
appendStringInfoString(str, "QUOTE ");
deparseStringLiteral(str, strVal(def_elem->arg));
}
else if (strcmp(def_elem->defname, "escape") == 0)
{
appendStringInfoString(str, "ESCAPE ");
deparseStringLiteral(str, strVal(def_elem->arg));
}
else if (strcmp(def_elem->defname, "force_quote") == 0)
{
appendStringInfoString(str, "FORCE_QUOTE ");
if (IsA(def_elem->arg, A_Star))
{
appendStringInfoChar(str, '*');
}
else if (IsA(def_elem->arg, List))
{
appendStringInfoChar(str, '(');
deparseColumnList(str, castNode(List, def_elem->arg));
appendStringInfoChar(str, ')');
}
else
{
Assert(false);
}
}
else if (strcmp(def_elem->defname, "force_not_null") == 0)
{
appendStringInfoString(str, "FORCE_NOT_NULL (");
deparseColumnList(str, castNode(List, def_elem->arg));
appendStringInfoChar(str, ')');
}
else if (strcmp(def_elem->defname, "force_null") == 0)
{
appendStringInfoString(str, "FORCE_NULL (");
deparseColumnList(str, castNode(List, def_elem->arg));
appendStringInfoChar(str, ')');
}
else if (strcmp(def_elem->defname, "encoding") == 0)
{
appendStringInfoString(str, "ENCODING ");
deparseStringLiteral(str, strVal(def_elem->arg));
}
else
{
appendStringInfoString(str, quote_identifier(def_elem->defname));
if (def_elem->arg != NULL)
appendStringInfoChar(str, ' ');
if (def_elem->arg == NULL)
{
// Nothing
}
else if (IsA(def_elem->arg, String))
{
deparseOptBooleanOrString(str, strVal(def_elem->arg));
}
else if (IsA(def_elem->arg, Integer) || IsA(def_elem->arg, Float))
{
deparseNumericOnly(str, (Value *) def_elem->arg);
}
else if (IsA(def_elem->arg, A_Star))
{
deparseAStar(str, castNode(A_Star, def_elem->arg));
}
else if (IsA(def_elem->arg, List))
{
List *l = castNode(List, def_elem->arg);
appendStringInfoChar(str, '(');
foreach(lc2, l)
{
deparseOptBooleanOrString(str, strVal(lfirst(lc2)));
if (lnext(l, lc2))
appendStringInfoString(str, ", ");
}
appendStringInfoChar(str, ')');
}
}
if (lnext(copy_stmt->options, lc))
appendStringInfoString(str, ", ");
}
appendStringInfoString(str, ") ");
}
deparseWhereClause(str, copy_stmt->whereClause);
removeTrailingSpace(str);
}