MCPcopy
hub / github.com/jackc/pgx / Encode

Method Encode

pgtype/multirange.go:141–184  ·  view source on GitHub ↗
(value any, buf []byte)

Source from the content-addressed store, hash-verified

139}
140
141func (p *encodePlanMultirangeCodecBinary) Encode(value any, buf []byte) (newBuf []byte, err error) {
142 multirange := value.(MultirangeGetter)
143
144 if multirange.IsNull() {
145 return nil, nil
146 }
147
148 elementCount := multirange.Len()
149
150 buf = pgio.AppendInt32(buf, int32(elementCount))
151
152 var encodePlan EncodePlan
153 var lastElemType reflect.Type
154 for i := range elementCount {
155 sp := len(buf)
156 buf = pgio.AppendInt32(buf, -1)
157
158 elem := multirange.Index(i)
159 var elemBuf []byte
160 if elem != nil {
161 elemType := reflect.TypeOf(elem)
162 if lastElemType != elemType {
163 lastElemType = elemType
164 encodePlan = p.m.PlanEncode(p.ac.ElementType.OID, BinaryFormatCode, elem)
165 if encodePlan == nil {
166 return nil, fmt.Errorf("unable to encode %v", multirange.Index(i))
167 }
168 }
169 elemBuf, err = encodePlan.Encode(elem, buf)
170 if err != nil {
171 return nil, err
172 }
173 }
174
175 if elemBuf == nil {
176 return nil, fmt.Errorf("multirange cannot contain NULL element")
177 } else {
178 buf = elemBuf
179 pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
180 }
181 }
182
183 return buf, nil
184}
185
186func (c *MultirangeCodec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPlan {
187 multirangeScanner, ok := target.(MultirangeSetter)

Callers

nothing calls this directly

Calls 7

EncodeMethod · 0.95
AppendInt32Function · 0.92
SetInt32Function · 0.92
IsNullMethod · 0.65
LenMethod · 0.65
IndexMethod · 0.65
PlanEncodeMethod · 0.65

Tested by

no test coverage detected