diff --git a/bench_test.go b/bench_test.go index 1ea92cc4..30e31e2a 100644 --- a/bench_test.go +++ b/bench_test.go @@ -485,27 +485,27 @@ const benchmarkWriteTableInsertSQL = `insert into t( $13::bool )` -type benchmarkWriteTableCopyToSrc struct { +type benchmarkWriteTableCopyFromSrc struct { count int idx int row []interface{} } -func (s *benchmarkWriteTableCopyToSrc) Next() bool { +func (s *benchmarkWriteTableCopyFromSrc) Next() bool { s.idx++ return s.idx < s.count } -func (s *benchmarkWriteTableCopyToSrc) Values() ([]interface{}, error) { +func (s *benchmarkWriteTableCopyFromSrc) Values() ([]interface{}, error) { return s.row, nil } -func (s *benchmarkWriteTableCopyToSrc) Err() error { +func (s *benchmarkWriteTableCopyFromSrc) Err() error { return nil } -func newBenchmarkWriteTableCopyToSrc(count int) pgx.CopyToSource { - return &benchmarkWriteTableCopyToSrc{ +func newBenchmarkWriteTableCopyFromSrc(count int) pgx.CopyFromSource { + return &benchmarkWriteTableCopyFromSrc{ count: count, row: []interface{}{ "varchar_1", @@ -538,7 +538,7 @@ func benchmarkWriteNRowsViaInsert(b *testing.B, n int) { b.ResetTimer() for i := 0; i < b.N; i++ { - src := newBenchmarkWriteTableCopyToSrc(n) + src := newBenchmarkWriteTableCopyFromSrc(n) tx, err := conn.Begin() if err != nil { @@ -561,7 +561,7 @@ func benchmarkWriteNRowsViaInsert(b *testing.B, n int) { // note this function is only used for benchmarks -- it doesn't escape tableName // or columnNames -func multiInsert(conn *pgx.Conn, tableName string, columnNames []string, rowSrc pgx.CopyToSource) (int, error) { +func multiInsert(conn *pgx.Conn, tableName string, columnNames []string, rowSrc pgx.CopyFromSource) (int, error) { maxRowsPerInsert := 65535 / len(columnNames) rowsThisInsert := 0 rowCount := 0 @@ -649,7 +649,7 @@ func benchmarkWriteNRowsViaMultiInsert(b *testing.B, n int) { b.ResetTimer() for i := 0; i < b.N; i++ { - src := newBenchmarkWriteTableCopyToSrc(n) + src := newBenchmarkWriteTableCopyFromSrc(n) _, err := multiInsert(conn, "t", []string{"varchar_1", @@ -681,9 +681,9 @@ func benchmarkWriteNRowsViaCopy(b *testing.B, n int) { b.ResetTimer() for i := 0; i < b.N; i++ { - src := newBenchmarkWriteTableCopyToSrc(n) + src := newBenchmarkWriteTableCopyFromSrc(n) - _, err := conn.CopyTo("t", + _, err := conn.CopyFrom(pgx.Identifier{"t"}, []string{"varchar_1", "varchar_2", "varchar_null_1", diff --git a/conn_pool.go b/conn_pool.go index 6d04565d..2a7b4db2 100644 --- a/conn_pool.go +++ b/conn_pool.go @@ -504,7 +504,8 @@ func (p *ConnPool) BeginIso(iso string) (*Tx, error) { } } -// CopyTo acquires a connection, delegates the call to that connection, and releases the connection +// Deprecated. Use CopyFrom instead. CopyTo acquires a connection, delegates the +// call to that connection, and releases the connection. func (p *ConnPool) CopyTo(tableName string, columnNames []string, rowSrc CopyToSource) (int, error) { c, err := p.Acquire() if err != nil { @@ -514,3 +515,15 @@ func (p *ConnPool) CopyTo(tableName string, columnNames []string, rowSrc CopyToS return c.CopyTo(tableName, columnNames, rowSrc) } + +// CopyFrom acquires a connection, delegates the call to that connection, and +// releases the connection. +func (p *ConnPool) CopyFrom(tableName Identifier, columnNames []string, rowSrc CopyToSource) (int, error) { + c, err := p.Acquire() + if err != nil { + return 0, err + } + defer p.Release(c) + + return c.CopyFrom(tableName, columnNames, rowSrc) +} diff --git a/tx.go b/tx.go index 36f99c28..1e32cff3 100644 --- a/tx.go +++ b/tx.go @@ -158,7 +158,7 @@ func (tx *Tx) QueryRow(sql string, args ...interface{}) *Row { return (*Row)(rows) } -// CopyTo delegates to the underlying *Conn +// Deprecated. Use CopyFrom instead. CopyTo delegates to the underlying *Conn func (tx *Tx) CopyTo(tableName string, columnNames []string, rowSrc CopyToSource) (int, error) { if tx.status != TxStatusInProgress { return 0, ErrTxClosed @@ -167,6 +167,15 @@ func (tx *Tx) CopyTo(tableName string, columnNames []string, rowSrc CopyToSource return tx.conn.CopyTo(tableName, columnNames, rowSrc) } +// CopyFrom delegates to the underlying *Conn +func (tx *Tx) CopyFrom(tableName Identifier, columnNames []string, rowSrc CopyToSource) (int, error) { + if tx.status != TxStatusInProgress { + return 0, ErrTxClosed + } + + return tx.conn.CopyFrom(tableName, columnNames, rowSrc) +} + // Conn returns the *Conn this transaction is using. func (tx *Tx) Conn() *Conn { return tx.conn