scripting

package
v3.57.4 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: Apache-2.0 Imports: 4 Imported by: 2

Documentation

Overview

Example (Execute)
ctx := context.TODO()
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
	fmt.Printf("failed to connect: %v", err)

	return
}
defer db.Close(ctx) // cleanup resources
if err = retry.Retry(ctx, func(ctx context.Context) (err error) {
	res, err := db.Scripting().Execute(
		ctx,
		"SELECT 1+1",
		table.NewQueryParameters(),
	)
	if err != nil {
		return err
	}
	defer res.Close() // cleanup resources
	if !res.NextResultSet(ctx) {
		return retry.RetryableError(
			fmt.Errorf("no result sets"),
			retry.WithBackoff(retry.TypeNoBackoff),
		)
	}
	if !res.NextRow() {
		return retry.RetryableError(
			fmt.Errorf("no rows"),
			retry.WithBackoff(retry.TypeSlowBackoff),
		)
	}
	var sum int32
	if err = res.Scan(&sum); err != nil {
		return fmt.Errorf("scan failed: %w", err)
	}
	if sum != 2 {
		return fmt.Errorf("unexpected sum: %v", sum)
	}

	return res.Err()
}, retry.WithIdempotent(true)); err != nil {
	fmt.Printf("Execute failed: %v", err)
}
Output:

Example (ExplainPlan)
ctx := context.TODO()
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
	fmt.Printf("failed to connect: %v", err)

	return
}
defer db.Close(ctx) // cleanup resources
res, err := db.Scripting().Explain(
	ctx,
	"SELECT 1+1",
	scripting.ExplainModePlan,
)
if err != nil {
	fmt.Printf("Explain failed: %v", err)

	return
}
if res.Plan == "" {
	fmt.Printf("Unexpected empty plan")

	return
}
fmt.Printf("")
Output:

Example (ExplainValidate)
ctx := context.TODO()
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
	fmt.Printf("failed to connect: %v", err)

	return
}
defer db.Close(ctx) // cleanup resources
if err = retry.Retry(ctx, func(ctx context.Context) (err error) {
	res, err := db.Scripting().Explain(
		ctx,
		"SELECT 1+1",
		scripting.ExplainModeValidate,
	)
	if err != nil {
		return err
	}
	if len(res.ParameterTypes) > 0 {
		return retry.RetryableError(fmt.Errorf("unexpected parameter types"))
	}

	return nil
}, retry.WithIdempotent(true)); err != nil {
	fmt.Printf("Explain failed: %v", err)
}
Output:

Example (StreamExecute)
ctx := context.TODO()
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
	fmt.Printf("failed to connect: %v", err)

	return
}
defer db.Close(ctx) // cleanup resources
if err = retry.Retry(ctx, func(ctx context.Context) (err error) {
	res, err := db.Scripting().StreamExecute(
		ctx,
		"SELECT 1+1",
		table.NewQueryParameters(),
	)
	if err != nil {
		return err
	}
	defer res.Close() // cleanup resources
	if !res.NextResultSet(ctx) {
		return retry.RetryableError(
			fmt.Errorf("no result sets"),
			retry.WithBackoff(retry.TypeNoBackoff),
			retry.WithDeleteSession(),
		)
	}
	if !res.NextRow() {
		return retry.RetryableError(
			fmt.Errorf("no rows"),
			retry.WithBackoff(retry.TypeFastBackoff),
		)
	}
	var sum int32
	if err = res.Scan(&sum); err != nil {
		return fmt.Errorf("scan failed: %w", err)
	}
	if sum != 2 {
		return fmt.Errorf("unexpected sum: %v", sum)
	}

	return res.Err()
}, retry.WithIdempotent(true)); err != nil {
	fmt.Printf("StreamExecute failed: %v", err)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Execute(
		ctx context.Context,
		query string,
		params *params.Parameters,
	) (result.Result, error)
	Explain(
		ctx context.Context,
		query string,
		mode ExplainMode,
	) (table.ScriptingYQLExplanation, error)
	StreamExecute(
		ctx context.Context,
		query string,
		params *params.Parameters,
	) (result.StreamResult, error)
}

type ExplainMode

type ExplainMode = uint8
const (
	ExplainModeUnknown ExplainMode = iota
	ExplainModeValidate
	ExplainModePlan

	ExplainModeDefault = ExplainModePlan
)

Jump to

Keyboard shortcuts

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