package scripting

import "github.com/ydb-platform/ydb-go-sdk/v3/scripting"

Example (Execute)

Code:

{
	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)
	}
}
Example (ExplainPlan)

Code:

{
	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("")
}
Example (ExplainValidate)

Code:

{
	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)
	}
}
Example (StreamExecute)

Code:

{
	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)
	}
}

Index

Examples

Types

type Client

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

type ExplainMode

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

	ExplainModeDefault = ExplainModePlan
)

Source Files

scripting.go

Version
v3.99.11
Published
Feb 18, 2025
Platform
windows/amd64
Imports
4 packages
Last checked
11 minutes ago

Tools for package owners.