(q string)
| 39 | ) |
| 40 | |
| 41 | func (cn *conn) prepareCopyIn(q string) (_ driver.Stmt, resErr error) { |
| 42 | if !cn.isInTransaction() { |
| 43 | return nil, errCopyNotSupportedOutsideTxn |
| 44 | } |
| 45 | |
| 46 | ci := ©in{ |
| 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 | |
| 62 | awaitCopyInResponse: |
| 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 { |
no test coverage detected