forked from jackc/pgx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
copy_to.go
63 lines (55 loc) · 1.12 KB
/
copy_to.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package pgx
import (
"io"
"github.com/jackc/pgx/pgproto3"
)
func (c *Conn) readUntilCopyOutResponse() error {
for {
msg, err := c.rxMsg()
if err != nil {
return err
}
switch msg := msg.(type) {
case *pgproto3.CopyOutResponse:
return nil
default:
err = c.processContextFreeMsg(msg)
if err != nil {
return err
}
}
}
}
func (c *Conn) CopyToWriter(w io.Writer, sql string, args ...interface{}) (CommandTag, error) {
if err := c.sendSimpleQuery(sql, args...); err != nil {
return "", err
}
if err := c.readUntilCopyOutResponse(); err != nil {
return "", err
}
for {
msg, err := c.rxMsg()
if err != nil {
return "", err
}
switch msg := msg.(type) {
case *pgproto3.CopyDone:
break
case *pgproto3.CopyData:
_, err := w.Write(msg.Data)
if err != nil {
c.die(err)
return "", err
}
case *pgproto3.ReadyForQuery:
c.rxReadyForQuery(msg)
return "", nil
case *pgproto3.CommandComplete:
return CommandTag(msg.CommandTag), nil
case *pgproto3.ErrorResponse:
return "", c.rxErrorResponse(msg)
default:
return "", c.processContextFreeMsg(msg)
}
}
}