query

package
v3.54.4-rc0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2024 License: Apache-2.0 Imports: 15 Imported by: 3

Documentation

Overview

Example (SelectWithParameters)
ctx := context.TODO()
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
	fmt.Printf("failed connect: %v", err)
	return
}
defer db.Close(ctx) // cleanup resources
var (
	id    int32  // required value
	myStr string // optional value
)
// Do retry operation on errors with best effort
err = db.Query().Do(ctx, // context manage exiting from Do
	func(ctx context.Context, s query.Session) (err error) { // retry operation
		_, res, err := s.Execute(ctx,
			`SELECT CAST($id AS Uint64) AS id, CAST($myStr AS Text) AS myStr`,
			query.WithParameters(
				query.Param("$id", query.Uint64Value(123)),
				query.Param("$myStr", query.TextValue("test")),
			),
		)
		if err != nil {
			return err // for auto-retry with driver
		}
		defer func() { _ = res.Close() }() // cleanup resources
		for res.NextResultSet(ctx) {       // iterate over result sets
			for res.Next() { // iterate over rows
				err = res.ScanNamed(
					query.Named("id", &id),
					query.Named("myStr", &myStr),
				)
				if err != nil {
					return err // generally scan error not retryable, return it for driver check error
				}
			}
		}
		return res.Err() // return finally result error for auto-retry with driver
	},
	query.WithIdempotent(),
)
if err != nil {
	fmt.Printf("unexpected error: %v", err)
}
fmt.Printf("id=%v, myStr='%s'\n", id, myStr)
Output:

Example (SelectWithoutParameters)
ctx := context.TODO()
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
	fmt.Printf("failed connect: %v", err)
	return
}
defer db.Close(ctx) // cleanup resources
var (
	id    int32  // required value
	myStr string // optional value
)
// Do retry operation on errors with best effort
err = db.Query().Do(ctx, // context manage exiting from Do
	func(ctx context.Context, s query.Session) (err error) { // retry operation
		_, res, err := s.Execute(ctx,
			`SELECT 42 as id, "my string" as myStr`,
		)
		if err != nil {
			return err // for auto-retry with driver
		}
		defer func() { _ = res.Close() }() // cleanup resources
		for res.NextResultSet(ctx) {       // iterate over result sets
			for res.Next() { // iterate over rows
				if err = res.Scan(&id, &myStr); err != nil {
					return err // generally scan error not retryable, return it for driver check error
				}
			}
		}
		return res.Err() // return finally result error for auto-retry with driver
	},
	query.WithIdempotent(),
)
if err != nil {
	fmt.Printf("unexpected error: %v", err)
}
fmt.Printf("id=%v, myStr='%s'\n", id, myStr)
Output:

Example (TxSelect)
ctx := context.TODO()
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
	fmt.Printf("failed connect: %v", err)
	return
}
defer db.Close(ctx) // cleanup resources
var (
	id    int32  // required value
	myStr string // optional value
)
// Do retry operation on errors with best effort
err = db.Query().DoTx(ctx, // context manage exiting from Do
	func(ctx context.Context, tx query.TransactionActor) (err error) { // retry operation
		res, err := tx.Execute(ctx,
			`SELECT 42 as id, "my string" as myStr`,
		)
		if err != nil {
			return err // for auto-retry with driver
		}
		defer func() { _ = res.Close() }() // cleanup resources
		for res.NextResultSet(ctx) {       // iterate over result sets
			for res.Next() { // iterate over rows
				err = res.ScanNamed(
					query.Named("id", &id),
					query.Named("myStr", &myStr),
				)
				if err != nil {
					return err // generally scan error not retryable, return it for driver check error
				}
			}
		}
		return res.Err() // return finally result error for auto-retry with driver
	},
	query.WithIdempotent(),
	query.WithTxSettings(query.TxSettings(
		query.WithSnapshotReadOnly(),
	)),
)
if err != nil {
	fmt.Printf("unexpected error: %v", err)
}
fmt.Printf("id=%v, myStr='%s'\n", id, myStr)
Output:

Index

Examples

Constants

View Source
const (
	SessionStatusUnknown = SessionStatus(iota)
	SessionStatusReady
	SessionStatusBusy
	SessionStatusClosing
	SessionStatusClosed
)

