in ext/pg_query/pg_query_deparse.c [2772:2824]
static void deparseBoolExpr(StringInfo str, BoolExpr *bool_expr)
{
const ListCell *lc = NULL;
switch (bool_expr->boolop)
{
case AND_EXPR:
foreach(lc, bool_expr->args)
{
// Put parantheses around AND + OR nodes that are inside
bool need_parens = IsA(lfirst(lc), BoolExpr) && (castNode(BoolExpr, lfirst(lc))->boolop == AND_EXPR || castNode(BoolExpr, lfirst(lc))->boolop == OR_EXPR);
if (need_parens)
appendStringInfoChar(str, '(');
deparseExpr(str, lfirst(lc));
if (need_parens)
appendStringInfoChar(str, ')');
if (lnext(bool_expr->args, lc))
appendStringInfoString(str, " AND ");
}
return;
case OR_EXPR:
foreach(lc, bool_expr->args)
{
// Put parantheses around AND + OR nodes that are inside
bool need_parens = IsA(lfirst(lc), BoolExpr) && (castNode(BoolExpr, lfirst(lc))->boolop == AND_EXPR || castNode(BoolExpr, lfirst(lc))->boolop == OR_EXPR);
if (need_parens)
appendStringInfoChar(str, '(');
deparseExpr(str, lfirst(lc));
if (need_parens)
appendStringInfoChar(str, ')');
if (lnext(bool_expr->args, lc))
appendStringInfoString(str, " OR ");
}
return;
case NOT_EXPR:
Assert(list_length(bool_expr->args) == 1);
bool need_parens = IsA(linitial(bool_expr->args), BoolExpr) && (castNode(BoolExpr, linitial(bool_expr->args))->boolop == AND_EXPR || castNode(BoolExpr, linitial(bool_expr->args))->boolop == OR_EXPR);
appendStringInfoString(str, "NOT ");
if (need_parens)
appendStringInfoChar(str, '(');
deparseExpr(str, linitial(bool_expr->args));
if (need_parens)
appendStringInfoChar(str, ')');
return;
}
}