Source File
copy.go
Belonging Package
github.com/lib/pq
package pq
import (
)
var (
errCopyInClosed = errors.New("pq: copyin statement has already been closed")
errBinaryCopyNotSupported = errors.New("pq: only text format supported for COPY")
errCopyToNotSupported = errors.New("pq: COPY TO is not supported")
errCopyNotSupportedOutsideTxn = errors.New("pq: COPY is only allowed inside a transaction")
errCopyInProgress = errors.New("pq: COPY in progress")
)
func ( string, ...string) string {
:= "COPY " + QuoteIdentifier() + " ("
for , := range {
if != 0 {
+= ", "
}
+= QuoteIdentifier()
}
+= ") FROM STDIN"
return
}
func (, string, ...string) string {
:= "COPY " + QuoteIdentifier() + "." + QuoteIdentifier() + " ("
for , := range {
if != 0 {
+= ", "
}
+= QuoteIdentifier()
}
+= ") FROM STDIN"
return
}
type copyin struct {
cn *conn
buffer []byte
rowData chan []byte
done chan bool
closed bool
sync.Mutex // guards err
err error
}
const ciBufferSize = 64 * 1024
.buffer = append(.buffer, 'd', 0, 0, 0, 0)
:= .writeBuf('Q')
.string()
.send()
:
for {
, := .recv1()
switch {
case 'G':
if .byte() != 0 {
= errBinaryCopyNotSupported
break
}
go .resploop()
return , nil
case 'H':
= errCopyToNotSupported
break
case 'E':
= parseError()
case 'Z':
if == nil {
.setBad()
errorf("unexpected ReadyForQuery in response to COPY")
}
.processReadyForQuery()
return nil,
default:
.setBad()
errorf("unknown response for copy query: %q", )
}
}
.processReadyForQuery()
return nil,
default:
.setBad()
errorf("unknown response for CopyFail: %q", )
}
}
}
case 'Z':
.cn.processReadyForQuery(&)
.done <- true
return
case 'E':
:= parseError(&)
.setError()
default:
.setBad()
.setError(fmt.Errorf("unknown response during CopyIn: %q", ))
.done <- true
return
}
}
}
func ( *copyin) () {
.Lock()
.cn.bad = true
.Unlock()
}
func ( *copyin) () bool {
.Lock()
:= .cn.bad
.Unlock()
return
}
func ( *copyin) () bool {
.Lock()
:= (.err != nil)
.Unlock()
return
}
func ( *copyin) ( []driver.Value) ( driver.Result, error) {
if .closed {
return nil, errCopyInClosed
}
if .isBad() {
return nil, driver.ErrBadConn
}
defer .cn.errRecover(&)
if .isErrorSet() {
return nil, .err
}
if len() == 0 {
return driver.RowsAffected(0), .Close()
}
:= len()
for , := range {
.buffer = appendEncodedText(&.cn.parameterStatus, .buffer, )
if < -1 {
.buffer = append(.buffer, '\t')
}
}
.buffer = append(.buffer, '\n')
if len(.buffer) > ciBufferFlushSize {
= .cn.sendSimpleMessage('c')
if != nil {
return
}
<-.done
.cn.inCopy = false
if .isErrorSet() {
= .err
return
}
return nil
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |