static void deparseAlterTableCmd()

in ext/pg_query/pg_query_deparse.c [5593:5933]


static void deparseAlterTableCmd(StringInfo str, AlterTableCmd *alter_table_cmd, DeparseNodeContext context)
{
	ListCell *lc = NULL;
	const char *options = NULL;
	bool trailing_missing_ok = false;

	switch (alter_table_cmd->subtype)
	{
		case AT_AddColumn: /* add column */
			if (context == DEPARSE_NODE_CONTEXT_ALTER_TYPE)
				appendStringInfoString(str, "ADD ATTRIBUTE ");
			else
				appendStringInfoString(str, "ADD COLUMN ");
			break;
		case AT_AddColumnRecurse: /* internal to commands/tablecmds.c */
			Assert(false);
			break;
		case AT_AddColumnToView: /* implicitly via CREATE OR REPLACE VIEW */
			// Not present in raw parser output
			Assert(false);
			break;
		case AT_ColumnDefault: /* alter column default */
			appendStringInfoString(str, "ALTER COLUMN ");
			if (alter_table_cmd->def != NULL)
				options = "SET DEFAULT";
			else
				options = "DROP DEFAULT";
			break;
		case AT_CookedColumnDefault: /* add a pre-cooked column default */
			// Not present in raw parser output
			Assert(false);
			break;
		case AT_DropNotNull: /* alter column drop not null */
			appendStringInfoString(str, "ALTER COLUMN ");
			options = "DROP NOT NULL";
			break;
		case AT_SetNotNull: /* alter column set not null */
			appendStringInfoString(str, "ALTER COLUMN ");
			options = "SET NOT NULL";
			break;
		case AT_DropExpression: /* alter column drop expression */
			appendStringInfoString(str, "ALTER COLUMN ");
			options = "DROP EXPRESSION";
			trailing_missing_ok = true;
			break;
		case AT_CheckNotNull: /* check column is already marked not null */
			// Not present in raw parser output
			Assert(false);
			break;
		case AT_SetStatistics: /* alter column set statistics */
			appendStringInfoString(str, "ALTER COLUMN ");
			options = "SET STATISTICS";
			break;
		case AT_SetOptions: /* alter column set ( options ) */
			appendStringInfoString(str, "ALTER COLUMN ");
			options = "SET";
			break;
		case AT_ResetOptions: /* alter column reset ( options ) */
			appendStringInfoString(str, "ALTER COLUMN ");
			options = "RESET";
			break;
		case AT_SetStorage: /* alter column set storage */
			appendStringInfoString(str, "ALTER COLUMN ");
			options = "SET STORAGE";
			break;
		case AT_DropColumn: /* drop column */
			if (context == DEPARSE_NODE_CONTEXT_ALTER_TYPE)
				appendStringInfoString(str, "DROP ATTRIBUTE ");
			else
				appendStringInfoString(str, "DROP ");
			break;
		case AT_DropColumnRecurse: /* internal to commands/tablecmds.c */
			Assert(false);
			break;
		case AT_AddIndex: /* add index */
			appendStringInfoString(str, "ADD INDEX ");
			break;
		case AT_ReAddIndex: /* internal to commands/tablecmds.c */
			Assert(false);
			break;
		case AT_AddConstraint: /* add constraint */
			appendStringInfoString(str, "ADD ");
			break;
		case AT_AddConstraintRecurse: /* internal to commands/tablecmds.c */
			Assert(false);
			break;
		case AT_ReAddConstraint: /* internal to commands/tablecmds.c */
			Assert(false);
			break;
		case AT_ReAddDomainConstraint: /* internal to commands/tablecmds.c */
			Assert(false);
			break;
		case AT_AlterConstraint: /* alter constraint */
			appendStringInfoString(str, "ALTER "); // CONSTRAINT keyword gets added by the Constraint itself (when deparsing def)
			break;
		case AT_ValidateConstraint: /* validate constraint */
			appendStringInfoString(str, "VALIDATE CONSTRAINT ");
			break;
		case AT_ValidateConstraintRecurse: /* internal to commands/tablecmds.c */
			Assert(false);
			break;
		case AT_AddIndexConstraint: /* add constraint using existing index */
			// Not present in raw parser output
			Assert(false);
			break;
		case AT_DropConstraint: /* drop constraint */
			appendStringInfoString(str, "DROP CONSTRAINT ");
			break;
		case AT_DropConstraintRecurse: /* internal to commands/tablecmds.c */
			Assert(false);
			break;
		case AT_ReAddComment: /* internal to commands/tablecmds.c */
			Assert(false);
			break;
		case AT_AlterColumnType: /* alter column type */
			if (context == DEPARSE_NODE_CONTEXT_ALTER_TYPE)
				appendStringInfoString(str, "ALTER ATTRIBUTE ");
			else
				appendStringInfoString(str, "ALTER COLUMN ");
			options = "TYPE";
			break;
		case AT_AlterColumnGenericOptions: /* alter column OPTIONS (...) */
			appendStringInfoString(str, "ALTER COLUMN ");
			// Handled via special case in def handling
			break;
		case AT_ChangeOwner: /* change owner */
			appendStringInfoString(str, "OWNER TO ");
			deparseRoleSpec(str, alter_table_cmd->newowner);
			break;
		case AT_ClusterOn: /* CLUSTER ON */
			appendStringInfoString(str, "CLUSTER ON ");
			break;
		case AT_DropCluster: /* SET WITHOUT CLUSTER */
			appendStringInfoString(str, "SET WITHOUT CLUSTER ");
			break;
		case AT_SetLogged: /* SET LOGGED */
			appendStringInfoString(str, "SET LOGGED ");
			break;
		case AT_SetUnLogged: /* SET UNLOGGED */
			appendStringInfoString(str, "SET UNLOGGED ");
			break;
		case AT_DropOids: /* SET WITHOUT OIDS */
			appendStringInfoString(str, "SET WITHOUT OIDS ");
			break;
		case AT_SetTableSpace: /* SET TABLESPACE */
			appendStringInfoString(str, "SET TABLESPACE ");
			break;
		case AT_SetRelOptions: /* SET (...) -- AM specific parameters */
			appendStringInfoString(str, "SET ");
			break;
		case AT_ResetRelOptions: /* RESET (...) -- AM specific parameters */
			appendStringInfoString(str, "RESET ");
			break;
		case AT_ReplaceRelOptions: /* replace reloption list in its entirety */
			// Not present in raw parser output
			Assert(false);
			break;
		case AT_EnableTrig: /* ENABLE TRIGGER name */
			appendStringInfoString(str, "ENABLE TRIGGER ");
			break;
		case AT_EnableAlwaysTrig: /* ENABLE ALWAYS TRIGGER name */
			appendStringInfoString(str, "ENABLE ALWAYS TRIGGER ");
			break;
		case AT_EnableReplicaTrig: /* ENABLE REPLICA TRIGGER name */
			appendStringInfoString(str, "ENABLE REPLICA TRIGGER ");
			break;
		case AT_DisableTrig: /* DISABLE TRIGGER name */
			appendStringInfoString(str, "DISABLE TRIGGER ");
			break;
		case AT_EnableTrigAll: /* ENABLE TRIGGER ALL */
			appendStringInfoString(str, "ENABLE TRIGGER ");
			break;
		case AT_DisableTrigAll: /* DISABLE TRIGGER ALL */
			appendStringInfoString(str, "DISABLE TRIGGER ALL ");
			break;
		case AT_EnableTrigUser: /* ENABLE TRIGGER USER */
			appendStringInfoString(str, "ENABLE TRIGGER USER ");
			break;
		case AT_DisableTrigUser: /* DISABLE TRIGGER USER */
			appendStringInfoString(str, "DISABLE TRIGGER USER ");
			break;
		case AT_EnableRule: /* ENABLE RULE name */
			appendStringInfoString(str, "ENABLE RULE ");
			break;
		case AT_EnableAlwaysRule: /* ENABLE ALWAYS RULE name */
			appendStringInfoString(str, "ENABLE ALWAYS RULE ");
			break;
		case AT_EnableReplicaRule: /* ENABLE REPLICA RULE name */
			appendStringInfoString(str, "ENABLE REPLICA RULE ");
			break;
		case AT_DisableRule: /* DISABLE RULE name */
			appendStringInfoString(str, "DISABLE RULE ");
			break;
		case AT_AddInherit: /* INHERIT parent */
			appendStringInfoString(str, "INHERIT ");
			break;
		case AT_DropInherit: /* NO INHERIT parent */
			appendStringInfoString(str, "NO INHERIT ");
			break;
		case AT_AddOf: /* OF <type_name> */
			appendStringInfoString(str, "OF ");
			break;
		case AT_DropOf: /* NOT OF */
			appendStringInfoString(str, "NOT OF ");
			break;
		case AT_ReplicaIdentity: /* REPLICA IDENTITY */
			appendStringInfoString(str, "REPLICA IDENTITY ");
			break;
		case AT_EnableRowSecurity: /* ENABLE ROW SECURITY */
			appendStringInfoString(str, "ENABLE ROW LEVEL SECURITY ");
			break;
		case AT_DisableRowSecurity: /* DISABLE ROW SECURITY */
			appendStringInfoString(str, "DISABLE ROW LEVEL SECURITY ");
			break;
		case AT_ForceRowSecurity: /* FORCE ROW SECURITY */
			appendStringInfoString(str, "FORCE ROW LEVEL SECURITY ");
			break;
		case AT_NoForceRowSecurity: /* NO FORCE ROW SECURITY */
			appendStringInfoString(str, "NO FORCE ROW LEVEL SECURITY ");
			break;
		case AT_GenericOptions: /* OPTIONS (...) */
			// Handled in def field handling
			break;
		case AT_AttachPartition: /* ATTACH PARTITION */
			appendStringInfoString(str, "ATTACH PARTITION ");
			break;
		case AT_DetachPartition: /* DETACH PARTITION */
			appendStringInfoString(str, "DETACH PARTITION ");
			break;
		case AT_AddIdentity: /* ADD IDENTITY */
			appendStringInfoString(str, "ALTER ");
			options = "ADD";
			// Other details are output via the constraint node (in def field)
			break;
		case AT_SetIdentity: /* SET identity column options */
			appendStringInfoString(str, "ALTER ");
			break;
		case AT_DropIdentity: /* DROP IDENTITY */
			appendStringInfoString(str, "ALTER COLUMN ");
			options = "DROP IDENTITY";
			trailing_missing_ok = true;
			break;
	}

	if (alter_table_cmd->missing_ok && !trailing_missing_ok)
	{
		if (alter_table_cmd->subtype == AT_AddColumn)
			appendStringInfoString(str, "IF NOT EXISTS ");
		else
			appendStringInfoString(str, "IF EXISTS ");
	}

	if (alter_table_cmd->name != NULL)
	{
		appendStringInfoString(str, quote_identifier(alter_table_cmd->name));
		appendStringInfoChar(str, ' ');
	}

	if (alter_table_cmd->num > 0)
		appendStringInfo(str, "%d ", alter_table_cmd->num);

	if (options != NULL)
	{
		appendStringInfoString(str, options);
		appendStringInfoChar(str, ' ');
	}

	if (alter_table_cmd->missing_ok && trailing_missing_ok)
		appendStringInfoString(str, "IF EXISTS ");

	switch (alter_table_cmd->subtype)
	{
		case AT_AttachPartition:
		case AT_DetachPartition:
			deparsePartitionCmd(str, castNode(PartitionCmd, alter_table_cmd->def));
			appendStringInfoChar(str, ' ');
			break;
		case AT_AddColumn:
		case AT_AlterColumnType:
			deparseColumnDef(str, castNode(ColumnDef, alter_table_cmd->def));
			appendStringInfoChar(str, ' ');
			break;
		case AT_ColumnDefault:
			if (alter_table_cmd->def != NULL)
			{
				deparseExpr(str, alter_table_cmd->def);
				appendStringInfoChar(str, ' ');
			}
			break;
		case AT_SetStatistics:
			deparseSignedIconst(str, alter_table_cmd->def);
			appendStringInfoChar(str, ' ');
			break;
		case AT_SetOptions:
		case AT_ResetOptions:
		case AT_SetRelOptions:
		case AT_ResetRelOptions:
			deparseRelOptions(str, castNode(List, alter_table_cmd->def));
			appendStringInfoChar(str, ' ');
			break;
		case AT_SetStorage:
			deparseColId(str, strVal(alter_table_cmd->def));
			appendStringInfoChar(str, ' ');
			break;
		case AT_AddIdentity:
		case AT_AddConstraint:
		case AT_AlterConstraint:
			deparseConstraint(str, castNode(Constraint, alter_table_cmd->def));
			appendStringInfoChar(str, ' ');
			break;
		case AT_SetIdentity:
			deparseAlterIdentityColumnOptionList(str, castNode(List, alter_table_cmd->def));
			appendStringInfoChar(str, ' ');
			break;
		case AT_AlterColumnGenericOptions:
		case AT_GenericOptions:
			deparseAlterGenericOptions(str, castNode(List, alter_table_cmd->def));
			appendStringInfoChar(str, ' ');
			break;
		case AT_AddInherit:
		case AT_DropInherit:
			deparseRangeVar(str, castNode(RangeVar, alter_table_cmd->def), DEPARSE_NODE_CONTEXT_NONE);
			appendStringInfoChar(str, ' ');
			break;
		case AT_AddOf:
			deparseTypeName(str, castNode(TypeName, alter_table_cmd->def));
			appendStringInfoChar(str, ' ');
			break;
		case AT_ReplicaIdentity:
			deparseReplicaIdentityStmt(str, castNode(ReplicaIdentityStmt, alter_table_cmd->def));
			appendStringInfoChar(str, ' ');
			break;
		default:
			Assert(alter_table_cmd->def == NULL);
			break;
	}

	deparseOptDropBehavior(str, alter_table_cmd->behavior);

	removeTrailingSpace(str);
}