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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 07:44:06 +00:00
|
|
|
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 {
|
2019-04-13 07:44:06 +00:00
|
|
|
return err
|
2018-10-03 16:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.readUntilCopyOutResponse(); err != nil {
|
2019-04-13 07:44:06 +00:00
|
|
|
return err
|
2018-10-03 16:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
msg, err := c.rxMsg()
|
|
|
|
if err != nil {
|
2019-04-13 07:44:06 +00:00
|
|
|
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)
|
2019-04-13 07:44:06 +00:00
|
|
|
return err
|
2018-10-03 16:55:26 +00:00
|
|
|
}
|
|
|
|
case *pgproto3.ReadyForQuery:
|
|
|
|
c.rxReadyForQuery(msg)
|
2019-04-13 07:44:06 +00:00
|
|
|
return nil
|
2018-10-03 16:55:26 +00:00
|
|
|
case *pgproto3.CommandComplete:
|
|
|
|
case *pgproto3.ErrorResponse:
|
2019-04-13 07:44:06 +00:00
|
|
|
return c.rxErrorResponse(msg)
|
2018-10-03 16:55:26 +00:00
|
|
|
default:
|
2019-04-13 07:44:06 +00:00
|
|
|
return c.processContextFreeMsg(msg)
|
2018-10-03 16:55:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-13 07:44:06 +00:00
|
|
|
|
|
|
|
return nil
|
2018-10-03 16:55:26 +00:00
|
|
|
}
|