YDB queries often require explicit DECLARE statements for parameters and PRAGMA TablePathPrefix for table scoping. To avoid writing this boilerplate manually, you can enable Query Enrichment (also known as bindings). This allows you to write simple SQL (like SELECT ?, ?) and have the driver automatically expand it into valid YQL.
Supported Enrichments
TablePathPrefix: Automatically adds a PRAGMA TablePathPrefix to scope table names.Declare: Automatically generates DECLARE statements for parameters.Positional: Enables the use of ? positional arguments.Numeric: Enables numeric parameter binding.
How to enable
- Connection String: Add
go_auto_bind to your DSN with comma-separated options. - Connector Options: Use
query.TablePathPrefix, query.Declare, etc., when initializing the connector via ydb.MustConnector.
// Using Connection String
db, _ := sql.Open("ydb", "grpc://localhost:2136/local?go_auto_bind=table_path_prefix(/local/path/to/my/folder),declare,positional")
// Using Connector Options
nativeDriver := ydb.MustOpen(ctx, "grpc://localhost:2136/local")
db := sql.OpenDB(ydb.MustConnector(nativeDriver,
query.TablePathPrefix("/local/path/to/my/folder"),
query.Declare(),
query.Positional(),
))