Skip to content
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

add Records method to ResultWithContext #600

Open
wants to merge 2 commits into
base: 5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions neo4j/driver_with_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,10 @@ func (f *fakeResult) Record() *Record {
return f.nextRecords[f.nextIndex]
}

func (f *fakeResult) Records(context.Context) (func(yield func(*Record, error) bool)) {
panic("implement me")
}

func (f *fakeResult) Collect(context.Context) ([]*Record, error) {
panic("implement me")
}
Expand Down
37 changes: 29 additions & 8 deletions neo4j/result_with_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package neo4j

import (
"context"

"github.com/neo4j/neo4j-go-driver/v5/neo4j/db"
idb "github.com/neo4j/neo4j-go-driver/v5/neo4j/internal/db"
"github.com/neo4j/neo4j-go-driver/v5/neo4j/internal/errorutil"
Expand All @@ -44,6 +45,8 @@ type ResultWithContext interface {
Record() *Record
// Collect fetches all remaining records and returns them.
Collect(ctx context.Context) ([]*Record, error)
// Collect fetches all remaining records and returns them.
Records(ctx context.Context) func(yield func(*Record, error) bool)
// Single returns the only remaining record from the stream.
// If none or more than one record is left, an error is returned.
// The result is fully consumed after this call and its summary is immediately available when calling Consume.
Expand Down Expand Up @@ -147,18 +150,36 @@ func (r *resultWithContext) Record() *Record {
return r.record
}

func (r *resultWithContext) Records(ctx context.Context) func(yield func(*Record, error) bool) {
return func(yield func(*db.Record, error) bool) {
defer r.callAfterConsumptionHook()
for r.summary == nil && r.err == nil {
r.advance(ctx)
if r.record != nil && !yield(r.record, nil) {
return
}
}
if r.err != nil {
yield(nil, errorutil.WrapError(r.err))
}
}
}

func (r *resultWithContext) Collect(ctx context.Context) ([]*Record, error) {
recs := make([]*Record, 0, 1024)
for r.summary == nil && r.err == nil {
r.advance(ctx)
if r.record != nil {
recs = append(recs, r.record)
var err error
r.Records(ctx)(func(r *Record, innerErr error) bool {
if innerErr != nil {
err = innerErr
return false
}

recs = append(recs, r)
return true
})
if err != nil {
return nil, err
}
if r.err != nil {
return nil, errorutil.WrapError(r.err)
}
r.callAfterConsumptionHook()
return recs, nil
}

Expand Down