void commaList(SqlWriter writer) { // The precedence of the comma operator if low but not zero. For // instance, this ensures parentheses in // select x, (select * from foo order by z), y from t for (SqlNode node : list) { writer.sep(","); node.unparse(writer, 2, 3); } }
public void unparse(SqlWriter writer, SqlNode[] operands, int leftPrec, int rightPrec) { final SqlWriter.Frame frame = writer.startList(BetweenFrameType, "", ""); operands[VALUE_OPERAND].unparse(writer, getLeftPrec(), 0); writer.sep(getName()); operands[SYMFLAG_OPERAND].unparse(writer, 0, 0); // If the expression for the lower bound contains a call to an AND // operator, we need to wrap the expression in parentheses to prevent // the AND from associating with BETWEEN. For example, we should // unparse // a BETWEEN b OR (c AND d) OR e AND f // as // a BETWEEN (b OR c AND d) OR e) AND f // If it were unparsed as // a BETWEEN b OR c AND d OR e AND f // then it would be interpreted as // (a BETWEEN (b OR c) AND d) OR (e AND f) // which would be wrong. int lowerPrec = new AndFinder().containsAnd(operands[LOWER_OPERAND]) ? 100 : 0; operands[LOWER_OPERAND].unparse(writer, lowerPrec, lowerPrec); writer.sep("AND"); operands[UPPER_OPERAND].unparse(writer, 0, getRightPrec()); writer.endList(frame); }
void andOrList(SqlWriter writer, SqlKind sepKind) { SqlBinaryOperator sepOp = sepKind == SqlKind.AND ? SqlStdOperatorTable.AND : SqlStdOperatorTable.OR; for (int i = 0; i < list.size(); i++) { SqlNode node = list.get(i); writer.sep(sepKind.name(), false); // The precedence pulling on the LHS of a node is the // right-precedence of the separator operator, except at the start // of the list; similarly for the RHS of a node. If the operator // has left precedence 4 and right precedence 5, the precedences // in a 3-node list will look as follows: // 0 <- node1 -> 4 5 <- node2 -> 4 5 <- node3 -> 0 int lprec = (i == 0) ? 0 : sepOp.getRightPrec(); int rprec = (i == (list.size() - 1)) ? 0 : sepOp.getLeftPrec(); node.unparse(writer, lprec, rprec); } }
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) { final SqlWriter.Frame frame = ((leftPrec > 0) || (rightPrec > 0)) ? writer.startList("(", ")") : writer.startList("", ""); commaList(writer); writer.endList(frame); }