MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / pgwireFormat

Method pgwireFormat

pkg/sql/sem/tree/pgwire_encode.go:47–99  ·  view source on GitHub ↗
(ctx *FmtCtx)

Source from the content-addressed store, hash-verified

45}
46
47func (d *DTuple) pgwireFormat(ctx *FmtCtx) {
48 // When converting a tuple to text in "postgres mode" there is
49 // special behavior: values are printed in "postgres mode" then the
50 // result string itself is rendered in "postgres mode".
51 // Immediate NULL tuple elements are printed as the empty string.
52 //
53 // In this last conversion, for *tuples* the special double quote
54 // and backslash characters are *doubled* (not escaped). Other
55 // special characters from C like \t \n etc are not escaped and
56 // instead printed as-is. Only non-valid characters get escaped to
57 // hex. So we delegate this formatting to a tuple-specific
58 // string printer called pgwireFormatStringInTuple().
59 ctx.WriteByte('(')
60 comma := ""
61 tc := d.ResolvedType().TupleContents()
62 for i, v := range d.D {
63 ctx.WriteString(comma)
64 var t *types.T
65 if i < len(tc) {
66 t = tc[i]
67 } else {
68 t = v.ResolvedType()
69 }
70 switch dv := UnwrapDOidWrapper(v).(type) {
71 case dNull:
72 case *DString:
73 s := ResolveBlankPaddedChar(string(*dv), t)
74 pgwireFormatStringInTuple(&ctx.Buffer, s)
75 case *DCollatedString:
76 s := ResolveBlankPaddedChar(dv.Contents, t)
77 pgwireFormatStringInTuple(&ctx.Buffer, s)
78 // Bytes cannot use the default case because they will be incorrectly
79 // double escaped.
80 case *DBytes:
81 ctx.WriteString(`"\`)
82 ctx.FormatNode(dv)
83 ctx.WriteString(`"`)
84 case *DJSON:
85 var buf bytes.Buffer
86 dv.JSON.Format(&buf)
87 pgwireFormatStringInTuple(&ctx.Buffer, buf.String())
88 case *DFloat:
89 fl := float64(*dv)
90 b := PgwireFormatFloat(nil /*buf*/, fl, ctx.dataConversionConfig, t)
91 ctx.WriteString(string(b))
92 default:
93 s := AsStringWithFlags(v, ctx.flags, FmtDataConversionConfig(ctx.dataConversionConfig), FmtLocation(ctx.location))
94 pgwireFormatStringInTuple(&ctx.Buffer, s)
95 }
96 comma = ","
97 }
98 ctx.WriteByte(')')
99}
100
101func pgwireFormatStringInTuple(buf *bytes.Buffer, in string) {
102 quote := pgwireQuoteStringInTuple(in)

Callers 1

FormatMethod · 0.95

Calls 13

ResolvedTypeMethod · 0.95
UnwrapDOidWrapperFunction · 0.85
ResolveBlankPaddedCharFunction · 0.85
PgwireFormatFloatFunction · 0.85
AsStringWithFlagsFunction · 0.85
FmtDataConversionConfigFunction · 0.85
FmtLocationFunction · 0.85
TupleContentsMethod · 0.80
FormatNodeMethod · 0.80
ResolvedTypeMethod · 0.65
FormatMethod · 0.65

Tested by

no test coverage detected