Skip to content

Commit b3b8332

Browse files
authoredApr 12, 2022
expose raw CopyData command (#1077)
1 parent 1ef134d commit b3b8332

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
 

‎copy.go

+33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pq
22

33
import (
4+
"context"
45
"database/sql/driver"
56
"encoding/binary"
67
"errors"
@@ -273,6 +274,38 @@ func (ci *copyin) Exec(v []driver.Value) (r driver.Result, err error) {
273274
return driver.RowsAffected(0), nil
274275
}
275276

277+
// CopyData executes a raw CopyData command using the PostgreSQL Frontend/Backend
278+
// protocol. Use Exec(nil) to finish the command.
279+
func (ci *copyin) CopyData(ctx context.Context, line string) (r driver.Result, err error) {
280+
if ci.closed {
281+
return nil, errCopyInClosed
282+
}
283+
284+
if finish := ci.cn.watchCancel(ctx); finish != nil {
285+
defer finish()
286+
}
287+
288+
if err := ci.getBad(); err != nil {
289+
return nil, err
290+
}
291+
defer ci.cn.errRecover(&err)
292+
293+
if err := ci.err(); err != nil {
294+
return nil, err
295+
}
296+
297+
ci.buffer = append(ci.buffer, []byte(line)...)
298+
ci.buffer = append(ci.buffer, '\n')
299+
300+
if len(ci.buffer) > ciBufferFlushSize {
301+
ci.flush(ci.buffer)
302+
// reset buffer, keep bytes for message identifier and length
303+
ci.buffer = ci.buffer[:5]
304+
}
305+
306+
return driver.RowsAffected(0), nil
307+
}
308+
276309
func (ci *copyin) Close() (err error) {
277310
if ci.closed { // Don't do anything, we're already closed
278311
return nil

0 commit comments

Comments
 (0)
Please sign in to comment.