MCPcopy Create free account
hub / github.com/dolthub/doltgresql / Format

Method Format

postgres/parser/sem/tree/expr.go:1516–1555  ·  view source on GitHub ↗

Format implements the NodeFormatter interface.

(ctx *FmtCtx)

Source from the content-addressed store, hash-verified

1514
1515// Format implements the NodeFormatter interface.
1516func (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.
1558func NewTypedCastExpr(expr TypedExpr, typ *types.T) *CastExpr {

Callers

nothing calls this directly

Calls 13

MakeScalarFunction · 0.92
EncodeLocaleNameFunction · 0.92
exprFmtWithParenFunction · 0.85
GetStaticallyKnownTypeFunction · 0.85
FormatTypeReferenceMethod · 0.80
FormatNodeMethod · 0.80
WriteStringMethod · 0.80
FamilyMethod · 0.80
OidMethod · 0.80
PrecisionMethod · 0.80
WidthMethod · 0.80
LocaleMethod · 0.80

Tested by

no test coverage detected