in ext/pg_query/pg_query_deparse.c [7855:7920]
static void deparseRuleStmt(StringInfo str, RuleStmt* rule_stmt)
{
ListCell *lc;
appendStringInfoString(str, "CREATE ");
if (rule_stmt->replace)
appendStringInfoString(str, "OR REPLACE ");
appendStringInfoString(str, "RULE ");
appendStringInfoString(str, quote_identifier(rule_stmt->rulename));
appendStringInfoString(str, " AS ON ");
switch (rule_stmt->event)
{
case CMD_UNKNOWN:
case CMD_UTILITY:
case CMD_NOTHING:
// Not supported here
Assert(false);
break;
case CMD_SELECT:
appendStringInfoString(str, "SELECT ");
break;
case CMD_UPDATE:
appendStringInfoString(str, "UPDATE ");
break;
case CMD_INSERT:
appendStringInfoString(str, "INSERT ");
break;
case CMD_DELETE:
appendStringInfoString(str, "DELETE ");
break;
}
appendStringInfoString(str, "TO ");
deparseRangeVar(str, rule_stmt->relation, DEPARSE_NODE_CONTEXT_NONE);
appendStringInfoChar(str, ' ');
deparseWhereClause(str, rule_stmt->whereClause);
appendStringInfoString(str, "DO ");
if (rule_stmt->instead)
appendStringInfoString(str, "INSTEAD ");
if (list_length(rule_stmt->actions) == 0)
{
appendStringInfoString(str, "NOTHING");
}
else if (list_length(rule_stmt->actions) == 1)
{
deparseRuleActionStmt(str, linitial(rule_stmt->actions));
}
else
{
appendStringInfoChar(str, '(');
foreach (lc, rule_stmt->actions)
{
deparseRuleActionStmt(str, lfirst(lc));
if (lnext(rule_stmt->actions, lc))
appendStringInfoString(str, "; ");
}
appendStringInfoChar(str, ')');
}
}