Variables

This section is empty.

Functions

func Named

func Named(columnName string, destinationValueReference interface{}) (dst namedDestination)

func NewExecuteOptions

func NewExecuteOptions(opts ...ExecuteOption) (executeOptions executeOptions)

func WithIdempotent

func WithIdempotent() idempotentOption

func WithTxSettings

func WithTxSettings(txSettings *TransactionSettings) txSettingsOption

Types

type Client

type Client interface {
	// Do provide the best effort for execute operation.
	//
	// Do implements internal busy loop until one of the following conditions is met:
	// - deadline was canceled or deadlined
	// - retry operation returned nil as error
	//
	// Warning: if context without deadline or cancellation func than Do can run indefinitely.
	Do(ctx context.Context, op Operation, opts ...DoOption) error

	// DoTx provide the best effort for execute transaction.
	//
	// DoTx implements internal busy loop until one of the following conditions is met:
	// - deadline was canceled or deadlined
	// - retry operation returned nil as error
	//
	// DoTx makes auto begin (with TxSettings, by default - SerializableReadWrite), commit and
	// rollback (on error) of transaction.
	//
	// If op TxOperation returns nil - transaction will be committed
	// If op TxOperation return non nil - transaction will be rollback
	// Warning: if context without deadline or cancellation func than DoTx can run indefinitely
	DoTx(ctx context.Context, op TxOperation, opts ...DoTxOption) error
}

type ClosableSession

type ClosableSession interface {
	closer.Closer

	Session
}

type DoOption

type DoOption interface {
	// contains filtered or unexported methods
}

type DoOptions

type DoOptions struct {
	Label        string
	Idempotent   bool
	RetryOptions []retry.Option
	Trace        *trace.Query
}

func NewDoOptions

func NewDoOptions(opts ...DoOption) (doOptions DoOptions)

type DoTxOption

type DoTxOption interface {
	// contains filtered or unexported methods
}

type DoTxOptions

type DoTxOptions struct {
	DoOptions

	TxSettings *TransactionSettings
}

func NewDoTxOptions

func NewDoTxOptions(opts ...DoTxOption) (doTxOptions DoTxOptions)

type ExecuteOption

type ExecuteOption interface {
	// contains filtered or unexported methods
}

type NamedDestination added in v3.73.0

type NamedDestination interface {
	Name() string
	Destination() interface{}
}

type Operation

type Operation func(ctx context.Context, s Session) error

Operation is the interface that holds an operation for retry. if Operation returns not nil - operation will retry if Operation returns nil - retry loop will break

type Parameter

type Parameter interface {
	Name() string
	Value() Value
}

Parameters

func Param

func Param(name string, v Value) Parameter

type Parameters

type Parameters struct {
	// contains filtered or unexported fields
}

Parameters

func WithParameters

func WithParameters(params ...Parameter) *Parameters

func (*Parameters) Add

func (params *Parameters) Add(parameters ...Parameter)

func (*Parameters) Count

func (params *Parameters) Count() int

func (*Parameters) Each

func (params *Parameters) Each(it func(name string, v Value))

func (*Parameters) Params

func (params *Parameters) Params() queryParams

func (*Parameters) String

func (params *Parameters) String() string

type Result

type Result interface {
	Close() error
	Err() error
	NextResultSet(ctx context.Context) bool
	Next() bool
	Scan(dst ...interface{}) error
	ScanNamed(dst ...NamedDestination) error
}

type Session

type Session interface {
	SessionInfo

	// Execute executes query.
	//
	// Execute used by default:
	// - DefaultTxControl
	// - flag WithKeepInCache(true) if params is not empty.
	Execute(ctx context.Context, query string, opts ...ExecuteOption) (txr Transaction, r Result, err error)

	Begin(ctx context.Context, txSettings *TransactionSettings) (Transaction, error)
}

type SessionInfo

type SessionInfo interface {
	ID() string
	NodeID() int64
	Status() SessionStatus
	LastUsage() time.Time
}

type SessionStatus

type SessionStatus uint32

func (SessionStatus) String

func (s SessionStatus) String() string

type Transaction

type Transaction interface {
	TransactionActor

	CommitTx(ctx context.Context) (err error)
	Rollback(ctx context.Context) (err error)
}

type TransactionActor

