Format implements the NodeFormatter interface.
(ctx *FmtCtx)
| 1463 | |
| 1464 | // Format implements the NodeFormatter interface. |
| 1465 | func (node *CastExpr) Format(ctx *FmtCtx) { |
| 1466 | switch node.SyntaxMode { |
| 1467 | case CastPrepend: |
| 1468 | // This is a special case for things like INTERVAL '1s'. These only work |
| 1469 | // with string constats; if the underlying expression was changed, we fall |
| 1470 | // back to the short syntax. |
| 1471 | if _, ok := node.Expr.(*StrVal); ok { |
| 1472 | ctx.WriteString(node.Type.SQLString()) |
| 1473 | ctx.WriteByte(' ') |
| 1474 | ctx.FormatNode(node.Expr) |
| 1475 | break |
| 1476 | } |
| 1477 | fallthrough |
| 1478 | case CastShort: |
| 1479 | exprFmtWithParen(ctx, node.Expr) |
| 1480 | ctx.WriteString("::") |
| 1481 | ctx.WriteString(node.Type.SQLString()) |
| 1482 | default: |
| 1483 | ctx.WriteString("CAST(") |
| 1484 | ctx.FormatNode(node.Expr) |
| 1485 | ctx.WriteString(" AS ") |
| 1486 | if node.Type.Family() == types.CollatedStringFamily { |
| 1487 | // Need to write closing parentheses before COLLATE clause, so create |
| 1488 | // equivalent string type without the locale. |
| 1489 | strTyp := types.MakeScalar( |
| 1490 | types.StringFamily, |
| 1491 | node.Type.Oid(), |
| 1492 | node.Type.Precision(), |
| 1493 | node.Type.Width(), |
| 1494 | "", /* locale */ |
| 1495 | ) |
| 1496 | ctx.WriteString(strTyp.SQLString()) |
| 1497 | ctx.WriteString(") COLLATE ") |
| 1498 | lex.EncodeLocaleName(&ctx.Buffer, node.Type.Locale()) |
| 1499 | } else { |
| 1500 | ctx.WriteString(node.Type.SQLString()) |
| 1501 | ctx.WriteByte(')') |
| 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | // NewTypedCastExpr returns a new CastExpr that is verified to be well-typed. |
| 1507 | func NewTypedCastExpr(expr TypedExpr, typ *types.T) (*CastExpr, error) { |
nothing calls this directly
no test coverage detected