From 5a374c467f57470446a9baaf8df618388580ba29 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 13 Apr 2019 08:37:15 -0500 Subject: [PATCH] Fix Exec prepared statement with 0 args --- conn.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/conn.go b/conn.go index 275e72e4..36503c18 100644 --- a/conn.go +++ b/conn.go @@ -760,6 +760,39 @@ func (c *Conn) Exec(ctx context.Context, sql string, arguments ...interface{}) ( } func (c *Conn) exec(ctx context.Context, sql string, arguments ...interface{}) (commandTag pgconn.CommandTag, err error) { + + if ps, ok := c.preparedStatements[sql]; ok { + args, err := convertDriverValuers(arguments) + if err != nil { + return "", err + } + + paramFormats := make([]int16, len(args)) + paramValues := make([][]byte, len(args)) + for i := range args { + paramFormats[i] = chooseParameterFormatCode(c.ConnInfo, ps.ParameterOIDs[i], args[i]) + paramValues[i], err = newencodePreparedStatementArgument(c.ConnInfo, ps.ParameterOIDs[i], args[i]) + if err != nil { + return "", err + } + } + + resultFormats := make([]int16, len(ps.FieldDescriptions)) + for i := range resultFormats { + if dt, ok := c.ConnInfo.DataTypeForOID(ps.FieldDescriptions[i].DataType); ok { + if _, ok := dt.Value.(pgtype.BinaryDecoder); ok { + resultFormats[i] = BinaryFormatCode + } else { + resultFormats[i] = TextFormatCode + } + } + } + + c.lastStmtSent = true + result := c.pgConn.ExecPrepared(ctx, ps.Name, paramValues, paramFormats, resultFormats).Read() + return result.CommandTag, result.Err + } + if len(arguments) == 0 { c.lastStmtSent = true results, err := c.pgConn.Exec(ctx, sql).ReadAll()