Skip to content

Commit

Permalink
Merge pull request #1556 from ydb-platform/params-as-map
Browse files Browse the repository at this point in the history
Added experimental ydb.ParamsFromMap and ydb.MustParamsFromMap for build query parameters
  • Loading branch information
asmyasnikov authored Nov 12, 2024
2 parents 4008d75 + b6b0597 commit 2cb71e1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Added experimental ydb.ParamsFromMap and ydb.MustParamsFromMap for build query parameters
* Refactored coordination traces
* gRPC connection will be forcefully closed on DNS resolver errors from now on

Expand Down
43 changes: 43 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,46 @@ func ExampleOpen_advanced() {
defer db.Close(ctx) // cleanup resources
fmt.Printf("connected to %s, database '%s'", db.Endpoint(), db.Name())
}

// func ExampleParamsMap
func ExampleMustParamsFromMap() {
ctx := context.TODO()
db, err := ydb.Open(
ctx,
"grpc://localhost:2135/local",
ydb.WithAnonymousCredentials(),
ydb.WithBalancer(
balancers.PreferLocationsWithFallback(
balancers.RandomChoice(), "a", "b",
),
),
ydb.WithSessionPoolSizeLimit(100),
)
if err != nil {
fmt.Printf("Driver failed: %v", err)
}
defer db.Close(ctx) // cleanup resources
fmt.Printf("connected to %s, database '%s'", db.Endpoint(), db.Name())

res, err := db.Query().QueryRow(ctx, `
DECLARE $textArg AS Text;
DECLARE $intArg AS Int64;
SELECT $textArg AS TextField, $intArg AS IntField
`, query.WithParameters(ydb.MustParamsFromMap(map[string]any{
"$textArg": "asd",
"$intArg": int64(123),
})))
if err != nil {
fmt.Printf("query failed")
}

var result struct {
TextField string
IntField int64
}
err = res.ScanStruct(&result)
if err != nil {
fmt.Printf("scan failed")
}
}
34 changes: 34 additions & 0 deletions params_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ydb

import (
"database/sql/driver"
"fmt"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/params"
)

// MustParamsFromMap build parameters from named map, panic if error
//
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
func MustParamsFromMap(m map[string]any) *params.Parameters {
p, err := ParamsFromMap(m)
if err != nil {
panic(fmt.Sprintf("ydb: MustParamsFromMap failed with error: %v", err))
}

return p
}

// ParamsFromMap build parameters from named map
//
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
func ParamsFromMap(m map[string]any) (*params.Parameters, error) {
namedParameters := make([]any, 0, len(m))
for name, val := range m {
namedParameters = append(namedParameters, driver.NamedValue{Name: name, Value: val})
}
p, err := bind.Params(namedParameters...)

return (*params.Parameters)(&p), err
}

0 comments on commit 2cb71e1

Please sign in to comment.