Skip to content

Commit 4e9a696

Browse files
committed
addressing the comments
add copy methods to the Tx struct
1 parent 5315995 commit 4e9a696

6 files changed

+54
-35
lines changed

conn_pool.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -542,26 +542,26 @@ func (p *ConnPool) CopyFrom(tableName Identifier, columnNames []string, rowSrc C
542542
return c.CopyFrom(tableName, columnNames, rowSrc)
543543
}
544544

545-
// CopyFromTextual acquires a connection, delegates the call to that connection, and releases the connection
546-
func (p *ConnPool) CopyFromTextual(r io.Reader, sql string, args ...interface{}) error {
545+
// CopyFromReader acquires a connection, delegates the call to that connection, and releases the connection
546+
func (p *ConnPool) CopyFromReader(r io.Reader, sql string) error {
547547
c, err := p.Acquire()
548548
if err != nil {
549549
return err
550550
}
551551
defer p.Release(c)
552552

553-
return c.CopyFromTextual(r, sql, args...)
553+
return c.CopyFromReader(r, sql)
554554
}
555555

556-
// CopyToTextual acquires a connection, delegates the call to that connection, and releases the connection
557-
func (p *ConnPool) CopyToTextual(w io.Writer, sql string, args ...interface{}) error {
556+
// CopyToWriter acquires a connection, delegates the call to that connection, and releases the connection
557+
func (p *ConnPool) CopyToWriter(w io.Writer, sql string, args ...interface{}) error {
558558
c, err := p.Acquire()
559559
if err != nil {
560560
return err
561561
}
562562
defer p.Release(c)
563563

564-
return c.CopyToTextual(w, sql, args...)
564+
return c.CopyToWriter(w, sql, args...)
565565
}
566566

567567
// BeginBatch acquires a connection and begins a batch on that connection. When

copy_from.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,9 @@ func (c *Conn) CopyFrom(tableName Identifier, columnNames []string, rowSrc CopyF
283283
return ct.run()
284284
}
285285

286-
// CopyFromTextual uses the PostgreSQL textual format of the copy protocol
287-
func (c *Conn) CopyFromTextual(r io.Reader, sql string, args ...interface{}) error {
288-
if err := c.sendSimpleQuery(sql, args...); err != nil {
286+
// CopyFromReader uses the PostgreSQL textual format of the copy protocol
287+
func (c *Conn) CopyFromReader(r io.Reader, sql string) error {
288+
if err := c.sendSimpleQuery(sql); err != nil {
289289
return err
290290
}
291291

copy_from_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func TestConnCopyFromSmall(t *testing.T) {
6767

6868
mustExec(t, conn, "truncate foo")
6969

70-
if err := conn.CopyFromTextual(inputReader, "copy foo from stdin"); err != nil {
71-
t.Errorf("Unexpected error for CopyFromTextual: %v", err)
70+
if err := conn.CopyFromReader(inputReader, "copy foo from stdin"); err != nil {
71+
t.Errorf("Unexpected error for CopyFromReader: %v", err)
7272
}
7373

7474
rows, err = conn.Query("select * from foo")
@@ -155,8 +155,8 @@ func TestConnCopyFromLarge(t *testing.T) {
155155

156156
mustExec(t, conn, "truncate foo")
157157

158-
if err := conn.CopyFromTextual(strings.NewReader(inputStringRows), "copy foo from stdin"); err != nil {
159-
t.Errorf("Unexpected error for CopyFromTextual: %v", err)
158+
if err := conn.CopyFromReader(strings.NewReader(inputStringRows), "copy foo from stdin"); err != nil {
159+
t.Errorf("Unexpected error for CopyFromReader: %v", err)
160160
}
161161

162162
rows, err = conn.Query("select * from foo")
@@ -239,7 +239,7 @@ func TestConnCopyFromJSON(t *testing.T) {
239239

240240
mustExec(t, conn, "truncate foo")
241241

242-
if err := conn.CopyFromTextual(inputReader, "copy foo from stdin"); err != nil {
242+
if err := conn.CopyFromReader(inputReader, "copy foo from stdin"); err != nil {
243243
t.Errorf("Unexpected error for CopyFrom: %v", err)
244244
}
245245

@@ -343,12 +343,12 @@ func TestConnCopyFromFailServerSideMidway(t *testing.T) {
343343

344344
mustExec(t, conn, "truncate foo")
345345

346-
err = conn.CopyFromTextual(inputReader, "copy foo from stdin")
346+
err = conn.CopyFromReader(inputReader, "copy foo from stdin")
347347
if err == nil {
348-
t.Errorf("Expected CopyFromTextual return error, but it did not")
348+
t.Errorf("Expected CopyFromReader return error, but it did not")
349349
}
350350
if _, ok := err.(pgx.PgError); !ok {
351-
t.Errorf("Expected CopyFromTextual return pgx.PgError, but instead it returned: %v", err)
351+
t.Errorf("Expected CopyFromReader return pgx.PgError, but instead it returned: %v", err)
352352
}
353353

354354
rows, err = conn.Query("select * from foo")
@@ -600,41 +600,41 @@ func TestConnCopyFromCopyFromSourceNextPanic(t *testing.T) {
600600
}
601601
}
602602

603-
func TestConnCopyFromTextualQueryError(t *testing.T) {
603+
func TestConnCopyFromReaderQueryError(t *testing.T) {
604604
t.Parallel()
605605

606606
conn := mustConnect(t, *defaultConnConfig)
607607
defer closeConn(t, conn)
608608

609609
inputReader := strings.NewReader("")
610610

611-
err := conn.CopyFromTextual(inputReader, "cropy foo from stdin")
611+
err := conn.CopyFromReader(inputReader, "cropy foo from stdin")
612612
if err == nil {
613-
t.Errorf("Expected CopyFromTextual return error, but it did not")
613+
t.Errorf("Expected CopyFromReader return error, but it did not")
614614
}
615615

616616
if _, ok := err.(pgx.PgError); !ok {
617-
t.Errorf("Expected CopyFromTextual return pgx.PgError, but instead it returned: %v", err)
617+
t.Errorf("Expected CopyFromReader return pgx.PgError, but instead it returned: %v", err)
618618
}
619619

620620
ensureConnValid(t, conn)
621621
}
622622

623-
func TestConnCopyFromTextualNoTableError(t *testing.T) {
623+
func TestConnCopyFromReaderNoTableError(t *testing.T) {
624624
t.Parallel()
625625

626626
conn := mustConnect(t, *defaultConnConfig)
627627
defer closeConn(t, conn)
628628

629629
inputReader := strings.NewReader("")
630630

631-
err := conn.CopyFromTextual(inputReader, "copy foo from stdin")
631+
err := conn.CopyFromReader(inputReader, "copy foo from stdin")
632632
if err == nil {
633-
t.Errorf("Expected CopyFromTextual return error, but it did not")
633+
t.Errorf("Expected CopyFromReader return error, but it did not")
634634
}
635635

636636
if _, ok := err.(pgx.PgError); !ok {
637-
t.Errorf("Expected CopyFromTextual return pgx.PgError, but instead it returned: %v", err)
637+
t.Errorf("Expected CopyFromReader return pgx.PgError, but instead it returned: %v", err)
638638
}
639639

640640
ensureConnValid(t, conn)

copy_to.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (c *Conn) readUntilCopyOutResponse() error {
2525
}
2626
}
2727

28-
func (c *Conn) CopyToTextual(w io.Writer, sql string, args ...interface{}) error {
28+
func (c *Conn) CopyToWriter(w io.Writer, sql string, args ...interface{}) error {
2929
if err := c.sendSimpleQuery(sql, args...); err != nil {
3030
return err
3131
}

copy_to_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/jackc/pgx"
88
)
99

10-
func TestConnCopyToTextualSmall(t *testing.T) {
10+
func TestConnCopyToWriterSmall(t *testing.T) {
1111
t.Parallel()
1212

1313
conn := mustConnect(t, *defaultConnConfig)
@@ -30,8 +30,8 @@ func TestConnCopyToTextualSmall(t *testing.T) {
3030

3131
outputWriter := bytes.NewBuffer(make([]byte, 0, len(inputBytes)))
3232

33-
if err := conn.CopyToTextual(outputWriter, "copy foo to stdout"); err != nil {
34-
t.Errorf("Unexpected error for CopyToTextual: %v", err)
33+
if err := conn.CopyToWriter(outputWriter, "copy foo to stdout"); err != nil {
34+
t.Errorf("Unexpected error for CopyToWriter: %v", err)
3535
}
3636

3737
if i := bytes.Compare(inputBytes, outputWriter.Bytes()); i != 0 {
@@ -41,7 +41,7 @@ func TestConnCopyToTextualSmall(t *testing.T) {
4141
ensureConnValid(t, conn)
4242
}
4343

44-
func TestConnCopyToTextualLarge(t *testing.T) {
44+
func TestConnCopyToWriterLarge(t *testing.T) {
4545
t.Parallel()
4646

4747
conn := mustConnect(t, *defaultConnConfig)
@@ -66,7 +66,7 @@ func TestConnCopyToTextualLarge(t *testing.T) {
6666

6767
outputWriter := bytes.NewBuffer(make([]byte, 0, len(inputBytes)))
6868

69-
if err := conn.CopyToTextual(outputWriter, "copy foo to stdout"); err != nil {
69+
if err := conn.CopyToWriter(outputWriter, "copy foo to stdout"); err != nil {
7070
t.Errorf("Unexpected error for CopyFrom: %v", err)
7171
}
7272

@@ -77,21 +77,21 @@ func TestConnCopyToTextualLarge(t *testing.T) {
7777
ensureConnValid(t, conn)
7878
}
7979

80-
func TestConnCopyToTextualQueryError(t *testing.T) {
80+
func TestConnCopyToWriterQueryError(t *testing.T) {
8181
t.Parallel()
8282

8383
conn := mustConnect(t, *defaultConnConfig)
8484
defer closeConn(t, conn)
8585

8686
outputWriter := bytes.NewBuffer(make([]byte, 0))
8787

88-
err := conn.CopyToTextual(outputWriter, "cropy foo to stdout")
88+
err := conn.CopyToWriter(outputWriter, "cropy foo to stdout")
8989
if err == nil {
90-
t.Errorf("Expected CopyFromTextual return error, but it did not")
90+
t.Errorf("Expected CopyFromReader return error, but it did not")
9191
}
9292

9393
if _, ok := err.(pgx.PgError); !ok {
94-
t.Errorf("Expected CopyFromTextual return pgx.PgError, but instead it returned: %v", err)
94+
t.Errorf("Expected CopyFromReader return pgx.PgError, but instead it returned: %v", err)
9595
}
9696

9797
ensureConnValid(t, conn)

tx.go

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"io"
78
"time"
89

910
"github.com/pkg/errors"
@@ -237,6 +238,24 @@ func (tx *Tx) CopyFrom(tableName Identifier, columnNames []string, rowSrc CopyFr
237238
return tx.conn.CopyFrom(tableName, columnNames, rowSrc)
238239
}
239240

241+
// CopyFromReader delegates to the underlying *Conn
242+
func (tx *Tx) CopyFromReader(r io.Reader, sql string) error {
243+
if tx.status != TxStatusInProgress {
244+
return ErrTxClosed
245+
}
246+
247+
return tx.conn.CopyFromReader(r, sql)
248+
}
249+
250+
// CopyToWriter delegates to the underlying *Conn
251+
func (tx *Tx) CopyToWriter(w io.Writer, sql string, args ...interface{}) error {
252+
if tx.status != TxStatusInProgress {
253+
return ErrTxClosed
254+
}
255+
256+
return tx.conn.CopyToWriter(w, sql, args...)
257+
}
258+
240259
// Status returns the status of the transaction from the set of
241260
// pgx.TxStatus* constants.
242261
func (tx *Tx) Status() int8 {

0 commit comments

Comments
 (0)