open-vault/vendor/github.com/jackc/pgx/copy_to.go

65 lines
1 KiB
Go
Raw Normal View History

2018-10-03 16:55:26 +00:00
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{}) error {
2018-10-03 16:55:26 +00:00
if err := c.sendSimpleQuery(sql, args...); err != nil {
return err
2018-10-03 16:55:26 +00:00
}
if err := c.readUntilCopyOutResponse(); err != nil {
return err
2018-10-03 16:55:26 +00:00
}
for {
msg, err := c.rxMsg()
if err != nil {
return err
2018-10-03 16:55:26 +00:00
}
switch msg := msg.(type) {
case *pgproto3.CopyDone:
break
case *pgproto3.CopyData:
_, err := w.Write(msg.Data)
if err != nil {
c.die(err)
return err
2018-10-03 16:55:26 +00:00
}
case *pgproto3.ReadyForQuery:
c.rxReadyForQuery(msg)
return nil
2018-10-03 16:55:26 +00:00
case *pgproto3.CommandComplete:
case *pgproto3.ErrorResponse:
return c.rxErrorResponse(msg)
2018-10-03 16:55:26 +00:00
default:
return c.processContextFreeMsg(msg)
2018-10-03 16:55:26 +00:00
}
}
return nil
2018-10-03 16:55:26 +00:00
}