type TransactionActor interface {
	// Execute executes query.
	//
	// Execute used by default:
	// - flag WithKeepInCache(true) if params is not empty.
	Execute(ctx context.Context, query string, opts ...TxExecuteOption) (r Result, err error)
}

type TransactionControl

type TransactionControl struct {
	// contains filtered or unexported fields
}

func DefaultTxControl

func DefaultTxControl() *TransactionControl

DefaultTxControl returns default transaction control with serializable read-write isolation mode and auto-commit

func OnlineReadOnlyTxControl

func OnlineReadOnlyTxControl(opts ...TxOnlineReadOnlyOption) *TransactionControl

OnlineReadOnlyTxControl returns online read-only transaction control

func SerializableReadWriteTxControl

func SerializableReadWriteTxControl(opts ...TxControlOption) *TransactionControl

SerializableReadWriteTxControl returns transaction control with serializable read-write isolation mode

func SnapshotReadOnlyTxControl

func SnapshotReadOnlyTxControl() *TransactionControl

SnapshotReadOnlyTxControl returns snapshot read-only transaction control

func StaleReadOnlyTxControl

func StaleReadOnlyTxControl() *TransactionControl

StaleReadOnlyTxControl returns stale read-only transaction control

func TxControl

func TxControl(opts ...TxControlOption) *TransactionControl

TxControl makes transaction control from given options

func (*TransactionControl) Desc

type TransactionIdentifier

type TransactionIdentifier interface {
	ID() string
}

type TransactionSettings

type TransactionSettings struct {
	// contains filtered or unexported fields
}

Transaction control options

func TxSettings

func TxSettings(opts ...TxOption) *TransactionSettings

TxSettings returns transaction settings

func (*TransactionSettings) Desc

type TxControlOption

type TxControlOption func(*txControlDesc)

func BeginTx

func BeginTx(opts ...TxOption) TxControlOption

BeginTx returns begin transaction control option

func CommitTx

func CommitTx() TxControlOption

CommitTx returns commit transaction control option

func WithTxID

func WithTxID(txID string) TxControlOption

type TxExecuteOption

type TxExecuteOption interface {
	// contains filtered or unexported methods
}

type TxExecuteOptions

type TxExecuteOptions struct {
	// contains filtered or unexported fields
}

func NewTxExecuteOptions

func NewTxExecuteOptions(opts ...TxExecuteOption) (txExecuteOptions TxExecuteOptions)

type TxOnlineReadOnlyOption

type TxOnlineReadOnlyOption func(*txOnlineReadOnly)

func WithInconsistentReads

func WithInconsistentReads() TxOnlineReadOnlyOption

type TxOperation

type TxOperation func(ctx context.Context, tx TransactionActor) error

TxOperation is the interface that holds an operation for retry. if TxOperation returns not nil - operation will retry if TxOperation returns nil - retry loop will break

type TxOption

type TxOption func(*TransactionSettings)

Transaction control options

func WithDefaultTxMode

func WithDefaultTxMode() TxOption

func WithOnlineReadOnly

func WithOnlineReadOnly(opts ...TxOnlineReadOnlyOption) TxOption

func WithSerializableReadWrite

func WithSerializableReadWrite() TxOption

func WithSnapshotReadOnly

func WithSnapshotReadOnly() TxOption

func WithStaleReadOnly

func WithStaleReadOnly() TxOption

type Type added in v3.73.0

type Type = value.Type

type Value

type Value = value.Value

func BoolValue

func BoolValue(v bool) Value

func BytesValue

func BytesValue(v []byte) Value

func DatetimeValue

func DatetimeValue(v time.Time) Value

func Int16Value

func Int16Value(v int16) Value

func Int32Value

func Int32Value(v int32) Value

func Int64Value

func Int64Value(v int64) Value

func Int8Value

func Int8Value(v int8) Value

func IntervalValue

func IntervalValue(v time.Duration) Value

func TextValue

func TextValue(v string) Value

func TimestampValue

func TimestampValue(v time.Time) Value

func Uint16Value

func Uint16Value(v uint16) Value

func Uint32Value

func Uint32Value(v uint32) Value

func Uint64Value

func Uint64Value(v uint64) Value

func Uint8Value

func Uint8Value(v uint8) Value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL