2
0

Add *Conn. CopyFromTextual, CopyToTextual, which use textual format for copying data

This commit is contained in:
Murat Kabilov
2018-07-30 17:29:26 +02:00
parent 93ee40e691
commit 5315995dfa
7 changed files with 443 additions and 2 deletions
+56
View File
@@ -3,6 +3,7 @@ package pgx
import (
"bytes"
"fmt"
"io"
"github.com/jackc/pgx/pgio"
"github.com/jackc/pgx/pgproto3"
@@ -281,3 +282,58 @@ func (c *Conn) CopyFrom(tableName Identifier, columnNames []string, rowSrc CopyF
return ct.run()
}
// CopyFromTextual uses the PostgreSQL textual format of the copy protocol
func (c *Conn) CopyFromTextual(r io.Reader, sql string, args ...interface{}) error {
if err := c.sendSimpleQuery(sql, args...); err != nil {
return err
}
if err := c.readUntilCopyInResponse(); err != nil {
return err
}
buf := c.wbuf
buf = append(buf, copyData)
sp := len(buf)
for {
n, err := r.Read(buf[5:cap(buf)])
if err == io.EOF {
break
}
buf = buf[0 : n+5]
pgio.SetInt32(buf[sp:], int32(n+4))
if _, err := c.conn.Write(buf); err != nil {
return err
}
}
buf = buf[:0]
buf = append(buf, copyDone)
buf = pgio.AppendInt32(buf, 4)
if _, err := c.conn.Write(buf); err != nil {
return err
}
for {
msg, err := c.rxMsg()
if err != nil {
return err
}
switch msg := msg.(type) {
case *pgproto3.ReadyForQuery:
c.rxReadyForQuery(msg)
return nil
case *pgproto3.CommandComplete:
case *pgproto3.ErrorResponse:
return c.rxErrorResponse(msg)
default:
return c.processContextFreeMsg(msg)
}
}
return nil
}