static void deparseCreateStmt()

in ext/pg_query/pg_query_deparse.c [4636:4735]


static void deparseCreateStmt(StringInfo str, CreateStmt *create_stmt, bool is_foreign_table)
{
	ListCell *lc;

	appendStringInfoString(str, "CREATE ");

	if (is_foreign_table)
		appendStringInfoString(str, "FOREIGN ");

	deparseOptTemp(str, create_stmt->relation->relpersistence);

	appendStringInfoString(str, "TABLE ");

	if (create_stmt->if_not_exists)
		appendStringInfoString(str, "IF NOT EXISTS ");

	deparseRangeVar(str, create_stmt->relation, DEPARSE_NODE_CONTEXT_NONE);
	appendStringInfoChar(str, ' ');

	if (create_stmt->ofTypename != NULL)
	{
		appendStringInfoString(str, "OF ");
		deparseTypeName(str, create_stmt->ofTypename);
		appendStringInfoChar(str, ' ');
	}

	if (create_stmt->partbound != NULL)
	{
		Assert(list_length(create_stmt->inhRelations) == 1);
		appendStringInfoString(str, "PARTITION OF ");
		deparseRangeVar(str, castNode(RangeVar, linitial(create_stmt->inhRelations)), DEPARSE_NODE_CONTEXT_NONE);
		appendStringInfoChar(str, ' ');
	}

	if (list_length(create_stmt->tableElts) > 0)
	{
		// In raw parse output tableElts contains both columns and constraints
		// (and the constraints field is NIL)
		appendStringInfoChar(str, '(');
		foreach(lc, create_stmt->tableElts)
		{
			deparseTableElement(str, lfirst(lc));
			if (lnext(create_stmt->tableElts, lc))
				appendStringInfoString(str, ", ");
		}
		appendStringInfoString(str, ") ");
	}
	else if (create_stmt->partbound == NULL && create_stmt->ofTypename == NULL)
	{
		appendStringInfoString(str, "() ");
	}

	if (create_stmt->partbound != NULL)
	{
		deparsePartitionBoundSpec(str, create_stmt->partbound);
		appendStringInfoChar(str, ' ');
	}
	else
	{
		deparseOptInherit(str, create_stmt->inhRelations);
	}

	if (create_stmt->partspec != NULL)
	{
		deparsePartitionSpec(str, create_stmt->partspec);
		appendStringInfoChar(str, ' ');
	}

	if (create_stmt->accessMethod != NULL)
	{
		appendStringInfoString(str, "USING ");
		appendStringInfoString(str, quote_identifier(create_stmt->accessMethod));
	}

	deparseOptWith(str, create_stmt->options);

	switch (create_stmt->oncommit)
	{
		case ONCOMMIT_NOOP:
			// No ON COMMIT clause
			break;
		case ONCOMMIT_PRESERVE_ROWS:
			appendStringInfoString(str, "ON COMMIT PRESERVE ROWS ");
			break;
		case ONCOMMIT_DELETE_ROWS:
			appendStringInfoString(str, "ON COMMIT DELETE ROWS ");
			break;
		case ONCOMMIT_DROP:
			appendStringInfoString(str, "ON COMMIT DROP ");
			break;
	}

	if (create_stmt->tablespacename != NULL)
	{
		appendStringInfoString(str, "TABLESPACE ");
		appendStringInfoString(str, quote_identifier(create_stmt->tablespacename));
	}

	removeTrailingSpace(str);
}