Skip to content

Commit

Permalink
feat: Added support for injecting params in the query (#7)
Browse files Browse the repository at this point in the history
* feat: Added support for injecting params in the query

* fix: Fixed test and docs
  • Loading branch information
shubham030 committed Aug 29, 2023
1 parent 43d742a commit e7dc293
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
4 changes: 4 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ type Runner interface {
// their identifiers exist in the returning scope.
Run(ctx context.Context) error

// RunWithParams is the same as Run, but injects the provided parameters into the
// query.
RunWithParams(ctx context.Context, params map[string]any) error

// Stream executes the query and returns an abstraction over a
// [pkg/github.com/neo4j/neo4j-go-driver/v5/neo4j.ResultWithContext], which
// allows records to be consumed one-by-one as a linked list, instead of all
Expand Down
12 changes: 8 additions & 4 deletions client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,20 @@ func (c *runnerImpl) Print() client.Runner {
return c
}

func (c *runnerImpl) Run(ctx context.Context) (err error) {
cy, err := c.cy.Compile()
func (c *runnerImpl) RunWithParams(ctx context.Context, params map[string]any) (err error) {
cy, err := c.cy.CompileWithParams(params)
if err != nil {
return fmt.Errorf("cannot compile cypher: %w", err)
}
params, err := canonicalizeParams(cy.Parameters)
canonicalizedParams, err := canonicalizeParams(cy.Parameters)
if err != nil {
return fmt.Errorf("cannot serialize parameters: %w", err)
}
return c.executeTransaction(
ctx, cy,
func(tx neo4j.ManagedTransaction) (any, error) {
var result neo4j.ResultWithContext
result, err = tx.Run(ctx, cy.Cypher, params)
result, err = tx.Run(ctx, cy.Cypher, canonicalizedParams)
if err != nil {
return nil, fmt.Errorf("cannot run cypher: %w", err)
}
Expand Down Expand Up @@ -268,6 +268,10 @@ func (c *runnerImpl) Run(ctx context.Context) (err error) {
})
}

func (c *runnerImpl) Run(ctx context.Context) (err error) {
return c.RunWithParams(ctx, nil)
}

func (c *runnerImpl) Stream(ctx context.Context, sink func(r client.Result) error) (err error) {
cy, err := c.cy.Compile()
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,31 @@ func ExampleDriver_writeSession() {
// Output: err: <nil>
// ids: [1 2 3 4 5 6 7 8 9 10]
}

func ExampleDriver_runWithParams() {
if testing.Short() {
fmt.Printf("err: %v\n", nil)
fmt.Printf("ns: %v\n", []int{1, 2, 3})
return
}

ctx := context.Background()
neo4j, cancel := startNeo4J(ctx)
d := New(neo4j)
defer func() {
if err := cancel(ctx); err != nil {
panic(err)
}
}()

var params = map[string]interface{}{
"ns": []int{1, 2, 3},
}

var ns []int
err := d.Exec().Return(db.Qual(&ns, "$ns")).RunWithParams(ctx, params)
fmt.Printf("err: %v\n", err)
fmt.Printf("ns: %v\n", ns)
// Output: err: <nil>
// ns: [1 2 3]
}

0 comments on commit e7dc293

Please sign in to comment.