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

feat: remove prepare operation in copy from #1085

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions copy_from.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"time"

"github.com/jackc/pgconn"
"github.com/jackc/pgio"
)

Expand Down Expand Up @@ -99,11 +98,6 @@ func (ct *copyFrom) run(ctx context.Context) (int64, error) {
}
quotedColumnNames := cbuf.String()

sd, err := ct.conn.Prepare(ctx, "", fmt.Sprintf("select %s from %s", quotedColumnNames, quotedTableName))
if err != nil {
return 0, err
}

r, w := io.Pipe()
doneChan := make(chan struct{})

Expand All @@ -120,7 +114,7 @@ func (ct *copyFrom) run(ctx context.Context) (int64, error) {
moreRows := true
for moreRows {
var err error
moreRows, buf, err = ct.buildCopyBuf(buf, sd)
moreRows, buf, err = ct.buildCopyBuf(buf)
if err != nil {
w.CloseWithError(err)
return
Expand Down Expand Up @@ -165,7 +159,7 @@ func (ct *copyFrom) run(ctx context.Context) (int64, error) {
return rowsAffected, err
}

func (ct *copyFrom) buildCopyBuf(buf []byte, sd *pgconn.StatementDescription) (bool, []byte, error) {
func (ct *copyFrom) buildCopyBuf(buf []byte) (bool, []byte, error) {

for ct.rowSrc.Next() {
values, err := ct.rowSrc.Values()
Expand All @@ -177,8 +171,8 @@ func (ct *copyFrom) buildCopyBuf(buf []byte, sd *pgconn.StatementDescription) (b
}

buf = pgio.AppendInt16(buf, int16(len(ct.columnNames)))
for i, val := range values {
buf, err = encodePreparedStatementArgument(ct.conn.connInfo, buf, sd.Fields[i].DataTypeOID, val)
for _, val := range values {
buf, err = encodePreparedStatementArgument(ct.conn.connInfo, buf, val)
if err != nil {
return false, nil, err
}
Expand Down
38 changes: 4 additions & 34 deletions values.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func convertSimpleArgument(ci *pgtype.ConnInfo, arg interface{}) (interface{}, e
return nil, SerializationError(fmt.Sprintf("Cannot encode %T in simple protocol - %T must implement driver.Valuer, pgtype.TextEncoder, or be a native type", arg, arg))
}

func encodePreparedStatementArgument(ci *pgtype.ConnInfo, buf []byte, oid uint32, arg interface{}) ([]byte, error) {
func encodePreparedStatementArgument(ci *pgtype.ConnInfo, buf []byte, arg interface{}) ([]byte, error) {
if arg == nil {
return pgio.AppendInt32(buf, -1), nil
}
Expand Down Expand Up @@ -184,43 +184,13 @@ func encodePreparedStatementArgument(ci *pgtype.ConnInfo, buf []byte, oid uint32
return pgio.AppendInt32(buf, -1), nil
}
arg = refVal.Elem().Interface()
return encodePreparedStatementArgument(ci, buf, oid, arg)
}

if dt, ok := ci.DataTypeForOID(oid); ok {
value := dt.Value
err := value.Set(arg)
if err != nil {
{
if arg, ok := arg.(driver.Valuer); ok {
v, err := callValuerValue(arg)
if err != nil {
return nil, err
}
return encodePreparedStatementArgument(ci, buf, oid, v)
}
}

return nil, err
}

sp := len(buf)
buf = pgio.AppendInt32(buf, -1)
argBuf, err := value.(pgtype.BinaryEncoder).EncodeBinary(ci, buf)
if err != nil {
return nil, err
}
if argBuf != nil {
buf = argBuf
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
}
return buf, nil
return encodePreparedStatementArgument(ci, buf, arg)
}

if strippedArg, ok := stripNamedType(&refVal); ok {
return encodePreparedStatementArgument(ci, buf, oid, strippedArg)
return encodePreparedStatementArgument(ci, buf, strippedArg)
}
return nil, SerializationError(fmt.Sprintf("Cannot encode %T into oid %v - %T must implement Encoder or be converted to a string", arg, oid, arg))
return nil, SerializationError(fmt.Sprintf("Cannot encode %T - %T must implement Encoder or be converted to a string", arg, arg))
}

// chooseParameterFormatCode determines the correct format code for an
Expand Down