Skip to content

Commit

Permalink
Decouple rows and stmt
Browse files Browse the repository at this point in the history
We need the ability to return rows objects which don't have an
underlying prepared statement in order to support the single round-trip
mode.
  • Loading branch information
johto committed Mar 30, 2015
1 parent 9570c32 commit 58a8e53
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,12 @@ func (cn *conn) simpleQuery(q string) (res driver.Rows, err error) {
cn.bad = true
errorf("unexpected message %q in simple query execution", t)
}
res = &rows{st: st, done: true}
res = &rows{
cn: cn,
cols: st.cols,
rowTyps: st.rowTyps,
done: true,
}
case 'Z':
cn.processReadyForQuery(r)
// done
Expand All @@ -543,8 +548,9 @@ func (cn *conn) simpleQuery(q string) (res driver.Rows, err error) {
case 'T':
// res might be non-nil here if we received a previous
// CommandComplete, but that's fine; just overwrite it
res = &rows{st: st}
st.cols, st.rowTyps = parseMeta(r)
rs := &rows{cn: cn}
rs.cols, rs.rowTyps = parseMeta(r)
res = rs

// To work around a bug in QueryRow in Go 1.2 and earlier, wait
// until the first DataRow has been received.
Expand Down Expand Up @@ -644,7 +650,11 @@ func (cn *conn) Query(query string, args []driver.Value) (_ driver.Rows, err err
}

st.exec(args)
return &rows{st: st}, nil
return &rows{
cn: cn,
cols: st.cols,
rowTyps: st.rowTyps,
}, nil
}

// Implement the optional "Execer" interface for one-shot queries
Expand Down Expand Up @@ -1099,7 +1109,11 @@ func (st *stmt) Query(v []driver.Value) (r driver.Rows, err error) {
defer st.cn.errRecover(&err)

st.exec(v)
return &rows{st: st}, nil
return &rows{
cn: st.cn,
cols: st.cols,
rowTyps: st.rowTyps,
}, nil
}

func (st *stmt) Exec(v []driver.Value) (res driver.Result, err error) {
Expand Down Expand Up @@ -1271,7 +1285,9 @@ func (cn *conn) parseComplete(commandTag string) (driver.Result, string) {
}

type rows struct {
st *stmt
cn *conn
cols []string
rowTyps []oid.Oid
done bool
rb readBuf
}
Expand All @@ -1291,15 +1307,15 @@ func (rs *rows) Close() error {
}

func (rs *rows) Columns() []string {
return rs.st.cols
return rs.cols
}

func (rs *rows) Next(dest []driver.Value) (err error) {
if rs.done {
return io.EOF
}

conn := rs.st.cn
conn := rs.cn
if conn.bad {
return driver.ErrBadConn
}
Expand Down Expand Up @@ -1330,7 +1346,7 @@ func (rs *rows) Next(dest []driver.Value) (err error) {
dest[i] = nil
continue
}
dest[i] = decode(&conn.parameterStatus, rs.rb.next(l), rs.st.rowTyps[i])
dest[i] = decode(&conn.parameterStatus, rs.rb.next(l), rs.rowTyps[i])
}
return
default:
Expand Down

0 comments on commit 58a8e53

Please sign in to comment.