Documentation ¶
Overview ¶
Package ydbsql provides integration of YDB table API with database/sql driver interface.
The recommended usage of this package is as follows:
import "ydb/ydbsql" func main() { db, err := sql.OpenDB(ydbsql.Connector( // Note that you could pass already configured instance of // ydb/table.Client here. This may be useful when tracing setup // must be prepared directly on Client structure. // // There are few other useful options, see ConnectorOption type for // more details. )) }
But, as common for sql driver implementations, there is a standard way of database initialization via sql.Open() function:
import _ "ydb/ydbsql" // for "ydb" sql driver registration. import "database/sql" func main() { db, err := sql.Open("ydb", "ydb://endpoint/database?auth-token=secret") }
That is, data source name must be a welformed URL, with scheme "ydb", host for YDB endpoint server, and path for database name. Note that token parameter is required.
Data source name parameters:
token – access token to be used during requests (required).
As you may notice, initialization via sql.Open() does not provide ability to setup tracing configuration.
Note that unlike ydb package, ydbsql retrying most ydb errors implicitly. That is, calling db.QueryContext() or db.ExecContext() will retry operation when it receives retriable error from ydb. But it is not possible to provide same logic for transactions. There is a TxDoer struct or DoTx() function (which is just a shortcut) for this purpose:
import "ydb/ydbsql" func main() { db, err := sql.OpenDB(ydbsql.Connector(...)) if err != nil { // handle error } ctx, cancel := context.WithTimeout(10 * time.Second) defer cancel() err = db.ExecContext(ctx, ...) // Retries are implicit. if err != nil { // handle error } err = db.QueryContext(ctx, ...) // Retries are implicit. if err != nil { // handle error } // Explicit retries for transactions. err = ydbsql.DoTx(ctx, db, func(ctx context.Context, tx *sql.Tx) error { // Execute statements here. // Note that Commit()/Rollback() are not neccessary here. }) if err != nil { // handle error } }
Note that database/sql package reuses sql.Conn instances which are wrappers around ydb/table.Session instances in case of ydbsql. It could be reasonable to increase the number of reused sessions via database/sql.DB.SetMaxIdleConns() and database/sql.DB.SetMaxOpenConns() calls. If doing so, it is also highly recommended to setup inner session pool's size limit to the same value by passing WithSessionPoolSizeLimit() option to the Connector() function.
It is worth noting that YDB supports server side operation timeout. That is, client could set up operation timeout among other operation options. When this timeout exceeds, YDB will try to cancel operation execution and in any result of the cancelation appropriate timeout error will be returned. By default, this package "mirrors" context.Context deadlines and passes operation timeout option if context deadline is not empty. To configure or disable such behavior please see appropriate Connector options.
Index ¶
- Variables
- func Connector(opts ...ConnectorOption) driver.Connector
- func ContextScanQueryMode(ctx context.Context) bool
- func DoTx(ctx context.Context, db *sql.DB, f TxOperationFunc) error
- func Nullable(s sql.Scanner) sql.Scanner
- func WithScanQuery(ctx context.Context) context.Context
- func WithTrace(ctx context.Context, t Trace) context.Context
- type ConnectorOption
- func WithClient(client *table.Client) ConnectorOption
- func WithClientTrace(t table.ClientTrace) ConnectorOption
- func WithCredentials(creds ydb.Credentials) ConnectorOption
- func WithDatabase(db string) ConnectorOption
- func WithDefaultExecDataQueryOption(opts ...table.ExecuteDataQueryOption) ConnectorOption
- func WithDefaultExecScanQueryOption(opts ...table.ExecuteScanQueryOption) ConnectorOption
- func WithDefaultTxControl(txControl *table.TransactionControl) ConnectorOption
- func WithDialer(d ydb.Dialer) ConnectorOption
- func WithDriverConfig(config ydb.DriverConfig) ConnectorOption
- func WithDriverTrace(t ydb.DriverTrace) ConnectorOption
- func WithEndpoint(addr string) ConnectorOption
- func WithMaxRetries(n int) ConnectorOption
- func WithRetryBackoff(b ydb.Backoff) ConnectorOption
- func WithRetryFastSlot(fastSlot time.Duration) ConnectorOption
- func WithRetrySlowSlot(slowSlot time.Duration) ConnectorOption
- func WithSessionPoolBusyCheckInterval(time.Duration) ConnectorOption
- func WithSessionPoolCreateSessionTimeout(d time.Duration) ConnectorOption
- func WithSessionPoolDeleteTimeout(d time.Duration) ConnectorOption
- func WithSessionPoolIdleThreshold(d time.Duration) ConnectorOption
- func WithSessionPoolKeepAliveBatchSize(int) ConnectorOption
- func WithSessionPoolKeepAliveTimeout(d time.Duration) ConnectorOption
- func WithSessionPoolSizeLimit(n int) ConnectorOption
- func WithSessionPoolTrace(t table.SessionPoolTrace) ConnectorOption
- type Date
- type Datetime
- type Decimal
- type Declaration
- type DialDoneInfo
- type DialStartInfo
- type Driver
- type Interval
- type RetryConfig
- type Timestamp
- type Trace
- type TxDoer
- type TxOperationFunc
- type TzDate
- type TzDatetime
- type TzTimestamp
Constants ¶
This section is empty.
Variables ¶
var ( DefaultIdleThreshold = 5 * time.Second DefaultSessionPoolSizeLimit = 1 << 12 )
var ( ErrDeprecated = errors.New("ydbsql: deprecated") ErrUnsupported = errors.New("ydbsql: not supported") ErrActiveTransaction = errors.New("ydbsql: can not begin tx within active tx") ErrNoActiveTransaction = errors.New("ydbsql: no active tx to work with") ErrResultTruncated = errors.New("ydbsql: result set has been truncated") // Deprecated: not used ErrSessionBusy = errors.New("ydbsql: session is busy") )
Functions ¶
func Connector ¶
func Connector(opts ...ConnectorOption) driver.Connector
func ContextScanQueryMode ¶
ContextScanQueryMode returns true if context contains scan query flag.
func DoTx ¶
DoTx is a shortcut for calling Do(ctx, f) on initialized TxDoer with DB field set to given db.
func WithScanQuery ¶
WithScanQuery returns a copy of parent context with scan query flag.
Types ¶
type ConnectorOption ¶
type ConnectorOption func(*connector)
func WithClient ¶
func WithClient(client *table.Client) ConnectorOption
func WithClientTrace ¶
func WithClientTrace(t table.ClientTrace) ConnectorOption
func WithCredentials ¶
func WithCredentials(creds ydb.Credentials) ConnectorOption
func WithDatabase ¶
func WithDatabase(db string) ConnectorOption
func WithDefaultExecDataQueryOption ¶
func WithDefaultExecDataQueryOption(opts ...table.ExecuteDataQueryOption) ConnectorOption
func WithDefaultExecScanQueryOption ¶
func WithDefaultExecScanQueryOption(opts ...table.ExecuteScanQueryOption) ConnectorOption
func WithDefaultTxControl ¶
func WithDefaultTxControl(txControl *table.TransactionControl) ConnectorOption
func WithDialer ¶
func WithDialer(d ydb.Dialer) ConnectorOption
func WithDriverConfig ¶
func WithDriverConfig(config ydb.DriverConfig) ConnectorOption
func WithDriverTrace ¶
func WithDriverTrace(t ydb.DriverTrace) ConnectorOption
func WithEndpoint ¶
func WithEndpoint(addr string) ConnectorOption
func WithMaxRetries ¶
func WithMaxRetries(n int) ConnectorOption
func WithRetryBackoff ¶
func WithRetryBackoff(b ydb.Backoff) ConnectorOption
func WithRetryFastSlot ¶ added in v2.7.0
func WithRetryFastSlot(fastSlot time.Duration) ConnectorOption
func WithRetrySlowSlot ¶ added in v2.7.0
func WithRetrySlowSlot(slowSlot time.Duration) ConnectorOption
func WithSessionPoolBusyCheckInterval ¶
func WithSessionPoolBusyCheckInterval(time.Duration) ConnectorOption
WithSessionPoolBusyCheckInterval Deprecated: has no effect, use database/sql session pool management
func WithSessionPoolCreateSessionTimeout ¶
func WithSessionPoolCreateSessionTimeout(d time.Duration) ConnectorOption
WithSessionPoolCreateSessionTimeout Deprecated: has no effect, use database/sql session pool management
func WithSessionPoolDeleteTimeout ¶
func WithSessionPoolDeleteTimeout(d time.Duration) ConnectorOption
WithSessionPoolDeleteTimeout Deprecated: has no effect, use database/sql session pool management
func WithSessionPoolIdleThreshold ¶
func WithSessionPoolIdleThreshold(d time.Duration) ConnectorOption
WithSessionPoolIdleThreshold Deprecated: has no effect, use database/sql session pool management
func WithSessionPoolKeepAliveBatchSize ¶
func WithSessionPoolKeepAliveBatchSize(int) ConnectorOption
WithSessionPoolKeepAliveBatchSize Deprecated: has no effect, use database/sql session pool management
func WithSessionPoolKeepAliveTimeout ¶
func WithSessionPoolKeepAliveTimeout(d time.Duration) ConnectorOption
WithSessionPoolKeepAliveTimeout Deprecated: has no effect, use database/sql session pool management
func WithSessionPoolSizeLimit ¶
func WithSessionPoolSizeLimit(n int) ConnectorOption
WithSessionPoolSizeLimit Deprecated: has no effect, use database/sql session pool management
func WithSessionPoolTrace ¶
func WithSessionPoolTrace(t table.SessionPoolTrace) ConnectorOption
WithSessionPoolTrace Deprecated: has no effect, use database/sql session pool management
type Declaration ¶
type Declaration struct {
// contains filtered or unexported fields
}
func (*Declaration) Declare ¶
func (d *Declaration) Declare(name string, t ydb.Type)
func (*Declaration) String ¶
func (d *Declaration) String() string
type DialDoneInfo ¶
type DialStartInfo ¶
type Driver ¶
type Driver struct {
// contains filtered or unexported fields
}
Driver is an adapter to allow the use table client as sql.Driver instance.
type RetryConfig ¶
type RetryConfig struct { // MaxRetries is a number of maximum attempts to retry a failed operation. // If MaxRetries is zero then no attempts will be made. MaxRetries int // RetryChecker contains options of mapping errors to retry mode. RetryChecker ydb.RetryChecker // Backoff is a selected backoff policy. // Deprecated: use pair FastBackoff / SlowBackoff instead Backoff ydb.Backoff // FastBackoff is a selected backoff policy. // If backoff is nil, then the ydb.DefaultFastBackoff is used. FastBackoff ydb.Backoff // SlowBackoff is a selected backoff policy. // If backoff is nil, then the ydb.DefaultSlowBackoff is used. SlowBackoff ydb.Backoff // FastSlot is an init slot for fast retry // If FastSlot is zero then the ydb.DefaultFastSlot is used. FastSlot time.Duration // SlowSlot is as zero then the ydb.DefaultSlowSlot is used. SlowSlot time.Duration }
type Trace ¶
type Trace struct {
OnDial func(DialStartInfo) func(DialDoneInfo)
}
func ContextTrace ¶
ContextTrace returns Trace associated with ctx. If there is no Trace associated with ctx then zero value of Trace is returned.
type TxDoer ¶
type TxDoer struct { DB *sql.DB Options *sql.TxOptions // RetryConfig allows to override retry parameters from DB. RetryConfig *RetryConfig }
TxDoer contains options for retrying transactions.
func (TxDoer) Do ¶
func (d TxDoer) Do(ctx context.Context, f TxOperationFunc) (err error)
Do starts a transaction and calls f with it. If f() call returns a retryable error, it repeats it accordingly to retry configuration that TxDoer's DB driver holds.
Note that callers should mutate state outside of f carefully and keeping in mind that f could be called again even if no error returned – transaction commitment can be failed:
var results []int ydbsql.DoTx(ctx, db, TxOperationFunc(func(ctx context.Context, tx *sql.Tx) error { // Reset resulting slice to prevent duplicates when retry occured. results = results[:0] rows, err := tx.QueryContext(...) if err != nil { // handle error } for rows.Next() { results = append(results, ...) } return rows.Err() }))
type TzDatetime ¶
func (*TzDatetime) Scan ¶
func (d *TzDatetime) Scan(x interface{}) error
func (TzDatetime) Value ¶
func (d TzDatetime) Value() ydb.Value
type TzTimestamp ¶
func (*TzTimestamp) Scan ¶
func (d *TzTimestamp) Scan(x interface{}) error
func (TzTimestamp) Value ¶
func (d TzTimestamp) Value() ydb.Value