Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose raw CopyData command #1077

Merged
merged 1 commit into from Apr 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions copy.go
@@ -1,6 +1,7 @@
package pq

import (
"context"
"database/sql/driver"
"encoding/binary"
"errors"
Expand Down Expand Up @@ -273,6 +274,38 @@ func (ci *copyin) Exec(v []driver.Value) (r driver.Result, err error) {
return driver.RowsAffected(0), nil
}

// CopyData executes a raw CopyData command using the PostgreSQL Frontend/Backend
// protocol. Use Exec(nil) to finish the command.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could fix this comment

func (ci *copyin) CopyData(ctx context.Context, line string) (r driver.Result, err error) {
if ci.closed {
return nil, errCopyInClosed
}

if finish := ci.cn.watchCancel(ctx); finish != nil {
defer finish()
}

if err := ci.getBad(); err != nil {
return nil, err
}
defer ci.cn.errRecover(&err)

if err := ci.err(); err != nil {
return nil, err
}

ci.buffer = append(ci.buffer, []byte(line)...)
ci.buffer = append(ci.buffer, '\n')

if len(ci.buffer) > ciBufferFlushSize {
ci.flush(ci.buffer)
// reset buffer, keep bytes for message identifier and length
ci.buffer = ci.buffer[:5]
}

return driver.RowsAffected(0), nil
}

func (ci *copyin) Close() (err error) {
if ci.closed { // Don't do anything, we're already closed
return nil
Expand Down