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

Method buildCopyBuf

copy_from.go:217–255  ·  view source on GitHub ↗
(buf []byte, sd *pgconn.StatementDescription)

Source from the content-addressed store, hash-verified

215}
216
217func (ct *copyFrom) buildCopyBuf(buf []byte, sd *pgconn.StatementDescription) (bool, []byte, error) {
218 const sendBufSize = 65536 - 5 // The packet has a 5-byte header
219 lastBufLen := 0
220 largestRowLen := 0
221
222 for ct.rowSrc.Next() {
223 lastBufLen = len(buf)
224
225 values, err := ct.rowSrc.Values()
226 if err != nil {
227 return false, nil, err
228 }
229 if len(values) != len(ct.columnNames) {
230 return false, nil, fmt.Errorf("expected %d values, got %d values", len(ct.columnNames), len(values))
231 }
232
233 buf = pgio.AppendInt16(buf, int16(len(ct.columnNames)))
234 for i, val := range values {
235 buf, err = encodeCopyValue(ct.conn.typeMap, buf, sd.Fields[i].DataTypeOID, val)
236 if err != nil {
237 return false, nil, err
238 }
239 }
240
241 rowLen := len(buf) - lastBufLen
242 if rowLen > largestRowLen {
243 largestRowLen = rowLen
244 }
245
246 // Try not to overflow size of the buffer PgConn.CopyFrom will be reading into. If that happens then the nature of
247 // io.Pipe means that the next Read will be short. This can lead to pathological send sizes such as 65531, 13, 65531
248 // 13, 65531, 13, 65531, 13.
249 if len(buf) > sendBufSize-largestRowLen {
250 return true, buf, nil
251 }
252 }
253
254 return false, buf, nil
255}
256
257// CopyFrom uses the PostgreSQL copy protocol to perform bulk data insertion. It returns the number of rows copied and
258// an error.

Callers 1

runMethod · 0.95

Calls 4

AppendInt16Function · 0.92
encodeCopyValueFunction · 0.85
NextMethod · 0.65
ValuesMethod · 0.65

Tested by

no test coverage detected