Fix some invalid one round trip execs failing to return non-nil error

Prior to this commit, execEx() would write the one round trip exec to
the connection before first calling ensureConnectionReadyForQuery, which
ultimately caused any errors to be suppressed if the exec followed a
valid query, because the receive message processing would finish
successfully as soon as it received the ReadyForQuery that actually
belonged to the preceding query. So, the exec would never actually
receive the error message that it caused, leaving it to be incorrectly
received by the first subsequent query sent.
pull/311/head
Kelsey Francis 2017-08-28 18:39:12 -07:00
parent f65776f084
commit ea740fb316
2 changed files with 32 additions and 0 deletions

View File

@ -1458,6 +1458,10 @@ func (c *Conn) execEx(ctx context.Context, sql string, options *QueryExOptions,
return "", err
}
} else if options != nil && len(options.ParameterOIDs) > 0 {
if err := c.ensureConnectionReadyForQuery(); err != nil {
return "", err
}
buf, err := c.buildOneRoundTripExec(c.wbuf, sql, options, arguments)
if err != nil {
return "", err

View File

@ -1196,6 +1196,34 @@ func TestConnExecExSuppliedIncorrectParameterOIDs(t *testing.T) {
}
}
func TestConnExecExIncorrectParameterOIDsAfterAnotherQuery(t *testing.T) {
t.Parallel()
conn := mustConnect(t, *defaultConnConfig)
defer closeConn(t, conn)
mustExec(t, conn, "create temporary table foo(name varchar primary key);")
var s string
err := conn.QueryRow("insert into foo(name) values('baz') returning name;").Scan(&s)
if err != nil {
t.Errorf("Executing query failed: %v", err)
}
if s != "baz" {
t.Errorf("Query did not return expected value: %v", s)
}
_, err = conn.ExecEx(
context.Background(),
"insert into foo(name) values($1);",
&pgx.QueryExOptions{ParameterOIDs: []pgtype.OID{pgtype.Int4OID}},
"bar'; drop table foo;--",
)
if err == nil {
t.Fatal("expected error but got none")
}
}
func TestPrepare(t *testing.T) {
t.Parallel()