MCPcopy Create free account
hub / github.com/lib/pq / prepareCopyIn

Method prepareCopyIn

copy.go:41–119  ·  view source on GitHub ↗
(q string)

Source from the content-addressed store, hash-verified

39)
40
41func (cn *conn) prepareCopyIn(q string) (_ driver.Stmt, resErr error) {
42 if !cn.isInTransaction() {
43 return nil, errCopyNotSupportedOutsideTxn
44 }
45
46 ci := &copyin{
47 cn: cn,
48 buffer: make([]byte, 0, ciBufferSize),
49 rowData: make(chan []byte),
50 done: make(chan bool, 1),
51 }
52 // add CopyData identifier + 4 bytes for message length
53 ci.buffer = append(ci.buffer, byte(proto.CopyDataRequest), 0, 0, 0, 0)
54
55 b := cn.writeBuf(proto.Query)
56 b.string(q)
57 err := cn.send(b)
58 if err != nil {
59 return nil, err
60 }
61
62awaitCopyInResponse:
63 for {
64 t, r, err := cn.recv1()
65 if err != nil {
66 return nil, err
67 }
68 switch t {
69 case proto.CopyInResponse:
70 if r.byte() != 0 {
71 resErr = errBinaryCopyNotSupported
72 break awaitCopyInResponse
73 }
74 go ci.resploop()
75 return ci, nil
76 case proto.CopyOutResponse:
77 resErr = errCopyToNotSupported
78 break awaitCopyInResponse
79 case proto.ErrorResponse:
80 resErr = parseError(r, q)
81 case proto.ReadyForQuery:
82 if resErr == nil {
83 ci.setBad(driver.ErrBadConn)
84 return nil, fmt.Errorf("pq: unexpected ReadyForQuery in response to COPY")
85 }
86 cn.processReadyForQuery(r)
87 return nil, resErr
88 default:
89 ci.setBad(driver.ErrBadConn)
90 return nil, fmt.Errorf("pq: unknown response for copy query: %q", t)
91 }
92 }
93
94 // something went wrong, abort COPY before we return
95 b = cn.writeBuf(proto.CopyFail)
96 b.string(resErr.Error())
97 err = cn.send(b)
98 if err != nil {

Callers 1

PrepareMethod · 0.95

Calls 11

isInTransactionMethod · 0.95
writeBufMethod · 0.95
sendMethod · 0.95
recv1Method · 0.95
resploopMethod · 0.95
setBadMethod · 0.95
processReadyForQueryMethod · 0.95
parseErrorFunction · 0.85
ErrorMethod · 0.65
stringMethod · 0.45
byteMethod · 0.45

Tested by

no test coverage detected