Skip to content

Fetches all rows by default on query, like libpq. #117

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
81 changes: 77 additions & 4 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pq

import (
"bufio"
"container/list"
"crypto/md5"
"crypto/tls"
"database/sql"
Expand Down Expand Up @@ -35,9 +36,10 @@ func init() {
}

type conn struct {
c net.Conn
buf *bufio.Reader
namei int
c net.Conn
buf *bufio.Reader
namei int
singlerowmode bool
}

func Open(name string) (_ driver.Conn, err error) {
Expand Down Expand Up @@ -80,6 +82,9 @@ func Open(name string) (_ driver.Conn, err error) {
cn := &conn{c: c}
cn.ssl(o)
cn.buf = bufio.NewReader(cn.c)
if o.Get("singlerowmode") == "true" {
cn.singlerowmode = true
}
cn.startup(o)
return cn, nil
}
Expand Down Expand Up @@ -445,7 +450,15 @@ func (st *stmt) Close() (err error) {
func (st *stmt) Query(v []driver.Value) (_ driver.Rows, err error) {
defer errRecover(&err)
st.exec(v)
return &rows{st: st}, nil
r := &rows{st: st}
if st.cn.singlerowmode {
return r, nil
}

// fetch all rows
rc := &rowscomplete{}
err = rc.load(r)
return rc, err
}

func (st *stmt) Exec(v []driver.Value) (res driver.Result, err error) {
Expand Down Expand Up @@ -607,6 +620,66 @@ func (rs *rows) Next(dest []driver.Value) (err error) {
panic("not reached")
}

type rowscomplete struct {
rows *list.List
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just [][]driver.Value here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't slices costly to append? As I can't know beforehand how many records there will be.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

append behaviour is fairly reasonable, it doesn't just grow the slice by one each time you append. list.List also uses reflection, so there's a performance hit from that.

http://play.golang.org/p/SybwNAQOQ9

PASS
BenchmarkSlice1000     10000        125335 ns/op
BenchmarkSlice10000     1000       2363422 ns/op
BenchmarkSlice100000          50      28006284 ns/op
BenchmarkSlice1000000          5     273778224 ns/op
BenchmarkSlice10000000         1    3812814581 ns/op
BenchmarkList1000       5000        468516 ns/op
BenchmarkList10000       500       5936557 ns/op
BenchmarkList100000       20      70525959 ns/op
BenchmarkList1000000           5     737343756 ns/op
BenchmarkList10000000          1    8153616298 ns/op
ok      bench   31.456s

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know, I updated my fork.

current *list.Element
done bool
cols []string
}

func (rs *rowscomplete) load(r *rows) (err error) {
defer r.Close()

rs.rows = list.New()
rs.cols = r.st.cols

// fetch all records
for {
dest := make([]driver.Value, len(rs.cols))
if err = r.Next(dest); err != nil {
break
}
rs.rows.PushBack(dest)
}

if err == io.EOF {
return nil
}

return
}

func (rs *rowscomplete) Close() error {
return nil
}

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

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

if rs.current == nil {
rs.current = rs.rows.Front()
} else {
rs.current = rs.current.Next()
}

if rs.current == nil {
rs.done = true
return io.EOF
}

for i, v := range rs.current.Value.([]driver.Value) {
dest[i] = v
}

return nil
}

func md5s(s string) string {
h := md5.New()
h.Write([]byte(s))
Expand Down