mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
database/sql: add support for returning cursors to client
This CL add support for converting a returned cursor (presented to this package as a driver.Rows) and scanning it into a *Rows. Fixes #28515 Change-Id: Id8191c568dc135af9e5e8555efcd01987708edcb Reviewed-on: https://go-review.googlesource.com/c/145738 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
ad4a58e315
commit
968742a824
5 changed files with 116 additions and 8 deletions
|
|
@ -1338,6 +1338,52 @@ func TestConnQuery(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCursorFake(t *testing.T) {
|
||||
db := newTestDB(t, "people")
|
||||
defer closeDB(t, db)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
||||
defer cancel()
|
||||
|
||||
exec(t, db, "CREATE|peoplecursor|list=table")
|
||||
exec(t, db, "INSERT|peoplecursor|list=people!name!age")
|
||||
|
||||
rows, err := db.QueryContext(ctx, `SELECT|peoplecursor|list|`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
if !rows.Next() {
|
||||
t.Fatal("no rows")
|
||||
}
|
||||
var cursor = &Rows{}
|
||||
err = rows.Scan(cursor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cursor.Close()
|
||||
|
||||
const expectedRows = 3
|
||||
var currentRow int64
|
||||
|
||||
var n int64
|
||||
var s string
|
||||
for cursor.Next() {
|
||||
currentRow++
|
||||
err = cursor.Scan(&s, &n)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != currentRow {
|
||||
t.Errorf("expected number(Age)=%d, got %d", currentRow, n)
|
||||
}
|
||||
}
|
||||
if currentRow != expectedRows {
|
||||
t.Errorf("expected %d rows, got %d rows", expectedRows, currentRow)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidNilValues(t *testing.T) {
|
||||
var date1 time.Time
|
||||
var date2 int
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue