MCPcopy Create free account
hub / github.com/auxten/postgresql-parser / Format

Method Format

pkg/sql/sem/tree/expr.go:1465–1504  ·  view source on GitHub ↗

Format implements the NodeFormatter interface.

(ctx *FmtCtx)

Source from the content-addressed store, hash-verified

1463
1464// Format implements the NodeFormatter interface.
1465func (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.
1507func NewTypedCastExpr(expr TypedExpr, typ *types.T) (*CastExpr, error) {

Callers

nothing calls this directly

Calls 10

MakeScalarFunction · 0.92
EncodeLocaleNameFunction · 0.92
exprFmtWithParenFunction · 0.85
SQLStringMethod · 0.80
FormatNodeMethod · 0.80
FamilyMethod · 0.80
OidMethod · 0.80
PrecisionMethod · 0.80
WidthMethod · 0.80
LocaleMethod · 0.80

Tested by

no test coverage detected