Skip to content

Commit

Permalink
return rows copied for COPY command from the CommandComplete postgres…
Browse files Browse the repository at this point in the history
… message
  • Loading branch information
philipglazman committed Jul 8, 2020
1 parent bd4ec23 commit 538adf7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
25 changes: 24 additions & 1 deletion copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type copyin struct {
buffer []byte
rowData chan []byte
done chan bool
result driver.Result

closed bool

Expand Down Expand Up @@ -151,6 +152,8 @@ func (ci *copyin) resploop() {
switch t {
case 'C':
// complete
res, _ := ci.cn.parseComplete(r.string())
ci.setResult(res)
case 'N':
if n := ci.cn.noticeHandler; n != nil {
n(parseError(&r))
Expand Down Expand Up @@ -201,6 +204,22 @@ func (ci *copyin) setError(err error) {
ci.Unlock()
}

func (ci *copyin) setResult(result driver.Result) {
ci.Lock()
ci.result = result
ci.Unlock()
}

func (ci *copyin) getResult() driver.Result {
ci.Lock()
result := ci.result
if result == nil {
return driver.RowsAffected(0)
}
ci.Unlock()
return result
}

func (ci *copyin) NumInput() int {
return -1
}
Expand Down Expand Up @@ -231,7 +250,11 @@ func (ci *copyin) Exec(v []driver.Value) (r driver.Result, err error) {
}

if len(v) == 0 {
return driver.RowsAffected(0), ci.Close()
if err := ci.Close(); err != nil {
return driver.RowsAffected(0), err
}

return ci.getResult(), nil
}

numValues := len(v)
Expand Down
11 changes: 10 additions & 1 deletion copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,20 @@ func TestCopyInMultipleValues(t *testing.T) {
}
}

_, err = stmt.Exec()
result, err := stmt.Exec()
if err != nil {
t.Fatal(err)
}

rowsAffected, err := result.RowsAffected()
if err != nil {
t.Fatal(err)
}

if rowsAffected != 500 {
t.Fatalf("expected 500 rows affected, not %d", rowsAffected)
}

err = stmt.Close()
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 538adf7

Please sign in to comment.