Format implements the NodeFormatter interface.
(ctx *FmtCtx)
| 1514 | |
| 1515 | // Format implements the NodeFormatter interface. |
| 1516 | func (node *CastExpr) Format(ctx *FmtCtx) { |
| 1517 | switch node.SyntaxMode { |
| 1518 | case CastPrepend: |
| 1519 | // This is a special case for things like INTERVAL '1s'. These only work |
| 1520 | // with string constants; if the underlying expression was changed, we fall |
| 1521 | // back to the short syntax. |
| 1522 | if _, ok := node.Expr.(*StrVal); ok { |
| 1523 | ctx.FormatTypeReference(node.Type) |
| 1524 | ctx.WriteByte(' ') |
| 1525 | ctx.FormatNode(node.Expr) |
| 1526 | break |
| 1527 | } |
| 1528 | fallthrough |
| 1529 | case CastShort: |
| 1530 | exprFmtWithParen(ctx, node.Expr) |
| 1531 | ctx.WriteString("::") |
| 1532 | ctx.FormatTypeReference(node.Type) |
| 1533 | default: |
| 1534 | ctx.WriteString("CAST(") |
| 1535 | ctx.FormatNode(node.Expr) |
| 1536 | ctx.WriteString(" AS ") |
| 1537 | if typ, ok := GetStaticallyKnownType(node.Type); ok && typ.Family() == types.CollatedStringFamily { |
| 1538 | // Need to write closing parentheses before COLLATE clause, so create |
| 1539 | // equivalent string type without the locale. |
| 1540 | strTyp := types.MakeScalar( |
| 1541 | types.StringFamily, |
| 1542 | typ.Oid(), |
| 1543 | typ.Precision(), |
| 1544 | typ.Width(), |
| 1545 | "", /* locale */ |
| 1546 | ) |
| 1547 | ctx.WriteString(strTyp.SQLString()) |
| 1548 | ctx.WriteString(") COLLATE ") |
| 1549 | lex.EncodeLocaleName(&ctx.Buffer, typ.Locale()) |
| 1550 | } else { |
| 1551 | ctx.FormatTypeReference(node.Type) |
| 1552 | ctx.WriteByte(')') |
| 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | // NewTypedCastExpr returns a new CastExpr that is verified to be well-typed. |
| 1558 | func NewTypedCastExpr(expr TypedExpr, typ *types.T) *CastExpr { |
nothing calls this directly
no test coverage detected