Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix #8 - handle bind error earlier

  • Loading branch information...
commit 29bcb7ed95adac65cf1082a9b15cdbcebe28db47 1 parent 570d458
@bmizerany bmizerany authored
Showing with 48 additions and 4 deletions.
  1. +25 −4 conn.go
  2. +23 −0 conn_test.go
View
29 conn.go
@@ -397,9 +397,25 @@ func (st *stmt) exec(v []driver.Value) {
st.cn.send(newWriteBuf('S'))
- t, _ := st.cn.recv()
- if t != '2' {
- errorf("unexpected bind response: %q", t)
+ var err error
+ for {
+ t, r := st.cn.recv1()
+ switch t {
+ case 'E':
+ err = parseError(r)
+ case '2':
+ if err != nil {
+ panic(err)
+ }
+ return
+ case 'Z':
+ if err != nil {
+ panic(err)
+ }
+ return
+ default:
+ errorf("unexpected bind response: %q", t)
+ }
}
}
@@ -454,12 +470,17 @@ func (rs *rows) Next(dest []driver.Value) (err error) {
defer errRecover(&err)
for {
- t, r := rs.st.cn.recv()
+ t, r := rs.st.cn.recv1()
switch t {
+ case 'E':
+ err = parseError(r)
case 'C', 'S':
continue
case 'Z':
rs.done = true
+ if err != nil {
+ return err
+ }
return io.EOF
case 'D':
n := r.int16()
View
23 conn_test.go
@@ -300,3 +300,26 @@ func TestErrorOnQuery(t *testing.T) {
t.Fatal("unexpected row")
}
}
+
+func TestBindError(t *testing.T) {
+ db, err := sql.Open("postgres", cs)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ _, err = db.Exec("create temp table test (i integer)")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ _, err = db.Query("select * from test where i=$1", "hhh")
+ if err == nil {
+ t.Fatal("expected an error")
+ }
+
+ // Should not get error here
+ _, err = db.Query("select * from test where i=$1", 1)
+ if err != nil {
+ t.Fatal(err)
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.